PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/adodb/drivers/adodb-sqlite.inc.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 399 lines | 297 code | 54 blank | 48 comment | 52 complexity | d99d4947cef966e57da723807c307f6c MD5 | raw file
  1. <?php
  2. /*
  3. V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved.
  4. Released under both BSD license and Lesser GPL library license.
  5. Whenever there is any discrepancy between the two licenses,
  6. the BSD license will take precedence.
  7. Latest version is available at http://adodb.sourceforge.net
  8. SQLite info: http://www.hwaci.com/sw/sqlite/
  9. Install Instructions:
  10. ====================
  11. 1. Place this in adodb/drivers
  12. 2. Rename the file, remove the .txt prefix.
  13. */
  14. // security - hide paths
  15. if (!defined('ADODB_DIR')) die();
  16. class ADODB_sqlite extends ADOConnection {
  17. var $databaseType = "sqlite";
  18. var $replaceQuote = "''"; // string to use to replace quotes
  19. var $concat_operator='||';
  20. var $_errorNo = 0;
  21. var $hasLimit = true;
  22. var $hasInsertID = true; /// supports autoincrement ID?
  23. var $hasAffectedRows = true; /// supports affected rows for update/delete?
  24. var $metaTablesSQL = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
  25. var $sysDate = "adodb_date('Y-m-d')";
  26. var $sysTimeStamp = "adodb_date('Y-m-d H:i:s')";
  27. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  28. function ADODB_sqlite()
  29. {
  30. }
  31. /*
  32. function __get($name)
  33. {
  34. switch($name) {
  35. case 'sysDate': return "'".date($this->fmtDate)."'";
  36. case 'sysTimeStamp' : return "'".date($this->sysTimeStamp)."'";
  37. }
  38. }*/
  39. function ServerInfo()
  40. {
  41. $arr['version'] = sqlite_libversion();
  42. $arr['description'] = 'SQLite ';
  43. $arr['encoding'] = sqlite_libencoding();
  44. return $arr;
  45. }
  46. function BeginTrans()
  47. {
  48. if ($this->transOff) return true;
  49. $ret = $this->Execute("BEGIN TRANSACTION");
  50. $this->transCnt += 1;
  51. return true;
  52. }
  53. function CommitTrans($ok=true)
  54. {
  55. if ($this->transOff) return true;
  56. if (!$ok) return $this->RollbackTrans();
  57. $ret = $this->Execute("COMMIT");
  58. if ($this->transCnt>0)$this->transCnt -= 1;
  59. return !empty($ret);
  60. }
  61. function RollbackTrans()
  62. {
  63. if ($this->transOff) return true;
  64. $ret = $this->Execute("ROLLBACK");
  65. if ($this->transCnt>0)$this->transCnt -= 1;
  66. return !empty($ret);
  67. }
  68. // mark newnham
  69. function MetaColumns($table, $normalize=true)
  70. {
  71. global $ADODB_FETCH_MODE;
  72. $false = false;
  73. $save = $ADODB_FETCH_MODE;
  74. $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  75. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  76. $rs = $this->Execute("PRAGMA table_info('$table')");
  77. if (isset($savem)) $this->SetFetchMode($savem);
  78. if (!$rs) {
  79. $ADODB_FETCH_MODE = $save;
  80. return $false;
  81. }
  82. $arr = array();
  83. while ($r = $rs->FetchRow()) {
  84. $type = explode('(',$r['type']);
  85. $size = '';
  86. if (sizeof($type)==2)
  87. $size = trim($type[1],')');
  88. $fn = strtoupper($r['name']);
  89. $fld = new ADOFieldObject;
  90. $fld->name = $r['name'];
  91. $fld->type = $type[0];
  92. $fld->max_length = $size;
  93. $fld->not_null = $r['notnull'];
  94. $fld->default_value = $r['dflt_value'];
  95. $fld->scale = 0;
  96. if (isset($r['pk']) && $r['pk']) $fld->primary_key=1;
  97. if ($save == ADODB_FETCH_NUM) $arr[] = $fld;
  98. else $arr[strtoupper($fld->name)] = $fld;
  99. }
  100. $rs->Close();
  101. $ADODB_FETCH_MODE = $save;
  102. return $arr;
  103. }
  104. function _init($parentDriver)
  105. {
  106. $parentDriver->hasTransactions = false;
  107. $parentDriver->hasInsertID = true;
  108. }
  109. function _insertid()
  110. {
  111. return sqlite_last_insert_rowid($this->_connectionID);
  112. }
  113. function _affectedrows()
  114. {
  115. return sqlite_changes($this->_connectionID);
  116. }
  117. function ErrorMsg()
  118. {
  119. if ($this->_logsql) return $this->_errorMsg;
  120. return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) : '';
  121. }
  122. function ErrorNo()
  123. {
  124. return $this->_errorNo;
  125. }
  126. function SQLDate($fmt, $col=false)
  127. {
  128. $fmt = $this->qstr($fmt);
  129. return ($col) ? "adodb_date2($fmt,$col)" : "adodb_date($fmt)";
  130. }
  131. function _createFunctions()
  132. {
  133. @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
  134. @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
  135. }
  136. // returns true or false
  137. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  138. {
  139. if (!function_exists('sqlite_open')) return null;
  140. if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
  141. $this->_connectionID = sqlite_open($argHostname);
  142. if ($this->_connectionID === false) return false;
  143. $this->_createFunctions();
  144. return true;
  145. }
  146. // returns true or false
  147. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  148. {
  149. if (!function_exists('sqlite_open')) return null;
  150. if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
  151. $this->_connectionID = sqlite_popen($argHostname);
  152. if ($this->_connectionID === false) return false;
  153. $this->_createFunctions();
  154. return true;
  155. }
  156. // returns query ID if successful, otherwise false
  157. function _query($sql,$inputarr=false)
  158. {
  159. $rez = sqlite_query($sql,$this->_connectionID);
  160. if (!$rez) {
  161. $this->_errorNo = sqlite_last_error($this->_connectionID);
  162. }
  163. return $rez;
  164. }
  165. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  166. {
  167. $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
  168. $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
  169. if ($secs2cache)
  170. $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
  171. else
  172. $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
  173. return $rs;
  174. }
  175. /*
  176. This algorithm is not very efficient, but works even if table locking
  177. is not available.
  178. Will return false if unable to generate an ID after $MAXLOOPS attempts.
  179. */
  180. var $_genSeqSQL = "create table %s (id integer)";
  181. function GenID($seq='adodbseq',$start=1)
  182. {
  183. // if you have to modify the parameter below, your database is overloaded,
  184. // or you need to implement generation of id's yourself!
  185. $MAXLOOPS = 100;
  186. //$this->debug=1;
  187. while (--$MAXLOOPS>=0) {
  188. @($num = $this->GetOne("select id from $seq"));
  189. if ($num === false) {
  190. $this->Execute(sprintf($this->_genSeqSQL ,$seq));
  191. $start -= 1;
  192. $num = '0';
  193. $ok = $this->Execute("insert into $seq values($start)");
  194. if (!$ok) return false;
  195. }
  196. $this->Execute("update $seq set id=id+1 where id=$num");
  197. if ($this->affected_rows() > 0) {
  198. $num += 1;
  199. $this->genID = $num;
  200. return $num;
  201. }
  202. }
  203. if ($fn = $this->raiseErrorFn) {
  204. $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
  205. }
  206. return false;
  207. }
  208. function CreateSequence($seqname='adodbseq',$start=1)
  209. {
  210. if (empty($this->_genSeqSQL)) return false;
  211. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  212. if (!$ok) return false;
  213. $start -= 1;
  214. return $this->Execute("insert into $seqname values($start)");
  215. }
  216. var $_dropSeqSQL = 'drop table %s';
  217. function DropSequence($seqname)
  218. {
  219. if (empty($this->_dropSeqSQL)) return false;
  220. return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
  221. }
  222. // returns true or false
  223. function _close()
  224. {
  225. return @sqlite_close($this->_connectionID);
  226. }
  227. function MetaIndexes($table, $primary = FALSE, $owner=false, $owner = false)
  228. {
  229. $false = false;
  230. // save old fetch mode
  231. global $ADODB_FETCH_MODE;
  232. $save = $ADODB_FETCH_MODE;
  233. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  234. if ($this->fetchMode !== FALSE) {
  235. $savem = $this->SetFetchMode(FALSE);
  236. }
  237. $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table));
  238. $rs = $this->Execute($SQL);
  239. if (!is_object($rs)) {
  240. if (isset($savem))
  241. $this->SetFetchMode($savem);
  242. $ADODB_FETCH_MODE = $save;
  243. return $false;
  244. }
  245. $indexes = array ();
  246. while ($row = $rs->FetchRow()) {
  247. if ($primary && preg_match("/primary/i",$row[1]) == 0) continue;
  248. if (!isset($indexes[$row[0]])) {
  249. $indexes[$row[0]] = array(
  250. 'unique' => preg_match("/unique/i",$row[1]),
  251. 'columns' => array());
  252. }
  253. /**
  254. * There must be a more elegant way of doing this,
  255. * the index elements appear in the SQL statement
  256. * in cols[1] between parentheses
  257. * e.g CREATE UNIQUE INDEX ware_0 ON warehouse (org,warehouse)
  258. */
  259. $cols = explode("(",$row[1]);
  260. $cols = explode(")",$cols[1]);
  261. array_pop($cols);
  262. $indexes[$row[0]]['columns'] = $cols;
  263. }
  264. if (isset($savem)) {
  265. $this->SetFetchMode($savem);
  266. $ADODB_FETCH_MODE = $save;
  267. }
  268. return $indexes;
  269. }
  270. }
  271. /*--------------------------------------------------------------------------------------
  272. Class Name: Recordset
  273. --------------------------------------------------------------------------------------*/
  274. class ADORecordset_sqlite extends ADORecordSet {
  275. var $databaseType = "sqlite";
  276. var $bind = false;
  277. function ADORecordset_sqlite($queryID,$mode=false)
  278. {
  279. if ($mode === false) {
  280. global $ADODB_FETCH_MODE;
  281. $mode = $ADODB_FETCH_MODE;
  282. }
  283. switch($mode) {
  284. case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
  285. case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
  286. default: $this->fetchMode = SQLITE_BOTH; break;
  287. }
  288. $this->adodbFetchMode = $mode;
  289. $this->_queryID = $queryID;
  290. $this->_inited = true;
  291. $this->fields = array();
  292. if ($queryID) {
  293. $this->_currentRow = 0;
  294. $this->EOF = !$this->_fetch();
  295. @$this->_initrs();
  296. } else {
  297. $this->_numOfRows = 0;
  298. $this->_numOfFields = 0;
  299. $this->EOF = true;
  300. }
  301. return $this->_queryID;
  302. }
  303. function FetchField($fieldOffset = -1)
  304. {
  305. $fld = new ADOFieldObject;
  306. $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
  307. $fld->type = 'VARCHAR';
  308. $fld->max_length = -1;
  309. return $fld;
  310. }
  311. function _initrs()
  312. {
  313. $this->_numOfRows = @sqlite_num_rows($this->_queryID);
  314. $this->_numOfFields = @sqlite_num_fields($this->_queryID);
  315. }
  316. function Fields($colname)
  317. {
  318. if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
  319. if (!$this->bind) {
  320. $this->bind = array();
  321. for ($i=0; $i < $this->_numOfFields; $i++) {
  322. $o = $this->FetchField($i);
  323. $this->bind[strtoupper($o->name)] = $i;
  324. }
  325. }
  326. return $this->fields[$this->bind[strtoupper($colname)]];
  327. }
  328. function _seek($row)
  329. {
  330. return sqlite_seek($this->_queryID, $row);
  331. }
  332. function _fetch($ignore_fields=false)
  333. {
  334. $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
  335. return !empty($this->fields);
  336. }
  337. function _close()
  338. {
  339. }
  340. }
  341. ?>