PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/mcmis/reportico/adodb/drivers/adodb-mysqli.inc.php

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