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 SQLite3 database driver.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class SqliteDriver extends Nette\Object implements Nette\Database\ISupplementalDriver
 24: {
 25:     /** @var Nette\Database\Connection */
 26:     private $connection;
 27: 
 28:     /** @var string  Datetime format */
 29:     private $fmtDateTime;
 30: 
 31: 
 32: 
 33:     public function __construct(Nette\Database\Connection $connection, array $options)
 34:     {
 35:         $this->connection = $connection;
 36:         $this->fmtDateTime = isset($options['formatDateTime']) ? $options['formatDateTime'] : 'U';
 37:         //$connection->exec('PRAGMA foreign_keys = ON');
 38:     }
 39: 
 40: 
 41: 
 42:     /********************* SQL ****************d*g**/
 43: 
 44: 
 45: 
 46:     /**
 47:      * Delimites identifier for use in a SQL statement.
 48:      */
 49:     public function delimite($name)
 50:     {
 51:         return '[' . strtr($name, '[]', '  ') . ']';
 52:     }
 53: 
 54: 
 55: 
 56:     /**
 57:      * Formats date-time for use in a SQL statement.
 58:      */
 59:     public function formatDateTime(\DateTime $value)
 60:     {
 61:         return $value->format($this->fmtDateTime);
 62:     }
 63: 
 64: 
 65: 
 66:     /**
 67:      * Encodes string for use in a LIKE statement.
 68:      */
 69:     public function formatLike($value, $pos)
 70:     {
 71:         $value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_\\');
 72:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
 73:     }
 74: 
 75: 
 76: 
 77:     /**
 78:      * Injects LIMIT/OFFSET to the SQL query.
 79:      */
 80:     public function applyLimit(&$sql, $limit, $offset)
 81:     {
 82:         if ($limit >= 0 || $offset > 0) {
 83:             $sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
 84:         }
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Normalizes result row.
 91:      */
 92:     public function normalizeRow($row, $statement)
 93:     {
 94:         return $row;
 95:     }
 96: 
 97: 
 98: 
 99:     /********************* reflection ****************d*g**/
100: 
101: 
102: 
103:     /**
104:      * Returns list of tables.
105:      */
106:     public function getTables()
107:     {
108:         return $this->connection->query("
109:             SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
110:             UNION ALL
111:             SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
112:             ORDER BY name
113:         ")->fetchAll();
114:     }
115: 
116: 
117: 
118:     /**
119:      * Returns metadata for all columns in a table.
120:      */
121:     public function getColumns($table)
122:     {
123:         $meta = $this->connection->query("
124:             SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
125:             UNION ALL
126:             SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
127:         ")->fetch();
128: 
129:         $columns = array();
130:         foreach ($this->connection->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
131:             $column = $row['name'];
132:             $pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
133:             $type = explode('(', $row['type']);
134:             $columns[] = array(
135:                 'name' => $column,
136:                 'table' => $table,
137:                 'fullname' => "$table.$column",
138:                 'nativetype' => strtoupper($type[0]),
139:                 'size' => isset($type[1]) ? (int) $type[1] : NULL,
140:                 'nullable' => $row['notnull'] == '0',
141:                 'default' => $row['dflt_value'],
142:                 'autoincrement' => (bool) preg_match($pattern, $meta['sql']),
143:                 'primary' => $row['pk'] == '1',
144:                 'vendor' => (array) $row,
145:             );
146:         }
147:         return $columns;
148:     }
149: 
150: 
151: 
152:     /**
153:      * Returns metadata for all indexes in a table.
154:      */
155:     public function getIndexes($table)
156:     {
157:         $indexes = array();
158:         foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
159:             $indexes[$row['name']]['name'] = $row['name'];
160:             $indexes[$row['name']]['unique'] = (bool) $row['unique'];
161:         }
162: 
163:         foreach ($indexes as $index => $values) {
164:             $res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
165:             while ($row = $res->fetch(TRUE)) {
166:                 $indexes[$index]['columns'][$row['seqno']] = $row['name'];
167:             }
168:         }
169: 
170:         $columns = $this->getColumns($table);
171:         foreach ($indexes as $index => $values) {
172:             $column = $indexes[$index]['columns'][0];
173:             $primary = FALSE;
174:             foreach ($columns as $info) {
175:                 if ($column == $info['name']) {
176:                     $primary = $info['primary'];
177:                     break;
178:                 }
179:             }
180:             $indexes[$index]['primary'] = (bool) $primary;
181:         }
182:         if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid
183:             foreach ($columns as $column) {
184:                 if ($column['vendor']['pk']) {
185:                     $indexes[] = array(
186:                         'name' => 'ROWID',
187:                         'unique' => TRUE,
188:                         'primary' => TRUE,
189:                         'columns' => array($column['name']),
190:                     );
191:                     break;
192:                 }
193:             }
194:         }
195: 
196:         return array_values($indexes);
197:     }
198: 
199: 
200: 
201:     /**
202:      * Returns metadata for all foreign keys in a table.
203:      */
204:     public function getForeignKeys($table)
205:     {
206:         $keys = array();
207:         foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
208:             $keys[$row['id']]['name'] = $row['id']; // foreign key name
209:             $keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
210:             $keys[$row['id']]['table'] = $row['table']; // referenced table
211:             $keys[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
212:             $keys[$row['id']]['onDelete'] = $row['on_delete'];
213:             $keys[$row['id']]['onUpdate'] = $row['on_update'];
214: 
215:             if ($keys[$row['id']]['foreign'][0] == NULL) {
216:                 $keys[$row['id']]['foreign'] = NULL;
217:             }
218:         }
219:         return array_values($keys);
220:     }
221: 
222: 
223: 
224:     /**
225:      * @return bool
226:      */
227:     public function isSupported($item)
228:     {
229:         return FALSE;
230:     }
231: 
232: }
233: 
Nette Framework 2.0.5 API API documentation generated by ApiGen 2.7.0