• Main Page
  • Data Structures
  • Files
  • File List

objSQL.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * Database access controller class
00005  *
00006  * @package objSQL
00007  * @version 2.1.0
00008  * @author MT Jordan <mtjo62@gmail.com>
00009  * @link http://objsql.sourceforge.net
00010  * @copyright 2004-2010 openSource Partners
00011  * @license LGPL
00012  * @revision $Id: objSQL.php,v 1.20 2010-07-11 13:22:21-05 mt_jordan Exp $
00013  */
00014 
00015 class objSQL
00016 {
00017     /**********************************************
00018      * Internal variables
00019      *********************************************/
00020 
00021     /**
00022      * Transaction flag - false or resource(Firebird/Interbase) if transaction is initiated
00023      *
00024      * @access private
00025      * @var    mixed
00026      */
00027     private $objAutoCommit = true;
00028 
00029     /**
00030      * Database connection object
00031      *
00032      * @access private
00033      * @var    mixed
00034      */
00035     private $objConnection = false;
00036 
00037     /**
00038      * Database connection class instance
00039      *
00040      * @access private
00041      * @var    mixed
00042      */
00043      private $objConnectClass;
00044 
00045     /**
00046      * Database type array contains all supported databases
00047      * Internal elements are used in checking extension availability and as method prefixes
00048      *
00049      * @access private
00050      * @var    array
00051      */
00052     private $objDbTypeArray = array( 'db2'      => array( 'db2','ibm_db2' ),
00053                                      'firebird' => array( 'ibase','interbase' ),
00054                                      'ingres'   => array( 'ingres','ingres' ),
00055                                      'maxdb'    => array( 'maxdb','maxdb' ),
00056                                      'mssql'    => array( 'mssql','mssql' ),
00057                                      'mysql'    => array( 'mysqli','mysqli' ),
00058                                      'oracle'   => array( 'oci','oci8' ),
00059                                      'pgsql'    => array( 'pg','pgsql' ),
00060                                      'sqlite'   => array( 'sqlite','sqlite' ),
00061                                      'sqlite3'  => array( 'sqlite3','sqlite3' ),
00062                                      'sybase'   => array( 'sybase','sybase_ct' ) );
00063 
00064     /**
00065      * Database type as set in datasource argument. See $objDbTypeArray for valid database types.
00066      *
00067      * @access private
00068      * @var    str
00069      */
00070     private $objDbType;
00071 
00072     /**
00073      * Array of database types that support sequences/generators
00074      *
00075      * @access private
00076      * @var    array
00077      */
00078     private $objSeqArray = array( 'db2',
00079                                   'firebird',
00080                                   'ingres',
00081                                   'maxdb',
00082                                   'oracle',
00083                                   'pgsql' );
00084 
00085     /**********************************************
00086      * Class methods
00087      *********************************************/
00088 
00089     /**
00090      * Constructor
00091      *
00092      * @access public
00093      * @param  array $datasource
00094      */
00095     public function __construct( $datasource )
00096     {
00097         if ( !is_array( $datasource ) )
00098             trigger_error( 'Datasource argument must be an array', E_USER_WARNING );
00099         else
00100             $this->datasource( $datasource );
00101     }
00102 
00103     /**
00104      * Close database connection - destroy connection object
00105      *
00106      * @access public
00107      * @return bool
00108      */
00109     public function close()
00110     {
00111         $objDbClose = $this->objConnectClass->objDbClose();
00112         $this->objConnection = false;
00113 
00114         return $objDbClose;
00115     }
00116 
00117     /**
00118      * Return database connection object
00119      *
00120      * @access public
00121      * @return mixed
00122      */
00123     public function connection()
00124     {
00125         return $this->objConnection;
00126     }
00127 
00128     /**
00129      * Create sequence/generator object
00130      *
00131      * @access public
00132      * @param  str $sequence
00133      * @param  int $max
00134      * @return mixed
00135      */
00136     public function create_sequence( $sequence, $max=false )
00137     {
00138         if ( in_array( $this->objDbType, $this->objSeqArray ) )
00139         {
00140             require_once 'drivers/' . $this->objDbType . '/' . $this->objDbType . 'Sequence.php';
00141 
00142             $objSequence = $this->objDbType . 'Sequence';
00143             $seq = new $objSequence( $sequence, $max, $this->objConnection );
00144 
00145             return $seq->setSequence();
00146         }
00147     }
00148 
00149     /**
00150      * Set database connection object, database type and load statement driver
00151      *
00152      * @access protected
00153      * @param  array $datasource
00154      * @return bool
00155      */
00156     protected function datasource( $datasource )
00157     {
00158         $this->objDbType = $this->objDetect( strtolower( $datasource[0] ) );
00159 
00160         if ( $this->objDbType )
00161         {
00162             require_once 'drivers/' . $this->objDbType . '/' . $this->objDbType . 'Connection.php';
00163 
00164             $objConnection = $this->objDbType . 'Connection';
00165             $this->objConnectClass = new $objConnection( $datasource );
00166             $this->objConnection = $this->objConnectClass->objDbConnection();
00167 
00168             if ( $this->objConnection )
00169                 require_once 'drivers/' . $this->objDbType . '/' . $this->objDbType . 'Statement.php';
00170         }
00171     }
00172 
00173     /**
00174      * Return database type
00175      *
00176      * @access public
00177      * @return mixed
00178      */
00179     public function dbtype()
00180     {
00181         return $this->objDbType;
00182     }
00183 
00184     /**
00185      * Return delete query object
00186      *
00187      * @access public
00188      * @param  str   $table
00189      * @param  str   $where
00190      * @param  mixed $savepoint
00191      * @return mixed
00192      */
00193     public function delete( $table, $where=false, $savepoint=false )
00194     {
00195         $objWhere = ( !$where ) ? '' : ' WHERE ' . $where;
00196         $objQuery = 'DELETE FROM ' . $table . $objWhere;
00197         $objStatement = $this->objDbType . 'Statement';
00198 
00199         $stmt = new $objStatement( $objQuery, $this, $this->objAutoCommit, $savepoint );
00200 
00201         return $stmt->objExecuteNonPrepared();
00202     }
00203 
00204     /**
00205      * Return insert query result object
00206      *
00207      * @access public
00208      * @param  str   $table
00209      * @param  array $array
00210      * @param  mixed $savepoint
00211      * @return mixed
00212      */
00213     public function insert( $table, $array, $savepoint=false )
00214     {
00215         //$array MUST be a key value pair array
00216         $array_keys   = array_keys( $array );
00217         $array_values = array_values( $array );
00218         $objColumns   = '';
00219         $objValues    = '';
00220 
00221         for ( $i = 0; $i < count( $array ); $i++ )
00222         {
00223             if ( is_string( $array_values[$i] ) && !substr_count( strtolower( $array_values[$i] ), 'nextval' ) )
00224                 $objValues .= "'" . $array_values[$i] . "',";
00225             else
00226                 $objValues .= $array_values[$i] . ',';
00227 
00228             $objColumns .= $array_keys[$i] . ',';
00229         }
00230 
00231         $objValues  = ' VALUES ( ' . rtrim( $objValues, ',' ) . ')';
00232         $objColumns = ' (' . rtrim( $objColumns, ',' ) . ')';
00233         $objQuery   = 'INSERT INTO ' . $table . $objColumns . $objValues;
00234         $objStatement = $this->objDbType . 'Statement';
00235 
00236         $stmt = new $objStatement( $objQuery, $this, $this->objAutoCommit, $savepoint );
00237 
00238         return $stmt->objExecuteNonPrepared();
00239     }
00240 
00241     /**
00242      * Return valid database type and verify enabled extension/module
00243      *
00244      * @access private
00245      * @return mixed
00246      */
00247     private function objDetect( $dbtype )
00248     {
00249         $objDbType = ( $dbtype == 'interbase' ) ? 'firebird' : $dbtype;
00250 
00251         if ( array_key_exists( $objDbType, $this->objDbTypeArray ) )
00252         {
00253             if ( !extension_loaded( $this->objDbTypeArray[$objDbType][1] ) )
00254             {
00255                 trigger_error( $this->objDbTypeArray[$objDbType][1] . ' extension not enabled', E_USER_WARNING );
00256                 return false;
00257             }
00258             else
00259             {
00260                 return $objDbType;
00261             }
00262         }
00263 
00264         trigger_error( 'Unsupported database type: ' . $dbtype, E_USER_WARNING );
00265         return false;
00266     }
00267 
00268     /**
00269      * Execute non-prepared query and return resultset object
00270      *
00271      * @access public
00272      * @param  str   $query
00273      * @param  mixed $savepoint
00274      * @return mixed
00275      */
00276     public function query( $query, $savepoint=false )
00277     {
00278         $objStatement = $this->objDbType . 'Statement';
00279         $stmt = new $objStatement( $query, $this, $this->objAutoCommit, $savepoint );
00280 
00281         return $stmt->objExecuteNonPrepared();
00282     }
00283 
00284     /**
00285      * Return prepared statement
00286      *
00287      * @access public
00288      * @param  str   $query
00289      * @param  mixed $savepoint
00290      * @return mixed
00291      */
00292     public function prepare( $query, $savepoint=false )
00293     {
00294         $objStatement = $this->objDbType . 'Statement';
00295 
00296         return new $objStatement( $query, $this, $this->objAutoCommit, $savepoint );
00297     }
00298 
00299     /**
00300      * Return select query result object
00301      *
00302      * @access public
00303      * @param  str $table
00304      * @param  str $columns
00305      * @param  str $where
00306      * @param  str $order_by
00307      * @param  str $sort_order
00308      * @return mixed
00309      */
00310     public function select( $table, $columns=false, $where=false, $order_by=false, $sort_order=false )
00311     {
00312         $objColumns   = ( !$columns ) ? '*' : $columns;
00313         $objWhere     = ( !$where ) ? '' : ' WHERE ' . $where;
00314         $objOrderBy   = ( !$order_by ) ? '' : ' ORDER BY ' . $order_by;
00315         $objSortOrder = ( !$sort_order || strtolower( $sort_order ) != 'desc' || strtolower( $sort_order ) != 'asc' ) ? '' : ' ' . $sort_order;
00316 
00317         $objQuery = 'SELECT ' . $objColumns . ' FROM ' . $table . $objWhere . $objOrderBy . $objSortOrder;
00318         $objStatement = $this->objDbType . 'Statement';
00319 
00320         $stmt = new $objStatement( $objQuery, $this, false, false );
00321 
00322         return $stmt->objExecuteNonPrepared();
00323     }
00324 
00325     /**
00326      * Return sequence value/property for named sequence - use with non-prepared insert queries
00327      *
00328      * @access public
00329      * @param  str $sequence
00330      * @return mixed
00331      */
00332     public function sequence( $sequence )
00333     {
00334         if ( in_array( $this->objDbType, $this->objSeqArray ) )
00335         {
00336             require_once 'drivers/' . $this->objDbType . '/' . $this->objDbType . 'Sequence.php';
00337 
00338             $objSequence = $this->objDbType . 'Sequence';
00339             $seq = new $objSequence( $sequence, false, $this->objConnection );
00340 
00341             return $seq->getSequence();
00342         }
00343     }
00344 
00345     /**
00346      * Return database server version
00347      *
00348      * @access public
00349      * @return str
00350      */
00351     public function server_version()
00352     {
00353         return $this->objConnectClass->objServerVersion();
00354     }
00355 
00356     /**
00357      * Begin transaction
00358      *
00359      * @access public
00360      * @return mixed
00361      */
00362     public function transaction()
00363     {
00364         require_once 'drivers/' . $this->objDbType . '/' . $this->objDbType . 'Transaction.php';
00365 
00366         $this->objAutoCommit = ( $this->objDbType == 'firebird' ) ? ibase_trans( $this->objConnection ) : false;
00367         $objTransaction = $this->objDbType . 'Transaction';
00368 
00369         return new $objTransaction( $this->objConnection, $this->objAutoCommit );
00370     }
00371 
00372     /**
00373      * Return update query result object
00374      *
00375      * @access public
00376      * @param  str   $table
00377      * @param  array $array
00378      * @param  str   $where
00379      * @param  mixed $savepoint
00380      * @return mixed
00381      */
00382     public function update( $table, $array, $where=false, $savepoint=false )
00383     {
00384         //$array MUST be a key value pair array
00385         $objColumns = array_keys( $array );
00386         $objValues  = array_values( $array );
00387         $objUpdate  = '';
00388 
00389         for ( $i = 0; $i < count( $array ); $i++ )
00390         {
00391             if ( !$objValues[$i] )
00392             {
00393                 if ( is_numeric( $objValues[$i] ) )
00394                     $objValues[$i] = 0;
00395                 elseif ( is_bool( $objValues[$i] ) )
00396                     $objValues[$i] = false;
00397                 else
00398                     $objValues[$i] = '';
00399             }
00400 
00401             if ( is_string( $objValues[$i] ) && !is_numeric( $objValues[$i] ) )
00402                 $objUpdate .= $objColumns[$i] . "='" . $objValues[$i] . "',";
00403             else
00404                 $objUpdate .= $objColumns[$i] . '=' . $objValues[$i] . ',';
00405         }
00406 
00407         $objUpdate = rtrim( $objUpdate, ',' );
00408         $objWhere  = ( !$where ) ? '' : ' WHERE ' . $where;
00409         $objQuery  = 'UPDATE ' . $table . ' SET ' . $objUpdate . $objWhere;
00410 
00411         $objStatement = $this->objDbType . 'Statement';
00412         $stmt = new $objStatement( $objQuery, $this, $this->objAutoCommit, $savepoint );
00413 
00414         return $stmt->objExecuteNonPrepared();
00415     }
00416 }
00417 
00418 ?>

Generated on Sat Jul 10 2010 15:14:39 for objSQL 2.1.0 by  doxygen 1.7.1