/include/classes/sqlite_3.class.php

https://bitbucket.org/webinfopro/sqlitemanager · PHP · 311 lines · 302 code · 9 blank · 0 comment · 31 complexity · 4764eead67ec0ecb1cb568bf1d85ce9e MD5 · raw file

  1. <?php
  2. if( !defined('PDO_ATTR_TIMEOUT') ) define('PDO_ATTR_TIMEOUT', PDO::ATTR_TIMEOUT);
  3. if( !defined('PDO_FETCH_ASSOC') ) define('PDO_FETCH_ASSOC', PDO::FETCH_ASSOC);
  4. if( !defined('PDO_FETCH_NUM') ) define('PDO_FETCH_NUM', PDO::FETCH_NUM);
  5. if( !defined('PDO_FETCH_BOTH') ) define('PDO_FETCH_BOTH', PDO::FETCH_BOTH);
  6. if( !defined('PDO_ATTR_PERSISTENT') ) define('PDO_ATTR_PERSISTENT', PDO::ATTR_PERSISTENT);
  7. if( !defined('PDO_ATTR_CASE') ) define('PDO_ATTR_CASE', PDO::ATTR_CASE);
  8. if( !defined('PDO_CASE_NATURAL') ) define('PDO_CASE_NATURAL', PDO::CASE_NATURAL);
  9. if( !defined('PDO_ATTR_AUTOCOMMIT') ) define('PDO_ATTR_AUTOCOMMIT', PDO::ATTR_AUTOCOMMIT);
  10. if( !defined('PDO_ATTR_ERRMODE') ) define('PDO_ATTR_ERRMODE', PDO::ATTR_ERRMODE);
  11. if( !defined('PDO_ERRMODE_EXCEPTION') ) define('PDO_ERRMODE_EXCEPTION', PDO::ERRMODE_EXCEPTION);
  12. if( !defined('PDO_ERRMODE_SILENT') ) define('PDO_ERRMODE_SILENT', PDO::ERRMODE_SILENT);
  13. if( !defined('SQLITE_BOTH') ) define('SQLITE_BOTH', PDO_FETCH_BOTH);
  14. if( !defined('SQLITE_ASSOC') ) define('SQLITE_ASSOC', PDO_FETCH_ASSOC);
  15. if( !defined('SQLITE_NUM') ) define('SQLITE_NUM', PDO_FETCH_NUM);
  16. class sqlite_3 extends sqlite {
  17. function __construct($dbPath) {
  18. $this->dbVersion = 3;
  19. if($dbPath == ':memory:') {
  20. $this->readOnly = false;
  21. } else {
  22. $this->readOnly = !is_writable($dbPath);
  23. }
  24. parent::__construct($dbPath);
  25. if(class_exists('PDO') && $this->connect($dbPath)) {
  26. return $this->connId;
  27. } else {
  28. $this->getError();
  29. return false;
  30. }
  31. }
  32. function connect($dbPath) {
  33. try {
  34. $user = '';
  35. $password = '';
  36. $arrayAttrib = array();
  37. if($dbPath == ':memory:') $arrayAttrib[PDO_ATTR_PERSISTENT] = true;
  38. $this->connId = new PDO('sqlite:'.$dbPath, $user, $password, $arrayAttrib);
  39. $this->connId->setAttribute(PDO_ATTR_CASE, PDO_CASE_NATURAL);
  40. $this->connId->query("PRAGMA count_changes=1;");
  41. } catch (PDOException $e) {
  42. $this->error = true;
  43. $this->errorMessage = $e->getMessage();
  44. return false;
  45. }
  46. if(DEBUG) {
  47. $this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
  48. } else {
  49. $this->connId->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_SILENT);
  50. }
  51. return $this->connId;
  52. }
  53. function getError($errorCode = null) {
  54. if(is_resource($this->resId)) {
  55. $errorInfo = $this->resId->errorInfo();
  56. } else {
  57. $errorInfo = $this->connId->errorInfo();
  58. }
  59. if(is_array($errorInfo) && isset($errorInfo[2])) {
  60. $this->error = true;
  61. $this->errorMessage = $errorInfo[2];
  62. } else {
  63. $this->errorMessage = 'not an error';
  64. }
  65. return $this->errorMessage;
  66. }
  67. function getErrorMessage() {
  68. return $this->errorMessage;
  69. }
  70. function escape($string) {
  71. if(function_exists('sqlite_escape_string')) {
  72. $res = sqlite_escape_string($string);
  73. } else {
  74. $res = str_replace("'", "''", $string);
  75. }
  76. return $res;
  77. }
  78. function query($sqlString, $buffered = true, $assign = true) {
  79. try {
  80. if($assign && is_object($this->resId)) $this->resId->closeCursor();
  81. if(substr(trim($sqlString), -1) != ';') $sqlString .= ';';
  82. if(DEBUG) $resId = $this->connId->query($sqlString);
  83. else $resId = @$this->connId->query($sqlString);
  84. if($assign) $this->resId = $resId;
  85. } catch(PDOException $e) {
  86. $this->error = true;
  87. $this->errorMessage = $e->getMessage();
  88. return false;
  89. }
  90. $tempErrorInfo = $this->connId->errorInfo();
  91. if(is_array($tempErrorInfo) && isset($tempErrorInfo[0]) && ($tempErrorInfo[0] != '00000')) {
  92. $this->error = true;
  93. $this->errorMessage = $tempErrorInfo[2];
  94. return false;
  95. }
  96. return $resId;
  97. }
  98. function array_query($sqlString, $result_type=SQLITE_BOTH, $decode_binary=true) {
  99. try {
  100. $result_type = $this->convertResultType($result_type);
  101. $q = $this->query($sqlString);
  102. $rows = $q->fetchAll($result_type);
  103. } catch(PDOException $e) {
  104. $this->error = true;
  105. $this->errorMessage = $e->getMessage();
  106. return false;
  107. }
  108. return $rows;
  109. }
  110. function num_rows($resId = null) {
  111. try {
  112. if($resId == null) $resId = $this->resId;
  113. if(DEBUG) $out = $resId->rowCount();
  114. else $out = @$resId->rowCount();
  115. } catch(PDOException $e) {
  116. $this->error = true;
  117. $this->errorMessage = $e->getMessage();
  118. return false;
  119. }
  120. return $out;
  121. }
  122. function fetch_single($resId=null, $result_type=SQLITE_BOTH) {
  123. try {
  124. $result_type = $this->convertResultType($result_type);
  125. if($resId == null) $resId = $this->resId;
  126. if(DEBUG) $out = $resId->fetchColumn();
  127. else $out = @$resId->fetchColumn();
  128. } catch(PDOException $e) {
  129. $this->error = true;
  130. $this->errorMessage = $e->getMessage();
  131. return false;
  132. }
  133. return $out;
  134. }
  135. function fetch_array($resId=null, $result_type=SQLITE_BOTH, $decode_binary=true) {
  136. try {
  137. $result_type = $this->convertResultType($result_type);
  138. if($resId == null) $resId = $this->resId;
  139. if(DEBUG) $out = $resId->fetch($result_type);
  140. else $out = @$resId->fetch($result_type);
  141. } catch(PDOException $e) {
  142. $this->error = true;
  143. $this->errorMessage = $e->getMessage();
  144. return false;
  145. }
  146. return $out;
  147. }
  148. function last_insert_id() {
  149. try {
  150. if(DEBUG) $out = $this->connId->lastInsertId();
  151. else $out = @$this->connId->lastInsertId();
  152. } catch(PDOException $e) {
  153. $this->error = true;
  154. $this->errorMessage = $e->getMessage();
  155. return false;
  156. }
  157. return $out;
  158. }
  159. function changes($resId = null) {
  160. try {
  161. if($resId == null) $resId = $this->resId;
  162. if(is_object($resId)) $out = $this->num_rows($resId);
  163. else $out = 0;
  164. } catch(PDOException $e) {
  165. $this->error = true;
  166. $this->errorMessage = $e->getMessage();
  167. return false;
  168. }
  169. return $out;
  170. }
  171. function num_fields($resId = null) {
  172. try {
  173. if($resId == null) $resId = $this->resId;
  174. if(DEBUG) $out = $resId->columnCount();
  175. else $out = @$resId->columnCount();
  176. } catch(PDOException $e) {
  177. $this->error = true;
  178. $this->errorMessage = $e->getMessage();
  179. return false;
  180. }
  181. return $out;
  182. }
  183. function field_name($resId = null, $index) {
  184. try {
  185. if($resId == null) $resId = $this->resId;
  186. if(DEBUG) $tempColInfo = $resId->getColumnMeta($index);
  187. else $tempColInfo = @$resId->getColumnMeta($index);
  188. } catch(PDOException $e) {
  189. $this->error = true;
  190. $this->errorMessage = $e->getMessage();
  191. return false;
  192. }
  193. return $tempColInfo["name"];
  194. }
  195. function create_function($function_name, $callback, $num_arg=null) {
  196. try {
  197. if(method_exists($this->connId, 'sqliteCreateFunction')) {
  198. if(DEBUG) return $this->connId->sqliteCreateFunction($function_name, $callback, $num_arg);
  199. else return @$this->connId->sqliteCreateFunction($function_name, $callback, $num_arg);
  200. } else {
  201. return false;
  202. }
  203. } catch(PDOException $e) {
  204. $this->error = true;
  205. $this->errorMessage = $e->getMessage();
  206. return false;
  207. }
  208. }
  209. function create_aggregate($function_name, $step_func, $finalize_func, $num_args=null) {
  210. if(method_exists($this->connId, 'sqliteCreateAggregate')) {
  211. try {
  212. if(DEBUG) return $this->connId->sqliteCreateAggregate($function_name, $step_func, $finalize_func, $num_args);
  213. else return @$this->connId->sqliteCreateAggregate($function_name, $step_func, $finalize_func, $num_args);
  214. } catch(PDOException $e) {
  215. $this->error = true;
  216. $this->errorMessage = $e->getMessage();
  217. return false;
  218. }
  219. } else {
  220. return false;
  221. }
  222. }
  223. function sqlite_version() {
  224. try {
  225. $query = "SELECT sqlite_version();";
  226. $res = $this->query($query, true, false);
  227. $Result = $this->fetch_single($res, SQLITE_NUM);
  228. if($Result) return $Result;
  229. else return false;
  230. } catch(PDOException $e) {
  231. $this->error = true;
  232. $this->errorMessage = $e->getMessage();
  233. return false;
  234. }
  235. }
  236. function close() {
  237. // set obj to null
  238. }
  239. function sqlitem_busy_timeout($milliseconds=0) {
  240. try {
  241. $this->connId->setAttribute(PDO_ATTR_TIMEOUT, $milliseconds);
  242. return true;
  243. } catch(PDOException $e) {
  244. $this->error = true;
  245. $this->errorMessage = $e->getMessage();
  246. return false;
  247. }
  248. }
  249. function convertResultType($type) {
  250. $fetch_style = PDO_FETCH_BOTH;
  251. if($type == SQLITE_ASSOC) $fetch_style = PDO_FETCH_ASSOC;
  252. elseif($type == SQLITE_NUM) $fetch_style = PDO_FETCH_NUM;
  253. elseif($type == SQLITE_BOTH) $fetch_style = PDO_FETCH_BOTH;
  254. return $fetch_style;
  255. }
  256. function beginTransaction() {
  257. try {
  258. $this->connId->beginTransaction();
  259. } catch(PDOException $e) {
  260. $this->error = true;
  261. $this->errorMessage = $e->getMessage();
  262. return false;
  263. }
  264. }
  265. function commitTransaction() {
  266. try {
  267. $this->connId->commit();
  268. } catch(PDOException $e) {
  269. $this->error = true;
  270. $this->errorMessage = $e->getMessage();
  271. return false;
  272. }
  273. }
  274. function rollBackTransaction() {
  275. try {
  276. $this->connId->rollBack();
  277. } catch(PDOException $e) {
  278. $this->error = true;
  279. $this->errorMessage = $e->getMessage();
  280. return false;
  281. }
  282. }
  283. }
  284. ?>