PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/database/driver/sqlite.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 383 lines | 146 code | 54 blank | 183 comment | 8 complexity | cfeac3e22265d5b1647ec118701dfb8d MD5 | raw file
  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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * SQLite database driver
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Database
  15. * @see http://php.net/pdo
  16. * @since 12.1
  17. */
  18. class JDatabaseDriverSqlite extends JDatabaseDriverPdo
  19. {
  20. /**
  21. * The name of the database driver.
  22. *
  23. * @var string
  24. * @since 12.1
  25. */
  26. public $name = 'sqlite';
  27. /**
  28. * The character(s) used to quote SQL statement names such as table names or field names,
  29. * etc. The child classes should define this as necessary. If a single character string the
  30. * same character is used for both sides of the quoted name, else the first character will be
  31. * used for the opening quote and the second for the closing quote.
  32. *
  33. * @var string
  34. * @since 12.1
  35. */
  36. protected $nameQuote = '`';
  37. /**
  38. * Destructor.
  39. *
  40. * @since 12.1
  41. */
  42. public function __destruct()
  43. {
  44. $this->freeResult();
  45. unset($this->connection);
  46. }
  47. /**
  48. * Disconnects the database.
  49. *
  50. * @return void
  51. *
  52. * @since 12.1
  53. */
  54. public function disconnect()
  55. {
  56. $this->freeResult();
  57. unset($this->connection);
  58. }
  59. /**
  60. * Drops a table from the database.
  61. *
  62. * @param string $tableName The name of the database table to drop.
  63. * @param boolean $ifExists Optionally specify that the table must exist before it is dropped.
  64. *
  65. * @return JDatabaseDriverSqlite Returns this object to support chaining.
  66. *
  67. * @since 12.1
  68. */
  69. public function dropTable($tableName, $ifExists = true)
  70. {
  71. $this->connect();
  72. $query = $this->getQuery(true);
  73. $this->setQuery('DROP TABLE ' . ($ifExists ? 'IF EXISTS ' : '') . $query->quoteName($tableName));
  74. $this->execute();
  75. return $this;
  76. }
  77. /**
  78. * Method to escape a string for usage in an SQLite statement.
  79. *
  80. * Note: Using query objects with bound variables is
  81. * preferable to the below.
  82. *
  83. * @param string $text The string to be escaped.
  84. * @param boolean $extra Unused optional parameter to provide extra escaping.
  85. *
  86. * @return string The escaped string.
  87. *
  88. * @since 12.1
  89. */
  90. public function escape($text, $extra = false)
  91. {
  92. if (is_int($text) || is_float($text))
  93. {
  94. return $text;
  95. }
  96. return SQLite3::escapeString($text);
  97. }
  98. /**
  99. * Method to get the database collation in use by sampling a text field of a table in the database.
  100. *
  101. * @return mixed The collation in use by the database or boolean false if not supported.
  102. *
  103. * @since 12.1
  104. */
  105. public function getCollation()
  106. {
  107. return $this->charset;
  108. }
  109. /**
  110. * Shows the table CREATE statement that creates the given tables.
  111. *
  112. * Note: Doesn't appear to have support in SQLite
  113. *
  114. * @param mixed $tables A table name or a list of table names.
  115. *
  116. * @return array A list of the create SQL for the tables.
  117. *
  118. * @since 12.1
  119. * @throws RuntimeException
  120. */
  121. public function getTableCreate($tables)
  122. {
  123. $this->connect();
  124. // Sanitize input to an array and iterate over the list.
  125. settype($tables, 'array');
  126. return $tables;
  127. }
  128. /**
  129. * Retrieves field information about a given table.
  130. *
  131. * @param string $table The name of the database table.
  132. * @param boolean $typeOnly True to only return field types.
  133. *
  134. * @return array An array of fields for the database table.
  135. *
  136. * @since 12.1
  137. * @throws RuntimeException
  138. */
  139. public function getTableColumns($table, $typeOnly = true)
  140. {
  141. $this->connect();
  142. $columns = array();
  143. $query = $this->getQuery(true);
  144. $fieldCasing = $this->getOption(PDO::ATTR_CASE);
  145. $this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
  146. $table = strtoupper($table);
  147. $query->setQuery('pragma table_info(' . $table . ')');
  148. $this->setQuery($query);
  149. $fields = $this->loadObjectList();
  150. if ($typeOnly)
  151. {
  152. foreach ($fields as $field)
  153. {
  154. $columns[$field->NAME] = $field->TYPE;
  155. }
  156. }
  157. else
  158. {
  159. foreach ($fields as $field)
  160. {
  161. // Do some dirty translation to MySQL output.
  162. // TODO: Come up with and implement a standard across databases.
  163. $columns[$field->NAME] = (object) array(
  164. 'Field' => $field->NAME,
  165. 'Type' => $field->TYPE,
  166. 'Null' => ($field->NOTNULL == '1' ? 'NO' : 'YES'),
  167. 'Default' => $field->DFLT_VALUE,
  168. 'Key' => ($field->PK == '1' ? 'PRI' : '')
  169. );
  170. }
  171. }
  172. $this->setOption(PDO::ATTR_CASE, $fieldCasing);
  173. return $columns;
  174. }
  175. /**
  176. * Get the details list of keys for a table.
  177. *
  178. * @param string $table The name of the table.
  179. *
  180. * @return array An array of the column specification for the table.
  181. *
  182. * @since 12.1
  183. * @throws RuntimeException
  184. */
  185. public function getTableKeys($table)
  186. {
  187. $this->connect();
  188. $keys = array();
  189. $query = $this->getQuery(true);
  190. $fieldCasing = $this->getOption(PDO::ATTR_CASE);
  191. $this->setOption(PDO::ATTR_CASE, PDO::CASE_UPPER);
  192. $table = strtoupper($table);
  193. $query->setQuery('pragma table_info( ' . $table . ')');
  194. // $query->bind(':tableName', $table);
  195. $this->setQuery($query);
  196. $rows = $this->loadObjectList();
  197. foreach ($rows as $column)
  198. {
  199. if ($column->PK == 1)
  200. {
  201. $keys[$column->NAME] = $column;
  202. }
  203. }
  204. $this->setOption(PDO::ATTR_CASE, $fieldCasing);
  205. return $keys;
  206. }
  207. /**
  208. * Method to get an array of all tables in the database (schema).
  209. *
  210. * @return array An array of all the tables in the database.
  211. *
  212. * @since 12.1
  213. * @throws RuntimeException
  214. */
  215. public function getTableList()
  216. {
  217. $this->connect();
  218. $query = $this->getQuery(true);
  219. $tables = array();
  220. $type = 'table';
  221. $query->select('name')
  222. ->from('sqlite_master')
  223. ->where('type = :type')
  224. ->bind(':type', $type)
  225. ->order('name');
  226. $this->setQuery($query);
  227. $tables = $this->loadColumn();
  228. return $tables;
  229. }
  230. /**
  231. * Get the version of the database connector.
  232. *
  233. * @return string The database connector version.
  234. *
  235. * @since 12.1
  236. */
  237. public function getVersion()
  238. {
  239. $this->connect();
  240. $this->setQuery("SELECT sqlite_version()");
  241. return $this->loadResult();
  242. }
  243. /**
  244. * Select a database for use.
  245. *
  246. * @param string $database The name of the database to select for use.
  247. *
  248. * @return boolean True if the database was successfully selected.
  249. *
  250. * @since 12.1
  251. * @throws RuntimeException
  252. */
  253. public function select($database)
  254. {
  255. $this->connect();
  256. return true;
  257. }
  258. /**
  259. * Set the connection to use UTF-8 character encoding.
  260. *
  261. * Returns false automatically for the Oracle driver since
  262. * you can only set the character set when the connection
  263. * is created.
  264. *
  265. * @return boolean True on success.
  266. *
  267. * @since 12.1
  268. */
  269. public function setUTF()
  270. {
  271. $this->connect();
  272. return false;
  273. }
  274. /**
  275. * Locks a table in the database.
  276. *
  277. * @param string $table The name of the table to unlock.
  278. *
  279. * @return JDatabaseDriverSqlite Returns this object to support chaining.
  280. *
  281. * @since 12.1
  282. * @throws RuntimeException
  283. */
  284. public function lockTable($table)
  285. {
  286. return $this;
  287. }
  288. /**
  289. * Renames a table in the database.
  290. *
  291. * @param string $oldTable The name of the table to be renamed
  292. * @param string $newTable The new name for the table.
  293. * @param string $backup Not used by Sqlite.
  294. * @param string $prefix Not used by Sqlite.
  295. *
  296. * @return JDatabaseDriverSqlite Returns this object to support chaining.
  297. *
  298. * @since 12.1
  299. * @throws RuntimeException
  300. */
  301. public function renameTable($oldTable, $newTable, $backup = null, $prefix = null)
  302. {
  303. $this->setQuery('ALTER TABLE ' . $oldTable . ' RENAME TO ' . $newTable)->execute();
  304. return $this;
  305. }
  306. /**
  307. * Unlocks tables in the database.
  308. *
  309. * @return JDatabaseDriverSqlite Returns this object to support chaining.
  310. *
  311. * @since 12.1
  312. * @throws RuntimeException
  313. */
  314. public function unlockTables()
  315. {
  316. return $this;
  317. }
  318. /**
  319. * Test to see if the PDO ODBC connector is available.
  320. *
  321. * @return boolean True on success, false otherwise.
  322. *
  323. * @since 12.1
  324. */
  325. public static function isSupported()
  326. {
  327. return class_exists('PDO') && in_array('sqlite', PDO::getAvailableDrivers());
  328. }
  329. }