PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/adodb.493a/drivers/adodb-odbc_db2.inc.php

https://github.com/infused/retrospect-gds
PHP | 368 lines | 249 code | 22 blank | 97 comment | 42 complexity | deb44578abfc3fa35bab7365e1ae3d99 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. V4.93 10 Oct 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. 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('odbc_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. System Error 5
  60. ==============
  61. IF you get a System Error 5 when trying to Connect/Load, it could be a permission problem. Give the user connecting
  62. to DB2 full rights to the DB2 SQLLIB directory, and place the user in the DBUSERS group.
  63. */
  64. // security - hide paths
  65. if (!defined('ADODB_DIR')) die();
  66. if (!defined('_ADODB_ODBC_LAYER')) {
  67. include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
  68. }
  69. if (!defined('ADODB_ODBC_DB2')){
  70. define('ADODB_ODBC_DB2',1);
  71. class ADODB_ODBC_DB2 extends ADODB_odbc {
  72. var $databaseType = "db2";
  73. var $concat_operator = '||';
  74. var $sysTime = 'CURRENT TIME';
  75. var $sysDate = 'CURRENT DATE';
  76. var $sysTimeStamp = 'CURRENT TIMESTAMP';
  77. // The complete string representation of a timestamp has the form
  78. // yyyy-mm-dd-hh.mm.ss.nnnnnn.
  79. var $fmtTimeStamp = "'Y-m-d-H.i.s'";
  80. var $ansiOuter = true;
  81. var $identitySQL = 'values IDENTITY_VAL_LOCAL()';
  82. var $_bindInputArray = true;
  83. var $hasInsertID = true;
  84. var $rsPrefix = 'ADORecordset_odbc_';
  85. function ADODB_DB2()
  86. {
  87. if (strncmp(PHP_OS,'WIN',3) === 0) $this->curmode = SQL_CUR_USE_ODBC;
  88. $this->ADODB_odbc();
  89. }
  90. function IfNull( $field, $ifNull )
  91. {
  92. return " COALESCE($field, $ifNull) "; // if DB2 UDB
  93. }
  94. function ServerInfo()
  95. {
  96. //odbc_setoption($this->_connectionID,1,101 /*SQL_ATTR_ACCESS_MODE*/, 1 /*SQL_MODE_READ_ONLY*/);
  97. $vers = $this->GetOne('select versionnumber from sysibm.sysversions');
  98. //odbc_setoption($this->_connectionID,1,101, 0 /*SQL_MODE_READ_WRITE*/);
  99. return array('description'=>'DB2 ODBC driver', 'version'=>$vers);
  100. }
  101. function _insertid()
  102. {
  103. return $this->GetOne($this->identitySQL);
  104. }
  105. function RowLock($tables,$where,$flds='1 as ignore')
  106. {
  107. if ($this->_autocommit) $this->BeginTrans();
  108. return $this->GetOne("select $flds from $tables where $where for update");
  109. }
  110. function &MetaTables($ttype=false,$showSchema=false, $qtable="%", $qschema="%")
  111. {
  112. global $ADODB_FETCH_MODE;
  113. $savem = $ADODB_FETCH_MODE;
  114. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  115. $qid = odbc_tables($this->_connectionID, "", $qschema, $qtable, "");
  116. $rs = new ADORecordSet_odbc($qid);
  117. $ADODB_FETCH_MODE = $savem;
  118. if (!$rs) {
  119. $false = false;
  120. return $false;
  121. }
  122. $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
  123. $arr =& $rs->GetArray();
  124. //print_r($arr);
  125. $rs->Close();
  126. $arr2 = array();
  127. if ($ttype) {
  128. $isview = strncmp($ttype,'V',1) === 0;
  129. }
  130. for ($i=0; $i < sizeof($arr); $i++) {
  131. if (!$arr[$i][2]) continue;
  132. if (strncmp($arr[$i][1],'SYS',3) === 0) continue;
  133. $type = $arr[$i][3];
  134. if ($showSchema) $arr[$i][2] = $arr[$i][1].'.'.$arr[$i][2];
  135. if ($ttype) {
  136. if ($isview) {
  137. if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
  138. } else if (strncmp($type,'T',1) === 0) $arr2[] = $arr[$i][2];
  139. } else if (strncmp($type,'S',1) !== 0) $arr2[] = $arr[$i][2];
  140. }
  141. return $arr2;
  142. }
  143. function &MetaIndexes ($table, $primary = FALSE, $owner=false)
  144. {
  145. // save old fetch mode
  146. global $ADODB_FETCH_MODE;
  147. $save = $ADODB_FETCH_MODE;
  148. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  149. if ($this->fetchMode !== FALSE) {
  150. $savem = $this->SetFetchMode(FALSE);
  151. }
  152. $false = false;
  153. // get index details
  154. $table = strtoupper($table);
  155. $SQL="SELECT NAME, UNIQUERULE, COLNAMES FROM SYSIBM.SYSINDEXES WHERE TBNAME='$table'";
  156. if ($primary)
  157. $SQL.= " AND UNIQUERULE='P'";
  158. $rs = $this->Execute($SQL);
  159. if (!is_object($rs)) {
  160. if (isset($savem))
  161. $this->SetFetchMode($savem);
  162. $ADODB_FETCH_MODE = $save;
  163. return $false;
  164. }
  165. $indexes = array ();
  166. // parse index data into array
  167. while ($row = $rs->FetchRow()) {
  168. $indexes[$row[0]] = array(
  169. 'unique' => ($row[1] == 'U' || $row[1] == 'P'),
  170. 'columns' => array()
  171. );
  172. $cols = ltrim($row[2],'+');
  173. $indexes[$row[0]]['columns'] = explode('+', $cols);
  174. }
  175. if (isset($savem)) {
  176. $this->SetFetchMode($savem);
  177. $ADODB_FETCH_MODE = $save;
  178. }
  179. return $indexes;
  180. }
  181. // Format date column in sql string given an input format that understands Y M D
  182. function SQLDate($fmt, $col=false)
  183. {
  184. // use right() and replace() ?
  185. if (!$col) $col = $this->sysDate;
  186. $s = '';
  187. $len = strlen($fmt);
  188. for ($i=0; $i < $len; $i++) {
  189. if ($s) $s .= '||';
  190. $ch = $fmt[$i];
  191. switch($ch) {
  192. case 'Y':
  193. case 'y':
  194. $s .= "char(year($col))";
  195. break;
  196. case 'M':
  197. $s .= "substr(monthname($col),1,3)";
  198. break;
  199. case 'm':
  200. $s .= "right(digits(month($col)),2)";
  201. break;
  202. case 'D':
  203. case 'd':
  204. $s .= "right(digits(day($col)),2)";
  205. break;
  206. case 'H':
  207. case 'h':
  208. if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)";
  209. else $s .= "''";
  210. break;
  211. case 'i':
  212. case 'I':
  213. if ($col != $this->sysDate)
  214. $s .= "right(digits(minute($col)),2)";
  215. else $s .= "''";
  216. break;
  217. case 'S':
  218. case 's':
  219. if ($col != $this->sysDate)
  220. $s .= "right(digits(second($col)),2)";
  221. else $s .= "''";
  222. break;
  223. default:
  224. if ($ch == '\\') {
  225. $i++;
  226. $ch = substr($fmt,$i,1);
  227. }
  228. $s .= $this->qstr($ch);
  229. }
  230. }
  231. return $s;
  232. }
  233. function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputArr=false)
  234. {
  235. $nrows = (integer) $nrows;
  236. if ($offset <= 0) {
  237. // could also use " OPTIMIZE FOR $nrows ROWS "
  238. if ($nrows >= 0) $sql .= " FETCH FIRST $nrows ROWS ONLY ";
  239. $rs =& $this->Execute($sql,$inputArr);
  240. } else {
  241. if ($offset > 0 && $nrows < 0);
  242. else {
  243. $nrows += $offset;
  244. $sql .= " FETCH FIRST $nrows ROWS ONLY ";
  245. }
  246. $rs =& ADOConnection::SelectLimit($sql,-1,$offset,$inputArr);
  247. }
  248. return $rs;
  249. }
  250. };
  251. class ADORecordSet_odbc_db2 extends ADORecordSet_odbc {
  252. var $databaseType = "db2";
  253. function ADORecordSet_db2($id,$mode=false)
  254. {
  255. $this->ADORecordSet_odbc($id,$mode);
  256. }
  257. function MetaType($t,$len=-1,$fieldobj=false)
  258. {
  259. if (is_object($t)) {
  260. $fieldobj = $t;
  261. $t = $fieldobj->type;
  262. $len = $fieldobj->max_length;
  263. }
  264. switch (strtoupper($t)) {
  265. case 'VARCHAR':
  266. case 'CHAR':
  267. case 'CHARACTER':
  268. case 'C':
  269. if ($len <= $this->blobSize) return 'C';
  270. case 'LONGCHAR':
  271. case 'TEXT':
  272. case 'CLOB':
  273. case 'DBCLOB': // double-byte
  274. case 'X':
  275. return 'X';
  276. case 'BLOB':
  277. case 'GRAPHIC':
  278. case 'VARGRAPHIC':
  279. return 'B';
  280. case 'DATE':
  281. case 'D':
  282. return 'D';
  283. case 'TIME':
  284. case 'TIMESTAMP':
  285. case 'T':
  286. return 'T';
  287. //case 'BOOLEAN':
  288. //case 'BIT':
  289. // return 'L';
  290. //case 'COUNTER':
  291. // return 'R';
  292. case 'INT':
  293. case 'INTEGER':
  294. case 'BIGINT':
  295. case 'SMALLINT':
  296. case 'I':
  297. return 'I';
  298. default: return 'N';
  299. }
  300. }
  301. }
  302. } //define
  303. ?>