Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

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