Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
sqlite.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Database
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * SQLite database driver
14  *
15  * @package Joomla.Platform
16  * @subpackage Database
17  * @see http://php.net/pdo
18  * @since 12.1
19  */
21 {
22  /**
23  * The name of the database driver.
24  *
25  * @var string
26  * @since 12.1
27  */
28  public $name = 'sqlite';
29 
30  /**
31  * The character(s) used to quote SQL statement names such as table names or field names,
32  * etc. The child classes should define this as necessary. If a single character string the
33  * same character is used for both sides of the quoted name, else the first character will be
34  * used for the opening quote and the second for the closing quote.
35  *
36  * @var string
37  * @since 12.1
38  */
39  protected $nameQuote = '`';
40 
41  /**
42  * Destructor.
43  *
44  * @since 12.1
45  */
46  public function __destruct()
47  {
48  $this->freeResult();
49  unset($this->connection);
50  }
51 
52  /**
53  * Disconnects the database.
54  *
55  * @return void
56  *
57  * @since 12.1
58  */
59  public function disconnect()
60  {
61  $this->freeResult();
62  unset($this->connection);
63  }
64 
65  /**
66  * Drops a table from the database.
67  *
68  * @param string $tableName The name of the database table to drop.
69  * @param boolean $ifExists Optionally specify that the table must exist before it is dropped.
70  *
71  * @return JDatabaseDriverSqlite Returns this object to support chaining.
72  *
73  * @since 12.1
74  */
75  public function dropTable($tableName, $ifExists = true)
76  {
77  $this->connect();
78 
79  $query = $this->getQuery(true);
80 
81  $this->setQuery('DROP TABLE ' . ($ifExists ? 'IF EXISTS ' : '') . $query->quoteName($tableName));
82 
83  $this->execute();
84 
85  return $this;
86  }
87 
88  /**
89  * Method to escape a string for usage in an SQLite statement.
90  *
91  * Note: Using query objects with bound variables is
92  * preferable to the below.
93  *
94  * @param string $text The string to be escaped.
95  * @param boolean $extra Unused optional parameter to provide extra escaping.
96  *
97  * @return string The escaped string.
98  *
99  * @since 12.1
100  */
101  public function escape($text, $extra = false)
102  {
103  if (is_int($text) || is_float($text))
104  {
105  return $text;
106  }
107 
108  return SQLite3::escapeString($text);
109  }
110 
111  /**
112  * Method to get the database collation in use by sampling a text field of a table in the database.
113  *
114  * @return mixed The collation in use by the database or boolean false if not supported.
115  *
116  * @since 12.1
117  */
118  public function getCollation()
119  {
120  return $this->charset;
121  }
122 
123  /**
124  * Shows the table CREATE statement that creates the given tables.
125  *
126  * Note: Doesn't appear to have support in SQLite
127  *
128  * @param mixed $tables A table name or a list of table names.
129  *
130  * @return array A list of the create SQL for the tables.
131  *
132  * @since 12.1
133  * @throws RuntimeException
134  */
135  public function getTableCreate($tables)
136  {
137  $this->connect();
138 
139  // Sanitize input to an array and iterate over the list.
140  settype($tables, 'array');
141 
142  return $tables;
143  }
144 
145  /**
146  * Retrieves field information about a given table.
147  *
148  * @param string $table The name of the database table.
149  * @param boolean $typeOnly True to only return field types.
150  *
151  * @return array An array of fields for the database table.
152  *
153  * @since 12.1
154  * @throws RuntimeException
155  */
156  public function getTableColumns($table, $typeOnly = true)
157  {
158  $this->connect();
159 
160  $columns = array();
161  $query = $this->getQuery(true);
162 
163  $fieldCasing = $this->getOption(PDO::ATTR_CASE);
164 
165  $this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
166 
167  $table = strtoupper($table);
168 
169  $query->setQuery('pragma table_info(' . $table . ')');
170 
171  $this->setQuery($query);
172  $fields = $this->loadObjectList();
173 
174  if ($typeOnly)
175  {
176  foreach ($fields as $field)
177  {
178  $columns[$field->NAME] = $field->TYPE;
179  }
180  }
181  else
182  {
183  foreach ($fields as $field)
184  {
185  // Do some dirty translation to MySQL output.
186  // TODO: Come up with and implement a standard across databases.
187  $columns[$field->NAME] = (object) array(
188  'Field' => $field->NAME,
189  'Type' => $field->TYPE,
190  'Null' => ($field->NOTNULL == '1' ? 'NO' : 'YES'),
191  'Default' => $field->DFLT_VALUE,
192  'Key' => ($field->PK == '1' ? 'PRI' : '')
193  );
194  }
195  }
196 
197  $this->setOption(PDO::ATTR_CASE, $fieldCasing);
198 
199  return $columns;
200  }
201 
202  /**
203  * Get the details list of keys for a table.
204  *
205  * @param string $table The name of the table.
206  *
207  * @return array An array of the column specification for the table.
208  *
209  * @since 12.1
210  * @throws RuntimeException
211  */
212  public function getTableKeys($table)
213  {
214  $this->connect();
215 
216  $keys = array();
217  $query = $this->getQuery(true);
218 
219  $fieldCasing = $this->getOption(PDO::ATTR_CASE);
220 
221  $this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
222 
223  $table = strtoupper($table);
224  $query->setQuery('pragma table_info( ' . $table . ')');
225 
226  // $query->bind(':tableName', $table);
227 
228  $this->setQuery($query);
229  $rows = $this->loadObjectList();
230 
231  foreach ($rows as $column)
232  {
233  if ($column->PK == 1)
234  {
235  $keys[$column->NAME] = $column;
236  }
237  }
238 
239  $this->setOption(PDO::ATTR_CASE, $fieldCasing);
240 
241  return $keys;
242  }
243 
244  /**
245  * Method to get an array of all tables in the database (schema).
246  *
247  * @return array An array of all the tables in the database.
248  *
249  * @since 12.1
250  * @throws RuntimeException
251  */
252  public function getTableList()
253  {
254  $this->connect();
255 
256  $type = 'table';
257 
258  $query = $this->getQuery(true)
259  ->select('name')
260  ->from('sqlite_master')
261  ->where('type = :type')
262  ->bind(':type', $type)
263  ->order('name');
264 
265  $this->setQuery($query);
266 
267  $tables = $this->loadColumn();
268 
269  return $tables;
270  }
271 
272  /**
273  * Get the version of the database connector.
274  *
275  * @return string The database connector version.
276  *
277  * @since 12.1
278  */
279  public function getVersion()
280  {
281  $this->connect();
282 
283  $this->setQuery("SELECT sqlite_version()");
284 
285  return $this->loadResult();
286  }
287 
288  /**
289  * Select a database for use.
290  *
291  * @param string $database The name of the database to select for use.
292  *
293  * @return boolean True if the database was successfully selected.
294  *
295  * @since 12.1
296  * @throws RuntimeException
297  */
298  public function select($database)
299  {
300  $this->connect();
301 
302  return true;
303  }
304 
305  /**
306  * Set the connection to use UTF-8 character encoding.
307  *
308  * Returns false automatically for the Oracle driver since
309  * you can only set the character set when the connection
310  * is created.
311  *
312  * @return boolean True on success.
313  *
314  * @since 12.1
315  */
316  public function setUTF()
317  {
318  $this->connect();
319 
320  return false;
321  }
322 
323  /**
324  * Locks a table in the database.
325  *
326  * @param string $table The name of the table to unlock.
327  *
328  * @return JDatabaseDriverSqlite Returns this object to support chaining.
329  *
330  * @since 12.1
331  * @throws RuntimeException
332  */
333  public function lockTable($table)
334  {
335  return $this;
336  }
337 
338  /**
339  * Renames a table in the database.
340  *
341  * @param string $oldTable The name of the table to be renamed
342  * @param string $newTable The new name for the table.
343  * @param string $backup Not used by Sqlite.
344  * @param string $prefix Not used by Sqlite.
345  *
346  * @return JDatabaseDriverSqlite Returns this object to support chaining.
347  *
348  * @since 12.1
349  * @throws RuntimeException
350  */
351  public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
352  {
353  $this->setQuery('ALTER TABLE ' . $oldTable . ' RENAME TO ' . $newTable)->execute();
354 
355  return $this;
356  }
357 
358  /**
359  * Unlocks tables in the database.
360  *
361  * @return JDatabaseDriverSqlite Returns this object to support chaining.
362  *
363  * @since 12.1
364  * @throws RuntimeException
365  */
366  public function unlockTables()
367  {
368  return $this;
369  }
370 
371  /**
372  * Test to see if the PDO ODBC connector is available.
373  *
374  * @return boolean True on success, false otherwise.
375  *
376  * @since 12.1
377  */
378  public static function isSupported()
379  {
380  return class_exists('PDO') && in_array('sqlite', PDO::getAvailableDrivers());
381  }
382 
383  /**
384  * Method to commit a transaction.
385  *
386  * @param boolean $toSavepoint If true, commit to the last savepoint.
387  *
388  * @return void
389  *
390  * @since 12.3
391  * @throws RuntimeException
392  */
393  public function transactionCommit($toSavepoint = false)
394  {
395  $this->connect();
396 
397  if (!$toSavepoint || $this->transactionDepth <= 1)
398  {
399  parent::transactionCommit($toSavepoint);
400  }
401  else
402  {
403  $this->transactionDepth--;
404  }
405  }
406 
407  /**
408  * Method to roll back a transaction.
409  *
410  * @param boolean $toSavepoint If true, rollback to the last savepoint.
411  *
412  * @return void
413  *
414  * @since 12.3
415  * @throws RuntimeException
416  */
417  public function transactionRollback($toSavepoint = false)
418  {
419  $this->connect();
420 
421  if (!$toSavepoint || $this->transactionDepth <= 1)
422  {
423  parent::transactionRollback($toSavepoint);
424  }
425  else
426  {
427  $savepoint = 'SP_' . ($this->transactionDepth - 1);
428  $this->setQuery('ROLLBACK TO ' . $this->quoteName($savepoint));
429 
430  if ($this->execute())
431  {
432  $this->transactionDepth--;
433  }
434  }
435  }
436 
437  /**
438  * Method to initialize a transaction.
439  *
440  * @param boolean $asSavepoint If true and a transaction is already active, a savepoint will be created.
441  *
442  * @return void
443  *
444  * @since 12.3
445  * @throws RuntimeException
446  */
447  public function transactionStart($asSavepoint = false)
448  {
449  $this->connect();
450 
451  if (!$asSavepoint || !$this->transactionDepth)
452  {
453  parent::transactionStart($asSavepoint);
454  }
455 
456  $savepoint = 'SP_' . $this->transactionDepth;
457  $this->setQuery('SAVEPOINT ' . $this->quoteName($savepoint));
458 
459  if ($this->execute())
460  {
461  $this->transactionDepth++;
462  }
463  }
464 }