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: if (isset($connectConfig['options']) && is_array($connectConfig['options'])) {
97: foreach ($connectConfig['options'] as $optKey => $optVal) {
98: mysqli_options($dbHandler, $optKey, $optVal);
99: }
100: }
101:
102: if (($iPos = strpos($connectConfig['host'], ':')) !== false) {
103: $hostData = explode(':', $connectConfig['host']);
104: $connectConfig['host'] = $hostData[0];
105: if (is_numeric($hostData[1])) {
106: $connectConfig['port'] = $hostData[1];
107: } else {
108: $connectConfig['socket'] = $hostData[1];
109: }
110: }
111:
112: if (!isset($connectConfig['port'])) {
113: $connectConfig['port'] = NULL;
114: }
115: if (!isset($connectConfig['socket'])) {
116: $connectConfig['socket'] = NULL;
117: }
118:
119: if (!isset($connectConfig['flags'])) {
120: $connectConfig['flags'] = NULL;
121: }
122: if (!isset($connectConfig['database'])) {
123: $connectConfig['database'] = NULL;
124: }
125:
126: $res = mysqli_real_connect($dbHandler, $connectConfig['host'], $connectConfig['user'], $connectConfig['password'], $connectConfig['database'], $connectConfig['port'], $connectConfig['socket'], $connectConfig['flags']);
127:
128:
129: if (false === $res) {
130: $this->_handler->halt('MySQLi _connect() Error connecting to database ' . $connectConfig['database']);
131: return NULL;
132: }
133:
134: if ($res && $dbHandler && $connectConfig['database']) {
135: if (!@mysqli_select_db($dbHandler, $connectConfig['database'])) {
136: $this->_handler->halt('MySQLi _connect() Cannot use database ' . $connectConfig['database']);
137: return NULL;
138: } else {
139:
140: if (isset($connectConfig['charset']) && $connectConfig['charset'] != '') {
141: if (!@mysqli_set_charset($dbHandler, $connectConfig['charset'])) {
142: $this->_handler->halt('Could not set database charset to ' . $connectConfig['charset']);
143: return NULL;
144: }
145: }
146: }
147: }
148:
149: return $dbHandler;
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163:
164: public function buildInsert($tableName, array $fields) {
165: $fieldList = '';
166: $valueList = '';
167:
168: foreach ($fields as $field => $value) {
169: $fieldList .= '`' . $field . '`, ';
170: if (is_int($value)) {
171: $valueList .= $value . ', ';
172: } else {
173: $valueList .= "'" . $this->escape($value) . "', ";
174: }
175: }
176:
177: $fieldList = substr($fieldList, 0, -2);
178: $valueList = substr($valueList, 0, -2);
179: return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $tableName, $fieldList, $valueList);
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: public function buildUpdate($tableName, array $fields, array $whereClauses) {
198: $updateList = '';
199: $whereList = '';
200:
201: foreach ($fields as $field => $value) {
202: $updateList .= '`' . $field . '`=';
203: if (is_int($value)) {
204: $updateList .= $value . ', ';
205: } else {
206: $updateList .= "'" . $this->escape($value) . "', ";
207: }
208: }
209:
210: foreach ($whereClauses as $field => $value) {
211: $whereList .= '`' . $field . '`=';
212: if (is_int($value)) {
213: $whereList .= $value . ' AND ';
214: } else {
215: $whereList .= "'" . $this->escape($value) . "' AND ";
216: }
217: }
218:
219: $updateList = substr($updateList, 0, -2);
220: $whereList = substr($whereList, 0, -5);
221:
222: return sprintf('UPDATE `%s` SET %s WHERE %s', $tableName, $updateList, $whereList);
223: }
224:
225: 226: 227: 228: 229: 230: 231:
232: public function query($query) {
233: $linkId = $this->_handler->getLinkId();
234: $queryId = mysqli_query($linkId, $query);
235:
236: $this->_handler->setQueryId($queryId);
237: $this->_handler->setRow(0);
238: $this->_handler->setErrorNumber($this->getErrorNumber());
239: $this->_handler->setErrorMessage($this->getErrorMessage());
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249:
250: public function nextRecord() {
251: $queryId = $this->_handler->getQueryId();
252: $record = mysqli_fetch_array($queryId, MYSQLI_BOTH);
253:
254: $this->_handler->setRecord($record);
255: $this->_handler->incrementRow();
256: $this->_handler->setErrorNumber($this->getErrorNumber());
257: $this->_handler->setErrorMessage($this->getErrorMessage());
258:
259: return is_array($record);
260: }
261:
262: 263: 264: 265: 266: 267: 268: 269: 270:
271: public function getResultObject($className = NULL) {
272: $result = NULL;
273: $queryId = $this->_handler->getQueryId();
274:
275: if ($queryId) {
276: if ($className == NULL) {
277: $result = mysqli_fetch_object($queryId);
278: } else {
279: $result = mysqli_fetch_object($queryId, $className);
280: }
281: }
282:
283: return $result;
284: }
285:
286: 287: 288: 289: 290: 291: 292:
293: public function affectedRows() {
294: $linkId = $this->_handler->getLinkId();
295: return ($linkId) ? mysqli_affected_rows($linkId) : 0;
296: }
297:
298: 299: 300: 301: 302: 303: 304:
305: public function numRows() {
306: $queryId = $this->_handler->getQueryId();
307: return ($queryId) ? mysqli_num_rows($queryId) : 0;
308: }
309:
310: 311: 312: 313: 314: 315: 316:
317: public function numFields() {
318: $queryId = $this->_handler->getQueryId();
319: return ($queryId) ? mysqli_num_fields($queryId) : 0;
320: }
321:
322: 323: 324: 325: 326: 327: 328: 329: 330:
331: public function free() {
332: if (!is_object($this->_handler->getQueryId())) {
333: return $this;
334: }
335:
336: mysqli_free_result($this->_handler->getQueryId());
337: $this->_handler->setQueryId(0);
338: }
339:
340: 341: 342: 343: 344: 345: 346: 347: 348:
349: public function escape($string) {
350: $linkId = $this->_handler->getLinkId();
351: return mysqli_real_escape_string($linkId, $string);
352: }
353:
354: 355: 356: 357: 358: 359: 360: 361:
362: public function seek($pos = 0) {
363: $queryId = $this->_handler->getQueryId();
364:
365: $status = mysqli_data_seek($queryId, $pos);
366: if ($status) {
367: $this->_handler->setRow($pos);
368: } else {
369: return 0;
370: }
371:
372: return 1;
373: }
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: public function getMetaData($tableName, $full = false) {
411: $res = array();
412:
413: $this->query(sprintf('SELECT * FROM `%s` LIMIT 1', $tableName));
414:
415: $id = $this->_handler->getQueryId();
416: if (!$id) {
417: $this->_handler->halt('Metadata query failed.');
418: return false;
419: }
420:
421:
422: $count = mysqli_num_fields($id);
423: for ($i = 0; $i < $count; $i++) {
424: $finfo = mysqli_fetch_field($id);
425: $res[$i]['table'] = $finfo->table;
426: $res[$i]['name'] = $finfo->name;
427: $res[$i]['type'] = $this->_dataTypes[$finfo->type];
428: $res[$i]['len'] = $finfo->max_length;
429: $res[$i]['flags'] = $finfo->flags;
430: if ($full) {
431: $res['meta'][$res[$i]['name']] = $i;
432: }
433: }
434: if ($full) {
435: $res['num_fields'] = $count;
436: }
437:
438: $this->free();
439:
440: return $res;
441: }
442:
443: 444: 445: 446: 447: 448:
449: public function getTableNames() {
450: $return = array();
451: if ($this->query('SHOW TABLES')) {
452: while ($this->nextRecord()) {
453: $record = $this->getRecord();
454: $return[] = array(
455: 'table_name' => $record[0],
456: 'tablespace_name' => $this->_dbCfg['connection']['database'],
457: 'database' => $this->_dbCfg['connection']['database']
458: );
459: }
460:
461: $this->free();
462: }
463: return $return;
464: }
465:
466: 467: 468: 469: 470: 471:
472: public function getServerInfo() {
473: $linkId = $this->_handler->getLinkId();
474:
475: if ($linkId) {
476: $arr = array();
477: $arr['description'] = mysqli_get_server_info($linkId);
478: return $arr;
479: }
480:
481: return NULL;
482: }
483:
484: 485: 486: 487: 488: 489:
490: public function getErrorNumber() {
491: $linkId = $this->_handler->getLinkId();
492:
493: if ($linkId) {
494: return @mysqli_errno($linkId);
495: } else {
496: return @mysqli_connect_errno();
497: }
498: }
499:
500: 501: 502: 503: 504: 505:
506: public function getErrorMessage() {
507: $linkId = $this->_handler->getLinkId();
508:
509: if ($linkId) {
510: return @mysqli_error($linkId);
511: } else {
512: return @mysqli_connect_error();
513: }
514: }
515:
516: 517: 518: 519: 520:
521: public function disconnect() {
522: mysqli_close($this->_handler->getLinkId());
523: }
524:
525: }
526: