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 boolean for use in a SQL statement.
 58:      */
 59:     public function formatBool($value)
 60:     {
 61:         return $value ? '1' : '0';
 62:     }
 63: 
 64: 
 65: 
 66:     /**
 67:      * Formats date-time for use in a SQL statement.
 68:      */
 69:     public function formatDateTime(\DateTime $value)
 70:     {
 71:         return $value->format($this->fmtDateTime);
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Encodes string for use in a LIKE statement.
 78:      */
 79:     public function formatLike($value, $pos)
 80:     {
 81:         $value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_\\');
 82:         return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
 83:     }
 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:             $sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
 94:         }
 95:     }
 96: 
 97: 
 98: 
 99:     /**
100:      * Normalizes result row.
101:      */
102:     public function normalizeRow($row, $statement)
103:     {
104:         return $row;
105:     }
106: 
107: 
108: 
109:     /********************* reflection ****************d*g**/
110: 
111: 
112: 
113:     /**
114:      * Returns list of tables.
115:      */
116:     public function getTables()
117:     {
118:         return $this->connection->query("
119:             SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view')
120:             UNION ALL
121:             SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view')
122:             ORDER BY name
123:         ")->fetchAll();
124:     }
125: 
126: 
127: 
128:     /**
129:      * Returns metadata for all columns in a table.
130:      */
131:     public function getColumns($table)
132:     {
133:         $meta = $this->connection->query("
134:             SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
135:             UNION ALL
136:             SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
137:         ")->fetch();
138: 
139:         $columns = array();
140:         foreach ($this->connection->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
141:             $column = $row['name'];
142:             $pattern = "/(\"$column\"|\[$column\]|$column)\s+[^,]+\s+PRIMARY\s+KEY\s+AUTOINCREMENT/Ui";
143:             $type = explode('(', $row['type']);
144:             $columns[] = array(
145:                 'name' => $column,
146:                 'table' => $table,
147:                 'fullname' => "$table.$column",
148:                 'nativetype' => strtoupper($type[0]),
149:                 'size' => isset($type[1]) ? (int) $type[1] : NULL,
150:                 'nullable' => $row['notnull'] == '0',
151:                 'default' => $row['dflt_value'],
152:                 'autoincrement' => (bool) preg_match($pattern, $meta['sql']),
153:                 'primary' => $row['pk'] == '1',
154:                 'vendor' => (array) $row,
155:             );
156:         }
157:         return $columns;
158:     }
159: 
160: 
161: 
162:     /**
163:      * Returns metadata for all indexes in a table.
164:      */
165:     public function getIndexes($table)
166:     {
167:         $indexes = array();
168:         foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
169:             $indexes[$row['name']]['name'] = $row['name'];
170:             $indexes[$row['name']]['unique'] = (bool) $row['unique'];
171:         }
172: 
173:         foreach ($indexes as $index => $values) {
174:             $res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
175:             while ($row = $res->fetch(TRUE)) {
176:                 $indexes[$index]['columns'][$row['seqno']] = $row['name'];
177:             }
178:         }
179: 
180:         $columns = $this->getColumns($table);
181:         foreach ($indexes as $index => $values) {
182:             $column = $indexes[$index]['columns'][0];
183:             $primary = FALSE;
184:             foreach ($columns as $info) {
185:                 if ($column == $info['name']) {
186:                     $primary = $info['primary'];
187:                     break;
188:                 }
189:             }
190:             $indexes[$index]['primary'] = (bool) $primary;
191:         }
192:         if (!$indexes) { // @see http://www.sqlite.org/lang_createtable.html#rowid
193:             foreach ($columns as $column) {
194:                 if ($column['vendor']['pk']) {
195:                     $indexes[] = array(
196:                         'name' => 'ROWID',
197:                         'unique' => TRUE,
198:                         'primary' => TRUE,
199:                         'columns' => array($column['name']),
200:                     );
201:                     break;
202:                 }
203:             }
204:         }
205: 
206:         return array_values($indexes);
207:     }
208: 
209: 
210: 
211:     /**
212:      * Returns metadata for all foreign keys in a table.
213:      */
214:     public function getForeignKeys($table)
215:     {
216:         $keys = array();
217:         foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
218:             $keys[$row['id']]['name'] = $row['id']; // foreign key name
219:             $keys[$row['id']]['local'][$row['seq']] = $row['from']; // local columns
220:             $keys[$row['id']]['table'] = $row['table']; // referenced table
221:             $keys[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns
222:             $keys[$row['id']]['onDelete'] = $row['on_delete'];
223:             $keys[$row['id']]['onUpdate'] = $row['on_update'];
224: 
225:             if ($keys[$row['id']]['foreign'][0] == NULL) {
226:                 $keys[$row['id']]['foreign'] = NULL;
227:             }
228:         }
229:         return array_values($keys);
230:     }
231: 
232: 
233: 
234:     /**
235:      * @return bool
236:      */
237:     public function isSupported($item)
238:     {
239:         return FALSE;
240:     }
241: 
242: }
243: 
Nette Framework 2.0.6 API API documentation generated by ApiGen 2.7.0