adodb-ibase.inc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. <?php
  2. /*
  3. @version v5.20.17 31-Mar-2020
  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. Latest version is available at http://adodb.org/
  10. Interbase data driver. Requires interbase client. Works on Windows and Unix.
  11. 3 Jan 2002 -- suggestions by Hans-Peter Oeri <kampfcaspar75@oeri.ch>
  12. changed transaction handling and added experimental blob stuff
  13. Docs to interbase at the website
  14. http://www.synectics.co.za/php3/tutorial/IB_PHP3_API.html
  15. To use gen_id(), see
  16. http://www.volny.cz/iprenosil/interbase/ip_ib_code.htm#_code_creategen
  17. $rs = $conn->Execute('select gen_id(adodb,1) from rdb$database');
  18. $id = $rs->fields[0];
  19. $conn->Execute("insert into table (id, col1,...) values ($id, $val1,...)");
  20. */
  21. // security - hide paths
  22. if (!defined('ADODB_DIR')) die();
  23. class ADODB_ibase extends ADOConnection {
  24. var $databaseType = "ibase";
  25. var $dataProvider = "ibase";
  26. var $replaceQuote = "''"; // string to use to replace quotes
  27. var $ibase_datefmt = '%Y-%m-%d'; // For hours,mins,secs change to '%Y-%m-%d %H:%M:%S';
  28. var $fmtDate = "'Y-m-d'";
  29. var $ibase_timestampfmt = "%Y-%m-%d %H:%M:%S";
  30. var $ibase_timefmt = "%H:%M:%S";
  31. var $fmtTimeStamp = "'Y-m-d, H:i:s'";
  32. var $concat_operator='||';
  33. var $_transactionID;
  34. var $metaTablesSQL = "select rdb\$relation_name from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
  35. //OPN STUFF start
  36. var $metaColumnsSQL = "select a.rdb\$field_name, a.rdb\$null_flag, a.rdb\$default_source, b.rdb\$field_length, b.rdb\$field_scale, b.rdb\$field_sub_type, b.rdb\$field_precision, b.rdb\$field_type from rdb\$relation_fields a, rdb\$fields b where a.rdb\$field_source = b.rdb\$field_name and a.rdb\$relation_name = '%s' order by a.rdb\$field_position asc";
  37. //OPN STUFF end
  38. var $ibasetrans;
  39. var $hasGenID = true;
  40. var $_bindInputArray = true;
  41. var $buffers = 0;
  42. var $dialect = 1;
  43. var $sysDate = "cast('TODAY' as timestamp)";
  44. var $sysTimeStamp = "CURRENT_TIMESTAMP"; //"cast('NOW' as timestamp)";
  45. var $ansiOuter = true;
  46. var $hasAffectedRows = false;
  47. var $poorAffectedRows = true;
  48. var $blobEncodeType = 'C';
  49. var $role = false;
  50. function __construct()
  51. {
  52. if (defined('IBASE_DEFAULT')) $this->ibasetrans = IBASE_DEFAULT;
  53. }
  54. // returns true or false
  55. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename,$persist=false)
  56. {
  57. if (!function_exists('ibase_pconnect')) return null;
  58. if ($argDatabasename) $argHostname .= ':'.$argDatabasename;
  59. $fn = ($persist) ? 'ibase_pconnect':'ibase_connect';
  60. if ($this->role)
  61. $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
  62. $this->charSet,$this->buffers,$this->dialect,$this->role);
  63. else
  64. $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
  65. $this->charSet,$this->buffers,$this->dialect);
  66. if ($this->dialect != 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
  67. $this->replaceQuote = "''";
  68. }
  69. if ($this->_connectionID === false) {
  70. $this->_handleerror();
  71. return false;
  72. }
  73. // PHP5 change.
  74. if (function_exists('ibase_timefmt')) {
  75. ibase_timefmt($this->ibase_datefmt,IBASE_DATE );
  76. if ($this->dialect == 1) {
  77. ibase_timefmt($this->ibase_datefmt,IBASE_TIMESTAMP );
  78. }
  79. else {
  80. ibase_timefmt($this->ibase_timestampfmt,IBASE_TIMESTAMP );
  81. }
  82. ibase_timefmt($this->ibase_timefmt,IBASE_TIME );
  83. } else {
  84. ini_set("ibase.timestampformat", $this->ibase_timestampfmt);
  85. ini_set("ibase.dateformat", $this->ibase_datefmt);
  86. ini_set("ibase.timeformat", $this->ibase_timefmt);
  87. }
  88. return true;
  89. }
  90. // returns true or false
  91. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  92. {
  93. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename,true);
  94. }
  95. function MetaPrimaryKeys($table,$owner_notused=false,$internalKey=false)
  96. {
  97. if ($internalKey) {
  98. return array('RDB$DB_KEY');
  99. }
  100. $table = strtoupper($table);
  101. $sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
  102. FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME
  103. WHERE I.RDB$RELATION_NAME=\''.$table.'\' and I.RDB$INDEX_NAME like \'RDB$PRIMARY%\'
  104. ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
  105. $a = $this->GetCol($sql,false,true);
  106. if ($a && sizeof($a)>0) return $a;
  107. return false;
  108. }
  109. function ServerInfo()
  110. {
  111. $arr['dialect'] = $this->dialect;
  112. switch($arr['dialect']) {
  113. case '':
  114. case '1': $s = 'Interbase 5.5 or earlier'; break;
  115. case '2': $s = 'Interbase 5.6'; break;
  116. default:
  117. case '3': $s = 'Interbase 6.0'; break;
  118. }
  119. $arr['version'] = ADOConnection::_findvers($s);
  120. $arr['description'] = $s;
  121. return $arr;
  122. }
  123. function BeginTrans()
  124. {
  125. if ($this->transOff) return true;
  126. $this->transCnt += 1;
  127. $this->autoCommit = false;
  128. $this->_transactionID = $this->_connectionID;//ibase_trans($this->ibasetrans, $this->_connectionID);
  129. return $this->_transactionID;
  130. }
  131. function CommitTrans($ok=true)
  132. {
  133. if (!$ok) {
  134. return $this->RollbackTrans();
  135. }
  136. if ($this->transOff) {
  137. return true;
  138. }
  139. if ($this->transCnt) {
  140. $this->transCnt -= 1;
  141. }
  142. $ret = false;
  143. $this->autoCommit = true;
  144. if ($this->_transactionID) {
  145. //print ' commit ';
  146. $ret = ibase_commit($this->_transactionID);
  147. }
  148. $this->_transactionID = false;
  149. return $ret;
  150. }
  151. // there are some compat problems with ADODB_COUNTRECS=false and $this->_logsql currently.
  152. // it appears that ibase extension cannot support multiple concurrent queryid's
  153. function _Execute($sql,$inputarr=false)
  154. {
  155. global $ADODB_COUNTRECS;
  156. if ($this->_logsql) {
  157. $savecrecs = $ADODB_COUNTRECS;
  158. $ADODB_COUNTRECS = true; // force countrecs
  159. $ret = ADOConnection::_Execute($sql,$inputarr);
  160. $ADODB_COUNTRECS = $savecrecs;
  161. } else {
  162. $ret = ADOConnection::_Execute($sql,$inputarr);
  163. }
  164. return $ret;
  165. }
  166. function RollbackTrans()
  167. {
  168. if ($this->transOff) return true;
  169. if ($this->transCnt) $this->transCnt -= 1;
  170. $ret = false;
  171. $this->autoCommit = true;
  172. if ($this->_transactionID) {
  173. $ret = ibase_rollback($this->_transactionID);
  174. }
  175. $this->_transactionID = false;
  176. return $ret;
  177. }
  178. function MetaIndexes ($table, $primary = FALSE, $owner=false)
  179. {
  180. // save old fetch mode
  181. global $ADODB_FETCH_MODE;
  182. $false = false;
  183. $save = $ADODB_FETCH_MODE;
  184. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  185. if ($this->fetchMode !== FALSE) {
  186. $savem = $this->SetFetchMode(FALSE);
  187. }
  188. $table = strtoupper($table);
  189. $sql = "SELECT * FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = '".$table."'";
  190. if (!$primary) {
  191. $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$%'";
  192. } else {
  193. $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$FOREIGN%'";
  194. }
  195. // get index details
  196. $rs = $this->Execute($sql);
  197. if (!is_object($rs)) {
  198. // restore fetchmode
  199. if (isset($savem)) {
  200. $this->SetFetchMode($savem);
  201. }
  202. $ADODB_FETCH_MODE = $save;
  203. return $false;
  204. }
  205. $indexes = array();
  206. while ($row = $rs->FetchRow()) {
  207. $index = $row[0];
  208. if (!isset($indexes[$index])) {
  209. if (is_null($row[3])) {
  210. $row[3] = 0;
  211. }
  212. $indexes[$index] = array(
  213. 'unique' => ($row[3] == 1),
  214. 'columns' => array()
  215. );
  216. }
  217. $sql = "SELECT * FROM RDB\$INDEX_SEGMENTS WHERE RDB\$INDEX_NAME = '".$index."' ORDER BY RDB\$FIELD_POSITION ASC";
  218. $rs1 = $this->Execute($sql);
  219. while ($row1 = $rs1->FetchRow()) {
  220. $indexes[$index]['columns'][$row1[2]] = $row1[1];
  221. }
  222. }
  223. // restore fetchmode
  224. if (isset($savem)) {
  225. $this->SetFetchMode($savem);
  226. }
  227. $ADODB_FETCH_MODE = $save;
  228. return $indexes;
  229. }
  230. // See http://community.borland.com/article/0,1410,25844,00.html
  231. function RowLock($tables,$where,$col=false)
  232. {
  233. if ($this->autoCommit) {
  234. $this->BeginTrans();
  235. }
  236. $this->Execute("UPDATE $table SET $col=$col WHERE $where "); // is this correct - jlim?
  237. return 1;
  238. }
  239. function CreateSequence($seqname = 'adodbseq', $startID = 1)
  240. {
  241. $ok = $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
  242. if (!$ok) return false;
  243. return $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
  244. }
  245. function DropSequence($seqname = 'adodbseq')
  246. {
  247. $seqname = strtoupper($seqname);
  248. $this->Execute("delete from RDB\$GENERATORS where RDB\$GENERATOR_NAME='$seqname'");
  249. }
  250. function GenID($seqname='adodbseq',$startID=1)
  251. {
  252. $getnext = ("SELECT Gen_ID($seqname,1) FROM RDB\$DATABASE");
  253. $rs = @$this->Execute($getnext);
  254. if (!$rs) {
  255. $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
  256. $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
  257. $rs = $this->Execute($getnext);
  258. }
  259. if ($rs && !$rs->EOF) {
  260. $this->genID = (integer) reset($rs->fields);
  261. }
  262. else {
  263. $this->genID = 0; // false
  264. }
  265. if ($rs) {
  266. $rs->Close();
  267. }
  268. return $this->genID;
  269. }
  270. function SelectDB($dbName)
  271. {
  272. return false;
  273. }
  274. function _handleerror()
  275. {
  276. $this->_errorMsg = ibase_errmsg();
  277. }
  278. function ErrorNo()
  279. {
  280. if (preg_match('/error code = ([\-0-9]*)/i', $this->_errorMsg,$arr)) return (integer) $arr[1];
  281. else return 0;
  282. }
  283. function ErrorMsg()
  284. {
  285. return $this->_errorMsg;
  286. }
  287. function Prepare($sql)
  288. {
  289. $stmt = ibase_prepare($this->_connectionID,$sql);
  290. if (!$stmt) return false;
  291. return array($sql,$stmt);
  292. }
  293. // returns query ID if successful, otherwise false
  294. // there have been reports of problems with nested queries - the code is probably not re-entrant?
  295. function _query($sql,$iarr=false)
  296. {
  297. if (!$this->autoCommit && $this->_transactionID) {
  298. $conn = $this->_transactionID;
  299. $docommit = false;
  300. } else {
  301. $conn = $this->_connectionID;
  302. $docommit = true;
  303. }
  304. if (is_array($sql)) {
  305. $fn = 'ibase_execute';
  306. $sql = $sql[1];
  307. if (is_array($iarr)) {
  308. if (ADODB_PHPVER >= 0x4050) { // actually 4.0.4
  309. if ( !isset($iarr[0]) ) $iarr[0] = ''; // PHP5 compat hack
  310. $fnarr = array_merge( array($sql) , $iarr);
  311. $ret = call_user_func_array($fn,$fnarr);
  312. } else {
  313. switch(sizeof($iarr)) {
  314. case 1: $ret = $fn($sql,$iarr[0]); break;
  315. case 2: $ret = $fn($sql,$iarr[0],$iarr[1]); break;
  316. case 3: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2]); break;
  317. case 4: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]); break;
  318. case 5: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]); break;
  319. case 6: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]); break;
  320. case 7: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]); break;
  321. default: ADOConnection::outp( "Too many parameters to ibase query $sql");
  322. case 8: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]); break;
  323. }
  324. }
  325. } else $ret = $fn($sql);
  326. } else {
  327. $fn = 'ibase_query';
  328. if (is_array($iarr)) {
  329. if (ADODB_PHPVER >= 0x4050) { // actually 4.0.4
  330. if (sizeof($iarr) == 0) $iarr[0] = ''; // PHP5 compat hack
  331. $fnarr = array_merge( array($conn,$sql) , $iarr);
  332. $ret = call_user_func_array($fn,$fnarr);
  333. } else {
  334. switch(sizeof($iarr)) {
  335. case 1: $ret = $fn($conn,$sql,$iarr[0]); break;
  336. case 2: $ret = $fn($conn,$sql,$iarr[0],$iarr[1]); break;
  337. case 3: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2]); break;
  338. case 4: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]); break;
  339. case 5: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]); break;
  340. case 6: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]); break;
  341. case 7: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]); break;
  342. default: ADOConnection::outp( "Too many parameters to ibase query $sql");
  343. case 8: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]); break;
  344. }
  345. }
  346. } else $ret = $fn($conn,$sql);
  347. }
  348. if ($docommit && $ret === true) {
  349. ibase_commit($this->_connectionID);
  350. }
  351. $this->_handleerror();
  352. return $ret;
  353. }
  354. // returns true or false
  355. function _close()
  356. {
  357. if (!$this->autoCommit) {
  358. @ibase_rollback($this->_connectionID);
  359. }
  360. return @ibase_close($this->_connectionID);
  361. }
  362. //OPN STUFF start
  363. function _ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $dialect3)
  364. {
  365. $fscale = abs($fscale);
  366. $fld->max_length = $flen;
  367. $fld->scale = null;
  368. switch($ftype){
  369. case 7:
  370. case 8:
  371. if ($dialect3) {
  372. switch($fsubtype){
  373. case 0:
  374. $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
  375. break;
  376. case 1:
  377. $fld->type = 'numeric';
  378. $fld->max_length = $fprecision;
  379. $fld->scale = $fscale;
  380. break;
  381. case 2:
  382. $fld->type = 'decimal';
  383. $fld->max_length = $fprecision;
  384. $fld->scale = $fscale;
  385. break;
  386. } // switch
  387. } else {
  388. if ($fscale !=0) {
  389. $fld->type = 'decimal';
  390. $fld->scale = $fscale;
  391. $fld->max_length = ($ftype == 7 ? 4 : 9);
  392. } else {
  393. $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
  394. }
  395. }
  396. break;
  397. case 16:
  398. if ($dialect3) {
  399. switch($fsubtype){
  400. case 0:
  401. $fld->type = 'decimal';
  402. $fld->max_length = 18;
  403. $fld->scale = 0;
  404. break;
  405. case 1:
  406. $fld->type = 'numeric';
  407. $fld->max_length = $fprecision;
  408. $fld->scale = $fscale;
  409. break;
  410. case 2:
  411. $fld->type = 'decimal';
  412. $fld->max_length = $fprecision;
  413. $fld->scale = $fscale;
  414. break;
  415. } // switch
  416. }
  417. break;
  418. case 10:
  419. $fld->type = 'float';
  420. break;
  421. case 14:
  422. $fld->type = 'char';
  423. break;
  424. case 27:
  425. if ($fscale !=0) {
  426. $fld->type = 'decimal';
  427. $fld->max_length = 15;
  428. $fld->scale = 5;
  429. } else {
  430. $fld->type = 'double';
  431. }
  432. break;
  433. case 35:
  434. if ($dialect3) {
  435. $fld->type = 'timestamp';
  436. } else {
  437. $fld->type = 'date';
  438. }
  439. break;
  440. case 12:
  441. $fld->type = 'date';
  442. break;
  443. case 13:
  444. $fld->type = 'time';
  445. break;
  446. case 37:
  447. $fld->type = 'varchar';
  448. break;
  449. case 40:
  450. $fld->type = 'cstring';
  451. break;
  452. case 261:
  453. $fld->type = 'blob';
  454. $fld->max_length = -1;
  455. break;
  456. } // switch
  457. }
  458. //OPN STUFF end
  459. // returns array of ADOFieldObjects for current table
  460. function MetaColumns($table, $normalize=true)
  461. {
  462. global $ADODB_FETCH_MODE;
  463. $save = $ADODB_FETCH_MODE;
  464. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  465. $rs = $this->Execute(sprintf($this->metaColumnsSQL,strtoupper($table)));
  466. $ADODB_FETCH_MODE = $save;
  467. $false = false;
  468. if ($rs === false) {
  469. return $false;
  470. }
  471. $retarr = array();
  472. //OPN STUFF start
  473. $dialect3 = ($this->dialect==3 ? true : false);
  474. //OPN STUFF end
  475. while (!$rs->EOF) { //print_r($rs->fields);
  476. $fld = new ADOFieldObject();
  477. $fld->name = trim($rs->fields[0]);
  478. //OPN STUFF start
  479. $this->_ConvertFieldType($fld, $rs->fields[7], $rs->fields[3], $rs->fields[4], $rs->fields[5], $rs->fields[6], $dialect3);
  480. if (isset($rs->fields[1]) && $rs->fields[1]) {
  481. $fld->not_null = true;
  482. }
  483. if (isset($rs->fields[2])) {
  484. $fld->has_default = true;
  485. $d = substr($rs->fields[2],strlen('default '));
  486. switch ($fld->type)
  487. {
  488. case 'smallint':
  489. case 'integer': $fld->default_value = (int) $d; break;
  490. case 'char':
  491. case 'blob':
  492. case 'text':
  493. case 'varchar': $fld->default_value = (string) substr($d,1,strlen($d)-2); break;
  494. case 'double':
  495. case 'float': $fld->default_value = (float) $d; break;
  496. default: $fld->default_value = $d; break;
  497. }
  498. // case 35:$tt = 'TIMESTAMP'; break;
  499. }
  500. if ((isset($rs->fields[5])) && ($fld->type == 'blob')) {
  501. $fld->sub_type = $rs->fields[5];
  502. } else {
  503. $fld->sub_type = null;
  504. }
  505. //OPN STUFF end
  506. if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
  507. else $retarr[strtoupper($fld->name)] = $fld;
  508. $rs->MoveNext();
  509. }
  510. $rs->Close();
  511. if ( empty($retarr)) return $false;
  512. else return $retarr;
  513. }
  514. function BlobEncode( $blob )
  515. {
  516. $blobid = ibase_blob_create( $this->_connectionID);
  517. ibase_blob_add( $blobid, $blob );
  518. return ibase_blob_close( $blobid );
  519. }
  520. // since we auto-decode all blob's since 2.42,
  521. // BlobDecode should not do any transforms
  522. function BlobDecode($blob)
  523. {
  524. return $blob;
  525. }
  526. // old blobdecode function
  527. // still used to auto-decode all blob's
  528. function _BlobDecode_old( $blob )
  529. {
  530. $blobid = ibase_blob_open($this->_connectionID, $blob );
  531. $realblob = ibase_blob_get( $blobid,$this->maxblobsize); // 2nd param is max size of blob -- Kevin Boillet <kevinboillet@yahoo.fr>
  532. while($string = ibase_blob_get($blobid, 8192)){
  533. $realblob .= $string;
  534. }
  535. ibase_blob_close( $blobid );
  536. return( $realblob );
  537. }
  538. function _BlobDecode( $blob )
  539. {
  540. if (ADODB_PHPVER >= 0x5000) {
  541. $blob_data = ibase_blob_info($this->_connectionID, $blob );
  542. $blobid = ibase_blob_open($this->_connectionID, $blob );
  543. } else {
  544. $blob_data = ibase_blob_info( $blob );
  545. $blobid = ibase_blob_open( $blob );
  546. }
  547. if( $blob_data[0] > $this->maxblobsize ) {
  548. $realblob = ibase_blob_get($blobid, $this->maxblobsize);
  549. while($string = ibase_blob_get($blobid, 8192)){
  550. $realblob .= $string;
  551. }
  552. } else {
  553. $realblob = ibase_blob_get($blobid, $blob_data[0]);
  554. }
  555. ibase_blob_close( $blobid );
  556. return( $realblob );
  557. }
  558. function UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB')
  559. {
  560. $fd = fopen($path,'rb');
  561. if ($fd === false) return false;
  562. $blob_id = ibase_blob_create($this->_connectionID);
  563. /* fill with data */
  564. while ($val = fread($fd,32768)){
  565. ibase_blob_add($blob_id, $val);
  566. }
  567. /* close and get $blob_id_str for inserting into table */
  568. $blob_id_str = ibase_blob_close($blob_id);
  569. fclose($fd);
  570. return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
  571. }
  572. /*
  573. Insert a null into the blob field of the table first.
  574. Then use UpdateBlob to store the blob.
  575. Usage:
  576. $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
  577. $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
  578. */
  579. function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
  580. {
  581. $blob_id = ibase_blob_create($this->_connectionID);
  582. // ibase_blob_add($blob_id, $val);
  583. // replacement that solves the problem by which only the first modulus 64K /
  584. // of $val are stored at the blob field ////////////////////////////////////
  585. // Thx Abel Berenstein aberenstein#afip.gov.ar
  586. $len = strlen($val);
  587. $chunk_size = 32768;
  588. $tail_size = $len % $chunk_size;
  589. $n_chunks = ($len - $tail_size) / $chunk_size;
  590. for ($n = 0; $n < $n_chunks; $n++) {
  591. $start = $n * $chunk_size;
  592. $data = substr($val, $start, $chunk_size);
  593. ibase_blob_add($blob_id, $data);
  594. }
  595. if ($tail_size) {
  596. $start = $n_chunks * $chunk_size;
  597. $data = substr($val, $start, $tail_size);
  598. ibase_blob_add($blob_id, $data);
  599. }
  600. // end replacement /////////////////////////////////////////////////////////
  601. $blob_id_str = ibase_blob_close($blob_id);
  602. return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
  603. }
  604. function OldUpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
  605. {
  606. $blob_id = ibase_blob_create($this->_connectionID);
  607. ibase_blob_add($blob_id, $val);
  608. $blob_id_str = ibase_blob_close($blob_id);
  609. return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
  610. }
  611. // Format date column in sql string given an input format that understands Y M D
  612. // Only since Interbase 6.0 - uses EXTRACT
  613. // problem - does not zero-fill the day and month yet
  614. function SQLDate($fmt, $col=false)
  615. {
  616. if (!$col) $col = $this->sysDate;
  617. $s = '';
  618. $len = strlen($fmt);
  619. for ($i=0; $i < $len; $i++) {
  620. if ($s) $s .= '||';
  621. $ch = $fmt[$i];
  622. switch($ch) {
  623. case 'Y':
  624. case 'y':
  625. $s .= "extract(year from $col)";
  626. break;
  627. case 'M':
  628. case 'm':
  629. $s .= "extract(month from $col)";
  630. break;
  631. case 'Q':
  632. case 'q':
  633. $s .= "cast(((extract(month from $col)+2) / 3) as integer)";
  634. break;
  635. case 'D':
  636. case 'd':
  637. $s .= "(extract(day from $col))";
  638. break;
  639. case 'H':
  640. case 'h':
  641. $s .= "(extract(hour from $col))";
  642. break;
  643. case 'I':
  644. case 'i':
  645. $s .= "(extract(minute from $col))";
  646. break;
  647. case 'S':
  648. case 's':
  649. $s .= "CAST((extract(second from $col)) AS INTEGER)";
  650. break;
  651. default:
  652. if ($ch == '\\') {
  653. $i++;
  654. $ch = substr($fmt,$i,1);
  655. }
  656. $s .= $this->qstr($ch);
  657. break;
  658. }
  659. }
  660. return $s;
  661. }
  662. }
  663. /*--------------------------------------------------------------------------------------
  664. Class Name: Recordset
  665. --------------------------------------------------------------------------------------*/
  666. class ADORecordset_ibase extends ADORecordSet
  667. {
  668. var $databaseType = "ibase";
  669. var $bind=false;
  670. var $_cacheType;
  671. function __construct($id,$mode=false)
  672. {
  673. global $ADODB_FETCH_MODE;
  674. $this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
  675. parent::__construct($id);
  676. }
  677. /* Returns: an object containing field information.
  678. Get column information in the Recordset object. fetchField() can be used in order to obtain information about
  679. fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
  680. fetchField() is retrieved. */
  681. function FetchField($fieldOffset = -1)
  682. {
  683. $fld = new ADOFieldObject;
  684. $ibf = ibase_field_info($this->_queryID,$fieldOffset);
  685. $name = empty($ibf['alias']) ? $ibf['name'] : $ibf['alias'];
  686. switch (ADODB_ASSOC_CASE) {
  687. case ADODB_ASSOC_CASE_UPPER:
  688. $fld->name = strtoupper($name);
  689. break;
  690. case ADODB_ASSOC_CASE_LOWER:
  691. $fld->name = strtolower($name);
  692. break;
  693. case ADODB_ASSOC_CASE_NATIVE:
  694. default:
  695. $fld->name = $name;
  696. break;
  697. }
  698. $fld->type = $ibf['type'];
  699. $fld->max_length = $ibf['length'];
  700. /* This needs to be populated from the metadata */
  701. $fld->not_null = false;
  702. $fld->has_default = false;
  703. $fld->default_value = 'null';
  704. return $fld;
  705. }
  706. function _initrs()
  707. {
  708. $this->_numOfRows = -1;
  709. $this->_numOfFields = @ibase_num_fields($this->_queryID);
  710. // cache types for blob decode check
  711. for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
  712. $f1 = $this->FetchField($i);
  713. $this->_cacheType[] = $f1->type;
  714. }
  715. }
  716. function _seek($row)
  717. {
  718. return false;
  719. }
  720. function _fetch()
  721. {
  722. $f = @ibase_fetch_row($this->_queryID);
  723. if ($f === false) {
  724. $this->fields = false;
  725. return false;
  726. }
  727. // OPN stuff start - optimized
  728. // fix missing nulls and decode blobs automatically
  729. global $ADODB_ANSI_PADDING_OFF;
  730. //$ADODB_ANSI_PADDING_OFF=1;
  731. $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
  732. for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
  733. if ($this->_cacheType[$i]=="BLOB") {
  734. if (isset($f[$i])) {
  735. $f[$i] = $this->connection->_BlobDecode($f[$i]);
  736. } else {
  737. $f[$i] = null;
  738. }
  739. } else {
  740. if (!isset($f[$i])) {
  741. $f[$i] = null;
  742. } else if ($rtrim && is_string($f[$i])) {
  743. $f[$i] = rtrim($f[$i]);
  744. }
  745. }
  746. }
  747. // OPN stuff end
  748. $this->fields = $f;
  749. if ($this->fetchMode == ADODB_FETCH_ASSOC) {
  750. $this->fields = $this->GetRowAssoc();
  751. } else if ($this->fetchMode == ADODB_FETCH_BOTH) {
  752. $this->fields = array_merge($this->fields,$this->GetRowAssoc());
  753. }
  754. return true;
  755. }
  756. /* Use associative array to get fields array */
  757. function Fields($colname)
  758. {
  759. if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
  760. if (!$this->bind) {
  761. $this->bind = array();
  762. for ($i=0; $i < $this->_numOfFields; $i++) {
  763. $o = $this->FetchField($i);
  764. $this->bind[strtoupper($o->name)] = $i;
  765. }
  766. }
  767. return $this->fields[$this->bind[strtoupper($colname)]];
  768. }
  769. function _close()
  770. {
  771. return @ibase_free_result($this->_queryID);
  772. }
  773. function MetaType($t,$len=-1,$fieldobj=false)
  774. {
  775. if (is_object($t)) {
  776. $fieldobj = $t;
  777. $t = $fieldobj->type;
  778. $len = $fieldobj->max_length;
  779. }
  780. switch (strtoupper($t)) {
  781. case 'CHAR':
  782. return 'C';
  783. case 'TEXT':
  784. case 'VARCHAR':
  785. case 'VARYING':
  786. if ($len <= $this->blobSize) return 'C';
  787. return 'X';
  788. case 'BLOB':
  789. return 'B';
  790. case 'TIMESTAMP':
  791. case 'DATE': return 'D';
  792. case 'TIME': return 'T';
  793. //case 'T': return 'T';
  794. //case 'L': return 'L';
  795. case 'INT':
  796. case 'SHORT':
  797. case 'INTEGER': return 'I';
  798. default: return 'N';
  799. }
  800. }
  801. }