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

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

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