PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/include/phpgacl/adodb/drivers/adodb-ibase.inc.php

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