PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/guzzisto/retrospect-gds
PHP | 1003 lines | 735 code | 142 blank | 126 comment | 125 complexity | 9c401778140f21555379443bbd6c6bcc 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 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. 21 October 2003: MySQLi extension implementation by Arjen de Rijke (a.de.rijke@xs4all.nl)
  11. Based on adodb 3.40
  12. */
  13. // security - hide paths
  14. if (!defined('ADODB_DIR')) die();
  15. if (! defined("_ADODB_MYSQLI_LAYER")) {
  16. define("_ADODB_MYSQLI_LAYER", 1 );
  17. // PHP5 compat...
  18. if (! defined("MYSQLI_BINARY_FLAG")) define("MYSQLI_BINARY_FLAG", 128);
  19. if (!defined('MYSQLI_READ_DEFAULT_GROUP')) define('MYSQLI_READ_DEFAULT_GROUP',1);
  20. // disable adodb extension - currently incompatible.
  21. global $ADODB_EXTENSION; $ADODB_EXTENSION = false;
  22. class ADODB_mysqli extends ADOConnection {
  23. var $databaseType = 'mysqli';
  24. var $dataProvider = 'native';
  25. var $hasInsertID = true;
  26. var $hasAffectedRows = true;
  27. var $metaTablesSQL = "SHOW TABLES";
  28. var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
  29. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  30. var $hasLimit = true;
  31. var $hasMoveFirst = true;
  32. var $hasGenID = true;
  33. var $isoDates = true; // accepts dates in ISO format
  34. var $sysDate = 'CURDATE()';
  35. var $sysTimeStamp = 'NOW()';
  36. var $hasTransactions = true;
  37. var $forceNewConnect = false;
  38. var $poorAffectedRows = true;
  39. var $clientFlags = 0;
  40. var $substr = "substring";
  41. var $port = false;
  42. var $socket = false;
  43. var $_bindInputArray = false;
  44. var $nameQuote = '`'; /// string to use to quote identifiers and names
  45. var $optionFlags = array(array(MYSQLI_READ_DEFAULT_GROUP,0));
  46. function ADODB_mysqli()
  47. {
  48. // if(!extension_loaded("mysqli"))
  49. ;//trigger_error("You must have the mysqli extension installed.", E_USER_ERROR);
  50. }
  51. function SetTransactionMode( $transaction_mode )
  52. {
  53. $this->_transmode = $transaction_mode;
  54. if (empty($transaction_mode)) {
  55. $this->Execute('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ');
  56. return;
  57. }
  58. if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
  59. $this->Execute("SET SESSION TRANSACTION ".$transaction_mode);
  60. }
  61. // returns true or false
  62. // To add: parameter int $port,
  63. // parameter string $socket
  64. function _connect($argHostname = NULL,
  65. $argUsername = NULL,
  66. $argPassword = NULL,
  67. $argDatabasename = NULL, $persist=false)
  68. {
  69. if(!extension_loaded("mysqli")) {
  70. return null;
  71. }
  72. $this->_connectionID = @mysqli_init();
  73. if (is_null($this->_connectionID)) {
  74. // mysqli_init only fails if insufficient memory
  75. if ($this->debug)
  76. ADOConnection::outp("mysqli_init() failed : " . $this->ErrorMsg());
  77. return false;
  78. }
  79. /*
  80. I suggest a simple fix which would enable adodb and mysqli driver to
  81. read connection options from the standard mysql configuration file
  82. /etc/my.cnf - "Bastien Duclaux" <bduclaux#yahoo.com>
  83. */
  84. foreach($this->optionFlags as $arr) {
  85. mysqli_options($this->_connectionID,$arr[0],$arr[1]);
  86. }
  87. #if (!empty($this->port)) $argHostname .= ":".$this->port;
  88. $ok = mysqli_real_connect($this->_connectionID,
  89. $argHostname,
  90. $argUsername,
  91. $argPassword,
  92. $argDatabasename,
  93. $this->port,
  94. $this->socket,
  95. $this->clientFlags);
  96. if ($ok) {
  97. if ($argDatabasename) return $this->SelectDB($argDatabasename);
  98. return true;
  99. } else {
  100. if ($this->debug)
  101. ADOConnection::outp("Could't connect : " . $this->ErrorMsg());
  102. return false;
  103. }
  104. }
  105. // returns true or false
  106. // How to force a persistent connection
  107. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  108. {
  109. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, true);
  110. }
  111. // When is this used? Close old connection first?
  112. // In _connect(), check $this->forceNewConnect?
  113. function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  114. {
  115. $this->forceNewConnect = true;
  116. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
  117. }
  118. function IfNull( $field, $ifNull )
  119. {
  120. return " IFNULL($field, $ifNull) "; // if MySQL
  121. }
  122. function ServerInfo()
  123. {
  124. $arr['description'] = $this->GetOne("select version()");
  125. $arr['version'] = ADOConnection::_findvers($arr['description']);
  126. return $arr;
  127. }
  128. function BeginTrans()
  129. {
  130. if ($this->transOff) return true;
  131. $this->transCnt += 1;
  132. $this->Execute('SET AUTOCOMMIT=0');
  133. $this->Execute('BEGIN');
  134. return true;
  135. }
  136. function CommitTrans($ok=true)
  137. {
  138. if ($this->transOff) return true;
  139. if (!$ok) return $this->RollbackTrans();
  140. if ($this->transCnt) $this->transCnt -= 1;
  141. $this->Execute('COMMIT');
  142. $this->Execute('SET AUTOCOMMIT=1');
  143. return true;
  144. }
  145. function RollbackTrans()
  146. {
  147. if ($this->transOff) return true;
  148. if ($this->transCnt) $this->transCnt -= 1;
  149. $this->Execute('ROLLBACK');
  150. $this->Execute('SET AUTOCOMMIT=1');
  151. return true;
  152. }
  153. function RowLock($tables,$where='',$flds='1 as adodb_ignore')
  154. {
  155. if ($this->transCnt==0) $this->BeginTrans();
  156. if ($where) $where = ' where '.$where;
  157. $rs =& $this->Execute("select $flds from $tables $where for update");
  158. return !empty($rs);
  159. }
  160. // if magic quotes disabled, use mysql_real_escape_string()
  161. // From readme.htm:
  162. // Quotes a string to be sent to the database. The $magic_quotes_enabled
  163. // parameter may look funny, but the idea is if you are quoting a
  164. // string extracted from a POST/GET variable, then
  165. // pass get_magic_quotes_gpc() as the second parameter. This will
  166. // ensure that the variable is not quoted twice, once by qstr and once
  167. // by the magic_quotes_gpc.
  168. //
  169. //Eg. $s = $db->qstr(_GET['name'],get_magic_quotes_gpc());
  170. function qstr($s, $magic_quotes = false)
  171. {
  172. if (!$magic_quotes) {
  173. if (PHP_VERSION >= 5)
  174. return "'" . mysqli_real_escape_string($this->_connectionID, $s) . "'";
  175. if ($this->replaceQuote[0] == '\\')
  176. $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
  177. return "'".str_replace("'",$this->replaceQuote,$s)."'";
  178. }
  179. // undo magic quotes for "
  180. $s = str_replace('\\"','"',$s);
  181. return "'$s'";
  182. }
  183. function _insertid()
  184. {
  185. $result = @mysqli_insert_id($this->_connectionID);
  186. if ($result == -1){
  187. if ($this->debug) ADOConnection::outp("mysqli_insert_id() failed : " . $this->ErrorMsg());
  188. }
  189. return $result;
  190. }
  191. // Only works for INSERT, UPDATE and DELETE query's
  192. function _affectedrows()
  193. {
  194. $result = @mysqli_affected_rows($this->_connectionID);
  195. if ($result == -1) {
  196. if ($this->debug) ADOConnection::outp("mysqli_affected_rows() failed : " . $this->ErrorMsg());
  197. }
  198. return $result;
  199. }
  200. // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
  201. // Reference on Last_Insert_ID on the recommended way to simulate sequences
  202. var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
  203. var $_genSeqSQL = "create table %s (id int not null)";
  204. var $_genSeq2SQL = "insert into %s values (%s)";
  205. var $_dropSeqSQL = "drop table %s";
  206. function CreateSequence($seqname='adodbseq',$startID=1)
  207. {
  208. if (empty($this->_genSeqSQL)) return false;
  209. $u = strtoupper($seqname);
  210. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  211. if (!$ok) return false;
  212. return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  213. }
  214. function GenID($seqname='adodbseq',$startID=1)
  215. {
  216. // post-nuke sets hasGenID to false
  217. if (!$this->hasGenID) return false;
  218. $getnext = sprintf($this->_genIDSQL,$seqname);
  219. $holdtransOK = $this->_transOK; // save the current status
  220. $rs = @$this->Execute($getnext);
  221. if (!$rs) {
  222. if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
  223. $u = strtoupper($seqname);
  224. $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  225. $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  226. $rs = $this->Execute($getnext);
  227. }
  228. $this->genID = mysqli_insert_id($this->_connectionID);
  229. if ($rs) $rs->Close();
  230. return $this->genID;
  231. }
  232. function &MetaDatabases()
  233. {
  234. $query = "SHOW DATABASES";
  235. $ret =& $this->Execute($query);
  236. if ($ret && is_object($ret)){
  237. $arr = array();
  238. while (!$ret->EOF){
  239. $db = $ret->Fields('Database');
  240. if ($db != 'mysql') $arr[] = $db;
  241. $ret->MoveNext();
  242. }
  243. return $arr;
  244. }
  245. return $ret;
  246. }
  247. function &MetaIndexes ($table, $primary = FALSE)
  248. {
  249. // save old fetch mode
  250. global $ADODB_FETCH_MODE;
  251. $false = false;
  252. $save = $ADODB_FETCH_MODE;
  253. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  254. if ($this->fetchMode !== FALSE) {
  255. $savem = $this->SetFetchMode(FALSE);
  256. }
  257. // get index details
  258. $rs = $this->Execute(sprintf('SHOW INDEXES FROM %s',$table));
  259. // restore fetchmode
  260. if (isset($savem)) {
  261. $this->SetFetchMode($savem);
  262. }
  263. $ADODB_FETCH_MODE = $save;
  264. if (!is_object($rs)) {
  265. return $false;
  266. }
  267. $indexes = array ();
  268. // parse index data into array
  269. while ($row = $rs->FetchRow()) {
  270. if ($primary == FALSE AND $row[2] == 'PRIMARY') {
  271. continue;
  272. }
  273. if (!isset($indexes[$row[2]])) {
  274. $indexes[$row[2]] = array(
  275. 'unique' => ($row[1] == 0),
  276. 'columns' => array()
  277. );
  278. }
  279. $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
  280. }
  281. // sort columns by order in the index
  282. foreach ( array_keys ($indexes) as $index )
  283. {
  284. ksort ($indexes[$index]['columns']);
  285. }
  286. return $indexes;
  287. }
  288. // Format date column in sql string given an input format that understands Y M D
  289. function SQLDate($fmt, $col=false)
  290. {
  291. if (!$col) $col = $this->sysTimeStamp;
  292. $s = 'DATE_FORMAT('.$col.",'";
  293. $concat = false;
  294. $len = strlen($fmt);
  295. for ($i=0; $i < $len; $i++) {
  296. $ch = $fmt[$i];
  297. switch($ch) {
  298. case 'Y':
  299. case 'y':
  300. $s .= '%Y';
  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 'M':
  310. $s .= '%b';
  311. break;
  312. case 'm':
  313. $s .= '%m';
  314. break;
  315. case 'D':
  316. case 'd':
  317. $s .= '%d';
  318. break;
  319. case 'H':
  320. $s .= '%H';
  321. break;
  322. case 'h':
  323. $s .= '%I';
  324. break;
  325. case 'i':
  326. $s .= '%i';
  327. break;
  328. case 's':
  329. $s .= '%s';
  330. break;
  331. case 'a':
  332. case 'A':
  333. $s .= '%p';
  334. break;
  335. case 'w':
  336. $s .= '%w';
  337. break;
  338. case 'l':
  339. $s .= '%W';
  340. break;
  341. default:
  342. if ($ch == '\\') {
  343. $i++;
  344. $ch = substr($fmt,$i,1);
  345. }
  346. $s .= $ch;
  347. break;
  348. }
  349. }
  350. $s.="')";
  351. if ($concat) $s = "CONCAT($s)";
  352. return $s;
  353. }
  354. // returns concatenated string
  355. // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
  356. function Concat()
  357. {
  358. $s = "";
  359. $arr = func_get_args();
  360. // suggestion by andrew005@mnogo.ru
  361. $s = implode(',',$arr);
  362. if (strlen($s) > 0) return "CONCAT($s)";
  363. else return '';
  364. }
  365. // dayFraction is a day in floating point
  366. function OffsetDate($dayFraction,$date=false)
  367. {
  368. if (!$date) $date = $this->sysDate;
  369. $fraction = $dayFraction * 24 * 3600;
  370. return $date . ' + INTERVAL ' . $fraction.' SECOND';
  371. // return "from_unixtime(unix_timestamp($date)+$fraction)";
  372. }
  373. function &MetaTables($ttype=false,$showSchema=false,$mask=false)
  374. {
  375. $save = $this->metaTablesSQL;
  376. if ($showSchema && is_string($showSchema)) {
  377. $this->metaTablesSQL .= " from $showSchema";
  378. }
  379. if ($mask) {
  380. $mask = $this->qstr($mask);
  381. $this->metaTablesSQL .= " like $mask";
  382. }
  383. $ret =& ADOConnection::MetaTables($ttype,$showSchema);
  384. $this->metaTablesSQL = $save;
  385. return $ret;
  386. }
  387. // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
  388. function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE )
  389. {
  390. global $ADODB_FETCH_MODE;
  391. if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC || $this->fetchMode == ADODB_FETCH_ASSOC) $associative = true;
  392. if ( !empty($owner) ) {
  393. $table = "$owner.$table";
  394. }
  395. $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
  396. if ($associative) $create_sql = $a_create_table["Create Table"];
  397. else $create_sql = $a_create_table[1];
  398. $matches = array();
  399. if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false;
  400. $foreign_keys = array();
  401. $num_keys = count($matches[0]);
  402. for ( $i = 0; $i < $num_keys; $i ++ ) {
  403. $my_field = explode('`, `', $matches[1][$i]);
  404. $ref_table = $matches[2][$i];
  405. $ref_field = explode('`, `', $matches[3][$i]);
  406. if ( $upper ) {
  407. $ref_table = strtoupper($ref_table);
  408. }
  409. $foreign_keys[$ref_table] = array();
  410. $num_fields = count($my_field);
  411. for ( $j = 0; $j < $num_fields; $j ++ ) {
  412. if ( $associative ) {
  413. $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
  414. } else {
  415. $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
  416. }
  417. }
  418. }
  419. return $foreign_keys;
  420. }
  421. function &MetaColumns($table)
  422. {
  423. $false = false;
  424. if (!$this->metaColumnsSQL)
  425. return $false;
  426. global $ADODB_FETCH_MODE;
  427. $save = $ADODB_FETCH_MODE;
  428. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  429. if ($this->fetchMode !== false)
  430. $savem = $this->SetFetchMode(false);
  431. $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  432. if (isset($savem)) $this->SetFetchMode($savem);
  433. $ADODB_FETCH_MODE = $save;
  434. if (!is_object($rs))
  435. return $false;
  436. $retarr = array();
  437. while (!$rs->EOF) {
  438. $fld = new ADOFieldObject();
  439. $fld->name = $rs->fields[0];
  440. $type = $rs->fields[1];
  441. // split type into type(length):
  442. $fld->scale = null;
  443. if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
  444. $fld->type = $query_array[1];
  445. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  446. $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  447. } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
  448. $fld->type = $query_array[1];
  449. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  450. } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
  451. $fld->type = $query_array[1];
  452. $fld->max_length = max(array_map("strlen",explode(",",$query_array[2]))) - 2; // PHP >= 4.0.6
  453. $fld->max_length = ($fld->max_length == 0 ? 1 : $fld->max_length);
  454. } else {
  455. $fld->type = $type;
  456. $fld->max_length = -1;
  457. }
  458. $fld->not_null = ($rs->fields[2] != 'YES');
  459. $fld->primary_key = ($rs->fields[3] == 'PRI');
  460. $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  461. $fld->binary = (strpos($type,'blob') !== false);
  462. $fld->unsigned = (strpos($type,'unsigned') !== false);
  463. if (!$fld->binary) {
  464. $d = $rs->fields[4];
  465. if ($d != '' && $d != 'NULL') {
  466. $fld->has_default = true;
  467. $fld->default_value = $d;
  468. } else {
  469. $fld->has_default = false;
  470. }
  471. }
  472. if ($save == ADODB_FETCH_NUM) {
  473. $retarr[] = $fld;
  474. } else {
  475. $retarr[strtoupper($fld->name)] = $fld;
  476. }
  477. $rs->MoveNext();
  478. }
  479. $rs->Close();
  480. return $retarr;
  481. }
  482. // returns true or false
  483. function SelectDB($dbName)
  484. {
  485. // $this->_connectionID = $this->mysqli_resolve_link($this->_connectionID);
  486. $this->database = $dbName;
  487. $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
  488. if ($this->_connectionID) {
  489. $result = @mysqli_select_db($this->_connectionID, $dbName);
  490. if (!$result) {
  491. ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->ErrorMsg());
  492. }
  493. return $result;
  494. }
  495. return false;
  496. }
  497. // parameters use PostgreSQL convention, not MySQL
  498. function &SelectLimit($sql,
  499. $nrows = -1,
  500. $offset = -1,
  501. $inputarr = false,
  502. $arg3 = false,
  503. $secs = 0)
  504. {
  505. $offsetStr = ($offset >= 0) ? "$offset," : '';
  506. if ($nrows < 0) $nrows = '18446744073709551615';
  507. if ($secs)
  508. $rs =& $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
  509. else
  510. $rs =& $this->Execute($sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
  511. return $rs;
  512. }
  513. function Prepare($sql)
  514. {
  515. return $sql;
  516. $stmt = $this->_connectionID->prepare($sql);
  517. if (!$stmt) {
  518. echo $this->ErrorMsg();
  519. return $sql;
  520. }
  521. return array($sql,$stmt);
  522. }
  523. // returns queryID or false
  524. function _query($sql, $inputarr)
  525. {
  526. global $ADODB_COUNTRECS;
  527. if (is_array($sql)) {
  528. $stmt = $sql[1];
  529. $a = '';
  530. foreach($inputarr as $k => $v) {
  531. if (is_string($v)) $a .= 's';
  532. else if (is_integer($v)) $a .= 'i';
  533. else $a .= 'd';
  534. }
  535. $fnarr = array_merge( array($stmt,$a) , $inputarr);
  536. $ret = call_user_func_array('mysqli_stmt_bind_param',$fnarr);
  537. $ret = mysqli_stmt_execute($stmt);
  538. return $ret;
  539. }
  540. if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) {
  541. if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->ErrorMsg());
  542. return false;
  543. }
  544. return $mysql_res;
  545. }
  546. /* Returns: the last error message from previous database operation */
  547. function ErrorMsg()
  548. {
  549. if (empty($this->_connectionID))
  550. $this->_errorMsg = @mysqli_connect_error();
  551. else
  552. $this->_errorMsg = @mysqli_error($this->_connectionID);
  553. return $this->_errorMsg;
  554. }
  555. /* Returns: the last error number from previous database operation */
  556. function ErrorNo()
  557. {
  558. if (empty($this->_connectionID))
  559. return @mysqli_connect_errno();
  560. else
  561. return @mysqli_errno($this->_connectionID);
  562. }
  563. // returns true or false
  564. function _close()
  565. {
  566. @mysqli_close($this->_connectionID);
  567. $this->_connectionID = false;
  568. }
  569. /*
  570. * Maximum size of C field
  571. */
  572. function CharMax()
  573. {
  574. return 255;
  575. }
  576. /*
  577. * Maximum size of X field
  578. */
  579. function TextMax()
  580. {
  581. return 4294967295;
  582. }
  583. // this is a set of functions for managing client encoding - very important if the encodings
  584. // of your database and your output target (i.e. HTML) don't match
  585. // for instance, you may have UTF8 database and server it on-site as latin1 etc.
  586. // GetCharSet - get the name of the character set the client is using now
  587. // Under Windows, the functions should work with MySQL 4.1.11 and above, the set of charsets supported
  588. // depends on compile flags of mysql distribution
  589. function GetCharSet()
  590. {
  591. //we will use ADO's builtin property charSet
  592. if (!method_exists($this->_connectionID,'character_set_name'))
  593. return false;
  594. $this->charSet = @$this->_connectionID->character_set_name();
  595. if (!$this->charSet) {
  596. return false;
  597. } else {
  598. return $this->charSet;
  599. }
  600. }
  601. // SetCharSet - switch the client encoding
  602. function SetCharSet($charset_name)
  603. {
  604. if (!method_exists($this->_connectionID,'set_charset'))
  605. return false;
  606. if ($this->charSet !== $charset_name) {
  607. $if = @$this->_connectionID->set_charset($charset_name);
  608. if ($if == "0" & $this->GetCharSet() == $charset_name) {
  609. return true;
  610. } else return false;
  611. } else return true;
  612. }
  613. }
  614. /*--------------------------------------------------------------------------------------
  615. Class Name: Recordset
  616. --------------------------------------------------------------------------------------*/
  617. class ADORecordSet_mysqli extends ADORecordSet{
  618. var $databaseType = "mysqli";
  619. var $canSeek = true;
  620. function ADORecordSet_mysqli($queryID, $mode = false)
  621. {
  622. if ($mode === false)
  623. {
  624. global $ADODB_FETCH_MODE;
  625. $mode = $ADODB_FETCH_MODE;
  626. }
  627. switch ($mode)
  628. {
  629. case ADODB_FETCH_NUM:
  630. $this->fetchMode = MYSQLI_NUM;
  631. break;
  632. case ADODB_FETCH_ASSOC:
  633. $this->fetchMode = MYSQLI_ASSOC;
  634. break;
  635. case ADODB_FETCH_DEFAULT:
  636. case ADODB_FETCH_BOTH:
  637. default:
  638. $this->fetchMode = MYSQLI_BOTH;
  639. break;
  640. }
  641. $this->adodbFetchMode = $mode;
  642. $this->ADORecordSet($queryID);
  643. }
  644. function _initrs()
  645. {
  646. global $ADODB_COUNTRECS;
  647. $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1;
  648. $this->_numOfFields = @mysqli_num_fields($this->_queryID);
  649. }
  650. /*
  651. 1 = MYSQLI_NOT_NULL_FLAG
  652. 2 = MYSQLI_PRI_KEY_FLAG
  653. 4 = MYSQLI_UNIQUE_KEY_FLAG
  654. 8 = MYSQLI_MULTIPLE_KEY_FLAG
  655. 16 = MYSQLI_BLOB_FLAG
  656. 32 = MYSQLI_UNSIGNED_FLAG
  657. 64 = MYSQLI_ZEROFILL_FLAG
  658. 128 = MYSQLI_BINARY_FLAG
  659. 256 = MYSQLI_ENUM_FLAG
  660. 512 = MYSQLI_AUTO_INCREMENT_FLAG
  661. 1024 = MYSQLI_TIMESTAMP_FLAG
  662. 2048 = MYSQLI_SET_FLAG
  663. 32768 = MYSQLI_NUM_FLAG
  664. 16384 = MYSQLI_PART_KEY_FLAG
  665. 32768 = MYSQLI_GROUP_FLAG
  666. 65536 = MYSQLI_UNIQUE_FLAG
  667. 131072 = MYSQLI_BINCMP_FLAG
  668. */
  669. function &FetchField($fieldOffset = -1)
  670. {
  671. $fieldnr = $fieldOffset;
  672. if ($fieldOffset != -1) {
  673. $fieldOffset = mysqli_field_seek($this->_queryID, $fieldnr);
  674. }
  675. $o = mysqli_fetch_field($this->_queryID);
  676. /* Properties of an ADOFieldObject as set by MetaColumns */
  677. $o->primary_key = $o->flags & MYSQLI_PRI_KEY_FLAG;
  678. $o->not_null = $o->flags & MYSQLI_NOT_NULL_FLAG;
  679. $o->auto_increment = $o->flags & MYSQLI_AUTO_INCREMENT_FLAG;
  680. $o->binary = $o->flags & MYSQLI_BINARY_FLAG;
  681. // $o->blob = $o->flags & MYSQLI_BLOB_FLAG; /* not returned by MetaColumns */
  682. $o->unsigned = $o->flags & MYSQLI_UNSIGNED_FLAG;
  683. return $o;
  684. }
  685. function &GetRowAssoc($upper = true)
  686. {
  687. if ($this->fetchMode == MYSQLI_ASSOC && !$upper)
  688. return $this->fields;
  689. $row =& ADORecordSet::GetRowAssoc($upper);
  690. return $row;
  691. }
  692. /* Use associative array to get fields array */
  693. function Fields($colname)
  694. {
  695. if ($this->fetchMode != MYSQLI_NUM)
  696. return @$this->fields[$colname];
  697. if (!$this->bind) {
  698. $this->bind = array();
  699. for ($i = 0; $i < $this->_numOfFields; $i++) {
  700. $o = $this->FetchField($i);
  701. $this->bind[strtoupper($o->name)] = $i;
  702. }
  703. }
  704. return $this->fields[$this->bind[strtoupper($colname)]];
  705. }
  706. function _seek($row)
  707. {
  708. if ($this->_numOfRows == 0)
  709. return false;
  710. if ($row < 0)
  711. return false;
  712. mysqli_data_seek($this->_queryID, $row);
  713. $this->EOF = false;
  714. return true;
  715. }
  716. // 10% speedup to move MoveNext to child class
  717. // This is the only implementation that works now (23-10-2003).
  718. // Other functions return no or the wrong results.
  719. function MoveNext()
  720. {
  721. if ($this->EOF) return false;
  722. $this->_currentRow++;
  723. $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode);
  724. if (is_array($this->fields)) return true;
  725. $this->EOF = true;
  726. return false;
  727. }
  728. function _fetch()
  729. {
  730. $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode);
  731. return is_array($this->fields);
  732. }
  733. function _close()
  734. {
  735. mysqli_free_result($this->_queryID);
  736. $this->_queryID = false;
  737. }
  738. /*
  739. 0 = MYSQLI_TYPE_DECIMAL
  740. 1 = MYSQLI_TYPE_CHAR
  741. 1 = MYSQLI_TYPE_TINY
  742. 2 = MYSQLI_TYPE_SHORT
  743. 3 = MYSQLI_TYPE_LONG
  744. 4 = MYSQLI_TYPE_FLOAT
  745. 5 = MYSQLI_TYPE_DOUBLE
  746. 6 = MYSQLI_TYPE_NULL
  747. 7 = MYSQLI_TYPE_TIMESTAMP
  748. 8 = MYSQLI_TYPE_LONGLONG
  749. 9 = MYSQLI_TYPE_INT24
  750. 10 = MYSQLI_TYPE_DATE
  751. 11 = MYSQLI_TYPE_TIME
  752. 12 = MYSQLI_TYPE_DATETIME
  753. 13 = MYSQLI_TYPE_YEAR
  754. 14 = MYSQLI_TYPE_NEWDATE
  755. 247 = MYSQLI_TYPE_ENUM
  756. 248 = MYSQLI_TYPE_SET
  757. 249 = MYSQLI_TYPE_TINY_BLOB
  758. 250 = MYSQLI_TYPE_MEDIUM_BLOB
  759. 251 = MYSQLI_TYPE_LONG_BLOB
  760. 252 = MYSQLI_TYPE_BLOB
  761. 253 = MYSQLI_TYPE_VAR_STRING
  762. 254 = MYSQLI_TYPE_STRING
  763. 255 = MYSQLI_TYPE_GEOMETRY
  764. */
  765. function MetaType($t, $len = -1, $fieldobj = false)
  766. {
  767. if (is_object($t)) {
  768. $fieldobj = $t;
  769. $t = $fieldobj->type;
  770. $len = $fieldobj->max_length;
  771. }
  772. $len = -1; // mysql max_length is not accurate
  773. switch (strtoupper($t)) {
  774. case 'STRING':
  775. case 'CHAR':
  776. case 'VARCHAR':
  777. case 'TINYBLOB':
  778. case 'TINYTEXT':
  779. case 'ENUM':
  780. case 'SET':
  781. case MYSQLI_TYPE_TINY_BLOB :
  782. case MYSQLI_TYPE_CHAR :
  783. case MYSQLI_TYPE_STRING :
  784. case MYSQLI_TYPE_ENUM :
  785. case MYSQLI_TYPE_SET :
  786. case 253 :
  787. if ($len <= $this->blobSize) return 'C';
  788. case 'TEXT':
  789. case 'LONGTEXT':
  790. case 'MEDIUMTEXT':
  791. return 'X';
  792. // php_mysql extension always returns 'blob' even if 'text'
  793. // so we have to check whether binary...
  794. case 'IMAGE':
  795. case 'LONGBLOB':
  796. case 'BLOB':
  797. case 'MEDIUMBLOB':
  798. case MYSQLI_TYPE_BLOB :
  799. case MYSQLI_TYPE_LONG_BLOB :
  800. case MYSQLI_TYPE_MEDIUM_BLOB :
  801. return !empty($fieldobj->binary) ? 'B' : 'X';
  802. case 'YEAR':
  803. case 'DATE':
  804. case MYSQLI_TYPE_DATE :
  805. case MYSQLI_TYPE_YEAR :
  806. return 'D';
  807. case 'TIME':
  808. case 'DATETIME':
  809. case 'TIMESTAMP':
  810. case MYSQLI_TYPE_DATETIME :
  811. case MYSQLI_TYPE_NEWDATE :
  812. case MYSQLI_TYPE_TIME :
  813. case MYSQLI_TYPE_TIMESTAMP :
  814. return 'T';
  815. case 'INT':
  816. case 'INTEGER':
  817. case 'BIGINT':
  818. case 'TINYINT':
  819. case 'MEDIUMINT':
  820. case 'SMALLINT':
  821. case MYSQLI_TYPE_INT24 :
  822. case MYSQLI_TYPE_LONG :
  823. case MYSQLI_TYPE_LONGLONG :
  824. case MYSQLI_TYPE_SHORT :
  825. case MYSQLI_TYPE_TINY :
  826. if (!empty($fieldobj->primary_key)) return 'R';
  827. return 'I';
  828. // Added floating-point types
  829. // Maybe not necessery.
  830. case 'FLOAT':
  831. case 'DOUBLE':
  832. // case 'DOUBLE PRECISION':
  833. case 'DECIMAL':
  834. case 'DEC':
  835. case 'FIXED':
  836. default:
  837. //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>";
  838. return 'N';
  839. }
  840. } // function
  841. } // rs class
  842. }
  843. ?>