Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 11:16:37 PM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
155531 Posts in 21713 Topics by 7738 Members
Latest Member: Pattieardathfe
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Discussion: Database Abstraction (better: Query Abstraction)  (Read 260 times)
WebBird
Guest
« on: December 15, 2009, 01:38:46 PM »

I am developing an Abstraction class based on PDO. As PDO already provides DB Abstraction, I am focussing on abstracting SQL statements.

I'd like to discuss the synopsis.

Search example:
Code:

        $this->db   = wbDatabase::singleton(
                          array(
                              'user'   => DB_USERNAME,
                              'pass'   => DB_PASSWORD,
                              'dbname' => DB_NAME,
                              'prefix' => TABLE_PREFIX,
                          )
                      );

        $result = $this->db->search(
            array(
                'tables' => array( 'sections', 'pages' ),
                'join'   => array( 't1.page_id == t2.page_id' ),
                'fields' => array( 'module', 'page_title' ),
                'where'  => 't1.page_id == ? and section_id == ?',
                'params' => array( 1, 1 )
            )
        );

Resulting SQL statement:
SELECT module, page_title FROM wb_sections AS t1  LEFT JOIN wb_pages AS t2 ON t1.page_id = t2.page_id  WHERE t1.page_id = ? AND section_id = ?

$result (Example):

Code:
array (
  0 =>
  array (
    'module' => 'wysiwyg',
    0 => 'wysiwyg',
    'page_title' => 'Homepage',
    1 => 'Homepage',
  ),
)

As you can see, the search() Syntax uses positional bind params to populate the query.

tables Attribute

All tables in the list are aliased t#, where # is starting with 1 for the first table, so the alias for the first table in the list is always t1, for the second table is t2, and so on. (If you only have one table, the tables attribute can be a string instead of array.)

fields Attribute

You can give a list of fields to retrieve. If you omit the attribute, '*' (all fields) is used.

join Attribute

Provide a valid join condition.

where Attribute

Provide a valid where condition.

params Attribute

A list of positional bind params to be used. Number of params must match the number of ? in the statement.


Example without 'join' and 'fields':

Code:
       $result = $this->db->search(
            array(
                'tables' => array( 'sections', 'pages' ),
                'where'  => 't1.page_id == ? and section_id == ?',
                'params' => array( 1, 1 )
            )
        );

SELECT * FROM wb_sections as t1, wb_pages as t2  WHERE t1.page_id = ? AND section_id = ?

Conditions

To abstract conditions, there are some operators and conjunction aliases.

Operators and their equivalents for mySQL:


        '='  => '=',
        'eq' => '=',
        'ne' => '<>',
        '==' => '=',
        '!=' => '<>',
        '=~' => 'REGEXP',
        '!~' => 'NOT REGEXP',


Conjunctions:


        'and' => 'AND',
        'AND' => 'AND',
        'OR'  => 'OR',
        'or'  => 'OR',
        '&&'  => 'AND',
        '||'  => 'OR'


(Please note: There's no alias for LIKE at the moment.)

Let me know what you think. smiley
« Last Edit: December 15, 2009, 01:42:35 PM by WebBird » Logged
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!