1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette\Database\Drivers
11: */
12:
13:
14:
15: /**
16: * Supplemental MySQL database driver.
17: *
18: * @author David Grudl
19: */
20: class NPdoMySqlDriver extends NObject implements ISupplementalDriver
21: {
22: /** @var NConnection */
23: private $connection;
24:
25:
26:
27: /**
28: * Driver options:
29: * - charset => character encoding to set (default is utf8)
30: * - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
31: */
32: public function __construct(NConnection $connection, array $options)
33: {
34: $this->connection = $connection;
35: $charset = isset($options['charset']) ? $options['charset'] : 'utf8';
36: if ($charset) {
37: $connection->exec("SET NAMES '$charset'");
38: }
39: if (isset($options['sqlmode'])) {
40: $connection->exec("SET sql_mode='$options[sqlmode]'");
41: }
42: $connection->exec("SET time_zone='" . date('P') . "'");
43: }
44:
45:
46:
47: /********************* SQL ****************d*g**/
48:
49:
50:
51: /**
52: * Delimites identifier for use in a SQL statement.
53: */
54: public function delimite($name)
55: {
56: // @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
57: return '`' . str_replace('`', '``', $name) . '`';
58: }
59:
60:
61:
62: /**
63: * Formats date-time for use in a SQL statement.
64: */
65: public function formatDateTime(DateTime $value)
66: {
67: return $value->format("'Y-m-d H:i:s'");
68: }
69:
70:
71:
72: /**
73: * Encodes string for use in a LIKE statement.
74: */
75: public function formatLike($value, $pos)
76: {
77: $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
78: return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
79: }
80:
81:
82:
83: /**
84: * Injects LIMIT/OFFSET to the SQL query.
85: */
86: public function applyLimit(&$sql, $limit, $offset)
87: {
88: if ($limit < 0 && $offset < 1) return;
89:
90: // see http://dev.mysql.com/doc/refman/5.0/en/select.html
91: $sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
92: . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
93: }
94:
95:
96:
97: /**
98: * Normalizes result row.
99: */
100: public function normalizeRow($row, $statement)
101: {
102: return $row;
103: }
104:
105: }
106: