PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/libraries/3rdparty/adodb/drivers/adodb-mysql.inc.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 875 lines | 675 code | 130 blank | 70 comment | 136 complexity | fcbd61efda2c6182959c171acd7c0cfc MD5 | raw file
  1. <?php
  2. /*
  3. V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). 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 8.
  8. MySQL code that does not support transactions. Use mysqlt if you need transactions.
  9. Requires mysql client. Works on Windows and Unix.
  10. 28 Feb 2001: MetaColumns bug fix - suggested by Freek Dijkstra (phpeverywhere@macfreek.com)
  11. */
  12. // security - hide paths
  13. if (!defined('ADODB_DIR')) die();
  14. if (! defined("_ADODB_MYSQL_LAYER")) {
  15. define("_ADODB_MYSQL_LAYER", 1 );
  16. class ADODB_mysql extends ADOConnection {
  17. var $databaseType = 'mysql';
  18. var $dataProvider = 'mysql';
  19. var $hasInsertID = true;
  20. var $hasAffectedRows = true;
  21. var $metaTablesSQL = "SELECT TABLE_NAME, CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=SCHEMA()";
  22. var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
  23. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  24. var $hasLimit = true;
  25. var $hasMoveFirst = true;
  26. var $hasGenID = true;
  27. var $isoDates = true; // accepts dates in ISO format
  28. var $sysDate = 'CURDATE()';
  29. var $sysTimeStamp = 'NOW()';
  30. var $hasTransactions = false;
  31. var $forceNewConnect = false;
  32. var $poorAffectedRows = true;
  33. var $clientFlags = 0;
  34. var $charSet = '';
  35. var $substr = "substring";
  36. var $nameQuote = '`'; /// string to use to quote identifiers and names
  37. var $compat323 = false; // true if compat with mysql 3.23
  38. function ADODB_mysql()
  39. {
  40. if (defined('ADODB_EXTENSION')) $this->rsPrefix .= 'ext_';
  41. }
  42. // SetCharSet - switch the client encoding
  43. function SetCharSet($charset_name)
  44. {
  45. if (!function_exists('mysql_set_charset'))
  46. return false;
  47. if ($this->charSet !== $charset_name) {
  48. $ok = @mysql_set_charset($charset_name,$this->_connectionID);
  49. if ($ok) {
  50. $this->charSet = $charset_name;
  51. return true;
  52. }
  53. return false;
  54. }
  55. return true;
  56. }
  57. function ServerInfo()
  58. {
  59. $arr['description'] = ADOConnection::GetOne("select version()");
  60. $arr['version'] = ADOConnection::_findvers($arr['description']);
  61. return $arr;
  62. }
  63. function IfNull( $field, $ifNull )
  64. {
  65. return " IFNULL($field, $ifNull) "; // if MySQL
  66. }
  67. function MetaProcedures($NamePattern = false, $catalog = null, $schemaPattern = null)
  68. {
  69. // save old fetch mode
  70. global $ADODB_FETCH_MODE;
  71. $false = false;
  72. $save = $ADODB_FETCH_MODE;
  73. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  74. if ($this->fetchMode !== FALSE) {
  75. $savem = $this->SetFetchMode(FALSE);
  76. }
  77. $procedures = array ();
  78. // get index details
  79. $likepattern = '';
  80. if ($NamePattern) {
  81. $likepattern = " LIKE '".$NamePattern."'";
  82. }
  83. $rs = $this->Execute('SHOW PROCEDURE STATUS'.$likepattern);
  84. if (is_object($rs)) {
  85. // parse index data into array
  86. while ($row = $rs->FetchRow()) {
  87. $procedures[$row[1]] = array(
  88. 'type' => 'PROCEDURE',
  89. 'catalog' => '',
  90. 'schema' => '',
  91. 'remarks' => $row[7],
  92. );
  93. }
  94. }
  95. $rs = $this->Execute('SHOW FUNCTION STATUS'.$likepattern);
  96. if (is_object($rs)) {
  97. // parse index data into array
  98. while ($row = $rs->FetchRow()) {
  99. $procedures[$row[1]] = array(
  100. 'type' => 'FUNCTION',
  101. 'catalog' => '',
  102. 'schema' => '',
  103. 'remarks' => $row[7]
  104. );
  105. }
  106. }
  107. // restore fetchmode
  108. if (isset($savem)) {
  109. $this->SetFetchMode($savem);
  110. }
  111. $ADODB_FETCH_MODE = $save;
  112. return $procedures;
  113. }
  114. function MetaTables($ttype=false,$showSchema=false,$mask=false)
  115. {
  116. $save = $this->metaTablesSQL;
  117. if ($showSchema && is_string($showSchema)) {
  118. $this->metaTablesSQL .= " from $showSchema";
  119. }
  120. if ($mask) {
  121. $mask = $this->qstr($mask);
  122. $this->metaTablesSQL .= " like $mask";
  123. }
  124. $ret = ADOConnection::MetaTables($ttype,$showSchema);
  125. $this->metaTablesSQL = $save;
  126. return $ret;
  127. }
  128. function MetaIndexes ($table, $primary = FALSE, $owner=false)
  129. {
  130. // save old fetch mode
  131. global $ADODB_FETCH_MODE;
  132. $false = false;
  133. $save = $ADODB_FETCH_MODE;
  134. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  135. if ($this->fetchMode !== FALSE) {
  136. $savem = $this->SetFetchMode(FALSE);
  137. }
  138. // get index details
  139. $rs = $this->Execute(sprintf('SHOW INDEX FROM %s',$table));
  140. // restore fetchmode
  141. if (isset($savem)) {
  142. $this->SetFetchMode($savem);
  143. }
  144. $ADODB_FETCH_MODE = $save;
  145. if (!is_object($rs)) {
  146. return $false;
  147. }
  148. $indexes = array ();
  149. // parse index data into array
  150. while ($row = $rs->FetchRow()) {
  151. if ($primary == FALSE AND $row[2] == 'PRIMARY') {
  152. continue;
  153. }
  154. if (!isset($indexes[$row[2]])) {
  155. $indexes[$row[2]] = array(
  156. 'unique' => ($row[1] == 0),
  157. 'columns' => array()
  158. );
  159. }
  160. $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
  161. }
  162. // sort columns by order in the index
  163. foreach ( array_keys ($indexes) as $index )
  164. {
  165. ksort ($indexes[$index]['columns']);
  166. }
  167. return $indexes;
  168. }
  169. // if magic quotes disabled, use mysql_real_escape_string()
  170. function qstr($s,$magic_quotes=false)
  171. {
  172. if (is_null($s)) return 'NULL';
  173. if (!$magic_quotes) {
  174. if (ADODB_PHPVER >= 0x4300) {
  175. if (is_resource($this->_connectionID))
  176. return "'".mysql_real_escape_string($s,$this->_connectionID)."'";
  177. }
  178. if ($this->replaceQuote[0] == '\\'){
  179. $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
  180. }
  181. return "'".str_replace("'",$this->replaceQuote,$s)."'";
  182. }
  183. // undo magic quotes for "
  184. $s = str_replace('\\"','"',$s);
  185. return "'$s'";
  186. }
  187. function _insertid()
  188. {
  189. return ADOConnection::GetOne('SELECT LAST_INSERT_ID()');
  190. //return mysql_insert_id($this->_connectionID);
  191. }
  192. function GetOne($sql,$inputarr=false)
  193. {
  194. global $ADODB_GETONE_EOF;
  195. if ($this->compat323 == false && strncasecmp($sql,'sele',4) == 0) {
  196. $rs = $this->SelectLimit($sql,1,-1,$inputarr);
  197. if ($rs) {
  198. $rs->Close();
  199. if ($rs->EOF) return $ADODB_GETONE_EOF;
  200. return reset($rs->fields);
  201. }
  202. } else {
  203. return ADOConnection::GetOne($sql,$inputarr);
  204. }
  205. return false;
  206. }
  207. function BeginTrans()
  208. {
  209. if ($this->debug) ADOConnection::outp("Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver");
  210. }
  211. function _affectedrows()
  212. {
  213. return mysql_affected_rows($this->_connectionID);
  214. }
  215. // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
  216. // Reference on Last_Insert_ID on the recommended way to simulate sequences
  217. var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
  218. var $_genSeqSQL = "create table %s (id int not null)";
  219. var $_genSeqCountSQL = "select count(*) from %s";
  220. var $_genSeq2SQL = "insert into %s values (%s)";
  221. var $_dropSeqSQL = "drop table %s";
  222. function CreateSequence($seqname='adodbseq',$startID=1)
  223. {
  224. if (empty($this->_genSeqSQL)) return false;
  225. $u = strtoupper($seqname);
  226. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  227. if (!$ok) return false;
  228. return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  229. }
  230. function GenID($seqname='adodbseq',$startID=1)
  231. {
  232. // post-nuke sets hasGenID to false
  233. if (!$this->hasGenID) return false;
  234. $savelog = $this->_logsql;
  235. $this->_logsql = false;
  236. $getnext = sprintf($this->_genIDSQL,$seqname);
  237. $holdtransOK = $this->_transOK; // save the current status
  238. $rs = @$this->Execute($getnext);
  239. if (!$rs) {
  240. if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
  241. $u = strtoupper($seqname);
  242. $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  243. $cnt = $this->GetOne(sprintf($this->_genSeqCountSQL,$seqname));
  244. if (!$cnt) $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  245. $rs = $this->Execute($getnext);
  246. }
  247. if ($rs) {
  248. $this->genID = mysql_insert_id($this->_connectionID);
  249. $rs->Close();
  250. } else
  251. $this->genID = 0;
  252. $this->_logsql = $savelog;
  253. return $this->genID;
  254. }
  255. function MetaDatabases()
  256. {
  257. $qid = mysql_list_dbs($this->_connectionID);
  258. $arr = array();
  259. $i = 0;
  260. $max = mysql_num_rows($qid);
  261. while ($i < $max) {
  262. $db = mysql_tablename($qid,$i);
  263. if ($db != 'mysql') $arr[] = $db;
  264. $i += 1;
  265. }
  266. return $arr;
  267. }
  268. // Format date column in sql string given an input format that understands Y M D
  269. function SQLDate($fmt, $col=false)
  270. {
  271. if (!$col) $col = $this->sysTimeStamp;
  272. $s = 'DATE_FORMAT('.$col.",'";
  273. $concat = false;
  274. $len = strlen($fmt);
  275. for ($i=0; $i < $len; $i++) {
  276. $ch = $fmt[$i];
  277. switch($ch) {
  278. default:
  279. if ($ch == '\\') {
  280. $i++;
  281. $ch = substr($fmt,$i,1);
  282. }
  283. /** FALL THROUGH */
  284. case '-':
  285. case '/':
  286. $s .= $ch;
  287. break;
  288. case 'Y':
  289. case 'y':
  290. $s .= '%Y';
  291. break;
  292. case 'M':
  293. $s .= '%b';
  294. break;
  295. case 'm':
  296. $s .= '%m';
  297. break;
  298. case 'D':
  299. case 'd':
  300. $s .= '%d';
  301. break;
  302. case 'Q':
  303. case 'q':
  304. $s .= "'),Quarter($col)";
  305. if ($len > $i+1) $s .= ",DATE_FORMAT($col,'";
  306. else $s .= ",('";
  307. $concat = true;
  308. break;
  309. case 'H':
  310. $s .= '%H';
  311. break;
  312. case 'h':
  313. $s .= '%I';
  314. break;
  315. case 'i':
  316. $s .= '%i';
  317. break;
  318. case 's':
  319. $s .= '%s';
  320. break;
  321. case 'a':
  322. case 'A':
  323. $s .= '%p';
  324. break;
  325. case 'w':
  326. $s .= '%w';
  327. break;
  328. case 'W':
  329. $s .= '%U';
  330. break;
  331. case 'l':
  332. $s .= '%W';
  333. break;
  334. }
  335. }
  336. $s.="')";
  337. if ($concat) $s = "CONCAT($s)";
  338. return $s;
  339. }
  340. // returns concatenated string
  341. // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
  342. function Concat()
  343. {
  344. $s = "";
  345. $arr = func_get_args();
  346. // suggestion by andrew005@mnogo.ru
  347. $s = implode(',',$arr);
  348. if (strlen($s) > 0) return "CONCAT($s)";
  349. else return '';
  350. }
  351. function OffsetDate($dayFraction,$date=false)
  352. {
  353. if (!$date) $date = $this->sysDate;
  354. $fraction = $dayFraction * 24 * 3600;
  355. return '('. $date . ' + INTERVAL ' . $fraction.' SECOND)';
  356. // return "from_unixtime(unix_timestamp($date)+$fraction)";
  357. }
  358. // returns true or false
  359. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  360. {
  361. if (!empty($this->port)) $argHostname .= ":".$this->port;
  362. if (ADODB_PHPVER >= 0x4300)
  363. $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
  364. $this->forceNewConnect,$this->clientFlags);
  365. else if (ADODB_PHPVER >= 0x4200)
  366. $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
  367. $this->forceNewConnect);
  368. else
  369. $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
  370. if ($this->_connectionID === false) return false;
  371. if ($argDatabasename) return $this->SelectDB($argDatabasename);
  372. return true;
  373. }
  374. // returns true or false
  375. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  376. {
  377. if (!empty($this->port)) $argHostname .= ":".$this->port;
  378. if (ADODB_PHPVER >= 0x4300)
  379. $this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword,$this->clientFlags);
  380. else
  381. $this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
  382. if ($this->_connectionID === false) return false;
  383. if ($this->autoRollback) $this->RollbackTrans();
  384. if ($argDatabasename) return $this->SelectDB($argDatabasename);
  385. return true;
  386. }
  387. function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  388. {
  389. $this->forceNewConnect = true;
  390. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
  391. }
  392. function MetaColumns($table, $normalize=true)
  393. {
  394. $this->_findschema($table,$schema);
  395. if ($schema) {
  396. $dbName = $this->database;
  397. $this->SelectDB($schema);
  398. }
  399. global $ADODB_FETCH_MODE;
  400. $save = $ADODB_FETCH_MODE;
  401. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  402. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  403. $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  404. if ($schema) {
  405. $this->SelectDB($dbName);
  406. }
  407. if (isset($savem)) $this->SetFetchMode($savem);
  408. $ADODB_FETCH_MODE = $save;
  409. if (!is_object($rs)) {
  410. $false = false;
  411. return $false;
  412. }
  413. $retarr = array();
  414. while (!$rs->EOF){
  415. $fld = new ADOFieldObject();
  416. $fld->name = $rs->fields[0];
  417. $type = $rs->fields[1];
  418. // split type into type(length):
  419. $fld->scale = null;
  420. if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
  421. $fld->type = $query_array[1];
  422. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  423. $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  424. } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
  425. $fld->type = $query_array[1];
  426. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  427. } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
  428. $fld->type = $query_array[1];
  429. $arr = explode(",",$query_array[2]);
  430. $fld->enums = $arr;
  431. $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
  432. $fld->max_length = ($zlen > 0) ? $zlen : 1;
  433. } else {
  434. $fld->type = $type;
  435. $fld->max_length = -1;
  436. }
  437. $fld->not_null = ($rs->fields[2] != 'YES');
  438. $fld->primary_key = ($rs->fields[3] == 'PRI');
  439. $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  440. $fld->binary = (strpos($type,'blob') !== false || strpos($type,'binary') !== false);
  441. $fld->unsigned = (strpos($type,'unsigned') !== false);
  442. $fld->zerofill = (strpos($type,'zerofill') !== false);
  443. if (!$fld->binary) {
  444. $d = $rs->fields[4];
  445. if ($d != '' && $d != 'NULL') {
  446. $fld->has_default = true;
  447. $fld->default_value = $d;
  448. } else {
  449. $fld->has_default = false;
  450. }
  451. }
  452. if ($save == ADODB_FETCH_NUM) {
  453. $retarr[] = $fld;
  454. } else {
  455. $retarr[strtoupper($fld->name)] = $fld;
  456. }
  457. $rs->MoveNext();
  458. }
  459. $rs->Close();
  460. return $retarr;
  461. }
  462. // returns true or false
  463. function SelectDB($dbName)
  464. {
  465. $this->database = $dbName;
  466. $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
  467. if ($this->_connectionID) {
  468. return @mysql_select_db($dbName,$this->_connectionID);
  469. }
  470. else return false;
  471. }
  472. // parameters use PostgreSQL convention, not MySQL
  473. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
  474. {
  475. $offsetStr =($offset>=0) ? ((integer)$offset)."," : '';
  476. // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
  477. if ($nrows < 0) $nrows = '18446744073709551615';
  478. if ($secs)
  479. $rs = $this->CacheExecute($secs,$sql." LIMIT $offsetStr".((integer)$nrows),$inputarr);
  480. else
  481. $rs = $this->Execute($sql." LIMIT $offsetStr".((integer)$nrows),$inputarr);
  482. return $rs;
  483. }
  484. // returns queryID or false
  485. function _query($sql,$inputarr=false)
  486. {
  487. //global $ADODB_COUNTRECS;
  488. //if($ADODB_COUNTRECS)
  489. return mysql_query($sql,$this->_connectionID);
  490. //else return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6
  491. }
  492. /* Returns: the last error message from previous database operation */
  493. function ErrorMsg()
  494. {
  495. if ($this->_logsql) return $this->_errorMsg;
  496. if (empty($this->_connectionID)) $this->_errorMsg = @mysql_error();
  497. else $this->_errorMsg = @mysql_error($this->_connectionID);
  498. return $this->_errorMsg;
  499. }
  500. /* Returns: the last error number from previous database operation */
  501. function ErrorNo()
  502. {
  503. if ($this->_logsql) return $this->_errorCode;
  504. if (empty($this->_connectionID)) return @mysql_errno();
  505. else return @mysql_errno($this->_connectionID);
  506. }
  507. // returns true or false
  508. function _close()
  509. {
  510. @mysql_close($this->_connectionID);
  511. $this->charSet = '';
  512. $this->_connectionID = false;
  513. }
  514. /*
  515. * Maximum size of C field
  516. */
  517. function CharMax()
  518. {
  519. return 255;
  520. }
  521. /*
  522. * Maximum size of X field
  523. */
  524. function TextMax()
  525. {
  526. return 4294967295;
  527. }
  528. // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
  529. function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE )
  530. {
  531. global $ADODB_FETCH_MODE;
  532. if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC || $this->fetchMode == ADODB_FETCH_ASSOC) $associative = true;
  533. if ( !empty($owner) ) {
  534. $table = "$owner.$table";
  535. }
  536. $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
  537. if ($associative) {
  538. $create_sql = isset($a_create_table["Create Table"]) ? $a_create_table["Create Table"] : $a_create_table["Create View"];
  539. } else $create_sql = $a_create_table[1];
  540. $matches = array();
  541. if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false;
  542. $foreign_keys = array();
  543. $num_keys = count($matches[0]);
  544. for ( $i = 0; $i < $num_keys; $i ++ ) {
  545. $my_field = explode('`, `', $matches[1][$i]);
  546. $ref_table = $matches[2][$i];
  547. $ref_field = explode('`, `', $matches[3][$i]);
  548. if ( $upper ) {
  549. $ref_table = strtoupper($ref_table);
  550. }
  551. // see https://sourceforge.net/tracker/index.php?func=detail&aid=2287278&group_id=42718&atid=433976
  552. if (!isset($foreign_keys[$ref_table])) {
  553. $foreign_keys[$ref_table] = array();
  554. }
  555. $num_fields = count($my_field);
  556. for ( $j = 0; $j < $num_fields; $j ++ ) {
  557. if ( $associative ) {
  558. $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
  559. } else {
  560. $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
  561. }
  562. }
  563. }
  564. return $foreign_keys;
  565. }
  566. }
  567. /*--------------------------------------------------------------------------------------
  568. Class Name: Recordset
  569. --------------------------------------------------------------------------------------*/
  570. class ADORecordSet_mysql extends ADORecordSet{
  571. var $databaseType = "mysql";
  572. var $canSeek = true;
  573. function ADORecordSet_mysql($queryID,$mode=false)
  574. {
  575. if ($mode === false) {
  576. global $ADODB_FETCH_MODE;
  577. $mode = $ADODB_FETCH_MODE;
  578. }
  579. switch ($mode)
  580. {
  581. case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
  582. case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
  583. case ADODB_FETCH_DEFAULT:
  584. case ADODB_FETCH_BOTH:
  585. default:
  586. $this->fetchMode = MYSQL_BOTH; break;
  587. }
  588. $this->adodbFetchMode = $mode;
  589. $this->ADORecordSet($queryID);
  590. }
  591. function _initrs()
  592. {
  593. //GLOBAL $ADODB_COUNTRECS;
  594. // $this->_numOfRows = ($ADODB_COUNTRECS) ? @mysql_num_rows($this->_queryID):-1;
  595. $this->_numOfRows = @mysql_num_rows($this->_queryID);
  596. $this->_numOfFields = @mysql_num_fields($this->_queryID);
  597. }
  598. function FetchField($fieldOffset = -1)
  599. {
  600. if ($fieldOffset != -1) {
  601. $o = @mysql_fetch_field($this->_queryID, $fieldOffset);
  602. $f = @mysql_field_flags($this->_queryID,$fieldOffset);
  603. if ($o) $o->max_length = @mysql_field_len($this->_queryID,$fieldOffset); // suggested by: Jim Nicholson (jnich#att.com)
  604. //$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
  605. if ($o) $o->binary = (strpos($f,'binary')!== false);
  606. }
  607. else { /* The $fieldOffset argument is not provided thus its -1 */
  608. $o = @mysql_fetch_field($this->_queryID);
  609. //if ($o) $o->max_length = @mysql_field_len($this->_queryID); // suggested by: Jim Nicholson (jnich#att.com)
  610. $o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
  611. }
  612. return $o;
  613. }
  614. function GetRowAssoc($upper=true)
  615. {
  616. if ($this->fetchMode == MYSQL_ASSOC && !$upper) $row = $this->fields;
  617. else $row = ADORecordSet::GetRowAssoc($upper);
  618. return $row;
  619. }
  620. /* Use associative array to get fields array */
  621. function Fields($colname)
  622. {
  623. // added @ by "Michael William Miller" <mille562@pilot.msu.edu>
  624. if ($this->fetchMode != MYSQL_NUM) return @$this->fields[$colname];
  625. if (!$this->bind) {
  626. $this->bind = array();
  627. for ($i=0; $i < $this->_numOfFields; $i++) {
  628. $o = $this->FetchField($i);
  629. $this->bind[strtoupper($o->name)] = $i;
  630. }
  631. }
  632. return $this->fields[$this->bind[strtoupper($colname)]];
  633. }
  634. function _seek($row)
  635. {
  636. if ($this->_numOfRows == 0) return false;
  637. return @mysql_data_seek($this->_queryID,$row);
  638. }
  639. function MoveNext()
  640. {
  641. //return adodb_movenext($this);
  642. //if (defined('ADODB_EXTENSION')) return adodb_movenext($this);
  643. if (@$this->fields = mysql_fetch_array($this->_queryID,$this->fetchMode)) {
  644. $this->_currentRow += 1;
  645. return true;
  646. }
  647. if (!$this->EOF) {
  648. $this->_currentRow += 1;
  649. $this->EOF = true;
  650. }
  651. return false;
  652. }
  653. function _fetch()
  654. {
  655. $this->fields = @mysql_fetch_array($this->_queryID,$this->fetchMode);
  656. return is_array($this->fields);
  657. }
  658. function _close() {
  659. @mysql_free_result($this->_queryID);
  660. $this->_queryID = false;
  661. }
  662. function MetaType($t,$len=-1,$fieldobj=false)
  663. {
  664. if (is_object($t)) {
  665. $fieldobj = $t;
  666. $t = $fieldobj->type;
  667. $len = $fieldobj->max_length;
  668. }
  669. $len = -1; // mysql max_length is not accurate
  670. switch (strtoupper($t)) {
  671. case 'STRING':
  672. case 'CHAR':
  673. case 'VARCHAR':
  674. case 'TINYBLOB':
  675. case 'TINYTEXT':
  676. case 'ENUM':
  677. case 'SET':
  678. if ($len <= $this->blobSize) return 'C';
  679. case 'TEXT':
  680. case 'LONGTEXT':
  681. case 'MEDIUMTEXT':
  682. return 'X';
  683. // php_mysql extension always returns 'blob' even if 'text'
  684. // so we have to check whether binary...
  685. case 'IMAGE':
  686. case 'LONGBLOB':
  687. case 'BLOB':
  688. case 'MEDIUMBLOB':
  689. case 'BINARY':
  690. return !empty($fieldobj->binary) ? 'B' : 'X';
  691. case 'YEAR':
  692. case 'DATE': return 'D';
  693. case 'TIME':
  694. case 'DATETIME':
  695. case 'TIMESTAMP': return 'T';
  696. case 'INT':
  697. case 'INTEGER':
  698. case 'BIGINT':
  699. case 'TINYINT':
  700. case 'MEDIUMINT':
  701. case 'SMALLINT':
  702. if (!empty($fieldobj->primary_key)) return 'R';
  703. else return 'I';
  704. default: return 'N';
  705. }
  706. }
  707. }
  708. class ADORecordSet_ext_mysql extends ADORecordSet_mysql {
  709. function ADORecordSet_ext_mysql($queryID,$mode=false)
  710. {
  711. if ($mode === false) {
  712. global $ADODB_FETCH_MODE;
  713. $mode = $ADODB_FETCH_MODE;
  714. }
  715. switch ($mode)
  716. {
  717. case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
  718. case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
  719. case ADODB_FETCH_DEFAULT:
  720. case ADODB_FETCH_BOTH:
  721. default:
  722. $this->fetchMode = MYSQL_BOTH; break;
  723. }
  724. $this->adodbFetchMode = $mode;
  725. $this->ADORecordSet($queryID);
  726. }
  727. function MoveNext()
  728. {
  729. return @adodb_movenext($this);
  730. }
  731. }
  732. }
  733. ?>