1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
38: class cDbDriverMysqli extends cDbDriverAbstract {
39:
40: 41: 42: 43:
44: protected $_dataTypes = array(
45: 0 => 'decimal',
46: 1 => 'tinyint',
47: 2 => 'smallint',
48: 3 => 'int',
49: 4 => 'float',
50: 5 => 'double',
51: 7 => 'timestamp',
52: 8 => 'bigint',
53: 9 => 'mediumint',
54: 10 => 'date',
55: 11 => 'time',
56: 12 => 'datetime',
57: 13 => 'year',
58: 252 => 'blob',
59: 253 => 'string',
60: 254 => 'enum'
61: );
62:
63: 64: 65: 66: 67: 68: 69:
70: public function check() {
71: return extension_loaded('mysqli');
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function connect() {
82: $dbHandler = @mysqli_init();
83: if (!$dbHandler || $dbHandler->connect_error != "" || $dbHandler->error != "") {
84: $this->_handler->halt('Can not initialize database connection.');
85: return NULL;
86: }
87:
88: if (isset($this->_dbCfg['connection'])) {
89: $connectConfig = $this->_dbCfg['connection'];
90: }
91: if (empty($connectConfig) || !isset($connectConfig['host']) || !isset($connectConfig['user']) || !isset($connectConfig['password'])) {
92: $this->_handler->halt('Database connection settings incomplete');
93: return NULL;
94: }
95:
96:
97: if (isset($connectConfig['options']) && is_array($connectConfig['options'])) {
98: foreach ($connectConfig['options'] as $optKey => $optVal) {
99: mysqli_options($dbHandler, $optKey, $optVal);
100: }
101: }
102:
103: if (($iPos = strpos($connectConfig['host'], ':')) !== false) {
104: $hostData = explode(':', $connectConfig['host']);
105: $connectConfig['host'] = $hostData[0];
106: if (is_numeric($hostData[1])) {
107: $connectConfig['port'] = $hostData[1];
108: } else {
109: $connectConfig['socket'] = $hostData[1];
110: }
111: }
112:
113: if (!isset($connectConfig['port'])) {
114: $connectConfig['port'] = NULL;
115: }
116: if (!isset($connectConfig['socket'])) {
117: $connectConfig['socket'] = NULL;
118: }
119:
120: if (!isset($connectConfig['flags'])) {
121: $connectConfig['flags'] = NULL;
122: }
123: if (!isset($connectConfig['database'])) {
124: $connectConfig['database'] = NULL;
125: }
126:
127: $res = mysqli_real_connect($dbHandler, $connectConfig['host'], $connectConfig['user'], $connectConfig['password'], $connectConfig['database'], $connectConfig['port'], $connectConfig['socket'], $connectConfig['flags']);
128:
129:
130: if (false === $res) {
131: $this->_handler->halt('MySQLi _connect() Error connecting to database ' . $connectConfig['database']);
132: return NULL;
133: }
134:
135: if ($res && $dbHandler && $connectConfig['database']) {
136: if (!@mysqli_select_db($dbHandler, $connectConfig['database'])) {
137: $this->_handler->halt('MySQLi _connect() Cannot use database ' . $connectConfig['database']);
138: return NULL;
139: } else {
140:
141: if (isset($connectConfig['charset']) && $connectConfig['charset'] != '') {
142: if (!@mysqli_set_charset($dbHandler, $connectConfig['charset'])) {
143: $this->_handler->halt('Could not set database charset to ' . $connectConfig['charset']);
144: return NULL;
145: }
146: }
147: }
148: }
149:
150: return $dbHandler;
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164:
165: public function buildInsert($tableName, array $fields) {
166: $fieldList = '';
167: $valueList = '';
168:
169: foreach ($fields as $field => $value) {
170: $fieldList .= '`' . $field . '`, ';
171: if (is_int($value)) {
172: $valueList .= $value . ', ';
173: } else {
174: $valueList .= "'" . $this->escape($value) . "', ";
175: }
176: }
177:
178: $fieldList = substr($fieldList, 0, -2);
179: $valueList = substr($valueList, 0, -2);
180: return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $tableName, $fieldList, $valueList);
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197:
198: public function buildUpdate($tableName, array $fields, array $whereClauses) {
199: $updateList = '';
200: $whereList = '';
201:
202: foreach ($fields as $field => $value) {
203: $updateList .= '`' . $field . '`=';
204: if (is_int($value)) {
205: $updateList .= $value . ', ';
206: } else {
207: $updateList .= "'" . $this->escape($value) . "', ";
208: }
209: }
210:
211: foreach ($whereClauses as $field => $value) {
212: $whereList .= '`' . $field . '`=';
213: if (is_int($value)) {
214: $whereList .= $value . ' AND ';
215: } else {
216: $whereList .= "'" . $this->escape($value) . "' AND ";
217: }
218: }
219:
220: $updateList = substr($updateList, 0, -2);
221: $whereList = substr($whereList, 0, -5);
222:
223: return sprintf('UPDATE `%s` SET %s WHERE %s', $tableName, $updateList, $whereList);
224: }
225:
226: 227: 228: 229: 230: 231: 232:
233: public function query($query) {
234: $linkId = $this->_handler->getLinkId();
235: $queryId = mysqli_query($linkId, $query);
236:
237: $this->_handler->setQueryId($queryId);
238: $this->_handler->setRow(0);
239: $this->_handler->setErrorNumber($this->getErrorNumber());
240: $this->_handler->setErrorMessage($this->getErrorMessage());
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250:
251: public function nextRecord() {
252: $queryId = $this->_handler->getQueryId();
253: $record = mysqli_fetch_array($queryId, MYSQLI_BOTH);
254:
255: $this->_handler->setRecord($record);
256: $this->_handler->incrementRow();
257: $this->_handler->setErrorNumber($this->getErrorNumber());
258: $this->_handler->setErrorMessage($this->getErrorMessage());
259:
260: return is_array($record);
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270: 271:
272: public function getResultObject($className = NULL) {
273: $result = NULL;
274: $queryId = $this->_handler->getQueryId();
275:
276: if ($queryId) {
277: if ($className == NULL) {
278: $result = mysqli_fetch_object($queryId);
279: } else {
280: $result = mysqli_fetch_object($queryId, $className);
281: }
282: }
283:
284: return $result;
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: public function affectedRows() {
295: $linkId = $this->_handler->getLinkId();
296: return ($linkId) ? mysqli_affected_rows($linkId) : 0;
297: }
298:
299: 300: 301: 302: 303: 304: 305:
306: public function numRows() {
307: $queryId = $this->_handler->getQueryId();
308: return ($queryId) ? mysqli_num_rows($queryId) : 0;
309: }
310:
311: 312: 313: 314: 315: 316: 317:
318: public function numFields() {
319: $queryId = $this->_handler->getQueryId();
320: return ($queryId) ? mysqli_num_fields($queryId) : 0;
321: }
322:
323: 324: 325: 326: 327: 328: 329: 330: 331:
332: public function free() {
333: if (!is_object($this->_handler->getQueryId())) {
334: return $this;
335: }
336:
337: mysqli_free_result($this->_handler->getQueryId());
338: $this->_handler->setQueryId(0);
339: }
340:
341: 342: 343: 344: 345: 346: 347: 348: 349:
350: public function escape($string) {
351: $linkId = $this->_handler->getLinkId();
352: return mysqli_real_escape_string($linkId, $string);
353: }
354:
355: 356: 357: 358: 359: 360: 361: 362:
363: public function seek($pos = 0) {
364: $queryId = $this->_handler->getQueryId();
365:
366: $status = mysqli_data_seek($queryId, $pos);
367: if ($status) {
368: $this->_handler->setRow($pos);
369: } else {
370: return 0;
371: }
372:
373: return 1;
374: }
375:
376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410:
411: public function getMetaData($tableName, $full = false) {
412: $res = array();
413:
414: $this->query(sprintf('SELECT * FROM `%s` LIMIT 1', $tableName));
415:
416: $id = $this->_handler->getQueryId();
417: if (!$id) {
418: $this->_handler->halt('Metadata query failed.');
419: return false;
420: }
421:
422:
423: $count = mysqli_num_fields($id);
424: for ($i = 0; $i < $count; $i++) {
425: $finfo = mysqli_fetch_field($id);
426: $res[$i]['table'] = $finfo->table;
427: $res[$i]['name'] = $finfo->name;
428: $res[$i]['type'] = $this->_dataTypes[$finfo->type];
429: $res[$i]['len'] = $finfo->max_length;
430: $res[$i]['flags'] = $finfo->flags;
431: if ($full) {
432: $res['meta'][$res[$i]['name']] = $i;
433: }
434: }
435: if ($full) {
436: $res['num_fields'] = $count;
437: }
438:
439: $this->free();
440:
441: return $res;
442: }
443:
444: 445: 446: 447: 448: 449:
450: public function getTableNames() {
451: $return = array();
452: if ($this->query('SHOW TABLES')) {
453: while ($this->nextRecord()) {
454: $record = $this->getRecord();
455: $return[] = array(
456: 'table_name' => $record[0],
457: 'tablespace_name' => $this->_dbCfg['connection']['database'],
458: 'database' => $this->_dbCfg['connection']['database']
459: );
460: }
461:
462: $this->free();
463: }
464: return $return;
465: }
466:
467: 468: 469: 470: 471: 472:
473: public function getServerInfo() {
474: $linkId = $this->_handler->getLinkId();
475:
476: if ($linkId) {
477: $arr = array();
478: $arr['description'] = mysqli_get_server_info($linkId);
479: return $arr;
480: }
481:
482: return NULL;
483: }
484:
485: 486: 487: 488: 489: 490:
491: public function getErrorNumber() {
492: $linkId = $this->_handler->getLinkId();
493:
494: if ($linkId) {
495: return @mysqli_errno($linkId);
496: } else {
497: return @mysqli_connect_errno();
498: }
499: }
500:
501: 502: 503: 504: 505: 506:
507: public function getErrorMessage() {
508: $linkId = $this->_handler->getLinkId();
509:
510: if ($linkId) {
511: return @mysqli_error($linkId);
512: } else {
513: return @mysqli_connect_error();
514: }
515: }
516:
517: 518: 519: 520: 521:
522: public function disconnect() {
523: mysqli_close($this->_handler->getLinkId());
524: }
525:
526: }
527: