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