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