00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class DboMysqli extends DboSource {
00039
00040
00041
00042
00043
00044 var $description = "Mysqli DBO Driver";
00045
00046
00047
00048
00049
00050 var $startQuote = "`";
00051
00052
00053
00054
00055
00056 var $endQuote = "`";
00057
00058
00059
00060
00061
00062 var $_baseConfig = array('persistent' => true,
00063 'host' => 'localhost',
00064 'login' => 'root',
00065 'password' => '',
00066 'database' => 'cake',
00067 'port' => '3306',
00068 'connect' => 'mysqli_connect');
00069
00070
00071
00072
00073
00074 var $columns = array('primary_key' => array('name' => 'int(11) DEFAULT NULL auto_increment'),
00075 'string' => array('name' => 'varchar', 'limit' => '255'),
00076 'text' => array('name' => 'text'),
00077 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
00078 'float' => array('name' => 'float', 'formatter' => 'floatval'),
00079 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
00080 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
00081 'time' => array('name' => 'time', 'format' => 'H:i:s', 'formatter' => 'date'),
00082 'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
00083 'binary' => array('name' => 'blob'),
00084 'boolean' => array('name' => 'tinyint', 'limit' => '1'));
00085
00086
00087
00088
00089
00090 function connect() {
00091 $config = $this->config;
00092 $this->connected = false;
00093 $this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database'], $config['port']);
00094
00095 if ($this->connection !== false) {
00096 $this->connected = true;
00097 }
00098
00099 if (!empty($config['encoding'])) {
00100 $this->setEncoding($config['encoding']);
00101 }
00102 return $this->connected;
00103 }
00104
00105
00106
00107
00108
00109 function disconnect() {
00110 @mysqli_free_result($this->results);
00111 $this->connected = !@mysqli_close($this->connection);
00112 return !$this->connected;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 function _execute($sql) {
00122 return mysqli_query($this->connection, $sql);
00123 }
00124
00125
00126
00127
00128
00129 function listSources() {
00130 $cache = parent::listSources();
00131 if ($cache != null) {
00132 return $cache;
00133 }
00134 $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
00135
00136 if (!$result) {
00137 return array();
00138 } else {
00139 $tables = array();
00140
00141 while ($line = mysqli_fetch_array($result)) {
00142 $tables[] = $line[0];
00143 }
00144 parent::listSources($tables);
00145 return $tables;
00146 }
00147 }
00148
00149
00150
00151
00152
00153
00154 function describe(&$model) {
00155
00156 $cache = parent::describe($model);
00157 if ($cache != null) {
00158 return $cache;
00159 }
00160
00161 $fields = false;
00162 $cols = $this->query('DESCRIBE ' . $this->fullTableName($model));
00163
00164 foreach ($cols as $column) {
00165 $colKey = array_keys($column);
00166 if (isset($column[$colKey[0]]) && !isset($column[0])) {
00167 $column[0] = $column[$colKey[0]];
00168 }
00169 if (isset($column[0])) {
00170 $fields[$column[0]['Field']] = array(
00171 'type' => $this->column($column[0]['Type']),
00172 'null' => ($column[0]['Null'] == 'YES' ? true : false),
00173 'default' => $column[0]['Default'],
00174 'length' => $this->length($column[0]['Type'])
00175 );
00176 }
00177 }
00178
00179 $this->__cacheDescription($this->fullTableName($model, false), $fields);
00180 return $fields;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190 function value($data, $column = null, $safe = false) {
00191 $parent = parent::value($data, $column, $safe);
00192
00193 if ($parent != null) {
00194 return $parent;
00195 }
00196
00197 if ($data === null) {
00198 return 'NULL';
00199 }
00200
00201 if ($data === '') {
00202 return "''";
00203 }
00204
00205 switch ($column) {
00206 case 'boolean':
00207 $data = $this->boolean((bool)$data);
00208 break;
00209 case 'integer' :
00210 case 'float' :
00211 case null :
00212 if (is_numeric($data) && strpos($data, ',') === false && $data[0] != '0' && strpos($data, 'e') === false) {
00213 break;
00214 }
00215 default:
00216 $data = "'" . mysqli_real_escape_string($this->connection, $data) . "'";
00217 break;
00218 }
00219
00220 return $data;
00221 }
00222
00223
00224
00225
00226
00227
00228
00229 function begin(&$model) {
00230 if (parent::begin($model)) {
00231 if ($this->execute('START TRANSACTION')) {
00232 $this->_transactionStarted = true;
00233 return true;
00234 }
00235 }
00236 return false;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246 function commit(&$model) {
00247 if (parent::commit($model)) {
00248 $this->_transactionStarted = false;
00249 return $this->execute('COMMIT');
00250 }
00251 return false;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261 function rollback(&$model) {
00262 if (parent::rollback($model)) {
00263 return $this->execute('ROLLBACK');
00264 }
00265 return false;
00266 }
00267
00268
00269
00270
00271
00272 function lastError() {
00273 if (mysqli_errno($this->connection)) {
00274 return mysqli_errno($this->connection).': '.mysqli_error($this->connection);
00275 }
00276 return null;
00277 }
00278
00279
00280
00281
00282
00283
00284 function lastAffected() {
00285 if ($this->_result) {
00286 return mysqli_affected_rows($this->connection);
00287 }
00288 return null;
00289 }
00290
00291
00292
00293
00294
00295
00296 function lastNumRows() {
00297 if ($this->_result and is_object($this->_result)) {
00298 return @mysqli_num_rows($this->_result);
00299 }
00300 return null;
00301 }
00302
00303
00304
00305
00306
00307
00308 function lastInsertId($source = null) {
00309 $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false);
00310 if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) {
00311 return $id[0]['insertID'];
00312 }
00313
00314 return null;
00315 }
00316
00317
00318
00319
00320
00321
00322 function column($real) {
00323 if (is_array($real)) {
00324 $col = $real['name'];
00325 if (isset($real['limit'])) {
00326 $col .= '('.$real['limit'].')';
00327 }
00328 return $col;
00329 }
00330
00331 $col = r(')', '', $real);
00332 $limit = $this->length($real);
00333 @list($col,$vals) = explode('(', $col);
00334
00335 if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
00336 return $col;
00337 }
00338 if ($col == 'tinyint' && $limit == 1) {
00339 return 'boolean';
00340 }
00341 if (strpos($col, 'int') !== false) {
00342 return 'integer';
00343 }
00344 if (strpos($col, 'char') !== false || $col == 'tinytext') {
00345 return 'string';
00346 }
00347 if (strpos($col, 'text') !== false) {
00348 return 'text';
00349 }
00350 if (strpos($col, 'blob') !== false) {
00351 return 'binary';
00352 }
00353 if (in_array($col, array('float', 'double', 'decimal'))) {
00354 return 'float';
00355 }
00356 if (strpos($col, 'enum') !== false) {
00357 return "enum($vals)";
00358 }
00359 if ($col == 'boolean') {
00360 return $col;
00361 }
00362 return 'text';
00363 }
00364
00365
00366
00367
00368
00369
00370 function length($real) {
00371 $col = r(array(')', 'unsigned'), '', $real);
00372 $limit = null;
00373
00374 if (strpos($col, '(') !== false) {
00375 list($col, $limit) = explode('(', $col);
00376 }
00377
00378 if ($limit != null) {
00379 return intval($limit);
00380 }
00381 return null;
00382 }
00383
00384
00385
00386
00387
00388 function resultSet(&$results) {
00389 $this->results =& $results;
00390 $this->map = array();
00391 $num_fields = mysqli_num_fields($results);
00392 $index = 0;
00393 $j = 0;
00394 while ($j < $num_fields) {
00395 $column = mysqli_fetch_field_direct($results, $j);
00396 if (!empty($column->table)) {
00397 $this->map[$index++] = array($column->table, $column->name);
00398 } else {
00399 $this->map[$index++] = array(0, $column->name);
00400 }
00401 $j++;
00402 }
00403 }
00404
00405
00406
00407
00408
00409 function fetchResult() {
00410 if ($row = mysqli_fetch_row($this->results)) {
00411 $resultRow = array();
00412 $i = 0;
00413 foreach ($row as $index => $field) {
00414 @list($table, $column) = $this->map[$index];
00415 $resultRow[$table][$column] = $row[$index];
00416 $i++;
00417 }
00418 return $resultRow;
00419 } else {
00420 return false;
00421 }
00422 }
00423
00424
00425
00426
00427
00428
00429 function setEncoding($enc) {
00430 return $this->_execute('SET NAMES ' . $enc) != false;
00431 }
00432
00433
00434
00435
00436
00437 function getEncoding() {
00438 return mysqli_client_encoding($this->connection);
00439 }
00440 }
00441 ?>