PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/include/adodb/drivers/adodb-informix72.inc.php

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