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