adodb-ads.inc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php
  2. /*
  3. (c) 2000-2014 John Lim (jlim#natsoft.com.my). All rights reserved.
  4. Portions Copyright (c) 2007-2009, iAnywhere Solutions, Inc.
  5. All rights reserved. All unpublished rights reserved.
  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. NOTE: This driver requires the Advantage PHP client libraries, which
  11. can be downloaded for free via:
  12. http://devzone.advantagedatabase.com/dz/content.aspx?key=20
  13. DELPHI FOR PHP USERS:
  14. The following steps can be taken to utilize this driver from the
  15. CodeGear Delphi for PHP product:
  16. 1 - See note above, download and install the Advantage PHP client.
  17. 2 - Copy the following files to the Delphi for PHP\X.X\php\ext directory:
  18. ace32.dll
  19. axcws32.dll
  20. adsloc32.dll
  21. php_advantage.dll (rename the existing php_advantage.dll.5.x.x file)
  22. 3 - Add the following line to the Delphi for PHP\X.X\php\php.ini.template file:
  23. extension=php_advantage.dll
  24. 4 - To use: enter "ads" as the DriverName on a connection component, and set
  25. a Host property similar to "DataDirectory=c:\". See the Advantage PHP
  26. help file topic for ads_connect for details on connection path options
  27. and formatting.
  28. 5 - (optional) - Modify the Delphi for PHP\X.X\vcl\packages\database.packages.php
  29. file and add ads to the list of strings returned when registering the
  30. Database object's DriverName property.
  31. */
  32. // security - hide paths
  33. if (!defined('ADODB_DIR')) die();
  34. define("_ADODB_ADS_LAYER", 2 );
  35. /*--------------------------------------------------------------------------------------
  36. --------------------------------------------------------------------------------------*/
  37. class ADODB_ads extends ADOConnection {
  38. var $databaseType = "ads";
  39. var $fmt = "'m-d-Y'";
  40. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  41. var $concat_operator = '';
  42. var $replaceQuote = "''"; // string to use to replace quotes
  43. var $dataProvider = "ads";
  44. var $hasAffectedRows = true;
  45. var $binmode = ODBC_BINMODE_RETURN;
  46. var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive
  47. // breaking backward-compat
  48. //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field
  49. var $_bindInputArray = false;
  50. var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L
  51. var $_genSeqSQL = "create table %s (id integer)";
  52. var $_autocommit = true;
  53. var $_haserrorfunctions = true;
  54. var $_has_stupid_odbc_fetch_api_change = true;
  55. var $_lastAffectedRows = 0;
  56. var $uCaseTables = true; // for meta* functions, uppercase table names
  57. function __construct()
  58. {
  59. $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050;
  60. $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
  61. }
  62. // returns true or false
  63. function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
  64. {
  65. if (!function_exists('ads_connect')) return null;
  66. if ($this->debug && $argDatabasename && $this->databaseType != 'vfp') {
  67. ADOConnection::outp("For Advantage Connect(), $argDatabasename is not used. Place dsn in 1st parameter.");
  68. }
  69. $last_php_error = $this->resetLastError();
  70. if ($this->curmode === false) $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword);
  71. else $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword,$this->curmode);
  72. $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  73. if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
  74. return $this->_connectionID != false;
  75. }
  76. // returns true or false
  77. function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
  78. {
  79. if (!function_exists('ads_connect')) return null;
  80. $last_php_error = $this->resetLastError();
  81. $this->_errorMsg = '';
  82. if ($this->debug && $argDatabasename) {
  83. ADOConnection::outp("For PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
  84. }
  85. // print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
  86. if ($this->curmode === false) $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword);
  87. else $this->_connectionID = ads_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);
  88. $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  89. if ($this->_connectionID && $this->autoRollback) @ads_rollback($this->_connectionID);
  90. if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
  91. return $this->_connectionID != false;
  92. }
  93. // returns the Server version and Description
  94. function ServerInfo()
  95. {
  96. if (!empty($this->host) && ADODB_PHPVER >= 0x4300) {
  97. $stmt = $this->Prepare('EXECUTE PROCEDURE sp_mgGetInstallInfo()');
  98. $res = $this->Execute($stmt);
  99. if(!$res)
  100. print $this->ErrorMsg();
  101. else{
  102. $ret["version"]= $res->fields[3];
  103. $ret["description"]="Advantage Database Server";
  104. return $ret;
  105. }
  106. }
  107. else {
  108. return ADOConnection::ServerInfo();
  109. }
  110. }
  111. // returns true or false
  112. function CreateSequence($seqname = 'adodbseq', $start = 1)
  113. {
  114. $res = $this->Execute("CREATE TABLE $seqname ( ID autoinc( 1 ) ) IN DATABASE");
  115. if(!$res){
  116. print $this->ErrorMsg();
  117. return false;
  118. }
  119. else
  120. return true;
  121. }
  122. // returns true or false
  123. function DropSequence($seqname = 'adodbseq')
  124. {
  125. $res = $this->Execute("DROP TABLE $seqname");
  126. if(!$res){
  127. print $this->ErrorMsg();
  128. return false;
  129. }
  130. else
  131. return true;
  132. }
  133. // returns the generated ID or false
  134. // checks if the table already exists, else creates the table and inserts a record into the table
  135. // and gets the ID number of the last inserted record.
  136. function GenID($seqname = 'adodbseq', $start = 1)
  137. {
  138. $go = $this->Execute("select * from $seqname");
  139. if (!$go){
  140. $res = $this->Execute("CREATE TABLE $seqname ( ID autoinc( 1 ) ) IN DATABASE");
  141. if(!res){
  142. print $this->ErrorMsg();
  143. return false;
  144. }
  145. }
  146. $res = $this->Execute("INSERT INTO $seqname VALUES( DEFAULT )");
  147. if(!$res){
  148. print $this->ErrorMsg();
  149. return false;
  150. }
  151. else{
  152. $gen = $this->Execute("SELECT LastAutoInc( STATEMENT ) FROM system.iota");
  153. $ret = $gen->fields[0];
  154. return $ret;
  155. }
  156. }
  157. function ErrorMsg()
  158. {
  159. if ($this->_haserrorfunctions) {
  160. if ($this->_errorMsg !== false) return $this->_errorMsg;
  161. if (empty($this->_connectionID)) return @ads_errormsg();
  162. return @ads_errormsg($this->_connectionID);
  163. } else return ADOConnection::ErrorMsg();
  164. }
  165. function ErrorNo()
  166. {
  167. if ($this->_haserrorfunctions) {
  168. if ($this->_errorCode !== false) {
  169. // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
  170. return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
  171. }
  172. if (empty($this->_connectionID)) $e = @ads_error();
  173. else $e = @ads_error($this->_connectionID);
  174. // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
  175. // so we check and patch
  176. if (strlen($e)<=2) return 0;
  177. return $e;
  178. } else return ADOConnection::ErrorNo();
  179. }
  180. function BeginTrans()
  181. {
  182. if (!$this->hasTransactions) return false;
  183. if ($this->transOff) return true;
  184. $this->transCnt += 1;
  185. $this->_autocommit = false;
  186. return ads_autocommit($this->_connectionID,false);
  187. }
  188. function CommitTrans($ok=true)
  189. {
  190. if ($this->transOff) return true;
  191. if (!$ok) return $this->RollbackTrans();
  192. if ($this->transCnt) $this->transCnt -= 1;
  193. $this->_autocommit = true;
  194. $ret = ads_commit($this->_connectionID);
  195. ads_autocommit($this->_connectionID,true);
  196. return $ret;
  197. }
  198. function RollbackTrans()
  199. {
  200. if ($this->transOff) return true;
  201. if ($this->transCnt) $this->transCnt -= 1;
  202. $this->_autocommit = true;
  203. $ret = ads_rollback($this->_connectionID);
  204. ads_autocommit($this->_connectionID,true);
  205. return $ret;
  206. }
  207. // Returns tables,Views or both on succesfull execution. Returns
  208. // tables by default on succesfull execustion.
  209. function &MetaTables($ttype = false, $showSchema = false, $mask = false)
  210. {
  211. $recordSet1 = $this->Execute("select * from system.tables");
  212. if(!$recordSet1){
  213. print $this->ErrorMsg();
  214. return false;
  215. }
  216. $recordSet2 = $this->Execute("select * from system.views");
  217. if(!$recordSet2){
  218. print $this->ErrorMsg();
  219. return false;
  220. }
  221. $i=0;
  222. while (!$recordSet1->EOF){
  223. $arr["$i"] = $recordSet1->fields[0];
  224. $recordSet1->MoveNext();
  225. $i=$i+1;
  226. }
  227. if($ttype=='FALSE'){
  228. while (!$recordSet2->EOF){
  229. $arr["$i"] = $recordSet2->fields[0];
  230. $recordSet2->MoveNext();
  231. $i=$i+1;
  232. }
  233. return $arr;
  234. }
  235. elseif($ttype=='VIEWS'){
  236. while (!$recordSet2->EOF){
  237. $arrV["$i"] = $recordSet2->fields[0];
  238. $recordSet2->MoveNext();
  239. $i=$i+1;
  240. }
  241. return $arrV;
  242. }
  243. else{
  244. return $arr;
  245. }
  246. }
  247. function &MetaPrimaryKeys($table, $owner = false)
  248. {
  249. $recordSet = $this->Execute("select table_primary_key from system.tables where name='$table'");
  250. if(!$recordSet){
  251. print $this->ErrorMsg();
  252. return false;
  253. }
  254. $i=0;
  255. while (!$recordSet->EOF){
  256. $arr["$i"] = $recordSet->fields[0];
  257. $recordSet->MoveNext();
  258. $i=$i+1;
  259. }
  260. return $arr;
  261. }
  262. /*
  263. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp
  264. / SQL data type codes /
  265. #define SQL_UNKNOWN_TYPE 0
  266. #define SQL_CHAR 1
  267. #define SQL_NUMERIC 2
  268. #define SQL_DECIMAL 3
  269. #define SQL_INTEGER 4
  270. #define SQL_SMALLINT 5
  271. #define SQL_FLOAT 6
  272. #define SQL_REAL 7
  273. #define SQL_DOUBLE 8
  274. #if (ODBCVER >= 0x0300)
  275. #define SQL_DATETIME 9
  276. #endif
  277. #define SQL_VARCHAR 12
  278. / One-parameter shortcuts for date/time data types /
  279. #if (ODBCVER >= 0x0300)
  280. #define SQL_TYPE_DATE 91
  281. #define SQL_TYPE_TIME 92
  282. #define SQL_TYPE_TIMESTAMP 93
  283. #define SQL_UNICODE (-95)
  284. #define SQL_UNICODE_VARCHAR (-96)
  285. #define SQL_UNICODE_LONGVARCHAR (-97)
  286. */
  287. function ODBCTypes($t)
  288. {
  289. switch ((integer)$t) {
  290. case 1:
  291. case 12:
  292. case 0:
  293. case -95:
  294. case -96:
  295. return 'C';
  296. case -97:
  297. case -1: //text
  298. return 'X';
  299. case -4: //image
  300. return 'B';
  301. case 9:
  302. case 91:
  303. return 'D';
  304. case 10:
  305. case 11:
  306. case 92:
  307. case 93:
  308. return 'T';
  309. case 4:
  310. case 5:
  311. case -6:
  312. return 'I';
  313. case -11: // uniqidentifier
  314. return 'R';
  315. case -7: //bit
  316. return 'L';
  317. default:
  318. return 'N';
  319. }
  320. }
  321. function &MetaColumns($table, $normalize = true)
  322. {
  323. global $ADODB_FETCH_MODE;
  324. $false = false;
  325. if ($this->uCaseTables) $table = strtoupper($table);
  326. $schema = '';
  327. $this->_findschema($table,$schema);
  328. $savem = $ADODB_FETCH_MODE;
  329. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  330. /*if (false) { // after testing, confirmed that the following does not work becoz of a bug
  331. $qid2 = ads_tables($this->_connectionID);
  332. $rs = new ADORecordSet_ads($qid2);
  333. $ADODB_FETCH_MODE = $savem;
  334. if (!$rs) return false;
  335. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  336. $rs->_fetch();
  337. while (!$rs->EOF) {
  338. if ($table == strtoupper($rs->fields[2])) {
  339. $q = $rs->fields[0];
  340. $o = $rs->fields[1];
  341. break;
  342. }
  343. $rs->MoveNext();
  344. }
  345. $rs->Close();
  346. $qid = ads_columns($this->_connectionID,$q,$o,strtoupper($table),'%');
  347. } */
  348. switch ($this->databaseType) {
  349. case 'access':
  350. case 'vfp':
  351. $qid = ads_columns($this->_connectionID);#,'%','',strtoupper($table),'%');
  352. break;
  353. case 'db2':
  354. $colname = "%";
  355. $qid = ads_columns($this->_connectionID, "", $schema, $table, $colname);
  356. break;
  357. default:
  358. $qid = @ads_columns($this->_connectionID,'%','%',strtoupper($table),'%');
  359. if (empty($qid)) $qid = ads_columns($this->_connectionID);
  360. break;
  361. }
  362. if (empty($qid)) return $false;
  363. $rs = new ADORecordSet_ads($qid);
  364. $ADODB_FETCH_MODE = $savem;
  365. if (!$rs) return $false;
  366. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  367. $rs->_fetch();
  368. $retarr = array();
  369. /*
  370. $rs->fields indices
  371. 0 TABLE_QUALIFIER
  372. 1 TABLE_SCHEM
  373. 2 TABLE_NAME
  374. 3 COLUMN_NAME
  375. 4 DATA_TYPE
  376. 5 TYPE_NAME
  377. 6 PRECISION
  378. 7 LENGTH
  379. 8 SCALE
  380. 9 RADIX
  381. 10 NULLABLE
  382. 11 REMARKS
  383. */
  384. while (!$rs->EOF) {
  385. // adodb_pr($rs->fields);
  386. if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
  387. $fld = new ADOFieldObject();
  388. $fld->name = $rs->fields[3];
  389. $fld->type = $this->ODBCTypes($rs->fields[4]);
  390. // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
  391. // access uses precision to store length for char/varchar
  392. if ($fld->type == 'C' or $fld->type == 'X') {
  393. if ($this->databaseType == 'access')
  394. $fld->max_length = $rs->fields[6];
  395. else if ($rs->fields[4] <= -95) // UNICODE
  396. $fld->max_length = $rs->fields[7]/2;
  397. else
  398. $fld->max_length = $rs->fields[7];
  399. } else
  400. $fld->max_length = $rs->fields[7];
  401. $fld->not_null = !empty($rs->fields[10]);
  402. $fld->scale = $rs->fields[8];
  403. $retarr[strtoupper($fld->name)] = $fld;
  404. } else if (sizeof($retarr)>0)
  405. break;
  406. $rs->MoveNext();
  407. }
  408. $rs->Close(); //-- crashes 4.03pl1 -- why?
  409. if (empty($retarr)) $retarr = false;
  410. return $retarr;
  411. }
  412. // Returns an array of columns names for a given table
  413. function &MetaColumnNames($table, $numIndexes = false, $useattnum = false)
  414. {
  415. $recordSet = $this->Execute("select name from system.columns where parent='$table'");
  416. if(!$recordSet){
  417. print $this->ErrorMsg();
  418. return false;
  419. }
  420. else{
  421. $i=0;
  422. while (!$recordSet->EOF){
  423. $arr["FIELD$i"] = $recordSet->fields[0];
  424. $recordSet->MoveNext();
  425. $i=$i+1;
  426. }
  427. return $arr;
  428. }
  429. }
  430. function Prepare($sql)
  431. {
  432. if (! $this->_bindInputArray) return $sql; // no binding
  433. $stmt = ads_prepare($this->_connectionID,$sql);
  434. if (!$stmt) {
  435. // we don't know whether odbc driver is parsing prepared stmts, so just return sql
  436. return $sql;
  437. }
  438. return array($sql,$stmt,false);
  439. }
  440. /* returns queryID or false */
  441. function _query($sql,$inputarr=false)
  442. {
  443. $last_php_error = $this->resetLastError();
  444. $this->_errorMsg = '';
  445. if ($inputarr) {
  446. if (is_array($sql)) {
  447. $stmtid = $sql[1];
  448. } else {
  449. $stmtid = ads_prepare($this->_connectionID,$sql);
  450. if ($stmtid == false) {
  451. $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  452. return false;
  453. }
  454. }
  455. if (! ads_execute($stmtid,$inputarr)) {
  456. //@ads_free_result($stmtid);
  457. if ($this->_haserrorfunctions) {
  458. $this->_errorMsg = ads_errormsg();
  459. $this->_errorCode = ads_error();
  460. }
  461. return false;
  462. }
  463. } else if (is_array($sql)) {
  464. $stmtid = $sql[1];
  465. if (!ads_execute($stmtid)) {
  466. //@ads_free_result($stmtid);
  467. if ($this->_haserrorfunctions) {
  468. $this->_errorMsg = ads_errormsg();
  469. $this->_errorCode = ads_error();
  470. }
  471. return false;
  472. }
  473. } else
  474. {
  475. $stmtid = ads_exec($this->_connectionID,$sql);
  476. }
  477. $this->_lastAffectedRows = 0;
  478. if ($stmtid) {
  479. if (@ads_num_fields($stmtid) == 0) {
  480. $this->_lastAffectedRows = ads_num_rows($stmtid);
  481. $stmtid = true;
  482. } else {
  483. $this->_lastAffectedRows = 0;
  484. ads_binmode($stmtid,$this->binmode);
  485. ads_longreadlen($stmtid,$this->maxblobsize);
  486. }
  487. if ($this->_haserrorfunctions) {
  488. $this->_errorMsg = '';
  489. $this->_errorCode = 0;
  490. } else {
  491. $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  492. }
  493. } else {
  494. if ($this->_haserrorfunctions) {
  495. $this->_errorMsg = ads_errormsg();
  496. $this->_errorCode = ads_error();
  497. } else {
  498. $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  499. }
  500. }
  501. return $stmtid;
  502. }
  503. /*
  504. Insert a null into the blob field of the table first.
  505. Then use UpdateBlob to store the blob.
  506. Usage:
  507. $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
  508. $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
  509. */
  510. function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
  511. {
  512. $last_php_error = $this->resetLastError();
  513. $sql = "UPDATE $table SET $column=? WHERE $where";
  514. $stmtid = ads_prepare($this->_connectionID,$sql);
  515. if ($stmtid == false){
  516. $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  517. return false;
  518. }
  519. if (! ads_execute($stmtid,array($val),array(SQL_BINARY) )){
  520. if ($this->_haserrorfunctions){
  521. $this->_errorMsg = ads_errormsg();
  522. $this->_errorCode = ads_error();
  523. }
  524. return false;
  525. }
  526. return TRUE;
  527. }
  528. // returns true or false
  529. function _close()
  530. {
  531. $ret = @ads_close($this->_connectionID);
  532. $this->_connectionID = false;
  533. return $ret;
  534. }
  535. function _affectedrows()
  536. {
  537. return $this->_lastAffectedRows;
  538. }
  539. }
  540. /*--------------------------------------------------------------------------------------
  541. Class Name: Recordset
  542. --------------------------------------------------------------------------------------*/
  543. class ADORecordSet_ads extends ADORecordSet {
  544. var $bind = false;
  545. var $databaseType = "ads";
  546. var $dataProvider = "ads";
  547. var $useFetchArray;
  548. var $_has_stupid_odbc_fetch_api_change;
  549. function __construct($id,$mode=false)
  550. {
  551. if ($mode === false) {
  552. global $ADODB_FETCH_MODE;
  553. $mode = $ADODB_FETCH_MODE;
  554. }
  555. $this->fetchMode = $mode;
  556. $this->_queryID = $id;
  557. // the following is required for mysql odbc driver in 4.3.1 -- why?
  558. $this->EOF = false;
  559. $this->_currentRow = -1;
  560. //parent::__construct($id);
  561. }
  562. // returns the field object
  563. function &FetchField($fieldOffset = -1)
  564. {
  565. $off=$fieldOffset+1; // offsets begin at 1
  566. $o= new ADOFieldObject();
  567. $o->name = @ads_field_name($this->_queryID,$off);
  568. $o->type = @ads_field_type($this->_queryID,$off);
  569. $o->max_length = @ads_field_len($this->_queryID,$off);
  570. if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
  571. else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
  572. return $o;
  573. }
  574. /* Use associative array to get fields array */
  575. function Fields($colname)
  576. {
  577. if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
  578. if (!$this->bind) {
  579. $this->bind = array();
  580. for ($i=0; $i < $this->_numOfFields; $i++) {
  581. $o = $this->FetchField($i);
  582. $this->bind[strtoupper($o->name)] = $i;
  583. }
  584. }
  585. return $this->fields[$this->bind[strtoupper($colname)]];
  586. }
  587. function _initrs()
  588. {
  589. global $ADODB_COUNTRECS;
  590. $this->_numOfRows = ($ADODB_COUNTRECS) ? @ads_num_rows($this->_queryID) : -1;
  591. $this->_numOfFields = @ads_num_fields($this->_queryID);
  592. // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
  593. if ($this->_numOfRows == 0) $this->_numOfRows = -1;
  594. //$this->useFetchArray = $this->connection->useFetchArray;
  595. $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
  596. }
  597. function _seek($row)
  598. {
  599. return false;
  600. }
  601. // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
  602. function &GetArrayLimit($nrows,$offset=-1)
  603. {
  604. if ($offset <= 0) {
  605. $rs =& $this->GetArray($nrows);
  606. return $rs;
  607. }
  608. $savem = $this->fetchMode;
  609. $this->fetchMode = ADODB_FETCH_NUM;
  610. $this->Move($offset);
  611. $this->fetchMode = $savem;
  612. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  613. $this->fields =& $this->GetRowAssoc();
  614. }
  615. $results = array();
  616. $cnt = 0;
  617. while (!$this->EOF && $nrows != $cnt) {
  618. $results[$cnt++] = $this->fields;
  619. $this->MoveNext();
  620. }
  621. return $results;
  622. }
  623. function MoveNext()
  624. {
  625. if ($this->_numOfRows != 0 && !$this->EOF) {
  626. $this->_currentRow++;
  627. if( $this->_fetch() ) {
  628. return true;
  629. }
  630. }
  631. $this->fields = false;
  632. $this->EOF = true;
  633. return false;
  634. }
  635. function _fetch()
  636. {
  637. $this->fields = false;
  638. if ($this->_has_stupid_odbc_fetch_api_change)
  639. $rez = @ads_fetch_into($this->_queryID,$this->fields);
  640. else {
  641. $row = 0;
  642. $rez = @ads_fetch_into($this->_queryID,$row,$this->fields);
  643. }
  644. if ($rez) {
  645. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  646. $this->fields =& $this->GetRowAssoc();
  647. }
  648. return true;
  649. }
  650. return false;
  651. }
  652. function _close()
  653. {
  654. return @ads_free_result($this->_queryID);
  655. }
  656. }