Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sqlite.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
28 
29 
40 {
41 
47  protected $_pdoType = 'sqlite';
48 
60  protected $_numericDataTypes = array(
64  'INTEGER' => Zend_Db::BIGINT_TYPE,
65  'REAL' => Zend_Db::FLOAT_TYPE
66  );
67 
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  return parent::__construct($config);
95  }
96 
104  protected function _checkRequiredOptions(array $config)
105  {
106  // we need at least a dbname
107  if (! array_key_exists('dbname', $config)) {
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 
117  protected function _dsn()
118  {
119  return $this->_pdoType .':'. $this->_config['dbname'];
120  }
121 
128  protected function _connect()
129  {
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();
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();
151  #require_once 'Zend/Db/Adapter/Exception.php';
152  throw new Zend_Db_Adapter_Exception($error[2]);
153  }
154  }
155 
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 
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 
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;
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 
273  public function limit($sql, $count, $offset = 0)
274  {
275  $count = intval($count);
276  if ($count <= 0) {
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) {
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 
303  protected function _quote($value)
304  {
305  if (!is_int($value) && !is_float($value)) {
306  // Fix for null-byte injection
307  $value = addcslashes($value, "\000\032");
308  }
309  return parent::_quote($value);
310  }
311 }
$tableName
Definition: trigger.php:13
__construct(array $config=array())
Definition: Sqlite.php:84
fetchCol($sql, $bind=array())
Definition: Abstract.php:792
$config
Definition: fraud_order.php:17
const BIGINT_TYPE
Definition: Db.php:69
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
describeTable($tableName, $schemaName=null)
Definition: Sqlite.php:198
const FLOAT_TYPE
Definition: Db.php:70
$type
Definition: item.phtml:13
_checkRequiredOptions(array $config)
Definition: Sqlite.php:104
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
limit($sql, $count, $offset=0)
Definition: Sqlite.php:273
quoteIdentifier($ident, $auto=false)
Definition: Abstract.php:959
query($sql, $bind=array())
Definition: Abstract.php:221
if(!isset($_GET['name'])) $name
Definition: log.php:14