1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14: require_once dirname(__FILE__) . '/mysql.reflector.php';
15:
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: class DibiMySqliDriver extends DibiObject implements IDibiDriver, IDibiResultDriver
40: {
41: const ERROR_ACCESS_DENIED = 1045;
42: const ERROR_DUPLICATE_ENTRY = 1062;
43: const ERROR_DATA_TRUNCATED = 1265;
44:
45:
46: private $connection;
47:
48:
49: private $resultSet;
50:
51:
52: private $buffered;
53:
54:
55:
56: 57: 58:
59: public function __construct()
60: {
61: if (!extension_loaded('mysqli')) {
62: throw new DibiDriverException("PHP extension 'mysqli' is not loaded.");
63: }
64: }
65:
66:
67:
68: 69: 70: 71: 72:
73: public function connect(array &$config)
74: {
75: mysqli_report(MYSQLI_REPORT_OFF);
76: if (isset($config['resource'])) {
77: $this->connection = $config['resource'];
78:
79: } else {
80: 81: if (!isset($config['charset'])) $config['charset'] = 'utf8';
82: if (!isset($config['username'])) $config['username'] = ini_get('mysqli.default_user');
83: if (!isset($config['password'])) $config['password'] = ini_get('mysqli.default_pw');
84: if (!isset($config['socket'])) $config['socket'] = ini_get('mysqli.default_socket');
85: if (!isset($config['port'])) $config['port'] = NULL;
86: if (!isset($config['host'])) {
87: $host = ini_get('mysqli.default_host');
88: if ($host) {
89: $config['host'] = $host;
90: $config['port'] = ini_get('mysqli.default_port');
91: } else {
92: $config['host'] = NULL;
93: $config['port'] = NULL;
94: }
95: }
96:
97: $foo = & $config['flags'];
98: $foo = & $config['database'];
99:
100: $this->connection = mysqli_init();
101: if (isset($config['options'])) {
102: if (is_scalar($config['options'])) {
103: $config['flags'] = $config['options']; 104: trigger_error(__CLASS__ . ": configuration item 'options' must be array; for constants MYSQLI_CLIENT_* use 'flags'.", E_USER_NOTICE);
105: } else {
106: foreach ((array) $config['options'] as $key => $value) {
107: mysqli_options($this->connection, $key, $value);
108: }
109: }
110: }
111: @mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['flags']); 112:
113: if ($errno = mysqli_connect_errno()) {
114: throw new DibiDriverException(mysqli_connect_error(), $errno);
115: }
116: }
117:
118: if (isset($config['charset'])) {
119: $ok = FALSE;
120: if (version_compare(PHP_VERSION , '5.1.5', '>=')) {
121: 122: $ok = @mysqli_set_charset($this->connection, $config['charset']); 123: }
124: if (!$ok) {
125: $this->query("SET NAMES '$config[charset]'");
126: }
127: }
128:
129: if (isset($config['sqlmode'])) {
130: $this->query("SET sql_mode='$config[sqlmode]'");
131: }
132:
133: $this->query("SET time_zone='" . date('P') . "'");
134:
135: $this->buffered = empty($config['unbuffered']);
136: }
137:
138:
139:
140: 141: 142: 143:
144: public function disconnect()
145: {
146: mysqli_close($this->connection);
147: }
148:
149:
150:
151: 152: 153: 154: 155: 156:
157: public function query($sql)
158: {
159: $this->resultSet = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); 160:
161: if (mysqli_errno($this->connection)) {
162: throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql);
163: }
164:
165: return is_object($this->resultSet) ? clone $this : NULL;
166: }
167:
168:
169:
170: 171: 172: 173:
174: public function getInfo()
175: {
176: $res = array();
177: preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER);
178: if (preg_last_error()) throw new DibiPcreException;
179:
180: foreach ($matches as $m) {
181: $res[$m[1]] = (int) $m[2];
182: }
183: return $res;
184: }
185:
186:
187:
188: 189: 190: 191:
192: public function getAffectedRows()
193: {
194: return mysqli_affected_rows($this->connection);
195: }
196:
197:
198:
199: 200: 201: 202:
203: public function getInsertId($sequence)
204: {
205: return mysqli_insert_id($this->connection);
206: }
207:
208:
209:
210: 211: 212: 213: 214: 215:
216: public function begin($savepoint = NULL)
217: {
218: $this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
219: }
220:
221:
222:
223: 224: 225: 226: 227: 228:
229: public function commit($savepoint = NULL)
230: {
231: $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
232: }
233:
234:
235:
236: 237: 238: 239: 240: 241:
242: public function rollback($savepoint = NULL)
243: {
244: $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
245: }
246:
247:
248:
249: 250: 251: 252:
253: public function getResource()
254: {
255: return $this->connection;
256: }
257:
258:
259:
260: 261: 262: 263:
264: public function getReflector()
265: {
266: return new DibiMySqlReflector($this);
267: }
268:
269:
270:
271:
272:
273:
274:
275: 276: 277: 278: 279: 280: 281:
282: public function escape($value, $type)
283: {
284: switch ($type) {
285: case dibi::TEXT:
286: return "'" . mysqli_real_escape_string($this->connection, $value) . "'";
287:
288: case dibi::BINARY:
289: return "_binary'" . mysqli_real_escape_string($this->connection, $value) . "'";
290:
291: case dibi::IDENTIFIER:
292: return '`' . str_replace('`', '``', $value) . '`';
293:
294: case dibi::BOOL:
295: return $value ? 1 : 0;
296:
297: case dibi::DATE:
298: return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
299:
300: case dibi::DATETIME:
301: return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
302:
303: default:
304: throw new InvalidArgumentException('Unsupported type.');
305: }
306: }
307:
308:
309:
310: 311: 312: 313: 314: 315:
316: public function escapeLike($value, $pos)
317: {
318: $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
319: return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
320: }
321:
322:
323:
324: 325: 326: 327: 328: 329: 330:
331: public function unescape($value, $type)
332: {
333: if ($type === dibi::BINARY) {
334: return $value;
335: }
336: throw new InvalidArgumentException('Unsupported type.');
337: }
338:
339:
340:
341: 342: 343: 344: 345: 346: 347:
348: public function applyLimit(&$sql, $limit, $offset)
349: {
350: if ($limit < 0 && $offset < 1) return;
351:
352: 353: $sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
354: . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
355: }
356:
357:
358:
359:
360:
361:
362:
363: 364: 365: 366:
367: public function getRowCount()
368: {
369: if (!$this->buffered) {
370: throw new DibiDriverException('Row count is not available for unbuffered queries.');
371: }
372: return mysqli_num_rows($this->resultSet);
373: }
374:
375:
376:
377: 378: 379: 380: 381:
382: public function fetch($assoc)
383: {
384: return mysqli_fetch_array($this->resultSet, $assoc ? MYSQLI_ASSOC : MYSQLI_NUM);
385: }
386:
387:
388:
389: 390: 391: 392: 393: 394:
395: public function seek($row)
396: {
397: if (!$this->buffered) {
398: throw new DibiDriverException('Cannot seek an unbuffered result set.');
399: }
400: return mysqli_data_seek($this->resultSet, $row);
401: }
402:
403:
404:
405: 406: 407: 408:
409: public function free()
410: {
411: mysqli_free_result($this->resultSet);
412: $this->resultSet = NULL;
413: }
414:
415:
416:
417: 418: 419: 420:
421: public function getResultColumns()
422: {
423: static $types;
424: if (empty($types)) {
425: $consts = get_defined_constants(TRUE);
426: foreach ($consts['mysqli'] as $key => $value) {
427: if (strncmp($key, 'MYSQLI_TYPE_', 12) === 0) {
428: $types[$value] = substr($key, 12);
429: }
430: }
431: $types[MYSQLI_TYPE_TINY] = $types[MYSQLI_TYPE_SHORT] = $types[MYSQLI_TYPE_LONG] = 'INT';
432: }
433:
434: $count = mysqli_num_fields($this->resultSet);
435: $columns = array();
436: for ($i = 0; $i < $count; $i++) {
437: $row = (array) mysqli_fetch_field_direct($this->resultSet, $i);
438: $columns[] = array(
439: 'name' => $row['name'],
440: 'table' => $row['orgtable'],
441: 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
442: 'nativetype' => $types[$row['type']],
443: 'vendor' => $row,
444: );
445: }
446: return $columns;
447: }
448:
449:
450:
451: 452: 453: 454:
455: public function getResultResource()
456: {
457: return $this->resultSet;
458: }
459:
460: }
461: