PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/adodb/drivers/adodb-mysqli.inc.php

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