dbo_sqlite.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?PHP
  2. //////////////////////////////////////////////////////////////////////////
  3. // + $Id$
  4. // +------------------------------------------------------------------+ //
  5. // + Cake <https://developers.nextco.com/cake/> + //
  6. // + Copyright: (c) 2005, Cake Authors/Developers + //
  7. // + Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com> + //
  8. // + Larry E. Masters aka PhpNut <nut@phpnut.com> + //
  9. // + Kamil Dzielinski aka Brego <brego.dk@gmail.com> + //
  10. // +------------------------------------------------------------------+ //
  11. // + Licensed under The MIT License + //
  12. // + Redistributions of files must retain the above copyright notice. + //
  13. // + See: http://www.opensource.org/licenses/mit-license.php + //
  14. //////////////////////////////////////////////////////////////////////////
  15. /**
  16. * Purpose: DBO_SQLite
  17. * SQLite layer for DBO
  18. *
  19. * @filesource
  20. * @author Cake Authors/Developers
  21. * @copyright Copyright (c) 2005, Cake Authors/Developers
  22. * @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers
  23. * @package cake
  24. * @subpackage cake.libs
  25. * @since Cake v 0.9.0
  26. * @version $Revision$
  27. * @modifiedby $LastChangedBy$
  28. * @lastmodified $Date$
  29. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  30. */
  31. /**
  32. * Enter description here...
  33. *
  34. */
  35. uses('dbo');
  36. /**
  37. * SQLite layer for DBO.
  38. *
  39. * @package cake
  40. * @subpackage cake.libs
  41. * @since Cake v 0.9.0
  42. *
  43. */
  44. class DBO_SQLite extends DBO {
  45. /**
  46. * We are connecting to the database, and using config['host'] as a filename.
  47. *
  48. * @param array $config
  49. * @return mixed
  50. */
  51. function connect ($config) {
  52. if($config) {
  53. $this->config = $config;
  54. $this->_conn = sqlite_open($config['host']);
  55. }
  56. $this->connected = $this->_conn ? true: false;
  57. if($this->connected==false)
  58. die('Could not connect to DB.');
  59. else
  60. return $this->_conn;
  61. }
  62. /**
  63. * Disconnects from database.
  64. *
  65. * @return boolean True if the database could be disconnected, else false
  66. */
  67. function disconnect () {
  68. return sqlite_close($this->_conn);
  69. }
  70. /**
  71. * Executes given SQL statement.
  72. *
  73. * @param string $sql SQL statement
  74. * @return resource Result resource identifier
  75. */
  76. function execute ($sql) {
  77. return sqlite_query($this->_conn, $sql);
  78. }
  79. /**
  80. * Returns a row from given resultset as an array .
  81. *
  82. * @param unknown_type $res Resultset
  83. * @return array The fetched row as an array
  84. */
  85. function fetchRow ($res) {
  86. return sqlite_fetch_array($res);
  87. }
  88. /**
  89. * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
  90. *
  91. * @return array Array of tablenames in the database
  92. */
  93. function tables () {
  94. $result = sqlite_query($this->_conn, "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;");
  95. $this->_conn
  96. if (!$result) {
  97. trigger_error(ERROR_NO_TABLE_LIST, E_USER_NOTICE);
  98. exit;
  99. }
  100. else {
  101. $tables = array();
  102. while ($line = sqlite_fetch_array($result)) {
  103. $tables[] = $line[0];
  104. }
  105. return $tables;
  106. }
  107. }
  108. /**
  109. * Returns an array of the fields in given table name.
  110. *
  111. * @param string $table_name Name of database table to inspect
  112. * @return array Fields in table. Keys are name and type
  113. */
  114. function fields ($table_name)
  115. {
  116. $fields = false;
  117. $cols = sqlite_fetch_column_types($table_name, $this->_conn, SQLITE_ASSOC);
  118. foreach ($cols as $column => $type)
  119. $fields[] = array('name'=>$column, 'type'=>$type);
  120. return $fields;
  121. }
  122. /**
  123. * Returns a quoted and escaped string of $data for use in an SQL statement.
  124. *
  125. * @param string $data String to be prepared for use in an SQL statement
  126. * @return string Quoted and escaped
  127. */
  128. function prepareValue ($data) {
  129. return "'".sqlite_escape_string($data)."'";
  130. }
  131. /**
  132. * Returns a formatted error message from previous database operation.
  133. *
  134. * @return string Error message
  135. */
  136. function lastError () {
  137. return sqlite_last_error($this->_conn)? sqlite_last_error($this->_conn).': '.sqlite_error_string(sqlite_last_error($this->_conn)): null;
  138. }
  139. /**
  140. * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false.
  141. *
  142. * @return int Number of affected rows
  143. */
  144. function lastAffected () {
  145. return $this->_result? sqlite_changes($this->_conn): false;
  146. }
  147. /**
  148. * Returns number of rows in previous resultset. If no previous resultset exists,
  149. * this returns false.
  150. *
  151. * @return int Number of rows in resultset
  152. */
  153. function lastNumRows () {
  154. return $this->_result? sqlite_num_rows($this->_result): false;
  155. }
  156. /**
  157. * Returns the ID generated from the previous INSERT operation.
  158. *
  159. * @return int
  160. */
  161. function lastInsertId() {
  162. Return sqlite_last_insert_rowid($this->_conn);
  163. }
  164. }
  165. ?>