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: public function check() {
68: return extension_loaded('mysqli');
69: }
70:
71: 72: 73: 74:
75: public function connect() {
76: $dbHandler = @mysqli_init();
77: if (!$dbHandler) {
78: $this->_handler->halt('Can not initialize database connection.');
79: return NULL;
80: }
81:
82: if (isset($this->_dbCfg['connection'])) {
83: $connectConfig = $this->_dbCfg['connection'];
84: }
85: if (empty($connectConfig) || !isset($connectConfig['host']) || !isset($connectConfig['user']) || !isset($connectConfig['password'])) {
86: $this->_handler->halt('Database connection settings incomplete');
87: return NULL;
88: }
89:
90: if (isset($connectConfig['options']) && is_array($connectConfig['options'])) {
91: foreach ($connectConfig['options'] as $optKey => $optVal) {
92: mysqli_options($dbHandler, $optKey, $optVal);
93: }
94: }
95:
96: if (($iPos = strpos($connectConfig['host'], ':')) !== false) {
97: $hostData = explode(':', $connectConfig['host']);
98: $connectConfig['host'] = $hostData[0];
99: if (is_numeric($hostData[1])) {
100: $connectConfig['port'] = $hostData[1];
101: } else {
102: $connectConfig['socket'] = $hostData[1];
103: }
104: }
105:
106: if (!isset($connectConfig['port'])) {
107: $connectConfig['port'] = NULL;
108: }
109: if (!isset($connectConfig['socket'])) {
110: $connectConfig['socket'] = NULL;
111: }
112:
113: if (!isset($connectConfig['flags'])) {
114: $connectConfig['flags'] = NULL;
115: }
116: if (!isset($connectConfig['database'])) {
117: $connectConfig['database'] = NULL;
118: }
119:
120: $res = mysqli_real_connect($dbHandler, $connectConfig['host'], $connectConfig['user'], $connectConfig['password'], $connectConfig['database'], $connectConfig['port'], $connectConfig['socket'], $connectConfig['flags']);
121:
122: if ($res && $dbHandler && $connectConfig['database']) {
123: if (!@mysqli_select_db($dbHandler, $connectConfig['database'])) {
124: $this->_handler->halt('MySQLi _connect() Cannot use database ' . $connectConfig['database']);
125: return NULL;
126: } else {
127:
128: if (isset($connectConfig['charset']) && $connectConfig['charset'] != '') {
129: if (!@mysqli_set_charset($dbHandler, $connectConfig['charset'])) {
130: $this->_handler->halt('Could not set database charset to ' . $connectConfig['charset']);
131: return NULL;
132: }
133: }
134: }
135: }
136:
137: return $dbHandler;
138: }
139:
140: 141: 142: 143:
144: public function buildInsert($tableName, array $fields) {
145: $fieldList = '';
146: $valueList = '';
147:
148: foreach ($fields as $field => $value) {
149: $fieldList .= '`' . $field . '`, ';
150: if (is_int($value)) {
151: $valueList .= $value . ', ';
152: } else {
153: $valueList .= "'" . $this->escape($value) . "', ";
154: }
155: }
156:
157: $fieldList = substr($fieldList, 0, -2);
158: $valueList = substr($valueList, 0, -2);
159: return sprintf('INSERT INTO `%s` (%s) VALUES (%s)', $tableName, $fieldList, $valueList);
160: }
161:
162: 163: 164: 165:
166: public function buildUpdate($tableName, array $fields, array $whereClauses) {
167: $updateList = '';
168: $whereList = '';
169:
170: foreach ($fields as $field => $value) {
171: $updateList .= '`' . $field . '`=';
172: if (is_int($value)) {
173: $updateList .= $value . ', ';
174: } else {
175: $updateList .= "'" . $this->escape($value) . "', ";
176: }
177: }
178:
179: foreach ($whereClauses as $field => $value) {
180: $whereList .= '`' . $field . '`=';
181: if (is_int($value)) {
182: $whereList .= $value . ' AND ';
183: } else {
184: $whereList .= "'" . $this->escape($value) . "' AND ";
185: }
186: }
187:
188: $updateList = substr($updateList, 0, -2);
189: $whereList = substr($whereList, 0, -5);
190:
191: return sprintf('UPDATE `%s` SET %s WHERE %s', $tableName, $updateList, $whereList);
192: }
193:
194: 195: 196: 197:
198: public function query($query) {
199: $linkId = $this->_handler->getLinkId();
200: $queryId = mysqli_query($linkId, $query);
201:
202: $this->_handler->setQueryId($queryId);
203: $this->_handler->setRow(0);
204: $this->_handler->setErrorNumber($this->getErrorNumber());
205: $this->_handler->setErrorMessage($this->getErrorMessage());
206: }
207:
208: 209: 210: 211:
212: public function nextRecord() {
213: $queryId = $this->_handler->getQueryId();
214: $record = mysqli_fetch_array($queryId, MYSQLI_BOTH);
215:
216: $this->_handler->setRecord($record);
217: $this->_handler->incrementRow();
218: $this->_handler->setErrorNumber($this->getErrorNumber());
219: $this->_handler->setErrorMessage($this->getErrorMessage());
220:
221: return is_array($record);
222: }
223:
224: 225: 226: 227:
228: public function getResultObject($className = NULL) {
229: $result = NULL;
230: $queryId = $this->_handler->getQueryId();
231:
232: if ($queryId) {
233: if ($className == NULL) {
234: $result = mysqli_fetch_object($queryId);
235: } else {
236: $result = mysqli_fetch_object($queryId, $className);
237: }
238: }
239:
240: return $result;
241: }
242:
243: 244: 245: 246:
247: public function affectedRows() {
248: $linkId = $this->_handler->getLinkId();
249: return ($linkId) ? mysqli_affected_rows($linkId) : 0;
250: }
251:
252: 253: 254: 255:
256: public function numRows() {
257: $queryId = $this->_handler->getQueryId();
258: return ($queryId) ? mysqli_num_rows($queryId) : 0;
259: }
260:
261: 262: 263: 264:
265: public function numFields() {
266: $queryId = $this->_handler->getQueryId();
267: return ($queryId) ? mysqli_num_fields($queryId) : 0;
268: }
269:
270: 271: 272: 273:
274: public function free() {
275: if (!is_object($this->_handler->getQueryId())) {
276: return $this;
277: }
278:
279: mysqli_free_result($this->_handler->getQueryId());
280: $this->_handler->setQueryId(0);
281: }
282:
283: 284: 285: 286:
287: public function escape($string) {
288: $linkId = $this->_handler->getLinkId();
289: return mysqli_real_escape_string($linkId, $string);
290: }
291:
292: 293: 294: 295:
296: public function seek($pos = 0) {
297: $queryId = $this->_handler->getQueryId();
298:
299: $status = mysqli_data_seek($queryId, $pos);
300: if ($status) {
301: $this->_handler->setRow($pos);
302: } else {
303: return 0;
304: }
305:
306: return 1;
307: }
308:
309: 310: 311: 312:
313: public function getMetaData($tableName, $full = false) {
314: $res = array();
315:
316: $this->query(sprintf('SELECT * FROM `%s` LIMIT 1', $tableName));
317:
318: $id = $this->_handler->getQueryId();
319: if (!$id) {
320: $this->_handler->halt('Metadata query failed.');
321: return false;
322: }
323:
324:
325: $count = mysqli_num_fields($id);
326: for ($i = 0; $i < $count; $i++) {
327: $finfo = mysqli_fetch_field($id);
328: $res[$i]['table'] = $finfo->table;
329: $res[$i]['name'] = $finfo->name;
330: $res[$i]['type'] = $this->_dataTypes[$finfo->type];
331: $res[$i]['len'] = $finfo->max_length;
332: $res[$i]['flags'] = $finfo->flags;
333: if ($full) {
334: $res['meta'][$res[$i]['name']] = $i;
335: }
336: }
337: if ($full) {
338: $res['num_fields'] = $count;
339: }
340:
341: $this->free();
342:
343: return $res;
344: }
345:
346: 347: 348: 349:
350: public function getTableNames() {
351: $return = array();
352: if ($this->query('SHOW TABLES')) {
353: while ($this->nextRecord()) {
354: $record = $this->getRecord();
355: $return[] = array(
356: 'table_name' => $record[0],
357: 'tablespace_name' => $this->_dbCfg['connection']['database'],
358: 'database' => $this->_dbCfg['connection']['database']
359: );
360: }
361:
362: $this->free();
363: }
364: return $return;
365: }
366:
367: 368: 369: 370:
371: public function getServerInfo() {
372: $linkId = $this->_handler->getLinkId();
373:
374: if ($linkId) {
375: $arr = array();
376: $arr['description'] = mysqli_get_server_info($linkId);
377: return $arr;
378: }
379:
380: return NULL;
381: }
382:
383: 384: 385: 386:
387: public function getErrorNumber() {
388: $linkId = $this->_handler->getLinkId();
389:
390: if ($linkId) {
391: return @mysqli_errno($linkId);
392: } else {
393: return @mysqli_connect_errno();
394: }
395: }
396:
397: 398: 399: 400:
401: public function getErrorMessage() {
402: $linkId = $this->_handler->getLinkId();
403:
404: if ($linkId) {
405: return @mysqli_error($linkId);
406: } else {
407: return @mysqli_connect_error();
408: }
409: }
410:
411: 412: 413: 414:
415: public function disconnect() {
416: mysqli_close($this->_handler->getLinkId());
417: }
418:
419: }