PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 626 lines | 447 code | 114 blank | 65 comment | 92 complexity | f5ad4f4da2bf6e12f316141b1bd40675 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. Set tabs to 4 for best viewing.
  8. Latest version is available at http://adodb.sourceforge.net
  9. Requires ODBC. Works on Windows and Unix.
  10. Problems:
  11. Where is float/decimal type in pdo_param_type
  12. LOB handling for CLOB/BLOB differs significantly
  13. */
  14. // security - hide paths
  15. if (!defined('ADODB_DIR')) die();
  16. /*
  17. enum pdo_param_type {
  18. PDO::PARAM_NULL, 0
  19. /* int as in long (the php native int type).
  20. * If you mark a column as an int, PDO expects get_col to return
  21. * a pointer to a long
  22. PDO::PARAM_INT, 1
  23. /* get_col ptr should point to start of the string buffer
  24. PDO::PARAM_STR, 2
  25. /* get_col: when len is 0 ptr should point to a php_stream *,
  26. * otherwise it should behave like a string. Indicate a NULL field
  27. * value by setting the ptr to NULL
  28. PDO::PARAM_LOB, 3
  29. /* get_col: will expect the ptr to point to a new PDOStatement object handle,
  30. * but this isn't wired up yet
  31. PDO::PARAM_STMT, 4 /* hierarchical result set
  32. /* get_col ptr should point to a zend_bool
  33. PDO::PARAM_BOOL, 5
  34. /* magic flag to denote a parameter as being input/output
  35. PDO::PARAM_INPUT_OUTPUT = 0x80000000
  36. };
  37. */
  38. function adodb_pdo_type($t)
  39. {
  40. switch($t) {
  41. case 2: return 'VARCHAR';
  42. case 3: return 'BLOB';
  43. default: return 'NUMERIC';
  44. }
  45. }
  46. /*--------------------------------------------------------------------------------------
  47. --------------------------------------------------------------------------------------*/
  48. ////////////////////////////////////////////////
  49. class ADODB_pdo extends ADOConnection {
  50. var $databaseType = "pdo";
  51. var $dataProvider = "pdo";
  52. var $fmtDate = "'Y-m-d'";
  53. var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  54. var $replaceQuote = "''"; // string to use to replace quotes
  55. var $hasAffectedRows = true;
  56. var $_bindInputArray = true;
  57. var $_genSeqSQL = "create table %s (id integer)";
  58. var $_autocommit = true;
  59. var $_haserrorfunctions = true;
  60. var $_lastAffectedRows = 0;
  61. var $_errormsg = false;
  62. var $_errorno = false;
  63. var $dsnType = '';
  64. var $stmt = false;
  65. function ADODB_pdo()
  66. {
  67. }
  68. function _UpdatePDO()
  69. {
  70. $d = $this->_driver;
  71. $this->fmtDate = $d->fmtDate;
  72. $this->fmtTimeStamp = $d->fmtTimeStamp;
  73. $this->replaceQuote = $d->replaceQuote;
  74. $this->sysDate = $d->sysDate;
  75. $this->sysTimeStamp = $d->sysTimeStamp;
  76. $this->random = $d->random;
  77. $this->concat_operator = $d->concat_operator;
  78. $this->nameQuote = $d->nameQuote;
  79. $this->hasGenID = $d->hasGenID;
  80. $this->_genIDSQL = $d->_genIDSQL;
  81. $this->_genSeqSQL = $d->_genSeqSQL;
  82. $this->_dropSeqSQL = $d->_dropSeqSQL;
  83. $d->_init($this);
  84. }
  85. function Time()
  86. {
  87. if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
  88. else $sql = "select $this->sysTimeStamp";
  89. $rs = $this->_Execute($sql);
  90. if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
  91. return false;
  92. }
  93. // returns true or false
  94. function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
  95. {
  96. $at = strpos($argDSN,':');
  97. $this->dsnType = substr($argDSN,0,$at);
  98. if ($argDatabasename) {
  99. $argDSN .= ';dbname='.$argDatabasename;
  100. }
  101. try {
  102. $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
  103. } catch (Exception $e) {
  104. $this->_connectionID = false;
  105. $this->_errorno = -1;
  106. //var_dump($e);
  107. $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
  108. return false;
  109. }
  110. if ($this->_connectionID) {
  111. switch(ADODB_ASSOC_CASE){
  112. case 0: $m = PDO::CASE_LOWER; break;
  113. case 1: $m = PDO::CASE_UPPER; break;
  114. default:
  115. case 2: $m = PDO::CASE_NATURAL; break;
  116. }
  117. //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
  118. $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
  119. $class = 'ADODB_pdo_'.$this->dsnType;
  120. //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
  121. switch($this->dsnType) {
  122. case 'oci':
  123. case 'mysql':
  124. case 'pgsql':
  125. case 'mssql':
  126. case 'sqlite':
  127. include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
  128. break;
  129. }
  130. if (class_exists($class))
  131. $this->_driver = new $class();
  132. else
  133. $this->_driver = new ADODB_pdo_base();
  134. $this->_driver->_connectionID = $this->_connectionID;
  135. $this->_UpdatePDO();
  136. return true;
  137. }
  138. $this->_driver = new ADODB_pdo_base();
  139. return false;
  140. }
  141. function Concat()
  142. {
  143. $args = func_get_args();
  144. if(method_exists($this->_driver, 'Concat'))
  145. return call_user_func_array(array($this->_driver, 'Concat'), $args);
  146. return call_user_func_array(array($this,'parent::Concat'), $args);
  147. }
  148. // returns true or false
  149. function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
  150. {
  151. return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
  152. }
  153. /*------------------------------------------------------------------------------*/
  154. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  155. {
  156. $save = $this->_driver->fetchMode;
  157. $this->_driver->fetchMode = $this->fetchMode;
  158. $this->_driver->debug = $this->debug;
  159. $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
  160. $this->_driver->fetchMode = $save;
  161. return $ret;
  162. }
  163. function ServerInfo()
  164. {
  165. return $this->_driver->ServerInfo();
  166. }
  167. function MetaTables($ttype=false,$showSchema=false,$mask=false)
  168. {
  169. return $this->_driver->MetaTables($ttype,$showSchema,$mask);
  170. }
  171. function MetaColumns($table,$normalize=true)
  172. {
  173. return $this->_driver->MetaColumns($table,$normalize);
  174. }
  175. function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
  176. {
  177. $obj = $stmt[1];
  178. if ($type) $obj->bindParam($name,$var,$type,$maxLen);
  179. else $obj->bindParam($name, $var);
  180. }
  181. function OffsetDate($dayFraction,$date=false)
  182. {
  183. return $this->_driver->OffsetDate($dayFraction,$date);
  184. }
  185. function ErrorMsg()
  186. {
  187. if ($this->_errormsg !== false) return $this->_errormsg;
  188. if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
  189. else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
  190. else return 'No Connection Established';
  191. if ($arr) {
  192. if (sizeof($arr)<2) return '';
  193. if ((integer)$arr[1]) return $arr[2];
  194. else return '';
  195. } else return '-1';
  196. }
  197. function ErrorNo()
  198. {
  199. if ($this->_errorno !== false) return $this->_errorno;
  200. if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
  201. else if (!empty($this->_connectionID)) {
  202. $arr = $this->_connectionID->errorInfo();
  203. if (isset($arr[0])) $err = $arr[0];
  204. else $err = -1;
  205. } else
  206. return 0;
  207. if ($err == '00000') return 0; // allows empty check
  208. return $err;
  209. }
  210. function SetTransactionMode($transaction_mode)
  211. {
  212. if(method_exists($this->_driver, 'SetTransactionMode'))
  213. return $this->_driver->SetTransactionMode($transaction_mode);
  214. return parent::SetTransactionMode($seqname);
  215. }
  216. function BeginTrans()
  217. {
  218. if(method_exists($this->_driver, 'BeginTrans'))
  219. return $this->_driver->BeginTrans();
  220. if (!$this->hasTransactions) return false;
  221. if ($this->transOff) return true;
  222. $this->transCnt += 1;
  223. $this->_autocommit = false;
  224. $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
  225. return $this->_connectionID->beginTransaction();
  226. }
  227. function CommitTrans($ok=true)
  228. {
  229. if(method_exists($this->_driver, 'CommitTrans'))
  230. return $this->_driver->CommitTrans($ok);
  231. if (!$this->hasTransactions) return false;
  232. if ($this->transOff) return true;
  233. if (!$ok) return $this->RollbackTrans();
  234. if ($this->transCnt) $this->transCnt -= 1;
  235. $this->_autocommit = true;
  236. $ret = $this->_connectionID->commit();
  237. $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
  238. return $ret;
  239. }
  240. function RollbackTrans()
  241. {
  242. if(method_exists($this->_driver, 'RollbackTrans'))
  243. return $this->_driver->RollbackTrans();
  244. if (!$this->hasTransactions) return false;
  245. if ($this->transOff) return true;
  246. if ($this->transCnt) $this->transCnt -= 1;
  247. $this->_autocommit = true;
  248. $ret = $this->_connectionID->rollback();
  249. $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
  250. return $ret;
  251. }
  252. function Prepare($sql)
  253. {
  254. $this->_stmt = $this->_connectionID->prepare($sql);
  255. if ($this->_stmt) return array($sql,$this->_stmt);
  256. return false;
  257. }
  258. function PrepareStmt($sql)
  259. {
  260. $stmt = $this->_connectionID->prepare($sql);
  261. if (!$stmt) return false;
  262. $obj = new ADOPDOStatement($stmt,$this);
  263. return $obj;
  264. }
  265. function CreateSequence($seqname='adodbseq',$startID=1)
  266. {
  267. if(method_exists($this->_driver, 'CreateSequence'))
  268. return $this->_driver->CreateSequence($seqname, $startID);
  269. return parent::CreateSequence($seqname, $startID);
  270. }
  271. function DropSequence($seqname='adodbseq')
  272. {
  273. if(method_exists($this->_driver, 'DropSequence'))
  274. return $this->_driver->DropSequence($seqname);
  275. return parent::DropSequence($seqname);
  276. }
  277. function GenID($seqname='adodbseq',$startID=1)
  278. {
  279. if(method_exists($this->_driver, 'GenID'))
  280. return $this->_driver->GenID($seqname, $startID);
  281. return parent::GenID($seqname, $startID);
  282. }
  283. /* returns queryID or false */
  284. function _query($sql,$inputarr=false)
  285. {
  286. if (is_array($sql)) {
  287. $stmt = $sql[1];
  288. } else {
  289. $stmt = $this->_connectionID->prepare($sql);
  290. }
  291. #adodb_backtrace();
  292. #var_dump($this->_bindInputArray);
  293. if ($stmt) {
  294. $this->_driver->debug = $this->debug;
  295. if ($inputarr) $ok = $stmt->execute($inputarr);
  296. else $ok = $stmt->execute();
  297. }
  298. $this->_errormsg = false;
  299. $this->_errorno = false;
  300. if ($ok) {
  301. $this->_stmt = $stmt;
  302. return $stmt;
  303. }
  304. if ($stmt) {
  305. $arr = $stmt->errorinfo();
  306. if ((integer)$arr[1]) {
  307. $this->_errormsg = $arr[2];
  308. $this->_errorno = $arr[1];
  309. }
  310. } else {
  311. $this->_errormsg = false;
  312. $this->_errorno = false;
  313. }
  314. return false;
  315. }
  316. // returns true or false
  317. function _close()
  318. {
  319. $this->_stmt = false;
  320. return true;
  321. }
  322. function _affectedrows()
  323. {
  324. return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
  325. }
  326. function _insertid()
  327. {
  328. return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
  329. }
  330. }
  331. class ADODB_pdo_base extends ADODB_pdo {
  332. var $sysDate = "'?'";
  333. var $sysTimeStamp = "'?'";
  334. function _init($parentDriver)
  335. {
  336. $parentDriver->_bindInputArray = true;
  337. #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
  338. }
  339. function ServerInfo()
  340. {
  341. return ADOConnection::ServerInfo();
  342. }
  343. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  344. {
  345. $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
  346. return $ret;
  347. }
  348. function MetaTables()
  349. {
  350. return false;
  351. }
  352. function MetaColumns()
  353. {
  354. return false;
  355. }
  356. }
  357. class ADOPDOStatement {
  358. var $databaseType = "pdo";
  359. var $dataProvider = "pdo";
  360. var $_stmt;
  361. var $_connectionID;
  362. function ADOPDOStatement($stmt,$connection)
  363. {
  364. $this->_stmt = $stmt;
  365. $this->_connectionID = $connection;
  366. }
  367. function Execute($inputArr=false)
  368. {
  369. $savestmt = $this->_connectionID->_stmt;
  370. $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
  371. $this->_connectionID->_stmt = $savestmt;
  372. return $rs;
  373. }
  374. function InParameter(&$var,$name,$maxLen=4000,$type=false)
  375. {
  376. if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
  377. else $this->_stmt->bindParam($name, $var);
  378. }
  379. function Affected_Rows()
  380. {
  381. return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
  382. }
  383. function ErrorMsg()
  384. {
  385. if ($this->_stmt) $arr = $this->_stmt->errorInfo();
  386. else $arr = $this->_connectionID->errorInfo();
  387. if (is_array($arr)) {
  388. if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
  389. else return '';
  390. } else return '-1';
  391. }
  392. function NumCols()
  393. {
  394. return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
  395. }
  396. function ErrorNo()
  397. {
  398. if ($this->_stmt) return $this->_stmt->errorCode();
  399. else return $this->_connectionID->errorInfo();
  400. }
  401. }
  402. /*--------------------------------------------------------------------------------------
  403. Class Name: Recordset
  404. --------------------------------------------------------------------------------------*/
  405. class ADORecordSet_pdo extends ADORecordSet {
  406. var $bind = false;
  407. var $databaseType = "pdo";
  408. var $dataProvider = "pdo";
  409. function ADORecordSet_pdo($id,$mode=false)
  410. {
  411. if ($mode === false) {
  412. global $ADODB_FETCH_MODE;
  413. $mode = $ADODB_FETCH_MODE;
  414. }
  415. $this->adodbFetchMode = $mode;
  416. switch($mode) {
  417. case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
  418. case ADODB_FETCH_ASSOC: $mode = PDO::FETCH_ASSOC; break;
  419. case ADODB_FETCH_BOTH:
  420. default: $mode = PDO::FETCH_BOTH; break;
  421. }
  422. $this->fetchMode = $mode;
  423. $this->_queryID = $id;
  424. $this->ADORecordSet($id);
  425. }
  426. function Init()
  427. {
  428. if ($this->_inited) return;
  429. $this->_inited = true;
  430. if ($this->_queryID) @$this->_initrs();
  431. else {
  432. $this->_numOfRows = 0;
  433. $this->_numOfFields = 0;
  434. }
  435. if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
  436. $this->_currentRow = 0;
  437. if ($this->EOF = ($this->_fetch() === false)) {
  438. $this->_numOfRows = 0; // _numOfRows could be -1
  439. }
  440. } else {
  441. $this->EOF = true;
  442. }
  443. }
  444. function _initrs()
  445. {
  446. global $ADODB_COUNTRECS;
  447. $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
  448. if (!$this->_numOfRows) $this->_numOfRows = -1;
  449. $this->_numOfFields = $this->_queryID->columnCount();
  450. }
  451. // returns the field object
  452. function FetchField($fieldOffset = -1)
  453. {
  454. $off=$fieldOffset+1; // offsets begin at 1
  455. $o= new ADOFieldObject();
  456. $arr = @$this->_queryID->getColumnMeta($fieldOffset);
  457. if (!$arr) {
  458. $o->name = 'bad getColumnMeta()';
  459. $o->max_length = -1;
  460. $o->type = 'VARCHAR';
  461. $o->precision = 0;
  462. # $false = false;
  463. return $o;
  464. }
  465. //adodb_pr($arr);
  466. $o->name = $arr['name'];
  467. if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
  468. else $o->type = adodb_pdo_type($arr['pdo_type']);
  469. $o->max_length = $arr['len'];
  470. $o->precision = $arr['precision'];
  471. if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
  472. else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
  473. return $o;
  474. }
  475. function _seek($row)
  476. {
  477. return false;
  478. }
  479. function _fetch()
  480. {
  481. if (!$this->_queryID) return false;
  482. $this->fields = $this->_queryID->fetch($this->fetchMode);
  483. return !empty($this->fields);
  484. }
  485. function _close()
  486. {
  487. $this->_queryID = false;
  488. }
  489. function Fields($colname)
  490. {
  491. if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
  492. if (!$this->bind) {
  493. $this->bind = array();
  494. for ($i=0; $i < $this->_numOfFields; $i++) {
  495. $o = $this->FetchField($i);
  496. $this->bind[strtoupper($o->name)] = $i;
  497. }
  498. }
  499. return $this->fields[$this->bind[strtoupper($colname)]];
  500. }
  501. }
  502. ?>