Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • MsSqlDriver
  • MySqlDriver
  • OciDriver
  • OdbcDriver
  • PgSqlDriver
  • Sqlite2Driver
  • SqliteDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 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:  */
 11: 
 12: namespace Nette\Database\Drivers;
 13: 
 14: use Nette;
 15: 
 16: 
 17: /**
 18:  * Supplemental MySQL database driver.
 19:  *
 20:  * @author     David Grudl
 21:  */
 22: class MySqlDriver extends Nette\Object implements Nette\Database\ISupplementalDriver
 23: {
 24:     const ERROR_ACCESS_DENIED = 1045;
 25:     const ERROR_DUPLICATE_ENTRY = 1062;
 26:     const ERROR_DATA_TRUNCATED = 1265;
 27: 
 28:     /** @var Nette\Database\Connection */
 29:     private $connection;
 30: 
 31: 
 32:     /**
 33:      * Driver options:
 34:      *   - charset => character encoding to set (default is utf8)
 35:      *   - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
 36:      */
 37:     public function __construct(Nette\Database\Connection $connection, array $options)
 38:     {
 39:         $this->connection = $connection;
 40:         $charset = isset($options['charset']) ? $options['charset'] : 'utf8';
 41:         if ($charset) {
 42:             $connection->query("SET NAMES '$charset'");
 43:         }
 44:         if (isset($options['sqlmode'])) {
 45:             $connection->query("SET sql_mode='$options[sqlmode]'");
 46:         }
 47:         $connection->query("SET time_zone='" . date('P') . "'");
 48:     }
 49: 
 50: 
 51:     /********************* SQL ****************d*g**/
 52: 
 53: 
 54:     /**
 55:      * Delimites identifier for use in a SQL statement.
 56:      */
 57:     public function delimite($name)
 58:     {
 59:         // @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
 60:         return '`' . str_replace('`', '``', $name) . '`';
 61:     }
 62: 
 63: 
 64:     /**
 65:      * Formats boolean for use in a SQL statement.
 66:      */
 67:     public function formatBool($value)
 68:     {
 69:         return $value ? '1' : '0';
 70:     }
 71: 
 72: 
 73:     /**
 74:      * Formats date-time for use in a SQL statement.
 75:      */
 76:     public function formatDateTime(\DateTime $value)
 77:     {
 78:         return $value->format("'Y-m-d H:i:s'");
 79:     }
 80: 
 81: 
 82:     /**
 83:      * Encodes string for use in a LIKE statement.
 84:      */
 85:     public function formatLike($value, $pos)
 86:     {
 87:         $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
 88:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Injects LIMIT/OFFSET to the SQL query.
 94:      */
 95:     public function applyLimit(& $sql, $limit, $offset)
 96:     {
 97:         if ($limit >= 0 || $offset > 0) {
 98:             // see http://dev.mysql.com/doc/refman/5.0/en/select.html
 99:             $sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
100:                 . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
101:         }
102:     }
103: 
104: 
105:     /**
106:      * Normalizes result row.
107:      */
108:     public function normalizeRow($row, $statement)
109:     {
110:         return $row;
111:     }
112: 
113: 
114:     /********************* reflection ****************d*g**/
115: 
116: 
117:     /**
118:      * Returns list of tables.
119:      */
120:     public function getTables()
121:     {
122:         /*$this->connection->query("
123:             SELECT TABLE_NAME as name, TABLE_TYPE = 'VIEW' as view
124:             FROM INFORMATION_SCHEMA.TABLES
125:             WHERE TABLE_SCHEMA = DATABASE()
126:         ");*/
127:         $tables = array();
128:         foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
129:             $tables[] = array(
130:                 'name' => $row[0],
131:                 'view' => isset($row[1]) && $row[1] === 'VIEW',
132:             );
133:         }
134:         return $tables;
135:     }
136: 
137: 
138:     /**
139:      * Returns metadata for all columns in a table.
140:      */
141:     public function getColumns($table)
142:     {
143:         /*$this->connection->query("
144:             SELECT *
145:             FROM INFORMATION_SCHEMA.COLUMNS
146:             WHERE TABLE_NAME = {$this->connection->quote($table)} AND TABLE_SCHEMA = DATABASE()
147:         ");*/
148:         $columns = array();
149:         foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
150:             $type = explode('(', $row['Type']);
151:             $columns[] = array(
152:                 'name' => $row['Field'],
153:                 'table' => $table,
154:                 'nativetype' => strtoupper($type[0]),
155:                 'size' => isset($type[1]) ? (int) $type[1] : NULL,
156:                 'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
157:                 'nullable' => $row['Null'] === 'YES',
158:                 'default' => $row['Default'],
159:                 'autoincrement' => $row['Extra'] === 'auto_increment',
160:                 'primary' => $row['Key'] === 'PRI',
161:                 'vendor' => (array) $row,
162:             );
163:         }
164:         return $columns;
165:     }
166: 
167: 
168:     /**
169:      * Returns metadata for all indexes in a table.
170:      */
171:     public function getIndexes($table)
172:     {
173:         /*$this->connection->query("
174:             SELECT *
175:             FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
176:             WHERE TABLE_NAME = {$this->connection->quote($table)} AND TABLE_SCHEMA = DATABASE()
177:             AND REFERENCED_COLUMN_NAME IS NULL
178:         ");*/
179:         $indexes = array();
180:         foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
181:             $indexes[$row['Key_name']]['name'] = $row['Key_name'];
182:             $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
183:             $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
184:             $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
185:         }
186:         return array_values($indexes);
187:     }
188: 
189: 
190:     /**
191:      * Returns metadata for all foreign keys in a table.
192:      */
193:     public function getForeignKeys($table)
194:     {
195:         $keys = array();
196:         $query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
197:             . 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
198: 
199:         foreach ($this->connection->query($query) as $id => $row) {
200:             $keys[$id]['name'] = $row['CONSTRAINT_NAME']; // foreign key name
201:             $keys[$id]['local'] = $row['COLUMN_NAME']; // local columns
202:             $keys[$id]['table'] = $row['REFERENCED_TABLE_NAME']; // referenced table
203:             $keys[$id]['foreign'] = $row['REFERENCED_COLUMN_NAME']; // referenced columns
204:         }
205: 
206:         return array_values($keys);
207:     }
208: 
209: 
210:     /**
211:      * @return bool
212:      */
213:     public function isSupported($item)
214:     {
215:         return $item === self::SUPPORT_COLUMNS_META || $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS;
216:     }
217: 
218: }
219: 
Nette Framework 2.0.12 API API documentation generated by ApiGen 2.8.0