PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/files/sqlite3/DB/sqlite3.php

https://github.com/alejo/BAPS2
PHP | 395 lines | 111 code | 76 blank | 208 comment | 12 complexity | 3377e0c51c7d141b30c7108adb5b022c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The PEAR DB driver for PHP's sqlite3 extension
  5. * for interacting with SQLite3 databases
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category Database
  16. * @package DB
  17. * @author Bruno Fleisch
  18. * @copyright 1997-2005 The PHP Group
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0 3.0
  20. * @version CVS: $Id: sqlite3.php,v 1.3 2007/04/04 14:13:19 bfleisch Exp $
  21. * @link http://pear.php.net/package/DB
  22. */
  23. include_once 'DB/common.php';
  24. /**
  25. * The methods PEAR DB uses to interact with PHP's sqlite extension
  26. * for interacting with SQLite databases
  27. *
  28. * These methods overload the ones declared in DB_common.
  29. *
  30. */
  31. class DB_sqlite3 extends DB_common
  32. {
  33. // {{{ PROPERTIES
  34. var $phptype = 'sqlite3';
  35. // }}}
  36. // {{{ constructor
  37. /**
  38. * constructor
  39. *
  40. * @return void
  41. */
  42. function DB_sqlite3()
  43. {
  44. $this->DB_common();
  45. }
  46. // }}}
  47. // {{{ string errorNative(void)
  48. /**
  49. * Gets the DBMS' native error code produced by the last query
  50. *
  51. * @return mixed the DBMS' error code. A DB_Error object on failure.
  52. */
  53. function errorNative()
  54. {
  55. return sqlite3_error ($this->connection);
  56. }
  57. // }}}
  58. // {{{ mixed connect(array $dsn, bool $persitent)
  59. /**
  60. * create or connect to the specified database.
  61. *
  62. * @param array $dsn the data source name
  63. * @param bool $persitent if the connection is persitent
  64. *
  65. * @return DB_OK on success or DB_error object on failure
  66. */
  67. function connect($dsn, $persitent = false)
  68. {
  69. $this->connection = sqlite3_open ($dsn['database']);
  70. if (!$this->connection)
  71. return $this->raiseError(DB_ERROR_NODBSELECTED);
  72. return DB_OK;
  73. }
  74. // }}}
  75. // {{{ bool disconnect (void)
  76. /**
  77. * release all resources for this connection, and close database
  78. *
  79. * @return bool TRUE on sucess, FALSE otherwise.
  80. */
  81. function disconnect()
  82. {
  83. return sqlite3_close ($this->connection);
  84. }
  85. // }}}
  86. // {{{ mixed simpleQuery(string $sql)
  87. /**
  88. * execute a SQL query.
  89. *
  90. * @param string $query the SQL query
  91. *
  92. * @return mixed + object DB_error object on failure
  93. * + object Result resource for SELECT requests
  94. * + bool TRUE for other sucessful requests
  95. */
  96. function simpleQuery($query)
  97. {
  98. $isSelect = preg_match ("/^\s*SELECT/i", $query);
  99. if (! $isSelect)
  100. $this->result = sqlite3_exec($this->connection, $query);
  101. else
  102. $this->result = sqlite3_query($this->connection, $query);
  103. if (!$this->result)
  104. return $this->RaiseError($this->errorNative());
  105. return $this->result;
  106. }
  107. // }}}
  108. // {{{ mixed fetchInto(resource $res, array $arr, int $fetchmode [, int $rownum])
  109. /**
  110. * Fetch a row of data into an array which is passed by reference
  111. *
  112. * The type of array returned can be controlled either by setting this
  113. * method's <var>$fetchmode</var> parameter or by changing the default
  114. * fetch mode setFetchMode() before calling this method.
  115. *
  116. * There are two options for standardizing the information returned
  117. * from databases, ensuring their values are consistent when changing
  118. * DBMS's. These portability options can be turned on when creating a
  119. * new DB object or by using setOption().
  120. *
  121. * + <var>DB_PORTABILITY_LOWERCASE</var>
  122. * convert names of fields to lower case
  123. *
  124. * + <var>DB_PORTABILITY_RTRIM</var>
  125. * right trim the data
  126. *
  127. * @param resource $result the result resource
  128. * @param array &$arr the variable where the data should be placed
  129. * @param int $fetchmode the constant indicating how to format the data
  130. * @param int $rownum the row number to fetch (index starts at 0)
  131. *
  132. * @return mixed DB_OK if a row is processed, NULL when the end of the
  133. * result set is reached or a DB_Error object on failure
  134. *
  135. * @see DB_common::setOption(), DB_common::setFetchMode()
  136. */
  137. function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  138. {
  139. if ($rownum !==NULL)
  140. return $this->RaiseError (DB_ERROR_NOTIMPLEMENTED);
  141. switch ($fetchmode)
  142. {
  143. case DB_FETCHMODE_ORDERED:
  144. $fetchfunc="sqlite3_fetch";
  145. break;
  146. case DB_FETCHMODE_OBJECT:
  147. return $this->RaiseError(DB_ERROR_NODBSELECTED);
  148. break;
  149. case DB_FETCHMODE_ASSOC:
  150. default:
  151. $fetchfunc="sqlite3_fetch_array";
  152. break;
  153. }
  154. $arr = $fetchfunc($result);
  155. if ($arr)
  156. return DB_OK;
  157. return NULL;
  158. }
  159. // }}}
  160. // {{{ string modifyLimitQuery(string $query, int $from, int $count [,mixed $params])
  161. /**
  162. * Adds LIMIT clauses to a query string according to current DBMS standards
  163. *
  164. * It is defined here to assure that all implementations
  165. * have this method defined.
  166. *
  167. * @param string $query the query to modify
  168. * @param int $from the row to start to fetching (0 = the first row)
  169. * @param int $count the numbers of rows to fetch
  170. * @param mixed $params array, string or numeric data to be used in
  171. * execution of the statement. Quantity of items
  172. * passed must match quantity of placeholders in
  173. * query: meaning 1 placeholder for non-array
  174. * parameters or 1 placeholder per array element.
  175. *
  176. * @return string the query string with LIMIT clauses added
  177. *
  178. * @access protected
  179. */
  180. function modifyLimitQuery($query, $from, $count, $params = array())
  181. {
  182. return "$query LIMIT $count OFFSET $from";
  183. }
  184. // }}}
  185. // {{{ bool freeResult(void)
  186. /**
  187. * free the specified result.
  188. *
  189. * @param resource $result the query resource result
  190. *
  191. * @return bool DB_OK
  192. *
  193. */
  194. function freeResult($result)
  195. {
  196. sqlite3_query_close($result);
  197. return DB_OK; /* always sucessful ! */
  198. }
  199. // }}}
  200. // {{{ int affectedRow(void)
  201. /**
  202. * Determines the number of rows affected by a data maniuplation query
  203. *
  204. * 0 is returned for queries that don't manipulate data.
  205. *
  206. * @return int the number of rows. A DB_Error object on failure.
  207. */
  208. function affectedRows()
  209. {
  210. return sqlite3_changes ($this->connection);
  211. }
  212. // }}}
  213. // {{{ mixed numCols(resource $result)
  214. /**
  215. * Get the the number of columns in a result set
  216. *
  217. * @return int the number of columns. A DB_Error object on failure.
  218. */
  219. function numCols($result)
  220. {
  221. return sqlite3_column_count($result);
  222. }
  223. // }}}
  224. // {{{ mixed createSequence(string $seq_name)
  225. /**
  226. * Creates a new sequence
  227. *
  228. * The name of a given sequence is determined by passing the string
  229. * provided in the <var>$seq_name</var> argument through PHP's sprintf()
  230. * function using the value from the <var>seqname_format</var> option as
  231. * the sprintf()'s format argument.
  232. *
  233. * <var>seqname_format</var> is set via setOption().
  234. *
  235. * @param string $seq_name name of the new sequence
  236. *
  237. * @return int DB_OK on success. A DB_Error object on failure.
  238. *
  239. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  240. * DB_common::nextID()
  241. */
  242. function createSequence($seq_name)
  243. {
  244. return $this->query ("CREATE TABLE " . $this->getSequenceName($seq_name) . " (id INTEGER PRIMARY KEY AUTOINCREMENT)");
  245. }
  246. // }}}
  247. // {{{ mixed nextId(string $sequence [, bool $ondemand])
  248. /**
  249. * Returns the next free id in a sequence
  250. *
  251. * @param string $seq_name name of the sequence
  252. * @param boolean $ondemand when true, the seqence is automatically
  253. * created if it does not exist
  254. *
  255. * @return int the next id number in the sequence.
  256. * A DB_Error object on failure.
  257. *
  258. * @see DB_common::createSequence(), DB_common::dropSequence(),
  259. * DB_common::getSequenceName()
  260. */
  261. function nextId($seq_name, $ondemand = true)
  262. {
  263. $sqn = $this->getSequenceName($seq_name);
  264. if ($ondemand)
  265. {
  266. $tables = $this->getTables();
  267. if (DB::isError($tables)) return $tables;
  268. if (! in_array ($sqn, $tables))
  269. {
  270. $res = $this->createSequence($seq_name);
  271. if ( DB::isError($res))
  272. return $res;
  273. }
  274. }
  275. $res = $this->query ("INSERT INTO " . $sqn . " VALUES (NULL)");
  276. if (DB::isError($res)) return $res;
  277. return sqlite3_last_insert_rowid ($this->connection);
  278. }
  279. // }}}
  280. // {{{ mixed dropSequence (string $seq_name)
  281. /**
  282. * Deletes a sequence
  283. *
  284. * @param string $seq_name name of the sequence to be deleted
  285. *
  286. * @return int DB_OK on success. A DB_Error object on failure.
  287. *
  288. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  289. * DB_common::nextID()
  290. */
  291. function dropSequence($seq_name)
  292. {
  293. return $this->query("DROP TABLE ". $this->getSequenceName($seq_name));
  294. }
  295. // }}}
  296. // {{{ string getSpecialQuery(string $type)
  297. /**
  298. * Obtains the query string needed for listing a given type of objects
  299. *
  300. * @param string $type the kind of objects you want to retrieve
  301. *
  302. * @return string the SQL query string or null if the driver doesn't
  303. * support the object type requested
  304. *
  305. * @access protected
  306. * @see DB_common::getListOf()
  307. */
  308. function getSpecialQuery($type)
  309. {
  310. switch ($type) {
  311. case 'tables':
  312. return "SELECT name FROM SQLITE_MASTER ORDER BY name";
  313. default:
  314. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  315. }
  316. }
  317. // }}}
  318. }
  319. ?>