• Suki | Generic classes for multiple mods
    January 17, 2012, 09:53:31 AM
    Yes, this isn't exactly revolutionary, it's just something I've been thinking lately, to have a set of generic classes that can be used by multiple mods, for example:

    -Form generator class
    -Sanitize class
    -Database class

    It became pretty obvious when I have to include the DB class to the re-write of the FAQ mod and it hit home when I saw superglobals being used directly.


    Problem is, if I start to do it right now with the current classes that I have it will be messy to deal with later because I'm always changing things in those generic classes  and if I want to use them for all my mods then I need to have first a very solid class and make sure that, whatever changes I made to that class, is not going to brake some functionality in a mod that uses it.

    Oh well, one step at the time.
  • Suki | Re: Generic classes for multiple mods
    June 18, 2012, 08:49:35 AM
    This is a start:  https://github.com/MissAllSunday/Common-Classes

    Is still far from completed and, as always, I change things around more quickly than I should :P
  • Suki | Re: Generic classes for multiple mods
    September 05, 2012, 07:11:29 PM
    I made some changes to those common classes.

    The DB class is far from perfect, it cannot handle queries with a left, inner or right join and is limited to what it can actually perform.

    another downside is that I need to actually write more lines of code, for example:

    This is  a query using the standard $smcFunc var:

    Code: [Select]
                $result = $smcFunc['db_query']('', '
                    SELECT poster_time
                    FROM {db_prefix}messages
                    WHERE id_msg = {int:id_msg}
                    LIMIT 1',
                    array(
                        'id_msg' => $user_settings['id_msg_last_visit'],
                    )
                );
     

    and this is the exactly same query using the DB class:

    Code: [Select]
    $query = new OharaDB();
    $query->params(array(
    'rows' => 'poster_time',
    'table' => 'messages',
    'where' => 'id_msg = {int:msg}',
    'limit' => '1',
    ));
    $query->data(array(
    'msg' => $user_settings['id_msg_last_visit'],
    ));
    $query->getData();
    $query->dataResult();

    One can argue that this way separates all the arrays involved and that I don't need to write WHERE, FROM, AND, LIMIT and all that SQL code anymore.

    You probably ask yourself why do you want to write a BD class that doesn't add any performance or improvements, is limited and its actually harder to debug?

    That's easy, I hate to write the same stuff over and over again and I hate to always keep globalizing vars.

    Another point is that I can use this DB class with a Query class (make all other class to extend the Query class) and pretty much perform all the queries I want with just a few lines of code and reuse queries at will.

    Actually, the whole point of both the tools class and the DB class is to erase any trace of globals on my mods, this doesn't mean I won't use any globals at all, it simply means that I keep the globals controlled in a single file rather than adding globals to every method I use.
  • Suki | Re: Generic classes for multiple mods
    September 17, 2012, 10:19:17 AM
    Dunno why this whole DB layer idea seems so silly now...