00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 class objSQL
00016 {
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 private $objAutoCommit = true;
00028
00029
00030
00031
00032
00033
00034
00035 private $objConnection = false;
00036
00037
00038
00039
00040
00041
00042
00043 private $objConnectClass;
00044
00045
00046
00047
00048
00049
00050
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
00066
00067
00068
00069
00070 private $objDbType;
00071
00072
00073
00074
00075
00076
00077
00078 private $objSeqArray = array( 'db2',
00079 'firebird',
00080 'ingres',
00081 'maxdb',
00082 'oracle',
00083 'pgsql' );
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
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
00105
00106
00107
00108
00109 public function close()
00110 {
00111 $objDbClose = $this->objConnectClass->objDbClose();
00112 $this->objConnection = false;
00113
00114 return $objDbClose;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 public function connection()
00124 {
00125 return $this->objConnection;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
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
00151
00152
00153
00154
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
00175
00176
00177
00178
00179 public function dbtype()
00180 {
00181 return $this->objDbType;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
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
00206
00207
00208
00209
00210
00211
00212
00213 public function insert( $table, $array, $savepoint=false )
00214 {
00215
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
00243
00244
00245
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
00270
00271
00272
00273
00274
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
00286
00287
00288
00289
00290
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
00301
00302
00303
00304
00305
00306
00307
00308
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
00327
00328
00329
00330
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
00347
00348
00349
00350
00351 public function server_version()
00352 {
00353 return $this->objConnectClass->objServerVersion();
00354 }
00355
00356
00357
00358
00359
00360
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
00374
00375
00376
00377
00378
00379
00380
00381
00382 public function update( $table, $array, $where=false, $savepoint=false )
00383 {
00384
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 ?>