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

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