PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 398 lines | 296 code | 54 blank | 48 comment | 50 complexity | abbaecf6deb09a2c106af22510760739 MD5 | raw file
  1. <?php
  2. /*
  3. V5.10 10 Nov 2009 (c) 2000-2009 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 ($save == ADODB_FETCH_NUM) $arr[] = $fld;
  97. else $arr[strtoupper($fld->name)] = $fld;
  98. }
  99. $rs->Close();
  100. $ADODB_FETCH_MODE = $save;
  101. return $arr;
  102. }
  103. function _init($parentDriver)
  104. {
  105. $parentDriver->hasTransactions = false;
  106. $parentDriver->hasInsertID = true;
  107. }
  108. function _insertid()
  109. {
  110. return sqlite_last_insert_rowid($this->_connectionID);
  111. }
  112. function _affectedrows()
  113. {
  114. return sqlite_changes($this->_connectionID);
  115. }
  116. function ErrorMsg()
  117. {
  118. if ($this->_logsql) return $this->_errorMsg;
  119. return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) : '';
  120. }
  121. function ErrorNo()
  122. {
  123. return $this->_errorNo;
  124. }
  125. function SQLDate($fmt, $col=false)
  126. {
  127. $fmt = $this->qstr($fmt);
  128. return ($col) ? "adodb_date2($fmt,$col)" : "adodb_date($fmt)";
  129. }
  130. function _createFunctions()
  131. {
  132. @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
  133. @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
  134. }
  135. // returns true or false
  136. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  137. {
  138. if (!function_exists('sqlite_open')) return null;
  139. if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
  140. $this->_connectionID = sqlite_open($argHostname);
  141. if ($this->_connectionID === false) return false;
  142. $this->_createFunctions();
  143. return true;
  144. }
  145. // returns true or false
  146. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  147. {
  148. if (!function_exists('sqlite_open')) return null;
  149. if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
  150. $this->_connectionID = sqlite_popen($argHostname);
  151. if ($this->_connectionID === false) return false;
  152. $this->_createFunctions();
  153. return true;
  154. }
  155. // returns query ID if successful, otherwise false
  156. function _query($sql,$inputarr=false)
  157. {
  158. $rez = sqlite_query($sql,$this->_connectionID);
  159. if (!$rez) {
  160. $this->_errorNo = sqlite_last_error($this->_connectionID);
  161. }
  162. return $rez;
  163. }
  164. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  165. {
  166. $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
  167. $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
  168. if ($secs2cache)
  169. $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
  170. else
  171. $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
  172. return $rs;
  173. }
  174. /*
  175. This algorithm is not very efficient, but works even if table locking
  176. is not available.
  177. Will return false if unable to generate an ID after $MAXLOOPS attempts.
  178. */
  179. var $_genSeqSQL = "create table %s (id integer)";
  180. function GenID($seq='adodbseq',$start=1)
  181. {
  182. // if you have to modify the parameter below, your database is overloaded,
  183. // or you need to implement generation of id's yourself!
  184. $MAXLOOPS = 100;
  185. //$this->debug=1;
  186. while (--$MAXLOOPS>=0) {
  187. @($num = $this->GetOne("select id from $seq"));
  188. if ($num === false) {
  189. $this->Execute(sprintf($this->_genSeqSQL ,$seq));
  190. $start -= 1;
  191. $num = '0';
  192. $ok = $this->Execute("insert into $seq values($start)");
  193. if (!$ok) return false;
  194. }
  195. $this->Execute("update $seq set id=id+1 where id=$num");
  196. if ($this->affected_rows() > 0) {
  197. $num += 1;
  198. $this->genID = $num;
  199. return $num;
  200. }
  201. }
  202. if ($fn = $this->raiseErrorFn) {
  203. $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
  204. }
  205. return false;
  206. }
  207. function CreateSequence($seqname='adodbseq',$start=1)
  208. {
  209. if (empty($this->_genSeqSQL)) return false;
  210. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  211. if (!$ok) return false;
  212. $start -= 1;
  213. return $this->Execute("insert into $seqname values($start)");
  214. }
  215. var $_dropSeqSQL = 'drop table %s';
  216. function DropSequence($seqname)
  217. {
  218. if (empty($this->_dropSeqSQL)) return false;
  219. return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
  220. }
  221. // returns true or false
  222. function _close()
  223. {
  224. return @sqlite_close($this->_connectionID);
  225. }
  226. function MetaIndexes($table, $primary = FALSE, $owner=false)
  227. {
  228. $false = false;
  229. // save old fetch mode
  230. global $ADODB_FETCH_MODE;
  231. $save = $ADODB_FETCH_MODE;
  232. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  233. if ($this->fetchMode !== FALSE) {
  234. $savem = $this->SetFetchMode(FALSE);
  235. }
  236. $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table));
  237. $rs = $this->Execute($SQL);
  238. if (!is_object($rs)) {
  239. if (isset($savem))
  240. $this->SetFetchMode($savem);
  241. $ADODB_FETCH_MODE = $save;
  242. return $false;
  243. }
  244. $indexes = array ();
  245. while ($row = $rs->FetchRow()) {
  246. if ($primary && preg_match("/primary/i",$row[1]) == 0) continue;
  247. if (!isset($indexes[$row[0]])) {
  248. $indexes[$row[0]] = array(
  249. 'unique' => preg_match("/unique/i",$row[1]),
  250. 'columns' => array());
  251. }
  252. /**
  253. * There must be a more elegant way of doing this,
  254. * the index elements appear in the SQL statement
  255. * in cols[1] between parentheses
  256. * e.g CREATE UNIQUE INDEX ware_0 ON warehouse (org,warehouse)
  257. */
  258. $cols = explode("(",$row[1]);
  259. $cols = explode(")",$cols[1]);
  260. array_pop($cols);
  261. $indexes[$row[0]]['columns'] = $cols;
  262. }
  263. if (isset($savem)) {
  264. $this->SetFetchMode($savem);
  265. $ADODB_FETCH_MODE = $save;
  266. }
  267. return $indexes;
  268. }
  269. }
  270. /*--------------------------------------------------------------------------------------
  271. Class Name: Recordset
  272. --------------------------------------------------------------------------------------*/
  273. class ADORecordset_sqlite extends ADORecordSet {
  274. var $databaseType = "sqlite";
  275. var $bind = false;
  276. function ADORecordset_sqlite($queryID,$mode=false)
  277. {
  278. if ($mode === false) {
  279. global $ADODB_FETCH_MODE;
  280. $mode = $ADODB_FETCH_MODE;
  281. }
  282. switch($mode) {
  283. case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
  284. case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
  285. default: $this->fetchMode = SQLITE_BOTH; break;
  286. }
  287. $this->adodbFetchMode = $mode;
  288. $this->_queryID = $queryID;
  289. $this->_inited = true;
  290. $this->fields = array();
  291. if ($queryID) {
  292. $this->_currentRow = 0;
  293. $this->EOF = !$this->_fetch();
  294. @$this->_initrs();
  295. } else {
  296. $this->_numOfRows = 0;
  297. $this->_numOfFields = 0;
  298. $this->EOF = true;
  299. }
  300. return $this->_queryID;
  301. }
  302. function FetchField($fieldOffset = -1)
  303. {
  304. $fld = new ADOFieldObject;
  305. $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
  306. $fld->type = 'VARCHAR';
  307. $fld->max_length = -1;
  308. return $fld;
  309. }
  310. function _initrs()
  311. {
  312. $this->_numOfRows = @sqlite_num_rows($this->_queryID);
  313. $this->_numOfFields = @sqlite_num_fields($this->_queryID);
  314. }
  315. function Fields($colname)
  316. {
  317. if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
  318. if (!$this->bind) {
  319. $this->bind = array();
  320. for ($i=0; $i < $this->_numOfFields; $i++) {
  321. $o = $this->FetchField($i);
  322. $this->bind[strtoupper($o->name)] = $i;
  323. }
  324. }
  325. return $this->fields[$this->bind[strtoupper($colname)]];
  326. }
  327. function _seek($row)
  328. {
  329. return sqlite_seek($this->_queryID, $row);
  330. }
  331. function _fetch($ignore_fields=false)
  332. {
  333. $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
  334. return !empty($this->fields);
  335. }
  336. function _close()
  337. {
  338. }
  339. }
  340. ?>