PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/src/Lib/Data/DB/adodb_lite/adodbSQL_drivers/sybase/sybase_driver.inc

http://scalr.googlecode.com/
PHP | 662 lines | 587 code | 23 blank | 52 comment | 10 complexity | 32c1d639e3f2a9a231cf7b990b8299cc MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with
  4. * a subset of the ADODB Command Syntax.
  5. * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL,
  6. * PostgresSQL64, PostgresSQL7, SqLite and Sybase.
  7. *
  8. */
  9. class sybase_driver_ADOConnection extends ADOConnection
  10. {
  11. var $sysDate = 'GetDate()';
  12. function sybase_driver_ADOConnection()
  13. {
  14. $this->dbtype = 'sybase';
  15. $this->dataProvider = 'sybase';
  16. }
  17. /**
  18. * Connection to database server and selected database
  19. *
  20. * @access private
  21. */
  22. function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew)
  23. {
  24. if (!function_exists('sybase_connect')) return false;
  25. $this->host = $host;
  26. $this->username = $username;
  27. $this->password = $password;
  28. $this->database = $database;
  29. $this->persistent = $persistent;
  30. $this->forcenewconnection = $forcenew;
  31. if($this->persistent == 1)
  32. {
  33. $this->connectionId = @sybase_pconnect( $this->host, $this->username, $this->password );
  34. }
  35. else
  36. {
  37. $this->connectionId = @sybase_connect( $this->host, $this->username, $this->password );
  38. }
  39. if ($this->connectionId === false)
  40. {
  41. if ($fn = $this->raiseErrorFn)
  42. $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this);
  43. return false;
  44. }
  45. if (!empty($this->database))
  46. {
  47. if($this->SelectDB( $this->database ) == false)
  48. {
  49. $this->connectionId = false;
  50. return false;
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * Choose a database to connect.
  57. *
  58. * @param dbname is the name of the database to select
  59. * @return true or false
  60. * @access public
  61. */
  62. function SelectDB($dbname)
  63. {
  64. $this->database = $dbname;
  65. if ($this->connectionId === false)
  66. {
  67. $this->connectionId = false;
  68. return false;
  69. }
  70. else
  71. {
  72. @sybase_select_db( $this->database, $this->connectionId );
  73. return true;
  74. }
  75. }
  76. /**
  77. * Return database error message
  78. * Usage: $errormessage =& $db->ErrorMsg();
  79. *
  80. * @access public
  81. */
  82. function ErrorMsg()
  83. {
  84. return @sybase_get_last_message($this->connectionId);
  85. }
  86. /**
  87. * Return database error number
  88. * Usage: $errorbo =& $db->ErrorNo();
  89. *
  90. * @access public
  91. */
  92. function ErrorNo()
  93. {
  94. $error = @sybase_get_last_message($this->connectionId);
  95. $errorno = strlen($error) ? $error : 0;
  96. return $errorno;
  97. }
  98. /**
  99. * Returns # of affected rows from insert/delete/update query
  100. *
  101. * @access public
  102. * @return integer Affected rows
  103. */
  104. function Affected_Rows()
  105. {
  106. return @sybase_affected_rows($this->connectionId);
  107. }
  108. /**
  109. * Returns the last record id of an inserted item
  110. * Usage: $db->Insert_ID();
  111. *
  112. * @access public
  113. */
  114. function Insert_ID()
  115. {
  116. return false;
  117. }
  118. /**
  119. * Correctly quotes a string so that all strings are escape coded.
  120. * An example is $db->qstr("Haven't a clue.");
  121. *
  122. * @param string the string to quote
  123. * @param [magic_quotes] if $s is GET/POST var, set to get_magic_quotes_gpc().
  124. *
  125. * @return single-quoted string IE: 'Haven\'t a clue.'
  126. */
  127. function qstr($string, $magic_quotes=false)
  128. {
  129. if (!$magic_quotes) {
  130. return "'".str_replace("'", "''", $string)."'";
  131. }
  132. $string = str_replace("\\'", "''", str_replace('\\\\', '\\', str_replace('\\"', '"', $string)));
  133. return "'" . $string . "'";
  134. }
  135. function QMagic($string)
  136. {
  137. return $this->qstr($string, get_magic_quotes_gpc());
  138. }
  139. /**
  140. * Returns concatenated string
  141. * Usage: $db->Concat($str1,$str2);
  142. *
  143. * @return concatenated string
  144. */
  145. function Concat()
  146. {
  147. $arr = func_get_args();
  148. return implode("+", $arr);
  149. }
  150. function IfNull( $field, $ifNull )
  151. {
  152. return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
  153. }
  154. /**
  155. * Closes database connection
  156. * Usage: $db->close();
  157. *
  158. * @access public
  159. */
  160. function Close()
  161. {
  162. @sybase_close( $this->connectionId );
  163. $this->connectionId = false;
  164. }
  165. /**
  166. * Returns All Records in an array
  167. *
  168. * Usage: $db->GetAll($sql);
  169. * @access public
  170. */
  171. function &GetAll($sql, $inputarr = false)
  172. {
  173. $data =& $this->GetArray($sql, $inputarr);
  174. return $data;
  175. }
  176. /**
  177. * Returns All Records in an array
  178. *
  179. * Usage: $db->GetArray($sql);
  180. * @access public
  181. */
  182. function &GetArray($sql, $inputarr = false)
  183. {
  184. $data = false;
  185. $result =& $this->Execute($sql, $inputarr);
  186. if ($result)
  187. {
  188. $data =& $result->GetArray();
  189. $result->Close();
  190. }
  191. return $data;
  192. }
  193. /**
  194. * Executes SQL query and instantiates resultset methods
  195. *
  196. * @access private
  197. * @return mixed Resultset methods
  198. */
  199. function &do_query( $sql, $offset, $nrows, $inputarr=false )
  200. {
  201. global $ADODB_FETCH_MODE;
  202. $false = false;
  203. $nrows = (integer) $nrows;
  204. $offset = (integer) $offset;
  205. $cnt = ($nrows >= 0) ? $nrows : 999999999;
  206. if ($offset > 0 && $cnt) $cnt += $offset;
  207. // @sybase_query("set rowcount $cnt", $this->connectionId);
  208. // $resultId =& @sybase_query($this->sql, $this->connectionId);
  209. // @sybase_query("set rowcount 0", $this->connectionId);
  210. if ($inputarr && is_array($inputarr)) {
  211. $sqlarr = explode('?', $sql);
  212. if (!is_array(reset($inputarr))) $inputarr = array($inputarr);
  213. foreach($inputarr as $arr) {
  214. $sql = ''; $i = 0;
  215. foreach($arr as $v) {
  216. $sql .= $sqlarr[$i];
  217. switch(gettype($v)){
  218. case 'string':
  219. $sql .= $this->qstr($v);
  220. break;
  221. case 'double':
  222. $sql .= str_replace(',', '.', $v);
  223. break;
  224. case 'boolean':
  225. $sql .= $v ? 1 : 0;
  226. break;
  227. default:
  228. if ($v === null)
  229. $sql .= 'NULL';
  230. else $sql .= $v;
  231. }
  232. $i += 1;
  233. }
  234. $sql .= $sqlarr[$i];
  235. if ($i+1 != sizeof($sqlarr))
  236. return $false;
  237. $this->sql = $sql;
  238. $time_start = array_sum(explode(' ', microtime()));
  239. $this->query_count++;
  240. $resultId = @sybase_query( $this->sql, $this->connectionId );
  241. $time_total = (array_sum(explode(' ', microtime())) - $time_start);
  242. $this->query_time_total += $time_total;
  243. if($this->debug_console)
  244. {
  245. $this->query_list[] = $this->sql;
  246. $this->query_list_time[] = $time_total;
  247. $this->query_list_errors[] = $this->ErrorMsg();
  248. }
  249. if($this->debug)
  250. {
  251. $this->outp($sql);
  252. }
  253. if ($resultId === false) { // error handling if query fails
  254. if ($fn = $this->raiseErrorFn)
  255. $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
  256. return $false;
  257. }
  258. }
  259. }
  260. else
  261. {
  262. $this->sql = $sql;
  263. $time_start = array_sum(explode(' ', microtime()));
  264. $this->query_count++;
  265. $resultId = @sybase_query( $this->sql, $this->connectionId );
  266. $time_total = (array_sum(explode(' ', microtime())) - $time_start);
  267. $this->query_time_total += $time_total;
  268. if($this->debug_console)
  269. {
  270. $this->query_list[] = $this->sql;
  271. $this->query_list_time[] = $time_total;
  272. $this->query_list_errors[] = $this->ErrorMsg();
  273. }
  274. if($this->debug)
  275. {
  276. $this->outp($sql);
  277. }
  278. }
  279. if ($resultId === false) { // error handling if query fails
  280. if ($fn = $this->raiseErrorFn)
  281. $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
  282. return $false;
  283. }
  284. if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead
  285. $recordset = new ADORecordSet_empty();
  286. return $recordset;
  287. }
  288. $resultset_name = $this->last_module_name . "_ResultSet";
  289. $recordset = new $resultset_name( $resultId, $this->connectionId );
  290. $recordset->_currentRow = 0;
  291. switch ($ADODB_FETCH_MODE)
  292. {
  293. case ADODB_FETCH_NUM: $recordset->fetchMode = 0; break;
  294. case ADODB_FETCH_ASSOC:$recordset->fetchMode = 1; break;
  295. default:
  296. case ADODB_FETCH_DEFAULT:
  297. case ADODB_FETCH_BOTH:$recordset->fetchMode = 1; break;
  298. }
  299. $recordset->_numOfRows = @sybase_num_rows( $resultId );
  300. if( $recordset->_numOfRows == 0)
  301. {
  302. $recordset->EOF = true;
  303. }
  304. $recordset->_numOfFields = @sybase_num_fields( $resultId );
  305. if ($offset != -1 || $nrows != -1)
  306. {
  307. if($offset == -1 || ($offset == 0 && $nrows != -1))
  308. {
  309. $recordset->_numOfRows = ($nrows < $recordset->_numOfRows) ? $nrows : $recordset->_numOfRows;
  310. $recordset->_fetch();
  311. }
  312. else
  313. {
  314. if($offset > $recordset->_numOfRows)
  315. {
  316. $rs =& new ADORecordSet_empty();
  317. return $rs;
  318. }
  319. $recordset->_fetch();
  320. for($i = 0; $i < $offset; $i++)
  321. {
  322. $recordset->MoveNext();
  323. }
  324. $recordset->_currentRow = 0;
  325. if($nrows != -1)
  326. {
  327. $recordset->_numOfRows = ($nrows < ($recordset->_numOfRows - $offset)) ? $nrows : $recordset->_numOfRows - $offset;
  328. }
  329. else
  330. {
  331. $recordset->_numOfRows -= $offset;
  332. }
  333. }
  334. }
  335. else
  336. {
  337. $recordset->_fetch();
  338. }
  339. return $recordset;
  340. }
  341. }
  342. class sybase_driver_ResultSet
  343. {
  344. var $connectionId;
  345. var $fields;
  346. var $resultId;
  347. var $_currentRow = 0;
  348. var $_numOfRows = -1;
  349. var $_numOfFields = -1;
  350. var $fetchMode;
  351. var $EOF;
  352. /**
  353. * sybaseResultSet Constructor
  354. *
  355. * @access private
  356. * @param string $record
  357. * @param string $resultId
  358. */
  359. function sybase_driver_ResultSet( $resultId, $connectionId )
  360. {
  361. $this->fields = array();
  362. $this->connectionId = $connectionId;
  363. $this->record = array();
  364. $this->resultId = $resultId;
  365. $this->EOF = false;
  366. }
  367. /**
  368. * Frees resultset
  369. *
  370. * @access public
  371. */
  372. function close()
  373. {
  374. @sybase_free_result( $this->resultId );
  375. $this->fields = array();
  376. $this->resultId = false;
  377. }
  378. /**
  379. * Returns field name from select query
  380. *
  381. * @access public
  382. * @param string $field
  383. * @return string Field name
  384. */
  385. function fields( $field )
  386. {
  387. if(empty($field))
  388. {
  389. return $this->fields;
  390. }
  391. else
  392. {
  393. return $this->fields[$field];
  394. }
  395. }
  396. /**
  397. * Returns numrows from select query
  398. *
  399. * @access public
  400. * @return integer Numrows
  401. */
  402. function RecordCount()
  403. {
  404. return $this->_numOfRows;
  405. }
  406. /**
  407. * Returns num of fields from select query
  408. *
  409. * @access public
  410. * @return integer numfields
  411. */
  412. function FieldCount()
  413. {
  414. return $this->_numOfFields;
  415. }
  416. /**
  417. * Returns next record
  418. *
  419. * @access public
  420. */
  421. function MoveNext()
  422. {
  423. if($this->fetchMode == 0)
  424. {
  425. if (@$this->fields =& sybase_fetch_row($this->resultId)) {
  426. $this->_currentRow += 1;
  427. return true;
  428. }
  429. }
  430. else
  431. if($this->fetchMode == 1)
  432. {
  433. if (@$this->fields =& sybase_fetch_assoc($this->resultId)) {
  434. $this->_currentRow += 1;
  435. return true;
  436. }
  437. }
  438. if (!$this->EOF) {
  439. $this->_currentRow += 1;
  440. $this->EOF = true;
  441. }
  442. return false;
  443. }
  444. /**
  445. * Move to the first row in the recordset. Many databases do NOT support this.
  446. *
  447. * @return true or false
  448. */
  449. function MoveFirst()
  450. {
  451. if ($this->_currentRow == 0) return true;
  452. return $this->Move(0);
  453. }
  454. /**
  455. * Returns the Last Record
  456. *
  457. * @access public
  458. */
  459. function MoveLast()
  460. {
  461. if ($this->EOF) return false;
  462. return $this->Move($this->_numOfRows - 1);
  463. }
  464. /**
  465. * Random access to a specific row in the recordset. Some databases do not support
  466. * access to previous rows in the databases (no scrolling backwards).
  467. *
  468. * @param rowNumber is the row to move to (0-based)
  469. *
  470. * @return true if there still rows available, or false if there are no more rows (EOF).
  471. */
  472. function Move($rowNumber = 0)
  473. {
  474. if ($rowNumber == $this->_currentRow) return true;
  475. $this->EOF = false;
  476. if ($this->_numOfRows > 0){
  477. if ($rowNumber >= $this->_numOfRows - 1){
  478. $rowNumber = $this->_numOfRows - 1;
  479. }
  480. }
  481. if ($this->_seek($rowNumber)) {
  482. $this->_currentRow = $rowNumber;
  483. if ($this->_fetch()) {
  484. return true;
  485. }
  486. $this->fields = false;
  487. }
  488. $this->EOF = true;
  489. return false;
  490. }
  491. /**
  492. * Perform Seek to specific row
  493. *
  494. * @access private
  495. */
  496. function _seek($row)
  497. {
  498. if ($this->_numOfRows == 0) return false;
  499. return @sybase_data_seek($this->resultId,$row);
  500. }
  501. /**
  502. * Fills field array with first database element when query initially executed
  503. *
  504. * @access private
  505. */
  506. function _fetch()
  507. {
  508. if($this->fetchMode == 0)
  509. {
  510. $this->fields = sybase_fetch_row($this->resultId);
  511. }
  512. else
  513. if($this->fetchMode == 1)
  514. {
  515. $this->fields = sybase_fetch_assoc($this->resultId);
  516. }
  517. return is_array($this->fields);
  518. }
  519. /**
  520. * Check to see if last record reached
  521. *
  522. * @access public
  523. */
  524. function EOF()
  525. {
  526. if( $this->_currentRow < $this->_numOfRows)
  527. {
  528. return false;
  529. }
  530. else
  531. {
  532. $this->EOF = true;
  533. return true;
  534. }
  535. }
  536. /**
  537. * Returns All Records in an array
  538. *
  539. * @access public
  540. * @param [nRows] is the number of rows to return. -1 means every row.
  541. */
  542. function &GetArray($nRows = -1)
  543. {
  544. $results = array();
  545. $cnt = 0;
  546. while (!$this->EOF && $nRows != $cnt) {
  547. $results[] = $this->fields;
  548. $this->MoveNext();
  549. $cnt++;
  550. }
  551. return $results;
  552. }
  553. function &GetRows($nRows = -1)
  554. {
  555. $arr =& $this->GetArray($nRows);
  556. return $arr;
  557. }
  558. function &GetAll($nRows = -1)
  559. {
  560. $arr =& $this->GetArray($nRows);
  561. return $arr;
  562. }
  563. /**
  564. * Fetch field information for a table.
  565. *
  566. * @return object containing the name, type and max_length
  567. */
  568. function FetchField($fieldOffset = -1)
  569. {
  570. if ($fieldOffset != -1) {
  571. $fieldObject = @sybase_fetch_field($this->resultId, $fieldOffset);
  572. }
  573. else
  574. {
  575. $fieldObject = @sybase_fetch_field($this->resultId);
  576. }
  577. if ($fieldObject && !isset($fieldObject->type)) $fieldObject->type = ($fieldObject->numeric) ? 'float' : 'varchar';
  578. return $fieldObject;
  579. }
  580. }
  581. ?>