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