Ibm
6 years ago
Abstract.php
6 years ago
Ibm.php
6 years ago
Mssql.php
6 years ago
Mysql.php
6 years ago
Oci.php
6 years ago
Pgsql.php
6 years ago
Sqlite.php
5 years ago
Sqlite.php
298 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Zend Framework |
| 4 | * |
| 5 | * LICENSE |
| 6 | * |
| 7 | * This source file is subject to the new BSD license that is bundled |
| 8 | * with this package in the file LICENSE.txt. |
| 9 | * It is also available through the world-wide-web at this URL: |
| 10 | * http://framework.zend.com/license/new-bsd |
| 11 | * If you did not receive a copy of the license and are unable to |
| 12 | * obtain it through the world-wide-web, please send an email |
| 13 | * to license@zend.com so we can send you a copy immediately. |
| 14 | * |
| 15 | * @category Zend |
| 16 | * @package Zend_Db |
| 17 | * @subpackage Adapter |
| 18 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 19 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 20 | * @version $Id: Sqlite.php 23775 2011-03-01 17:25:24Z ralph $ |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @see Zend_Db_Adapter_Pdo_Abstract |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Adapter/Pdo/Abstract.php'; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Class for connecting to SQLite2 and SQLite3 databases and performing common operations. |
| 32 | * |
| 33 | * @category Zend |
| 34 | * @package Zend_Db |
| 35 | * @subpackage Adapter |
| 36 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 37 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 38 | */ |
| 39 | class Zend_Db_Adapter_Pdo_Sqlite extends Zend_Db_Adapter_Pdo_Abstract |
| 40 | { |
| 41 | |
| 42 | /** |
| 43 | * PDO type |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $_pdoType = 'sqlite'; |
| 48 | |
| 49 | /** |
| 50 | * Keys are UPPERCASE SQL datatypes or the constants |
| 51 | * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE. |
| 52 | * |
| 53 | * Values are: |
| 54 | * 0 = 32-bit integer |
| 55 | * 1 = 64-bit integer |
| 56 | * 2 = float or decimal |
| 57 | * |
| 58 | * @var array Associative array of datatypes to values 0, 1, or 2. |
| 59 | */ |
| 60 | protected $_numericDataTypes = array( |
| 61 | Zend_Db::INT_TYPE => Zend_Db::INT_TYPE, |
| 62 | Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE, |
| 63 | Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE, |
| 64 | 'INTEGER' => Zend_Db::BIGINT_TYPE, |
| 65 | 'REAL' => Zend_Db::FLOAT_TYPE |
| 66 | ); |
| 67 | |
| 68 | /** |
| 69 | * Constructor. |
| 70 | * |
| 71 | * $config is an array of key/value pairs containing configuration |
| 72 | * options. Note that the SQLite options are different than most of |
| 73 | * the other PDO adapters in that no username or password are needed. |
| 74 | * Also, an extra config key "sqlite2" specifies compatibility mode. |
| 75 | * |
| 76 | * dbname => (string) The name of the database to user (required, |
| 77 | * use :memory: for memory-based database) |
| 78 | * |
| 79 | * sqlite2 => (boolean) PDO_SQLITE defaults to SQLite 3. For compatibility |
| 80 | * with an older SQLite 2 database, set this to TRUE. |
| 81 | * |
| 82 | * @param array $config An array of configuration keys. |
| 83 | */ |
| 84 | public function __construct(array $config = array()) |
| 85 | { |
| 86 | if (isset($config['sqlite2']) && $config['sqlite2']) { |
| 87 | $this->_pdoType = 'sqlite2'; |
| 88 | } |
| 89 | |
| 90 | // SQLite uses no username/password. Stub to satisfy parent::_connect() |
| 91 | $this->_config['username'] = null; |
| 92 | $this->_config['password'] = null; |
| 93 | |
| 94 | parent::__construct($config); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check for config options that are mandatory. |
| 99 | * Throw exceptions if any are missing. |
| 100 | * |
| 101 | * @param array $config |
| 102 | * @throws Zend_Db_Adapter_Exception |
| 103 | */ |
| 104 | protected function _checkRequiredOptions(array $config) |
| 105 | { |
| 106 | // we need at least a dbname |
| 107 | if (! array_key_exists('dbname', $config)) { |
| 108 | /** @see Zend_Db_Adapter_Exception */ |
| 109 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 110 | throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'dbname' that names the database instance"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * DSN builder |
| 116 | */ |
| 117 | protected function _dsn() |
| 118 | { |
| 119 | return $this->_pdoType .':'. $this->_config['dbname']; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Special configuration for SQLite behavior: make sure that result sets |
| 124 | * contain keys like 'column' instead of 'table.column'. |
| 125 | * |
| 126 | * @throws Zend_Db_Adapter_Exception |
| 127 | */ |
| 128 | protected function _connect() |
| 129 | { |
| 130 | /** |
| 131 | * if we already have a PDO object, no need to re-connect. |
| 132 | */ |
| 133 | if ($this->_connection) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | parent::_connect(); |
| 138 | |
| 139 | $retval = $this->_connection->exec('PRAGMA full_column_names=0'); |
| 140 | if ($retval === false) { |
| 141 | $error = $this->_connection->errorInfo(); |
| 142 | /** @see Zend_Db_Adapter_Exception */ |
| 143 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 144 | throw new Zend_Db_Adapter_Exception($error[2]); |
| 145 | } |
| 146 | |
| 147 | $retval = $this->_connection->exec('PRAGMA short_column_names=1'); |
| 148 | if ($retval === false) { |
| 149 | $error = $this->_connection->errorInfo(); |
| 150 | /** @see Zend_Db_Adapter_Exception */ |
| 151 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 152 | throw new Zend_Db_Adapter_Exception($error[2]); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns a list of the tables in the database. |
| 158 | * |
| 159 | * @return array |
| 160 | */ |
| 161 | public function listTables() |
| 162 | { |
| 163 | $sql = "SELECT name FROM sqlite_master WHERE type='table' " |
| 164 | . "UNION ALL SELECT name FROM sqlite_temp_master " |
| 165 | . "WHERE type='table' ORDER BY name"; |
| 166 | |
| 167 | return $this->fetchCol($sql); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the column descriptions for a table. |
| 172 | * |
| 173 | * The return value is an associative array keyed by the column name, |
| 174 | * as returned by the RDBMS. |
| 175 | * |
| 176 | * The value of each array element is an associative array |
| 177 | * with the following keys: |
| 178 | * |
| 179 | * SCHEMA_NAME => string; name of database or schema |
| 180 | * TABLE_NAME => string; |
| 181 | * COLUMN_NAME => string; column name |
| 182 | * COLUMN_POSITION => number; ordinal position of column in table |
| 183 | * DATA_TYPE => string; SQL datatype name of column |
| 184 | * DEFAULT => string; default expression of column, null if none |
| 185 | * NULLABLE => boolean; true if column can have nulls |
| 186 | * LENGTH => number; length of CHAR/VARCHAR |
| 187 | * SCALE => number; scale of NUMERIC/DECIMAL |
| 188 | * PRECISION => number; precision of NUMERIC/DECIMAL |
| 189 | * UNSIGNED => boolean; unsigned property of an integer type |
| 190 | * PRIMARY => boolean; true if column is part of the primary key |
| 191 | * PRIMARY_POSITION => integer; position of column in primary key |
| 192 | * IDENTITY => integer; true if column is auto-generated with unique values |
| 193 | * |
| 194 | * @param string $tableName |
| 195 | * @param string $schemaName OPTIONAL |
| 196 | * @return array |
| 197 | */ |
| 198 | public function describeTable($tableName, $schemaName = null) |
| 199 | { |
| 200 | $sql = 'PRAGMA '; |
| 201 | |
| 202 | if ($schemaName) { |
| 203 | $sql .= $this->quoteIdentifier($schemaName) . '.'; |
| 204 | } |
| 205 | |
| 206 | $sql .= 'table_info('.$this->quoteIdentifier($tableName).')'; |
| 207 | |
| 208 | $stmt = $this->query($sql); |
| 209 | |
| 210 | /** |
| 211 | * Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection |
| 212 | */ |
| 213 | $result = $stmt->fetchAll(Zend_Db::FETCH_NUM); |
| 214 | |
| 215 | $cid = 0; |
| 216 | $name = 1; |
| 217 | $type = 2; |
| 218 | $notnull = 3; |
| 219 | $dflt_value = 4; |
| 220 | $pk = 5; |
| 221 | |
| 222 | $desc = array(); |
| 223 | |
| 224 | $p = 1; |
| 225 | foreach ($result as $key => $row) { |
| 226 | list($length, $scale, $precision, $primary, $primaryPosition, $identity) = |
| 227 | array(null, null, null, false, null, false); |
| 228 | if (preg_match('/^((?:var)?char)\((\d+)\)/i', $row[$type], $matches)) { |
| 229 | $row[$type] = $matches[1]; |
| 230 | $length = $matches[2]; |
| 231 | } else if (preg_match('/^decimal\((\d+),(\d+)\)/i', $row[$type], $matches)) { |
| 232 | $row[$type] = 'DECIMAL'; |
| 233 | $precision = $matches[1]; |
| 234 | $scale = $matches[2]; |
| 235 | } |
| 236 | if ((bool) $row[$pk]) { |
| 237 | $primary = true; |
| 238 | $primaryPosition = $p; |
| 239 | /** |
| 240 | * SQLite INTEGER primary key is always auto-increment. |
| 241 | */ |
| 242 | $identity = (bool) ($row[$type] == 'INTEGER'); |
| 243 | ++$p; |
| 244 | } |
| 245 | $desc[$this->foldCase($row[$name])] = array( |
| 246 | 'SCHEMA_NAME' => $this->foldCase($schemaName), |
| 247 | 'TABLE_NAME' => $this->foldCase($tableName), |
| 248 | 'COLUMN_NAME' => $this->foldCase($row[$name]), |
| 249 | 'COLUMN_POSITION' => $row[$cid]+1, |
| 250 | 'DATA_TYPE' => $row[$type], |
| 251 | 'DEFAULT' => $row[$dflt_value], |
| 252 | 'NULLABLE' => ! (bool) $row[$notnull], |
| 253 | 'LENGTH' => $length, |
| 254 | 'SCALE' => $scale, |
| 255 | 'PRECISION' => $precision, |
| 256 | 'UNSIGNED' => null, // Sqlite3 does not support unsigned data |
| 257 | 'PRIMARY' => $primary, |
| 258 | 'PRIMARY_POSITION' => $primaryPosition, |
| 259 | 'IDENTITY' => $identity |
| 260 | ); |
| 261 | } |
| 262 | return $desc; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Adds an adapter-specific LIMIT clause to the SELECT statement. |
| 267 | * |
| 268 | * @param string $sql |
| 269 | * @param integer $count |
| 270 | * @param integer $offset OPTIONAL |
| 271 | * @return string |
| 272 | */ |
| 273 | public function limit($sql, $count, $offset = 0) |
| 274 | { |
| 275 | $count = intval($count); |
| 276 | if ($count <= 0) { |
| 277 | /** @see Zend_Db_Adapter_Exception */ |
| 278 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 279 | throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid"); |
| 280 | } |
| 281 | |
| 282 | $offset = intval($offset); |
| 283 | if ($offset < 0) { |
| 284 | /** @see Zend_Db_Adapter_Exception */ |
| 285 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 286 | throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid"); |
| 287 | } |
| 288 | |
| 289 | $sql .= " LIMIT $count"; |
| 290 | if ($offset > 0) { |
| 291 | $sql .= " OFFSET $offset"; |
| 292 | } |
| 293 | |
| 294 | return $sql; |
| 295 | } |
| 296 | |
| 297 | } |
| 298 |