adodb-informix72.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. /*
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim. All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4 for best viewing.
  10. Latest version is available at http://adodb.org/
  11. Informix port by Mitchell T. Young (mitch@youngfamily.org)
  12. Further mods by "Samuel CARRIERE" <samuel_carriere@hotmail.com>
  13. */
  14. // security - hide paths
  15. if (!defined('ADODB_DIR')) die();
  16. if (!defined('IFX_SCROLL')) define('IFX_SCROLL',1);
  17. class ADODB_informix72 extends ADOConnection {
  18. var $databaseType = "informix72";
  19. var $dataProvider = "informix";
  20. var $replaceQuote = "''"; // string to use to replace quotes
  21. var $fmtDate = "'Y-m-d'";
  22. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  23. var $hasInsertID = true;
  24. var $hasAffectedRows = true;
  25. var $substr = 'substr';
  26. var $metaTablesSQL="select tabname,tabtype from systables where tabtype in ('T','V') and owner!='informix'"; //Don't get informix tables and pseudo-tables
  27. var $metaColumnsSQL =
  28. "select c.colname, c.coltype, c.collength, d.default,c.colno
  29. from syscolumns c, systables t,outer sysdefaults d
  30. where c.tabid=t.tabid and d.tabid=t.tabid and d.colno=c.colno
  31. and tabname='%s' order by c.colno";
  32. var $metaPrimaryKeySQL =
  33. "select part1,part2,part3,part4,part5,part6,part7,part8 from
  34. systables t,sysconstraints s,sysindexes i where t.tabname='%s'
  35. and s.tabid=t.tabid and s.constrtype='P'
  36. and i.idxname=s.idxname";
  37. var $concat_operator = '||';
  38. var $lastQuery = false;
  39. var $has_insertid = true;
  40. var $_autocommit = true;
  41. var $_bindInputArray = true; // set to true if ADOConnection.Execute() permits binding of array parameters.
  42. var $sysDate = 'TODAY';
  43. var $sysTimeStamp = 'CURRENT';
  44. var $cursorType = IFX_SCROLL; // IFX_SCROLL or IFX_HOLD or 0
  45. function __construct()
  46. {
  47. // alternatively, use older method:
  48. //putenv("DBDATE=Y4MD-");
  49. // force ISO date format
  50. putenv('GL_DATE=%Y-%m-%d');
  51. if (function_exists('ifx_byteasvarchar')) {
  52. ifx_byteasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
  53. ifx_textasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
  54. ifx_blobinfile_mode(0); // Mode "0" means save Byte-Blobs in memory, and mode "1" means save Byte-Blobs in a file.
  55. }
  56. }
  57. function ServerInfo()
  58. {
  59. if (isset($this->version)) return $this->version;
  60. $arr['description'] = $this->GetOne("select DBINFO('version','full') from systables where tabid = 1");
  61. $arr['version'] = $this->GetOne("select DBINFO('version','major') || DBINFO('version','minor') from systables where tabid = 1");
  62. $this->version = $arr;
  63. return $arr;
  64. }
  65. function _insertid()
  66. {
  67. $sqlca =ifx_getsqlca($this->lastQuery);
  68. return @$sqlca["sqlerrd1"];
  69. }
  70. function _affectedrows()
  71. {
  72. if ($this->lastQuery) {
  73. return @ifx_affected_rows ($this->lastQuery);
  74. }
  75. return 0;
  76. }
  77. function BeginTrans()
  78. {
  79. if ($this->transOff) return true;
  80. $this->transCnt += 1;
  81. $this->Execute('BEGIN');
  82. $this->_autocommit = false;
  83. return true;
  84. }
  85. function CommitTrans($ok=true)
  86. {
  87. if (!$ok) return $this->RollbackTrans();
  88. if ($this->transOff) return true;
  89. if ($this->transCnt) $this->transCnt -= 1;
  90. $this->Execute('COMMIT');
  91. $this->_autocommit = true;
  92. return true;
  93. }
  94. function RollbackTrans()
  95. {
  96. if ($this->transOff) return true;
  97. if ($this->transCnt) $this->transCnt -= 1;
  98. $this->Execute('ROLLBACK');
  99. $this->_autocommit = true;
  100. return true;
  101. }
  102. function RowLock($tables,$where,$col='1 as adodbignore')
  103. {
  104. if ($this->_autocommit) $this->BeginTrans();
  105. return $this->GetOne("select $col from $tables where $where for update");
  106. }
  107. /* Returns: the last error message from previous database operation
  108. Note: This function is NOT available for Microsoft SQL Server. */
  109. function ErrorMsg()
  110. {
  111. if (!empty($this->_logsql)) return $this->_errorMsg;
  112. $this->_errorMsg = ifx_errormsg();
  113. return $this->_errorMsg;
  114. }
  115. function ErrorNo()
  116. {
  117. preg_match("/.*SQLCODE=([^\]]*)/",ifx_error(),$parse);
  118. if (is_array($parse) && isset($parse[1])) return (int)$parse[1];
  119. return 0;
  120. }
  121. function MetaProcedures($NamePattern = false, $catalog = null, $schemaPattern = null)
  122. {
  123. // save old fetch mode
  124. global $ADODB_FETCH_MODE;
  125. $false = false;
  126. $save = $ADODB_FETCH_MODE;
  127. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  128. if ($this->fetchMode !== FALSE) {
  129. $savem = $this->SetFetchMode(FALSE);
  130. }
  131. $procedures = array ();
  132. // get index details
  133. $likepattern = '';
  134. if ($NamePattern) {
  135. $likepattern = " WHERE procname LIKE '".$NamePattern."'";
  136. }
  137. $rs = $this->Execute('SELECT procname, isproc FROM sysprocedures'.$likepattern);
  138. if (is_object($rs)) {
  139. // parse index data into array
  140. while ($row = $rs->FetchRow()) {
  141. $procedures[$row[0]] = array(
  142. 'type' => ($row[1] == 'f' ? 'FUNCTION' : 'PROCEDURE'),
  143. 'catalog' => '',
  144. 'schema' => '',
  145. 'remarks' => ''
  146. );
  147. }
  148. }
  149. // restore fetchmode
  150. if (isset($savem)) {
  151. $this->SetFetchMode($savem);
  152. }
  153. $ADODB_FETCH_MODE = $save;
  154. return $procedures;
  155. }
  156. function MetaColumns($table, $normalize=true)
  157. {
  158. global $ADODB_FETCH_MODE;
  159. $false = false;
  160. if (!empty($this->metaColumnsSQL)) {
  161. $save = $ADODB_FETCH_MODE;
  162. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  163. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  164. $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  165. if (isset($savem)) $this->SetFetchMode($savem);
  166. $ADODB_FETCH_MODE = $save;
  167. if ($rs === false) return $false;
  168. $rspkey = $this->Execute(sprintf($this->metaPrimaryKeySQL,$table)); //Added to get primary key colno items
  169. $retarr = array();
  170. while (!$rs->EOF) { //print_r($rs->fields);
  171. $fld = new ADOFieldObject();
  172. $fld->name = $rs->fields[0];
  173. /* //!eos.
  174. $rs->fields[1] is not the correct adodb type
  175. $rs->fields[2] is not correct max_length, because can include not-null bit
  176. $fld->type = $rs->fields[1];
  177. $fld->primary_key=$rspkey->fields && array_search($rs->fields[4],$rspkey->fields); //Added to set primary key flag
  178. $fld->max_length = $rs->fields[2];*/
  179. $pr=ifx_props($rs->fields[1],$rs->fields[2]); //!eos
  180. $fld->type = $pr[0] ;//!eos
  181. $fld->primary_key=$rspkey->fields && array_search($rs->fields[4],$rspkey->fields);
  182. $fld->max_length = $pr[1]; //!eos
  183. $fld->precision = $pr[2] ;//!eos
  184. $fld->not_null = $pr[3]=="N"; //!eos
  185. if (trim($rs->fields[3]) != "AAAAAA 0") {
  186. $fld->has_default = 1;
  187. $fld->default_value = $rs->fields[3];
  188. } else {
  189. $fld->has_default = 0;
  190. }
  191. $retarr[strtolower($fld->name)] = $fld;
  192. $rs->MoveNext();
  193. }
  194. $rs->Close();
  195. $rspkey->Close(); //!eos
  196. return $retarr;
  197. }
  198. return $false;
  199. }
  200. function xMetaColumns($table)
  201. {
  202. return ADOConnection::MetaColumns($table,false);
  203. }
  204. function MetaForeignKeys($table, $owner=false, $upper=false) //!Eos
  205. {
  206. $sql = "
  207. select tr.tabname,updrule,delrule,
  208. i.part1 o1,i2.part1 d1,i.part2 o2,i2.part2 d2,i.part3 o3,i2.part3 d3,i.part4 o4,i2.part4 d4,
  209. i.part5 o5,i2.part5 d5,i.part6 o6,i2.part6 d6,i.part7 o7,i2.part7 d7,i.part8 o8,i2.part8 d8
  210. from systables t,sysconstraints s,sysindexes i,
  211. sysreferences r,systables tr,sysconstraints s2,sysindexes i2
  212. where t.tabname='$table'
  213. and s.tabid=t.tabid and s.constrtype='R' and r.constrid=s.constrid
  214. and i.idxname=s.idxname and tr.tabid=r.ptabid
  215. and s2.constrid=r.primary and i2.idxname=s2.idxname";
  216. $rs = $this->Execute($sql);
  217. if (!$rs || $rs->EOF) return false;
  218. $arr = $rs->GetArray();
  219. $a = array();
  220. foreach($arr as $v) {
  221. $coldest=$this->metaColumnNames($v["tabname"]);
  222. $colorig=$this->metaColumnNames($table);
  223. $colnames=array();
  224. for($i=1;$i<=8 && $v["o$i"] ;$i++) {
  225. $colnames[]=$coldest[$v["d$i"]-1]."=".$colorig[$v["o$i"]-1];
  226. }
  227. if($upper)
  228. $a[strtoupper($v["tabname"])] = $colnames;
  229. else
  230. $a[$v["tabname"]] = $colnames;
  231. }
  232. return $a;
  233. }
  234. function UpdateBlob($table, $column, $val, $where, $blobtype = 'BLOB')
  235. {
  236. $type = ($blobtype == 'TEXT') ? 1 : 0;
  237. $blobid = ifx_create_blob($type,0,$val);
  238. return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blobid));
  239. }
  240. function BlobDecode($blobid)
  241. {
  242. return function_exists('ifx_byteasvarchar') ? $blobid : @ifx_get_blob($blobid);
  243. }
  244. // returns true or false
  245. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  246. {
  247. if (!function_exists('ifx_connect')) return null;
  248. $dbs = $argDatabasename . "@" . $argHostname;
  249. if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
  250. putenv("INFORMIXSERVER=".trim($argHostname));
  251. $this->_connectionID = ifx_connect($dbs,$argUsername,$argPassword);
  252. if ($this->_connectionID === false) return false;
  253. #if ($argDatabasename) return $this->SelectDB($argDatabasename);
  254. return true;
  255. }
  256. // returns true or false
  257. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  258. {
  259. if (!function_exists('ifx_connect')) return null;
  260. $dbs = $argDatabasename . "@" . $argHostname;
  261. putenv("INFORMIXSERVER=".trim($argHostname));
  262. $this->_connectionID = ifx_pconnect($dbs,$argUsername,$argPassword);
  263. if ($this->_connectionID === false) return false;
  264. #if ($argDatabasename) return $this->SelectDB($argDatabasename);
  265. return true;
  266. }
  267. /*
  268. // ifx_do does not accept bind parameters - weird ???
  269. function Prepare($sql)
  270. {
  271. $stmt = ifx_prepare($sql);
  272. if (!$stmt) return $sql;
  273. else return array($sql,$stmt);
  274. }
  275. */
  276. // returns query ID if successful, otherwise false
  277. function _query($sql,$inputarr=false)
  278. {
  279. global $ADODB_COUNTRECS;
  280. // String parameters have to be converted using ifx_create_char
  281. if ($inputarr) {
  282. foreach($inputarr as $v) {
  283. if (gettype($v) == 'string') {
  284. $tab[] = ifx_create_char($v);
  285. }
  286. else {
  287. $tab[] = $v;
  288. }
  289. }
  290. }
  291. // In case of select statement, we use a scroll cursor in order
  292. // to be able to call "move", or "movefirst" statements
  293. if (!$ADODB_COUNTRECS && preg_match("/^\s*select/is", $sql)) {
  294. if ($inputarr) {
  295. $this->lastQuery = ifx_query($sql,$this->_connectionID, $this->cursorType, $tab);
  296. }
  297. else {
  298. $this->lastQuery = ifx_query($sql,$this->_connectionID, $this->cursorType);
  299. }
  300. }
  301. else {
  302. if ($inputarr) {
  303. $this->lastQuery = ifx_query($sql,$this->_connectionID, $tab);
  304. }
  305. else {
  306. $this->lastQuery = ifx_query($sql,$this->_connectionID);
  307. }
  308. }
  309. // Following line have been commented because autocommit mode is
  310. // not supported by informix SE 7.2
  311. //if ($this->_autocommit) ifx_query('COMMIT',$this->_connectionID);
  312. return $this->lastQuery;
  313. }
  314. // returns true or false
  315. function _close()
  316. {
  317. $this->lastQuery = false;
  318. if($this->_connectionID) {
  319. return ifx_close($this->_connectionID);
  320. }
  321. return true;
  322. }
  323. }
  324. /*--------------------------------------------------------------------------------------
  325. Class Name: Recordset
  326. --------------------------------------------------------------------------------------*/
  327. class ADORecordset_informix72 extends ADORecordSet {
  328. var $databaseType = "informix72";
  329. var $canSeek = true;
  330. var $_fieldprops = false;
  331. function __construct($id,$mode=false)
  332. {
  333. if ($mode === false) {
  334. global $ADODB_FETCH_MODE;
  335. $mode = $ADODB_FETCH_MODE;
  336. }
  337. $this->fetchMode = $mode;
  338. return parent::__construct($id);
  339. }
  340. /* Returns: an object containing field information.
  341. Get column information in the Recordset object. fetchField() can be used in order to obtain information about
  342. fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
  343. fetchField() is retrieved. */
  344. function FetchField($fieldOffset = -1)
  345. {
  346. if (empty($this->_fieldprops)) {
  347. $fp = ifx_fieldproperties($this->_queryID);
  348. foreach($fp as $k => $v) {
  349. $o = new ADOFieldObject;
  350. $o->name = $k;
  351. $arr = explode(';',$v); //"SQLTYPE;length;precision;scale;ISNULLABLE"
  352. $o->type = $arr[0];
  353. $o->max_length = $arr[1];
  354. $this->_fieldprops[] = $o;
  355. $o->not_null = $arr[4]=="N";
  356. }
  357. }
  358. $ret = $this->_fieldprops[$fieldOffset];
  359. return $ret;
  360. }
  361. function _initrs()
  362. {
  363. $this->_numOfRows = -1; // ifx_affected_rows not reliable, only returns estimate -- ($ADODB_COUNTRECS)? ifx_affected_rows($this->_queryID):-1;
  364. $this->_numOfFields = ifx_num_fields($this->_queryID);
  365. }
  366. function _seek($row)
  367. {
  368. return @ifx_fetch_row($this->_queryID, (int) $row);
  369. }
  370. function MoveLast()
  371. {
  372. $this->fields = @ifx_fetch_row($this->_queryID, "LAST");
  373. if ($this->fields) $this->EOF = false;
  374. $this->_currentRow = -1;
  375. if ($this->fetchMode == ADODB_FETCH_NUM) {
  376. foreach($this->fields as $v) {
  377. $arr[] = $v;
  378. }
  379. $this->fields = $arr;
  380. }
  381. return true;
  382. }
  383. function MoveFirst()
  384. {
  385. $this->fields = @ifx_fetch_row($this->_queryID, "FIRST");
  386. if ($this->fields) $this->EOF = false;
  387. $this->_currentRow = 0;
  388. if ($this->fetchMode == ADODB_FETCH_NUM) {
  389. foreach($this->fields as $v) {
  390. $arr[] = $v;
  391. }
  392. $this->fields = $arr;
  393. }
  394. return true;
  395. }
  396. function _fetch($ignore_fields=false)
  397. {
  398. $this->fields = @ifx_fetch_row($this->_queryID);
  399. if (!is_array($this->fields)) return false;
  400. if ($this->fetchMode == ADODB_FETCH_NUM) {
  401. foreach($this->fields as $v) {
  402. $arr[] = $v;
  403. }
  404. $this->fields = $arr;
  405. }
  406. return true;
  407. }
  408. /* close() only needs to be called if you are worried about using too much memory while your script
  409. is running. All associated result memory for the specified result identifier will automatically be freed. */
  410. function _close()
  411. {
  412. if($this->_queryID) {
  413. return ifx_free_result($this->_queryID);
  414. }
  415. return true;
  416. }
  417. }
  418. /** !Eos
  419. * Auxiliar function to Parse coltype,collength. Used by Metacolumns
  420. * return: array ($mtype,$length,$precision,$nullable) (similar to ifx_fieldpropierties)
  421. */
  422. function ifx_props($coltype,$collength){
  423. $itype=fmod($coltype+1,256);
  424. $nullable=floor(($coltype+1) /256) ?"N":"Y";
  425. $mtype=substr(" CIIFFNNDN TBXCC ",$itype,1);
  426. switch ($itype){
  427. case 2:
  428. $length=4;
  429. case 6:
  430. case 9:
  431. case 14:
  432. $length=floor($collength/256);
  433. $precision=fmod($collength,256);
  434. break;
  435. default:
  436. $precision=0;
  437. $length=$collength;
  438. }
  439. return array($mtype,$length,$precision,$nullable);
  440. }