PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/php/extlib/adodb5/drivers/adodb-pdo.inc.php

http://github.com/movabletype/movabletype
PHP | 820 lines | 673 code | 74 blank | 73 comment | 64 complexity | 7d302f1d5c2d7af9daffd6ad56052dfb MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. @version v5.20.14 06-Jan-2019
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4 for best viewing.
  10. Latest version is available at http://adodb.org/
  11. Requires ODBC. Works on Windows and Unix.
  12. Problems:
  13. Where is float/decimal type in pdo_param_type
  14. LOB handling for CLOB/BLOB differs significantly
  15. */
  16. // security - hide paths
  17. if (!defined('ADODB_DIR')) die();
  18. /*
  19. enum pdo_param_type {
  20. PDO::PARAM_NULL, 0
  21. /* int as in long (the php native int type).
  22. * If you mark a column as an int, PDO expects get_col to return
  23. * a pointer to a long
  24. PDO::PARAM_INT, 1
  25. /* get_col ptr should point to start of the string buffer
  26. PDO::PARAM_STR, 2
  27. /* get_col: when len is 0 ptr should point to a php_stream *,
  28. * otherwise it should behave like a string. Indicate a NULL field
  29. * value by setting the ptr to NULL
  30. PDO::PARAM_LOB, 3
  31. /* get_col: will expect the ptr to point to a new PDOStatement object handle,
  32. * but this isn't wired up yet
  33. PDO::PARAM_STMT, 4 /* hierarchical result set
  34. /* get_col ptr should point to a zend_bool
  35. PDO::PARAM_BOOL, 5
  36. /* magic flag to denote a parameter as being input/output
  37. PDO::PARAM_INPUT_OUTPUT = 0x80000000
  38. };
  39. */
  40. function adodb_pdo_type($t)
  41. {
  42. switch($t) {
  43. case 2: return 'VARCHAR';
  44. case 3: return 'BLOB';
  45. default: return 'NUMERIC';
  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 $_genIDSQL;
  58. var $_genSeqSQL = "create table %s (id integer)";
  59. var $_dropSeqSQL;
  60. var $_autocommit = true;
  61. var $_haserrorfunctions = true;
  62. var $_lastAffectedRows = 0;
  63. var $_errormsg = false;
  64. var $_errorno = false;
  65. var $dsnType = '';
  66. var $stmt = false;
  67. var $_driver;
  68. function __construct()
  69. {
  70. }
  71. function _UpdatePDO()
  72. {
  73. $d = $this->_driver;
  74. $this->fmtDate = $d->fmtDate;
  75. $this->fmtTimeStamp = $d->fmtTimeStamp;
  76. $this->replaceQuote = $d->replaceQuote;
  77. $this->sysDate = $d->sysDate;
  78. $this->sysTimeStamp = $d->sysTimeStamp;
  79. $this->random = $d->random;
  80. $this->concat_operator = $d->concat_operator;
  81. $this->nameQuote = $d->nameQuote;
  82. $this->hasGenID = $d->hasGenID;
  83. $this->_genIDSQL = $d->_genIDSQL;
  84. $this->_genSeqSQL = $d->_genSeqSQL;
  85. $this->_dropSeqSQL = $d->_dropSeqSQL;
  86. $d->_init($this);
  87. }
  88. function Time()
  89. {
  90. if (!empty($this->_driver->_hasdual)) {
  91. $sql = "select $this->sysTimeStamp from dual";
  92. }
  93. else {
  94. $sql = "select $this->sysTimeStamp";
  95. }
  96. $rs = $this->_Execute($sql);
  97. if ($rs && !$rs->EOF) {
  98. return $this->UnixTimeStamp(reset($rs->fields));
  99. }
  100. return false;
  101. }
  102. // returns true or false
  103. function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
  104. {
  105. $at = strpos($argDSN,':');
  106. $this->dsnType = substr($argDSN,0,$at);
  107. if ($argDatabasename) {
  108. switch($this->dsnType){
  109. case 'sqlsrv':
  110. $argDSN .= ';database='.$argDatabasename;
  111. break;
  112. case 'mssql':
  113. case 'mysql':
  114. case 'oci':
  115. case 'pgsql':
  116. case 'sqlite':
  117. default:
  118. $argDSN .= ';dbname='.$argDatabasename;
  119. }
  120. }
  121. if ( $this->port ) {
  122. $argDSN .= ';port=' . $this->port;
  123. }
  124. try {
  125. $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
  126. } catch (Exception $e) {
  127. $this->_connectionID = false;
  128. $this->_errorno = -1;
  129. //var_dump($e);
  130. $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
  131. return false;
  132. }
  133. if ($this->_connectionID) {
  134. switch(ADODB_ASSOC_CASE){
  135. case ADODB_ASSOC_CASE_LOWER:
  136. $m = PDO::CASE_LOWER;
  137. break;
  138. case ADODB_ASSOC_CASE_UPPER:
  139. $m = PDO::CASE_UPPER;
  140. break;
  141. default:
  142. case ADODB_ASSOC_CASE_NATIVE:
  143. $m = PDO::CASE_NATURAL;
  144. break;
  145. }
  146. //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
  147. $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
  148. $class = 'ADODB_pdo_'.$this->dsnType;
  149. //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
  150. switch($this->dsnType) {
  151. case 'mssql':
  152. case 'mysql':
  153. case 'oci':
  154. case 'pgsql':
  155. case 'sqlite':
  156. case 'sqlsrv':
  157. include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
  158. break;
  159. }
  160. if (class_exists($class)) {
  161. $this->_driver = new $class();
  162. }
  163. else {
  164. $this->_driver = new ADODB_pdo_base();
  165. }
  166. $this->_driver->_connectionID = $this->_connectionID;
  167. $this->_UpdatePDO();
  168. $this->_driver->database = $this->database;
  169. return true;
  170. }
  171. $this->_driver = new ADODB_pdo_base();
  172. return false;
  173. }
  174. function Concat()
  175. {
  176. $args = func_get_args();
  177. if(method_exists($this->_driver, 'Concat')) {
  178. return call_user_func_array(array($this->_driver, 'Concat'), $args);
  179. }
  180. if (PHP_VERSION >= 5.3) {
  181. return call_user_func_array('parent::Concat', $args);
  182. }
  183. return call_user_func_array(array($this,'parent::Concat'), $args);
  184. }
  185. // returns true or false
  186. function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
  187. {
  188. return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
  189. }
  190. /*------------------------------------------------------------------------------*/
  191. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  192. {
  193. $save = $this->_driver->fetchMode;
  194. $this->_driver->fetchMode = $this->fetchMode;
  195. $this->_driver->debug = $this->debug;
  196. $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
  197. $this->_driver->fetchMode = $save;
  198. return $ret;
  199. }
  200. function ServerInfo()
  201. {
  202. return $this->_driver->ServerInfo();
  203. }
  204. function MetaTables($ttype=false,$showSchema=false,$mask=false)
  205. {
  206. return $this->_driver->MetaTables($ttype,$showSchema,$mask);
  207. }
  208. function MetaColumns($table,$normalize=true)
  209. {
  210. return $this->_driver->MetaColumns($table,$normalize);
  211. }
  212. function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
  213. {
  214. $obj = $stmt[1];
  215. if ($type) {
  216. $obj->bindParam($name, $var, $type, $maxLen);
  217. }
  218. else {
  219. $obj->bindParam($name, $var);
  220. }
  221. }
  222. function OffsetDate($dayFraction,$date=false)
  223. {
  224. return $this->_driver->OffsetDate($dayFraction,$date);
  225. }
  226. function SelectDB($dbName)
  227. {
  228. return $this->_driver->SelectDB($dbName);
  229. }
  230. function SQLDate($fmt, $col=false)
  231. {
  232. return $this->_driver->SQLDate($fmt, $col);
  233. }
  234. function ErrorMsg()
  235. {
  236. if ($this->_errormsg !== false) {
  237. return $this->_errormsg;
  238. }
  239. if (!empty($this->_stmt)) {
  240. $arr = $this->_stmt->errorInfo();
  241. }
  242. else if (!empty($this->_connectionID)) {
  243. $arr = $this->_connectionID->errorInfo();
  244. }
  245. else {
  246. return 'No Connection Established';
  247. }
  248. if ($arr) {
  249. if (sizeof($arr)<2) {
  250. return '';
  251. }
  252. if ((integer)$arr[0]) {
  253. return $arr[2];
  254. }
  255. else {
  256. return '';
  257. }
  258. }
  259. else {
  260. return '-1';
  261. }
  262. }
  263. function ErrorNo()
  264. {
  265. if ($this->_errorno !== false) {
  266. return $this->_errorno;
  267. }
  268. if (!empty($this->_stmt)) {
  269. $err = $this->_stmt->errorCode();
  270. }
  271. else if (!empty($this->_connectionID)) {
  272. $arr = $this->_connectionID->errorInfo();
  273. if (isset($arr[0])) {
  274. $err = $arr[0];
  275. }
  276. else {
  277. $err = -1;
  278. }
  279. } else {
  280. return 0;
  281. }
  282. if ($err == '00000') {
  283. return 0; // allows empty check
  284. }
  285. return $err;
  286. }
  287. /**
  288. * @param bool $auto_commit
  289. * @return void
  290. */
  291. function SetAutoCommit($auto_commit)
  292. {
  293. if(method_exists($this->_driver, 'SetAutoCommit')) {
  294. $this->_driver->SetAutoCommit($auto_commit);
  295. }
  296. }
  297. function SetTransactionMode($transaction_mode)
  298. {
  299. if(method_exists($this->_driver, 'SetTransactionMode')) {
  300. return $this->_driver->SetTransactionMode($transaction_mode);
  301. }
  302. return parent::SetTransactionMode($seqname);
  303. }
  304. function BeginTrans()
  305. {
  306. if(method_exists($this->_driver, 'BeginTrans')) {
  307. return $this->_driver->BeginTrans();
  308. }
  309. if (!$this->hasTransactions) {
  310. return false;
  311. }
  312. if ($this->transOff) {
  313. return true;
  314. }
  315. $this->transCnt += 1;
  316. $this->_autocommit = false;
  317. $this->SetAutoCommit(false);
  318. return $this->_connectionID->beginTransaction();
  319. }
  320. function CommitTrans($ok=true)
  321. {
  322. if(method_exists($this->_driver, 'CommitTrans')) {
  323. return $this->_driver->CommitTrans($ok);
  324. }
  325. if (!$this->hasTransactions) {
  326. return false;
  327. }
  328. if ($this->transOff) {
  329. return true;
  330. }
  331. if (!$ok) {
  332. return $this->RollbackTrans();
  333. }
  334. if ($this->transCnt) {
  335. $this->transCnt -= 1;
  336. }
  337. $this->_autocommit = true;
  338. $ret = $this->_connectionID->commit();
  339. $this->SetAutoCommit(true);
  340. return $ret;
  341. }
  342. function RollbackTrans()
  343. {
  344. if(method_exists($this->_driver, 'RollbackTrans')) {
  345. return $this->_driver->RollbackTrans();
  346. }
  347. if (!$this->hasTransactions) {
  348. return false;
  349. }
  350. if ($this->transOff) {
  351. return true;
  352. }
  353. if ($this->transCnt) {
  354. $this->transCnt -= 1;
  355. }
  356. $this->_autocommit = true;
  357. $ret = $this->_connectionID->rollback();
  358. $this->SetAutoCommit(true);
  359. return $ret;
  360. }
  361. function Prepare($sql)
  362. {
  363. $this->_stmt = $this->_connectionID->prepare($sql);
  364. if ($this->_stmt) {
  365. return array($sql,$this->_stmt);
  366. }
  367. return false;
  368. }
  369. function PrepareStmt($sql)
  370. {
  371. $stmt = $this->_connectionID->prepare($sql);
  372. if (!$stmt) {
  373. return false;
  374. }
  375. $obj = new ADOPDOStatement($stmt,$this);
  376. return $obj;
  377. }
  378. function CreateSequence($seqname='adodbseq',$startID=1)
  379. {
  380. if(method_exists($this->_driver, 'CreateSequence')) {
  381. return $this->_driver->CreateSequence($seqname, $startID);
  382. }
  383. return parent::CreateSequence($seqname, $startID);
  384. }
  385. function DropSequence($seqname='adodbseq')
  386. {
  387. if(method_exists($this->_driver, 'DropSequence')) {
  388. return $this->_driver->DropSequence($seqname);
  389. }
  390. return parent::DropSequence($seqname);
  391. }
  392. function GenID($seqname='adodbseq',$startID=1)
  393. {
  394. if(method_exists($this->_driver, 'GenID')) {
  395. return $this->_driver->GenID($seqname, $startID);
  396. }
  397. return parent::GenID($seqname, $startID);
  398. }
  399. /* returns queryID or false */
  400. function _query($sql,$inputarr=false)
  401. {
  402. $ok = false;
  403. if (is_array($sql)) {
  404. $stmt = $sql[1];
  405. } else {
  406. $stmt = $this->_connectionID->prepare($sql);
  407. }
  408. #adodb_backtrace();
  409. #var_dump($this->_bindInputArray);
  410. if ($stmt) {
  411. if (empty($this->_driver))
  412. $this->_driver = new stdClass;
  413. $this->_driver->debug = $this->debug;
  414. if ($inputarr) {
  415. $ok = $stmt->execute($inputarr);
  416. }
  417. else {
  418. $ok = $stmt->execute();
  419. }
  420. }
  421. $this->_errormsg = false;
  422. $this->_errorno = false;
  423. if ($ok) {
  424. $this->_stmt = $stmt;
  425. return $stmt;
  426. }
  427. if ($stmt) {
  428. $arr = $stmt->errorinfo();
  429. if ((integer)$arr[1]) {
  430. $this->_errormsg = $arr[2];
  431. $this->_errorno = $arr[1];
  432. }
  433. } else {
  434. $this->_errormsg = false;
  435. $this->_errorno = false;
  436. }
  437. return false;
  438. }
  439. // returns true or false
  440. function _close()
  441. {
  442. $this->_stmt = false;
  443. return true;
  444. }
  445. function _affectedrows()
  446. {
  447. return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
  448. }
  449. function _insertid()
  450. {
  451. return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
  452. }
  453. /**
  454. * Quotes a string to be sent to the database.
  455. * If we have an active connection, delegates quoting to the underlying
  456. * PDO object. Otherwise, replace "'" by the value of $replaceQuote (same
  457. * behavior as mysqli driver)
  458. * @param string $s The string to quote
  459. * @param boolean $magic_quotes If false, use PDO::quote().
  460. * @return string Quoted string
  461. */
  462. function qstr($s, $magic_quotes = false)
  463. {
  464. if (!$magic_quotes) {
  465. if ($this->_connectionID) {
  466. return $this->_connectionID->quote($s);
  467. }
  468. return "'" . str_replace("'", $this->replaceQuote, $s) . "'";
  469. }
  470. // undo magic quotes for "
  471. $s = str_replace('\\"', '"', $s);
  472. return "'$s'";
  473. }
  474. }
  475. class ADODB_pdo_base extends ADODB_pdo {
  476. var $sysDate = "'?'";
  477. var $sysTimeStamp = "'?'";
  478. function _init($parentDriver)
  479. {
  480. $parentDriver->_bindInputArray = true;
  481. #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
  482. }
  483. function ServerInfo()
  484. {
  485. return ADOConnection::ServerInfo();
  486. }
  487. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  488. {
  489. $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
  490. return $ret;
  491. }
  492. function MetaTables($ttype=false,$showSchema=false,$mask=false)
  493. {
  494. return false;
  495. }
  496. function MetaColumns($table,$normalize=true)
  497. {
  498. return false;
  499. }
  500. }
  501. class ADOPDOStatement {
  502. var $databaseType = "pdo";
  503. var $dataProvider = "pdo";
  504. var $_stmt;
  505. var $_connectionID;
  506. function __construct($stmt,$connection)
  507. {
  508. $this->_stmt = $stmt;
  509. $this->_connectionID = $connection;
  510. }
  511. function Execute($inputArr=false)
  512. {
  513. $savestmt = $this->_connectionID->_stmt;
  514. $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
  515. $this->_connectionID->_stmt = $savestmt;
  516. return $rs;
  517. }
  518. function InParameter(&$var,$name,$maxLen=4000,$type=false)
  519. {
  520. if ($type) {
  521. $this->_stmt->bindParam($name,$var,$type,$maxLen);
  522. }
  523. else {
  524. $this->_stmt->bindParam($name, $var);
  525. }
  526. }
  527. function Affected_Rows()
  528. {
  529. return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
  530. }
  531. function ErrorMsg()
  532. {
  533. if ($this->_stmt) {
  534. $arr = $this->_stmt->errorInfo();
  535. }
  536. else {
  537. $arr = $this->_connectionID->errorInfo();
  538. }
  539. if (is_array($arr)) {
  540. if ((integer) $arr[0] && isset($arr[2])) {
  541. return $arr[2];
  542. }
  543. else {
  544. return '';
  545. }
  546. } else {
  547. return '-1';
  548. }
  549. }
  550. function NumCols()
  551. {
  552. return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
  553. }
  554. function ErrorNo()
  555. {
  556. if ($this->_stmt) {
  557. return $this->_stmt->errorCode();
  558. }
  559. else {
  560. return $this->_connectionID->errorInfo();
  561. }
  562. }
  563. }
  564. /*--------------------------------------------------------------------------------------
  565. Class Name: Recordset
  566. --------------------------------------------------------------------------------------*/
  567. class ADORecordSet_pdo extends ADORecordSet {
  568. var $bind = false;
  569. var $databaseType = "pdo";
  570. var $dataProvider = "pdo";
  571. function __construct($id,$mode=false)
  572. {
  573. if ($mode === false) {
  574. global $ADODB_FETCH_MODE;
  575. $mode = $ADODB_FETCH_MODE;
  576. }
  577. $this->adodbFetchMode = $mode;
  578. switch($mode) {
  579. case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
  580. case ADODB_FETCH_ASSOC: $mode = PDO::FETCH_ASSOC; break;
  581. case ADODB_FETCH_BOTH:
  582. default: $mode = PDO::FETCH_BOTH; break;
  583. }
  584. $this->fetchMode = $mode;
  585. $this->_queryID = $id;
  586. parent::__construct($id);
  587. }
  588. function Init()
  589. {
  590. if ($this->_inited) {
  591. return;
  592. }
  593. $this->_inited = true;
  594. if ($this->_queryID) {
  595. @$this->_initrs();
  596. }
  597. else {
  598. $this->_numOfRows = 0;
  599. $this->_numOfFields = 0;
  600. }
  601. if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
  602. $this->_currentRow = 0;
  603. if ($this->EOF = ($this->_fetch() === false)) {
  604. $this->_numOfRows = 0; // _numOfRows could be -1
  605. }
  606. } else {
  607. $this->EOF = true;
  608. }
  609. }
  610. function _initrs()
  611. {
  612. global $ADODB_COUNTRECS;
  613. $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
  614. if (!$this->_numOfRows) {
  615. $this->_numOfRows = -1;
  616. }
  617. $this->_numOfFields = $this->_queryID->columnCount();
  618. }
  619. // returns the field object
  620. function FetchField($fieldOffset = -1)
  621. {
  622. $off=$fieldOffset+1; // offsets begin at 1
  623. $o= new ADOFieldObject();
  624. $arr = @$this->_queryID->getColumnMeta($fieldOffset);
  625. if (!$arr) {
  626. $o->name = 'bad getColumnMeta()';
  627. $o->max_length = -1;
  628. $o->type = 'VARCHAR';
  629. $o->precision = 0;
  630. # $false = false;
  631. return $o;
  632. }
  633. //adodb_pr($arr);
  634. $o->name = $arr['name'];
  635. if (isset($arr['sqlsrv:decl_type']) && $arr['sqlsrv:decl_type'] <> "null")
  636. {
  637. /*
  638. * If the database is SQL server, use the native built-ins
  639. */
  640. $o->type = $arr['sqlsrv:decl_type'];
  641. }
  642. elseif (isset($arr['native_type']) && $arr['native_type'] <> "null")
  643. {
  644. $o->type = $arr['native_type'];
  645. }
  646. else
  647. {
  648. $o->type = adodb_pdo_type($arr['pdo_type']);
  649. }
  650. $o->max_length = $arr['len'];
  651. $o->precision = $arr['precision'];
  652. switch(ADODB_ASSOC_CASE) {
  653. case ADODB_ASSOC_CASE_LOWER:
  654. $o->name = strtolower($o->name);
  655. break;
  656. case ADODB_ASSOC_CASE_UPPER:
  657. $o->name = strtoupper($o->name);
  658. break;
  659. }
  660. return $o;
  661. }
  662. function _seek($row)
  663. {
  664. return false;
  665. }
  666. function _fetch()
  667. {
  668. if (!$this->_queryID) {
  669. return false;
  670. }
  671. $this->fields = $this->_queryID->fetch($this->fetchMode);
  672. return !empty($this->fields);
  673. }
  674. function _close()
  675. {
  676. $this->_queryID = false;
  677. }
  678. function Fields($colname)
  679. {
  680. if ($this->adodbFetchMode != ADODB_FETCH_NUM) {
  681. return @$this->fields[$colname];
  682. }
  683. if (!$this->bind) {
  684. $this->bind = array();
  685. for ($i=0; $i < $this->_numOfFields; $i++) {
  686. $o = $this->FetchField($i);
  687. $this->bind[strtoupper($o->name)] = $i;
  688. }
  689. }
  690. return $this->fields[$this->bind[strtoupper($colname)]];
  691. }
  692. }