1
0

adodb-odbc.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <?php
  2. /*
  3. @version v5.20.3 01-Jan-2016
  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.sourceforge.net
  11. Requires ODBC. Works on Windows and Unix.
  12. */
  13. // security - hide paths
  14. if (!defined('ADODB_DIR')) die();
  15. define("_ADODB_ODBC_LAYER", 2 );
  16. /*--------------------------------------------------------------------------------------
  17. --------------------------------------------------------------------------------------*/
  18. class ADODB_odbc extends ADOConnection {
  19. var $databaseType = "odbc";
  20. var $fmtDate = "'Y-m-d'";
  21. var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  22. var $replaceQuote = "''"; // string to use to replace quotes
  23. var $dataProvider = "odbc";
  24. var $hasAffectedRows = true;
  25. var $binmode = ODBC_BINMODE_RETURN;
  26. var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive
  27. // breaking backward-compat
  28. //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field
  29. var $_bindInputArray = false;
  30. var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L
  31. var $_genSeqSQL = "create table %s (id integer)";
  32. var $_autocommit = true;
  33. var $_haserrorfunctions = true;
  34. var $_has_stupid_odbc_fetch_api_change = true;
  35. var $_lastAffectedRows = 0;
  36. var $uCaseTables = true; // for meta* functions, uppercase table names
  37. function __construct()
  38. {
  39. $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050;
  40. $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
  41. }
  42. // returns true or false
  43. function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
  44. {
  45. global $php_errormsg;
  46. if (!function_exists('odbc_connect')) return null;
  47. if (!empty($argDatabasename) && stristr($argDSN, 'Database=') === false) {
  48. $argDSN = trim($argDSN);
  49. $endDSN = substr($argDSN, strlen($argDSN) - 1);
  50. if ($endDSN != ';') $argDSN .= ';';
  51. $argDSN .= 'Database='.$argDatabasename;
  52. }
  53. if (isset($php_errormsg)) $php_errormsg = '';
  54. if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
  55. else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode);
  56. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  57. if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
  58. return $this->_connectionID != false;
  59. }
  60. // returns true or false
  61. function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
  62. {
  63. global $php_errormsg;
  64. if (!function_exists('odbc_connect')) return null;
  65. if (isset($php_errormsg)) $php_errormsg = '';
  66. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  67. if ($this->debug && $argDatabasename) {
  68. ADOConnection::outp("For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
  69. }
  70. // print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
  71. if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
  72. else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);
  73. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  74. if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID);
  75. if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
  76. return $this->_connectionID != false;
  77. }
  78. function ServerInfo()
  79. {
  80. if (!empty($this->host) && ADODB_PHPVER >= 0x4300) {
  81. $dsn = strtoupper($this->host);
  82. $first = true;
  83. $found = false;
  84. if (!function_exists('odbc_data_source')) return false;
  85. while(true) {
  86. $rez = @odbc_data_source($this->_connectionID,
  87. $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT);
  88. $first = false;
  89. if (!is_array($rez)) break;
  90. if (strtoupper($rez['server']) == $dsn) {
  91. $found = true;
  92. break;
  93. }
  94. }
  95. if (!$found) return ADOConnection::ServerInfo();
  96. if (!isset($rez['version'])) $rez['version'] = '';
  97. return $rez;
  98. } else {
  99. return ADOConnection::ServerInfo();
  100. }
  101. }
  102. function CreateSequence($seqname='adodbseq',$start=1)
  103. {
  104. if (empty($this->_genSeqSQL)) return false;
  105. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  106. if (!$ok) return false;
  107. $start -= 1;
  108. return $this->Execute("insert into $seqname values($start)");
  109. }
  110. var $_dropSeqSQL = 'drop table %s';
  111. function DropSequence($seqname = 'adodbseq')
  112. {
  113. if (empty($this->_dropSeqSQL)) return false;
  114. return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
  115. }
  116. /*
  117. This algorithm is not very efficient, but works even if table locking
  118. is not available.
  119. Will return false if unable to generate an ID after $MAXLOOPS attempts.
  120. */
  121. function GenID($seq='adodbseq',$start=1)
  122. {
  123. // if you have to modify the parameter below, your database is overloaded,
  124. // or you need to implement generation of id's yourself!
  125. $MAXLOOPS = 100;
  126. //$this->debug=1;
  127. while (--$MAXLOOPS>=0) {
  128. $num = $this->GetOne("select id from $seq");
  129. if ($num === false) {
  130. $this->Execute(sprintf($this->_genSeqSQL ,$seq));
  131. $start -= 1;
  132. $num = '0';
  133. $ok = $this->Execute("insert into $seq values($start)");
  134. if (!$ok) return false;
  135. }
  136. $this->Execute("update $seq set id=id+1 where id=$num");
  137. if ($this->affected_rows() > 0) {
  138. $num += 1;
  139. $this->genID = $num;
  140. return $num;
  141. } elseif ($this->affected_rows() == 0) {
  142. // some drivers do not return a valid value => try with another method
  143. $value = $this->GetOne("select id from $seq");
  144. if ($value == $num + 1) {
  145. return $value;
  146. }
  147. }
  148. }
  149. if ($fn = $this->raiseErrorFn) {
  150. $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
  151. }
  152. return false;
  153. }
  154. function ErrorMsg()
  155. {
  156. if ($this->_haserrorfunctions) {
  157. if ($this->_errorMsg !== false) return $this->_errorMsg;
  158. if (empty($this->_connectionID)) return @odbc_errormsg();
  159. return @odbc_errormsg($this->_connectionID);
  160. } else return ADOConnection::ErrorMsg();
  161. }
  162. function ErrorNo()
  163. {
  164. if ($this->_haserrorfunctions) {
  165. if ($this->_errorCode !== false) {
  166. // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
  167. return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
  168. }
  169. if (empty($this->_connectionID)) $e = @odbc_error();
  170. else $e = @odbc_error($this->_connectionID);
  171. // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
  172. // so we check and patch
  173. if (strlen($e)<=2) return 0;
  174. return $e;
  175. } else return ADOConnection::ErrorNo();
  176. }
  177. function BeginTrans()
  178. {
  179. if (!$this->hasTransactions) return false;
  180. if ($this->transOff) return true;
  181. $this->transCnt += 1;
  182. $this->_autocommit = false;
  183. return odbc_autocommit($this->_connectionID,false);
  184. }
  185. function CommitTrans($ok=true)
  186. {
  187. if ($this->transOff) return true;
  188. if (!$ok) return $this->RollbackTrans();
  189. if ($this->transCnt) $this->transCnt -= 1;
  190. $this->_autocommit = true;
  191. $ret = odbc_commit($this->_connectionID);
  192. odbc_autocommit($this->_connectionID,true);
  193. return $ret;
  194. }
  195. function RollbackTrans()
  196. {
  197. if ($this->transOff) return true;
  198. if ($this->transCnt) $this->transCnt -= 1;
  199. $this->_autocommit = true;
  200. $ret = odbc_rollback($this->_connectionID);
  201. odbc_autocommit($this->_connectionID,true);
  202. return $ret;
  203. }
  204. function MetaPrimaryKeys($table,$owner=false)
  205. {
  206. global $ADODB_FETCH_MODE;
  207. if ($this->uCaseTables) $table = strtoupper($table);
  208. $schema = '';
  209. $this->_findschema($table,$schema);
  210. $savem = $ADODB_FETCH_MODE;
  211. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  212. $qid = @odbc_primarykeys($this->_connectionID,'',$schema,$table);
  213. if (!$qid) {
  214. $ADODB_FETCH_MODE = $savem;
  215. return false;
  216. }
  217. $rs = new ADORecordSet_odbc($qid);
  218. $ADODB_FETCH_MODE = $savem;
  219. if (!$rs) return false;
  220. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  221. $arr = $rs->GetArray();
  222. $rs->Close();
  223. //print_r($arr);
  224. $arr2 = array();
  225. for ($i=0; $i < sizeof($arr); $i++) {
  226. if ($arr[$i][3]) $arr2[] = $arr[$i][3];
  227. }
  228. return $arr2;
  229. }
  230. function MetaTables($ttype=false,$showSchema=false,$mask=false)
  231. {
  232. global $ADODB_FETCH_MODE;
  233. $savem = $ADODB_FETCH_MODE;
  234. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  235. $qid = odbc_tables($this->_connectionID);
  236. $rs = new ADORecordSet_odbc($qid);
  237. $ADODB_FETCH_MODE = $savem;
  238. if (!$rs) {
  239. $false = false;
  240. return $false;
  241. }
  242. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  243. $arr = $rs->GetArray();
  244. //print_r($arr);
  245. $rs->Close();
  246. $arr2 = array();
  247. if ($ttype) {
  248. $isview = strncmp($ttype,'V',1) === 0;
  249. }
  250. for ($i=0; $i < sizeof($arr); $i++) {
  251. if (!$arr[$i][2]) continue;
  252. $type = $arr[$i][3];
  253. if ($ttype) {
  254. if ($isview) {
  255. if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
  256. } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2];
  257. } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2];
  258. }
  259. return $arr2;
  260. }
  261. /*
  262. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp
  263. / SQL data type codes /
  264. #define SQL_UNKNOWN_TYPE 0
  265. #define SQL_CHAR 1
  266. #define SQL_NUMERIC 2
  267. #define SQL_DECIMAL 3
  268. #define SQL_INTEGER 4
  269. #define SQL_SMALLINT 5
  270. #define SQL_FLOAT 6
  271. #define SQL_REAL 7
  272. #define SQL_DOUBLE 8
  273. #if (ODBCVER >= 0x0300)
  274. #define SQL_DATETIME 9
  275. #endif
  276. #define SQL_VARCHAR 12
  277. / One-parameter shortcuts for date/time data types /
  278. #if (ODBCVER >= 0x0300)
  279. #define SQL_TYPE_DATE 91
  280. #define SQL_TYPE_TIME 92
  281. #define SQL_TYPE_TIMESTAMP 93
  282. #define SQL_UNICODE (-95)
  283. #define SQL_UNICODE_VARCHAR (-96)
  284. #define SQL_UNICODE_LONGVARCHAR (-97)
  285. */
  286. function ODBCTypes($t)
  287. {
  288. switch ((integer)$t) {
  289. case 1:
  290. case 12:
  291. case 0:
  292. case -95:
  293. case -96:
  294. return 'C';
  295. case -97:
  296. case -1: //text
  297. return 'X';
  298. case -4: //image
  299. return 'B';
  300. case 9:
  301. case 91:
  302. return 'D';
  303. case 10:
  304. case 11:
  305. case 92:
  306. case 93:
  307. return 'T';
  308. case 4:
  309. case 5:
  310. case -6:
  311. return 'I';
  312. case -11: // uniqidentifier
  313. return 'R';
  314. case -7: //bit
  315. return 'L';
  316. default:
  317. return 'N';
  318. }
  319. }
  320. function MetaColumns($table, $normalize=true)
  321. {
  322. global $ADODB_FETCH_MODE;
  323. $false = false;
  324. if ($this->uCaseTables) $table = strtoupper($table);
  325. $schema = '';
  326. $this->_findschema($table,$schema);
  327. $savem = $ADODB_FETCH_MODE;
  328. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  329. /*if (false) { // after testing, confirmed that the following does not work becoz of a bug
  330. $qid2 = odbc_tables($this->_connectionID);
  331. $rs = new ADORecordSet_odbc($qid2);
  332. $ADODB_FETCH_MODE = $savem;
  333. if (!$rs) return false;
  334. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  335. $rs->_fetch();
  336. while (!$rs->EOF) {
  337. if ($table == strtoupper($rs->fields[2])) {
  338. $q = $rs->fields[0];
  339. $o = $rs->fields[1];
  340. break;
  341. }
  342. $rs->MoveNext();
  343. }
  344. $rs->Close();
  345. $qid = odbc_columns($this->_connectionID,$q,$o,strtoupper($table),'%');
  346. } */
  347. switch ($this->databaseType) {
  348. case 'access':
  349. case 'vfp':
  350. $qid = odbc_columns($this->_connectionID);#,'%','',strtoupper($table),'%');
  351. break;
  352. case 'db2':
  353. $colname = "%";
  354. $qid = odbc_columns($this->_connectionID, "", $schema, $table, $colname);
  355. break;
  356. default:
  357. $qid = @odbc_columns($this->_connectionID,'%','%',strtoupper($table),'%');
  358. if (empty($qid)) $qid = odbc_columns($this->_connectionID);
  359. break;
  360. }
  361. if (empty($qid)) return $false;
  362. $rs = new ADORecordSet_odbc($qid);
  363. $ADODB_FETCH_MODE = $savem;
  364. if (!$rs) return $false;
  365. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  366. $rs->_fetch();
  367. $retarr = array();
  368. /*
  369. $rs->fields indices
  370. 0 TABLE_QUALIFIER
  371. 1 TABLE_SCHEM
  372. 2 TABLE_NAME
  373. 3 COLUMN_NAME
  374. 4 DATA_TYPE
  375. 5 TYPE_NAME
  376. 6 PRECISION
  377. 7 LENGTH
  378. 8 SCALE
  379. 9 RADIX
  380. 10 NULLABLE
  381. 11 REMARKS
  382. */
  383. while (!$rs->EOF) {
  384. // adodb_pr($rs->fields);
  385. if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
  386. $fld = new ADOFieldObject();
  387. $fld->name = $rs->fields[3];
  388. $fld->type = $this->ODBCTypes($rs->fields[4]);
  389. // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
  390. // access uses precision to store length for char/varchar
  391. if ($fld->type == 'C' or $fld->type == 'X') {
  392. if ($this->databaseType == 'access')
  393. $fld->max_length = $rs->fields[6];
  394. else if ($rs->fields[4] <= -95) // UNICODE
  395. $fld->max_length = $rs->fields[7]/2;
  396. else
  397. $fld->max_length = $rs->fields[7];
  398. } else
  399. $fld->max_length = $rs->fields[7];
  400. $fld->not_null = !empty($rs->fields[10]);
  401. $fld->scale = $rs->fields[8];
  402. $retarr[strtoupper($fld->name)] = $fld;
  403. } else if (sizeof($retarr)>0)
  404. break;
  405. $rs->MoveNext();
  406. }
  407. $rs->Close(); //-- crashes 4.03pl1 -- why?
  408. if (empty($retarr)) $retarr = false;
  409. return $retarr;
  410. }
  411. function Prepare($sql)
  412. {
  413. if (! $this->_bindInputArray) return $sql; // no binding
  414. $stmt = odbc_prepare($this->_connectionID,$sql);
  415. if (!$stmt) {
  416. // we don't know whether odbc driver is parsing prepared stmts, so just return sql
  417. return $sql;
  418. }
  419. return array($sql,$stmt,false);
  420. }
  421. /* returns queryID or false */
  422. function _query($sql,$inputarr=false)
  423. {
  424. GLOBAL $php_errormsg;
  425. if (isset($php_errormsg)) $php_errormsg = '';
  426. $this->_error = '';
  427. if ($inputarr) {
  428. if (is_array($sql)) {
  429. $stmtid = $sql[1];
  430. } else {
  431. $stmtid = odbc_prepare($this->_connectionID,$sql);
  432. if ($stmtid == false) {
  433. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  434. return false;
  435. }
  436. }
  437. if (! odbc_execute($stmtid,$inputarr)) {
  438. //@odbc_free_result($stmtid);
  439. if ($this->_haserrorfunctions) {
  440. $this->_errorMsg = odbc_errormsg();
  441. $this->_errorCode = odbc_error();
  442. }
  443. return false;
  444. }
  445. } else if (is_array($sql)) {
  446. $stmtid = $sql[1];
  447. if (!odbc_execute($stmtid)) {
  448. //@odbc_free_result($stmtid);
  449. if ($this->_haserrorfunctions) {
  450. $this->_errorMsg = odbc_errormsg();
  451. $this->_errorCode = odbc_error();
  452. }
  453. return false;
  454. }
  455. } else
  456. $stmtid = odbc_exec($this->_connectionID,$sql);
  457. $this->_lastAffectedRows = 0;
  458. if ($stmtid) {
  459. if (@odbc_num_fields($stmtid) == 0) {
  460. $this->_lastAffectedRows = odbc_num_rows($stmtid);
  461. $stmtid = true;
  462. } else {
  463. $this->_lastAffectedRows = 0;
  464. odbc_binmode($stmtid,$this->binmode);
  465. odbc_longreadlen($stmtid,$this->maxblobsize);
  466. }
  467. if ($this->_haserrorfunctions) {
  468. $this->_errorMsg = '';
  469. $this->_errorCode = 0;
  470. } else
  471. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  472. } else {
  473. if ($this->_haserrorfunctions) {
  474. $this->_errorMsg = odbc_errormsg();
  475. $this->_errorCode = odbc_error();
  476. } else
  477. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  478. }
  479. return $stmtid;
  480. }
  481. /*
  482. Insert a null into the blob field of the table first.
  483. Then use UpdateBlob to store the blob.
  484. Usage:
  485. $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
  486. $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
  487. */
  488. function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
  489. {
  490. return $this->Execute("UPDATE $table SET $column=? WHERE $where",array($val)) != false;
  491. }
  492. // returns true or false
  493. function _close()
  494. {
  495. $ret = @odbc_close($this->_connectionID);
  496. $this->_connectionID = false;
  497. return $ret;
  498. }
  499. function _affectedrows()
  500. {
  501. return $this->_lastAffectedRows;
  502. }
  503. }
  504. /*--------------------------------------------------------------------------------------
  505. Class Name: Recordset
  506. --------------------------------------------------------------------------------------*/
  507. class ADORecordSet_odbc extends ADORecordSet {
  508. var $bind = false;
  509. var $databaseType = "odbc";
  510. var $dataProvider = "odbc";
  511. var $useFetchArray;
  512. var $_has_stupid_odbc_fetch_api_change;
  513. function __construct($id,$mode=false)
  514. {
  515. if ($mode === false) {
  516. global $ADODB_FETCH_MODE;
  517. $mode = $ADODB_FETCH_MODE;
  518. }
  519. $this->fetchMode = $mode;
  520. $this->_queryID = $id;
  521. // the following is required for mysql odbc driver in 4.3.1 -- why?
  522. $this->EOF = false;
  523. $this->_currentRow = -1;
  524. //parent::__construct($id);
  525. }
  526. // returns the field object
  527. function FetchField($fieldOffset = -1)
  528. {
  529. $off=$fieldOffset+1; // offsets begin at 1
  530. $o= new ADOFieldObject();
  531. $o->name = @odbc_field_name($this->_queryID,$off);
  532. $o->type = @odbc_field_type($this->_queryID,$off);
  533. $o->max_length = @odbc_field_len($this->_queryID,$off);
  534. if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
  535. else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
  536. return $o;
  537. }
  538. /* Use associative array to get fields array */
  539. function Fields($colname)
  540. {
  541. if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
  542. if (!$this->bind) {
  543. $this->bind = array();
  544. for ($i=0; $i < $this->_numOfFields; $i++) {
  545. $o = $this->FetchField($i);
  546. $this->bind[strtoupper($o->name)] = $i;
  547. }
  548. }
  549. return $this->fields[$this->bind[strtoupper($colname)]];
  550. }
  551. function _initrs()
  552. {
  553. global $ADODB_COUNTRECS;
  554. $this->_numOfRows = ($ADODB_COUNTRECS) ? @odbc_num_rows($this->_queryID) : -1;
  555. $this->_numOfFields = @odbc_num_fields($this->_queryID);
  556. // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
  557. if ($this->_numOfRows == 0) $this->_numOfRows = -1;
  558. //$this->useFetchArray = $this->connection->useFetchArray;
  559. $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
  560. }
  561. function _seek($row)
  562. {
  563. return false;
  564. }
  565. // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
  566. function GetArrayLimit($nrows,$offset=-1)
  567. {
  568. if ($offset <= 0) {
  569. $rs = $this->GetArray($nrows);
  570. return $rs;
  571. }
  572. $savem = $this->fetchMode;
  573. $this->fetchMode = ADODB_FETCH_NUM;
  574. $this->Move($offset);
  575. $this->fetchMode = $savem;
  576. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  577. $this->fields = $this->GetRowAssoc();
  578. }
  579. $results = array();
  580. $cnt = 0;
  581. while (!$this->EOF && $nrows != $cnt) {
  582. $results[$cnt++] = $this->fields;
  583. $this->MoveNext();
  584. }
  585. return $results;
  586. }
  587. function MoveNext()
  588. {
  589. if ($this->_numOfRows != 0 && !$this->EOF) {
  590. $this->_currentRow++;
  591. if( $this->_fetch() ) {
  592. return true;
  593. }
  594. }
  595. $this->fields = false;
  596. $this->EOF = true;
  597. return false;
  598. }
  599. function _fetch()
  600. {
  601. $this->fields = false;
  602. if ($this->_has_stupid_odbc_fetch_api_change)
  603. $rez = @odbc_fetch_into($this->_queryID,$this->fields);
  604. else {
  605. $row = 0;
  606. $rez = @odbc_fetch_into($this->_queryID,$row,$this->fields);
  607. }
  608. if ($rez) {
  609. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  610. $this->fields = $this->GetRowAssoc();
  611. }
  612. return true;
  613. }
  614. return false;
  615. }
  616. function _close()
  617. {
  618. return @odbc_free_result($this->_queryID);
  619. }
  620. }