PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/adodb/drivers/adodb-db2.inc.php

https://github.com/telmomenezes/bloxx
PHP | 361 lines | 246 code | 22 blank | 93 comment | 42 complexity | 24d40f010114e050b0dae11c0ffb202b MD5 | raw file
  1. <?php
  2. /*
  3. V4.64 20 June 2005 (c) 2000-2005 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. Set tabs to 4 for best viewing.
  8. Latest version is available at http://adodb.sourceforge.net
  9. DB2 data driver. Requires ODBC.
  10. From phpdb list:
  11. Hi Andrew,
  12. thanks a lot for your help. Today we discovered what
  13. our real problem was:
  14. After "playing" a little bit with the php-scripts that try
  15. to connect to the IBM DB2, we set the optional parameter
  16. Cursortype when calling odbc_pconnect(....).
  17. And the exciting thing: When we set the cursor type
  18. to SQL_CUR_USE_ODBC Cursor Type, then
  19. the whole query speed up from 1 till 10 seconds
  20. to 0.2 till 0.3 seconds for 100 records. Amazing!!!
  21. Therfore, PHP is just almost fast as calling the DB2
  22. from Servlets using JDBC (don't take too much care
  23. about the speed at whole: the database was on a
  24. completely other location, so the whole connection
  25. was made over a slow network connection).
  26. I hope this helps when other encounter the same
  27. problem when trying to connect to DB2 from
  28. PHP.
  29. Kind regards,
  30. Christian Szardenings
  31. 2 Oct 2001
  32. Mark Newnham has discovered that the SQL_CUR_USE_ODBC is not supported by
  33. IBM's DB2 ODBC driver, so this must be a 3rd party ODBC driver.
  34. From the IBM CLI Reference:
  35. SQL_ATTR_ODBC_CURSORS (DB2 CLI v5)
  36. This connection attribute is defined by ODBC, but is not supported by DB2
  37. CLI. Any attempt to set or get this attribute will result in an SQLSTATE of
  38. HYC00 (Driver not capable).
  39. A 32-bit option specifying how the Driver Manager uses the ODBC cursor
  40. library.
  41. So I guess this means the message [above] was related to using a 3rd party
  42. odbc driver.
  43. Setting SQL_CUR_USE_ODBC
  44. ========================
  45. To set SQL_CUR_USE_ODBC for drivers that require it, do this:
  46. $db = NewADOConnection('db2');
  47. $db->curMode = SQL_CUR_USE_ODBC;
  48. $db->Connect($dsn, $userid, $pwd);
  49. USING CLI INTERFACE
  50. ===================
  51. I have had reports that the $host and $database params have to be reversed in
  52. Connect() when using the CLI interface. From Halmai Csongor csongor.halmai#nexum.hu:
  53. > The symptom is that if I change the database engine from postgres or any other to DB2 then the following
  54. > connection command becomes wrong despite being described this version to be correct in the docs.
  55. >
  56. > $connection_object->Connect( $DATABASE_HOST, $DATABASE_AUTH_USER_NAME, $DATABASE_AUTH_PASSWORD, $DATABASE_NAME )
  57. >
  58. > In case of DB2 I had to swap the first and last arguments in order to connect properly.
  59. */
  60. // security - hide paths
  61. if (!defined('ADODB_DIR')) die();
  62. if (!defined('_ADODB_ODBC_LAYER')) {
  63. include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
  64. }
  65. if (!defined('ADODB_DB2')){
  66. define('ADODB_DB2',1);
  67. class ADODB_DB2 extends ADODB_odbc {
  68. var $databaseType = "db2";
  69. var $concat_operator = '||';
  70. var $sysDate = 'CURRENT_DATE';
  71. var $sysTimeStamp = 'CURRENT TIMESTAMP';
  72. // The complete string representation of a timestamp has the form
  73. // yyyy-mm-dd-hh.mm.ss.nnnnnn.
  74. var $fmtTimeStamp = "'Y-m-d-H.i.s'";
  75. var $ansiOuter = true;
  76. var $identitySQL = 'values IDENTITY_VAL_LOCAL()';
  77. var $_bindInputArray = true;
  78. var $hasInsertID = true;
  79. function ADODB_DB2()
  80. {
  81. if (strncmp(PHP_OS,'WIN',3) === 0) $this->curmode = SQL_CUR_USE_ODBC;
  82. $this->ADODB_odbc();
  83. }
  84. function IfNull( $field, $ifNull )
  85. {
  86. return " COALESCE($field, $ifNull) "; // if DB2 UDB
  87. }
  88. function ServerInfo()
  89. {
  90. //odbc_setoption($this->_connectionID,1,101 /*SQL_ATTR_ACCESS_MODE*/, 1 /*SQL_MODE_READ_ONLY*/);
  91. $vers = $this->GetOne('select versionnumber from sysibm.sysversions');
  92. //odbc_setoption($this->_connectionID,1,101, 0 /*SQL_MODE_READ_WRITE*/);
  93. return array('description'=>'DB2 ODBC driver', 'version'=>$vers);
  94. }
  95. function _insertid()
  96. {
  97. return $this->GetOne($this->identitySQL);
  98. }
  99. function RowLock($tables,$where,$flds='1 as ignore')
  100. {
  101. if ($this->_autocommit) $this->BeginTrans();
  102. return $this->GetOne("select $flds from $tables where $where for update");
  103. }
  104. function &MetaTables($ttype=false,$showSchema=false, $qtable="%", $qschema="%")
  105. {
  106. global $ADODB_FETCH_MODE;
  107. $savem = $ADODB_FETCH_MODE;
  108. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  109. $qid = odbc_tables($this->_connectionID, "", $qschema, $qtable, "");
  110. $rs = new ADORecordSet_odbc($qid);
  111. $ADODB_FETCH_MODE = $savem;
  112. if (!$rs) {
  113. $false = false;
  114. return $false;
  115. }
  116. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  117. $arr =& $rs->GetArray();
  118. //print_r($arr);
  119. $rs->Close();
  120. $arr2 = array();
  121. if ($ttype) {
  122. $isview = strncmp($ttype,'V',1) === 0;
  123. }
  124. for ($i=0; $i < sizeof($arr); $i++) {
  125. if (!$arr[$i][2]) continue;
  126. if (strncmp($arr[$i][1],'SYS',3) === 0) continue;
  127. $type = $arr[$i][3];
  128. if ($showSchema) $arr[$i][2] = $arr[$i][1].'.'.$arr[$i][2];
  129. if ($ttype) {
  130. if ($isview) {
  131. if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
  132. } else if (strncmp($type,'T',1) === 0) $arr2[] = $arr[$i][2];
  133. } else if (strncmp($type,'S',1) !== 0) $arr2[] = $arr[$i][2];
  134. }
  135. return $arr2;
  136. }
  137. function &MetaIndexes ($table, $primary = FALSE, $owner=false)
  138. {
  139. // save old fetch mode
  140. global $ADODB_FETCH_MODE;
  141. $save = $ADODB_FETCH_MODE;
  142. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  143. if ($this->fetchMode !== FALSE) {
  144. $savem = $this->SetFetchMode(FALSE);
  145. }
  146. $false = false;
  147. // get index details
  148. $table = strtoupper($table);
  149. $SQL="SELECT NAME, UNIQUERULE, COLNAMES FROM SYSIBM.SYSINDEXES WHERE TBNAME='$table'";
  150. if ($primary)
  151. $SQL.= " AND UNIQUERULE='P'";
  152. $rs = $this->Execute($SQL);
  153. if (!is_object($rs)) {
  154. if (isset($savem))
  155. $this->SetFetchMode($savem);
  156. $ADODB_FETCH_MODE = $save;
  157. return $false;
  158. }
  159. $indexes = array ();
  160. // parse index data into array
  161. while ($row = $rs->FetchRow()) {
  162. $indexes[$row[0]] = array(
  163. 'unique' => ($row[1] == 'U' || $row[1] == 'P'),
  164. 'columns' => array()
  165. );
  166. $cols = ltrim($row[2],'+');
  167. $indexes[$row[0]]['columns'] = explode('+', $cols);
  168. }
  169. if (isset($savem)) {
  170. $this->SetFetchMode($savem);
  171. $ADODB_FETCH_MODE = $save;
  172. }
  173. return $indexes;
  174. }
  175. // Format date column in sql string given an input format that understands Y M D
  176. function SQLDate($fmt, $col=false)
  177. {
  178. // use right() and replace() ?
  179. if (!$col) $col = $this->sysDate;
  180. $s = '';
  181. $len = strlen($fmt);
  182. for ($i=0; $i < $len; $i++) {
  183. if ($s) $s .= '||';
  184. $ch = $fmt[$i];
  185. switch($ch) {
  186. case 'Y':
  187. case 'y':
  188. $s .= "char(year($col))";
  189. break;
  190. case 'M':
  191. $s .= "substr(monthname($col),1,3)";
  192. break;
  193. case 'm':
  194. $s .= "right(digits(month($col)),2)";
  195. break;
  196. case 'D':
  197. case 'd':
  198. $s .= "right(digits(day($col)),2)";
  199. break;
  200. case 'H':
  201. case 'h':
  202. if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)";
  203. else $s .= "''";
  204. break;
  205. case 'i':
  206. case 'I':
  207. if ($col != $this->sysDate)
  208. $s .= "right(digits(minute($col)),2)";
  209. else $s .= "''";
  210. break;
  211. case 'S':
  212. case 's':
  213. if ($col != $this->sysDate)
  214. $s .= "right(digits(second($col)),2)";
  215. else $s .= "''";
  216. break;
  217. default:
  218. if ($ch == '\\') {
  219. $i++;
  220. $ch = substr($fmt,$i,1);
  221. }
  222. $s .= $this->qstr($ch);
  223. }
  224. }
  225. return $s;
  226. }
  227. function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputArr=false)
  228. {
  229. if ($offset <= 0) {
  230. // could also use " OPTIMIZE FOR $nrows ROWS "
  231. if ($nrows >= 0) $sql .= " FETCH FIRST $nrows ROWS ONLY ";
  232. $rs =& $this->Execute($sql,$inputArr);
  233. } else {
  234. if ($offset > 0 && $nrows < 0);
  235. else {
  236. $nrows += $offset;
  237. $sql .= " FETCH FIRST $nrows ROWS ONLY ";
  238. }
  239. $rs =& ADOConnection::SelectLimit($sql,-1,$offset,$inputArr);
  240. }
  241. return $rs;
  242. }
  243. };
  244. class ADORecordSet_db2 extends ADORecordSet_odbc {
  245. var $databaseType = "db2";
  246. function ADORecordSet_db2($id,$mode=false)
  247. {
  248. $this->ADORecordSet_odbc($id,$mode);
  249. }
  250. function MetaType($t,$len=-1,$fieldobj=false)
  251. {
  252. if (is_object($t)) {
  253. $fieldobj = $t;
  254. $t = $fieldobj->type;
  255. $len = $fieldobj->max_length;
  256. }
  257. switch (strtoupper($t)) {
  258. case 'VARCHAR':
  259. case 'CHAR':
  260. case 'CHARACTER':
  261. case 'C':
  262. if ($len <= $this->blobSize) return 'C';
  263. case 'LONGCHAR':
  264. case 'TEXT':
  265. case 'CLOB':
  266. case 'DBCLOB': // double-byte
  267. case 'X':
  268. return 'X';
  269. case 'BLOB':
  270. case 'GRAPHIC':
  271. case 'VARGRAPHIC':
  272. return 'B';
  273. case 'DATE':
  274. case 'D':
  275. return 'D';
  276. case 'TIME':
  277. case 'TIMESTAMP':
  278. case 'T':
  279. return 'T';
  280. //case 'BOOLEAN':
  281. //case 'BIT':
  282. // return 'L';
  283. //case 'COUNTER':
  284. // return 'R';
  285. case 'INT':
  286. case 'INTEGER':
  287. case 'BIGINT':
  288. case 'SMALLINT':
  289. case 'I':
  290. return 'I';
  291. default: return 'N';
  292. }
  293. }
  294. }
  295. } //define
  296. ?>