PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/src/Lib/Data/DB/adodb_lite/adodbSQL_drivers/msql/msql_driver.inc

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