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