Packages

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