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

Classes

  • MsSqlDriver
  • MySqlDriver
  • OciDriver
  • OdbcDriver
  • PgSqlDriver
  • Sqlite2Driver
  • SqliteDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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:         $connection->query("SET time_zone='" . date('P') . "'");
 44:     }
 45: 
 46: 
 47:     /********************* SQL ****************d*g**/
 48: 
 49: 
 50:     /**
 51:      * Delimites identifier for use in a SQL statement.
 52:      */
 53:     public function delimite($name)
 54:     {
 55:         // @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
 56:         return '`' . str_replace('`', '``', $name) . '`';
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Formats boolean for use in a SQL statement.
 62:      */
 63:     public function formatBool($value)
 64:     {
 65:         return $value ? '1' : '0';
 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:      * Encodes string for use in a LIKE statement.
 80:      */
 81:     public function formatLike($value, $pos)
 82:     {
 83:         $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
 84:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Injects LIMIT/OFFSET to the SQL query.
 90:      */
 91:     public function applyLimit(& $sql, $limit, $offset)
 92:     {
 93:         if ($limit >= 0 || $offset > 0) {
 94:             // see http://dev.mysql.com/doc/refman/5.0/en/select.html
 95:             $sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
 96:                 . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
 97:         }
 98:     }
 99: 
100: 
101:     /**
102:      * Normalizes result row.
103:      */
104:     public function normalizeRow($row, $statement)
105:     {
106:         return $row;
107:     }
108: 
109: 
110:     /********************* reflection ****************d*g**/
111: 
112: 
113:     /**
114:      * Returns list of tables.
115:      */
116:     public function getTables()
117:     {
118:         $tables = array();
119:         foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
120:             $tables[] = array(
121:                 'name' => $row[0],
122:                 'view' => isset($row[1]) && $row[1] === 'VIEW',
123:             );
124:         }
125:         return $tables;
126:     }
127: 
128: 
129:     /**
130:      * Returns metadata for all columns in a table.
131:      */
132:     public function getColumns($table)
133:     {
134:         $columns = array();
135:         foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
136:             $type = explode('(', $row['Type']);
137:             $columns[] = array(
138:                 'name' => $row['Field'],
139:                 'table' => $table,
140:                 'nativetype' => strtoupper($type[0]),
141:                 'size' => isset($type[1]) ? (int) $type[1] : NULL,
142:                 'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
143:                 'nullable' => $row['Null'] === 'YES',
144:                 'default' => $row['Default'],
145:                 'autoincrement' => $row['Extra'] === 'auto_increment',
146:                 'primary' => $row['Key'] === 'PRI',
147:                 'vendor' => (array) $row,
148:             );
149:         }
150:         return $columns;
151:     }
152: 
153: 
154:     /**
155:      * Returns metadata for all indexes in a table.
156:      */
157:     public function getIndexes($table)
158:     {
159:         $indexes = array();
160:         foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
161:             $indexes[$row['Key_name']]['name'] = $row['Key_name'];
162:             $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
163:             $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
164:             $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
165:         }
166:         return array_values($indexes);
167:     }
168: 
169: 
170:     /**
171:      * Returns metadata for all foreign keys in a table.
172:      */
173:     public function getForeignKeys($table)
174:     {
175:         $keys = array();
176:         $query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
177:             . 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
178: 
179:         foreach ($this->connection->query($query) as $id => $row) {
180:             $keys[$id]['name'] = $row['CONSTRAINT_NAME']; // foreign key name
181:             $keys[$id]['local'] = $row['COLUMN_NAME']; // local columns
182:             $keys[$id]['table'] = $row['REFERENCED_TABLE_NAME']; // referenced table
183:             $keys[$id]['foreign'] = $row['REFERENCED_COLUMN_NAME']; // referenced columns
184:         }
185: 
186:         return array_values($keys);
187:     }
188: 
189: 
190:     /**
191:      * @return bool
192:      */
193:     public function isSupported($item)
194:     {
195:         return $item === self::SUPPORT_COLUMNS_META || $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS;
196:     }
197: 
198: }
199: 
Nette 2.0 API documentation generated by ApiGen 2.8.0