PageRenderTime 35ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/yousef_fadila/vtiger
PHP | 1000 lines | 734 code | 140 blank | 126 comment | 124 complexity | 2686ec5d7f56b5a789a76185705facc9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*
  3. V4.90 8 June 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)
  369. $date = $this->sysDate;
  370. return "from_unixtime(unix_timestamp($date)+($dayFraction)*24*3600)";
  371. }
  372. function &MetaTables($ttype=false,$showSchema=false,$mask=false)
  373. {
  374. $save = $this->metaTablesSQL;
  375. if ($showSchema && is_string($showSchema)) {
  376. $this->metaTablesSQL .= " from $showSchema";
  377. }
  378. if ($mask) {
  379. $mask = $this->qstr($mask);
  380. $this->metaTablesSQL .= " like $mask";
  381. }
  382. $ret =& ADOConnection::MetaTables($ttype,$showSchema);
  383. $this->metaTablesSQL = $save;
  384. return $ret;
  385. }
  386. // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
  387. function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE )
  388. {
  389. global $ADODB_FETCH_MODE;
  390. if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC || $this->fetchMode == ADODB_FETCH_ASSOC) $associative = true;
  391. if ( !empty($owner) ) {
  392. $table = "$owner.$table";
  393. }
  394. $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
  395. if ($associative) $create_sql = $a_create_table["Create Table"];
  396. else $create_sql = $a_create_table[1];
  397. $matches = array();
  398. if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false;
  399. $foreign_keys = array();
  400. $num_keys = count($matches[0]);
  401. for ( $i = 0; $i < $num_keys; $i ++ ) {
  402. $my_field = explode('`, `', $matches[1][$i]);
  403. $ref_table = $matches[2][$i];
  404. $ref_field = explode('`, `', $matches[3][$i]);
  405. if ( $upper ) {
  406. $ref_table = strtoupper($ref_table);
  407. }
  408. $foreign_keys[$ref_table] = array();
  409. $num_fields = count($my_field);
  410. for ( $j = 0; $j < $num_fields; $j ++ ) {
  411. if ( $associative ) {
  412. $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
  413. } else {
  414. $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
  415. }
  416. }
  417. }
  418. return $foreign_keys;
  419. }
  420. function &MetaColumns($table)
  421. {
  422. $false = false;
  423. if (!$this->metaColumnsSQL)
  424. return $false;
  425. global $ADODB_FETCH_MODE;
  426. $save = $ADODB_FETCH_MODE;
  427. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  428. if ($this->fetchMode !== false)
  429. $savem = $this->SetFetchMode(false);
  430. $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  431. if (isset($savem)) $this->SetFetchMode($savem);
  432. $ADODB_FETCH_MODE = $save;
  433. if (!is_object($rs))
  434. return $false;
  435. $retarr = array();
  436. while (!$rs->EOF) {
  437. $fld = new ADOFieldObject();
  438. $fld->name = $rs->fields[0];
  439. $type = $rs->fields[1];
  440. // split type into type(length):
  441. $fld->scale = null;
  442. if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
  443. $fld->type = $query_array[1];
  444. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  445. $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  446. } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
  447. $fld->type = $query_array[1];
  448. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  449. } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
  450. $fld->type = $query_array[1];
  451. $fld->max_length = max(array_map("strlen",explode(",",$query_array[2]))) - 2; // PHP >= 4.0.6
  452. $fld->max_length = ($fld->max_length == 0 ? 1 : $fld->max_length);
  453. } else {
  454. $fld->type = $type;
  455. $fld->max_length = -1;
  456. }
  457. $fld->not_null = ($rs->fields[2] != 'YES');
  458. $fld->primary_key = ($rs->fields[3] == 'PRI');
  459. $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  460. $fld->binary = (strpos($type,'blob') !== false);
  461. $fld->unsigned = (strpos($type,'unsigned') !== false);
  462. if (!$fld->binary) {
  463. $d = $rs->fields[4];
  464. if ($d != '' && $d != 'NULL') {
  465. $fld->has_default = true;
  466. $fld->default_value = $d;
  467. } else {
  468. $fld->has_default = false;
  469. }
  470. }
  471. if ($save == ADODB_FETCH_NUM) {
  472. $retarr[] = $fld;
  473. } else {
  474. $retarr[strtoupper($fld->name)] = $fld;
  475. }
  476. $rs->MoveNext();
  477. }
  478. $rs->Close();
  479. return $retarr;
  480. }
  481. // returns true or false
  482. function SelectDB($dbName)
  483. {
  484. // $this->_connectionID = $this->mysqli_resolve_link($this->_connectionID);
  485. $this->database = $dbName;
  486. $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
  487. if ($this->_connectionID) {
  488. $result = @mysqli_select_db($this->_connectionID, $dbName);
  489. if (!$result) {
  490. ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->ErrorMsg());
  491. }
  492. return $result;
  493. }
  494. return false;
  495. }
  496. // parameters use PostgreSQL convention, not MySQL
  497. function &SelectLimit($sql,
  498. $nrows = -1,
  499. $offset = -1,
  500. $inputarr = false,
  501. $arg3 = false,
  502. $secs = 0)
  503. {
  504. $offsetStr = ($offset >= 0) ? "$offset," : '';
  505. if ($nrows < 0) $nrows = '18446744073709551615';
  506. if ($secs)
  507. $rs =& $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
  508. else
  509. $rs =& $this->Execute($sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
  510. return $rs;
  511. }
  512. function Prepare($sql)
  513. {
  514. return $sql;
  515. $stmt = $this->_connectionID->prepare($sql);
  516. if (!$stmt) {
  517. echo $this->ErrorMsg();
  518. return $sql;
  519. }
  520. return array($sql,$stmt);
  521. }
  522. // returns queryID or false
  523. function _query($sql, $inputarr)
  524. {
  525. global $ADODB_COUNTRECS;
  526. if (is_array($sql)) {
  527. $stmt = $sql[1];
  528. $a = '';
  529. foreach($inputarr as $k => $v) {
  530. if (is_string($v)) $a .= 's';
  531. else if (is_integer($v)) $a .= 'i';
  532. else $a .= 'd';
  533. }
  534. $fnarr = array_merge( array($stmt,$a) , $inputarr);
  535. $ret = call_user_func_array('mysqli_stmt_bind_param',$fnarr);
  536. $ret = mysqli_stmt_execute($stmt);
  537. return $ret;
  538. }
  539. if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) {
  540. if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->ErrorMsg());
  541. return false;
  542. }
  543. return $mysql_res;
  544. }
  545. /* Returns: the last error message from previous database operation */
  546. function ErrorMsg()
  547. {
  548. if (empty($this->_connectionID))
  549. $this->_errorMsg = @mysqli_connect_error();
  550. else
  551. $this->_errorMsg = @mysqli_error($this->_connectionID);
  552. return $this->_errorMsg;
  553. }
  554. /* Returns: the last error number from previous database operation */
  555. function ErrorNo()
  556. {
  557. if (empty($this->_connectionID))
  558. return @mysqli_connect_errno();
  559. else
  560. return @mysqli_errno($this->_connectionID);
  561. }
  562. // returns true or false
  563. function _close()
  564. {
  565. @mysqli_close($this->_connectionID);
  566. $this->_connectionID = false;
  567. }
  568. /*
  569. * Maximum size of C field
  570. */
  571. function CharMax()
  572. {
  573. return 255;
  574. }
  575. /*
  576. * Maximum size of X field
  577. */
  578. function TextMax()
  579. {
  580. return 4294967295;
  581. }
  582. // this is a set of functions for managing client encoding - very important if the encodings
  583. // of your database and your output target (i.e. HTML) don't match
  584. // for instance, you may have UTF8 database and server it on-site as latin1 etc.
  585. // GetCharSet - get the name of the character set the client is using now
  586. // Under Windows, the functions should work with MySQL 4.1.11 and above, the set of charsets supported
  587. // depends on compile flags of mysql distribution
  588. function GetCharSet()
  589. {
  590. //we will use ADO's builtin property charSet
  591. if (!method_exists($this->_connectionID,'character_set_name'))
  592. return false;
  593. $this->charSet = @$this->_connectionID->character_set_name();
  594. if (!$this->charSet) {
  595. return false;
  596. } else {
  597. return $this->charSet;
  598. }
  599. }
  600. // SetCharSet - switch the client encoding
  601. function SetCharSet($charset_name)
  602. {
  603. if (!method_exists($this->_connectionID,'set_charset'))
  604. return false;
  605. if ($this->charSet !== $charset_name) {
  606. $if = @$this->_connectionID->set_charset($charset_name);
  607. if ($if == "0" & $this->GetCharSet() == $charset_name) {
  608. return true;
  609. } else return false;
  610. } else return true;
  611. }
  612. }
  613. /*--------------------------------------------------------------------------------------
  614. Class Name: Recordset
  615. --------------------------------------------------------------------------------------*/
  616. class ADORecordSet_mysqli extends ADORecordSet{
  617. var $databaseType = "mysqli";
  618. var $canSeek = true;
  619. function ADORecordSet_mysqli($queryID, $mode = false)
  620. {
  621. if ($mode === false)
  622. {
  623. global $ADODB_FETCH_MODE;
  624. $mode = $ADODB_FETCH_MODE;
  625. }
  626. switch ($mode)
  627. {
  628. case ADODB_FETCH_NUM:
  629. $this->fetchMode = MYSQLI_NUM;
  630. break;
  631. case ADODB_FETCH_ASSOC:
  632. $this->fetchMode = MYSQLI_ASSOC;
  633. break;
  634. case ADODB_FETCH_DEFAULT:
  635. case ADODB_FETCH_BOTH:
  636. default:
  637. $this->fetchMode = MYSQLI_BOTH;
  638. break;
  639. }
  640. $this->adodbFetchMode = $mode;
  641. $this->ADORecordSet($queryID);
  642. }
  643. function _initrs()
  644. {
  645. global $ADODB_COUNTRECS;
  646. $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1;
  647. $this->_numOfFields = @mysqli_num_fields($this->_queryID);
  648. }
  649. /*
  650. 1 = MYSQLI_NOT_NULL_FLAG
  651. 2 = MYSQLI_PRI_KEY_FLAG
  652. 4 = MYSQLI_UNIQUE_KEY_FLAG
  653. 8 = MYSQLI_MULTIPLE_KEY_FLAG
  654. 16 = MYSQLI_BLOB_FLAG
  655. 32 = MYSQLI_UNSIGNED_FLAG
  656. 64 = MYSQLI_ZEROFILL_FLAG
  657. 128 = MYSQLI_BINARY_FLAG
  658. 256 = MYSQLI_ENUM_FLAG
  659. 512 = MYSQLI_AUTO_INCREMENT_FLAG
  660. 1024 = MYSQLI_TIMESTAMP_FLAG
  661. 2048 = MYSQLI_SET_FLAG
  662. 32768 = MYSQLI_NUM_FLAG
  663. 16384 = MYSQLI_PART_KEY_FLAG
  664. 32768 = MYSQLI_GROUP_FLAG
  665. 65536 = MYSQLI_UNIQUE_FLAG
  666. 131072 = MYSQLI_BINCMP_FLAG
  667. */
  668. function &FetchField($fieldOffset = -1)
  669. {
  670. $fieldnr = $fieldOffset;
  671. if ($fieldOffset != -1) {
  672. $fieldOffset = mysqli_field_seek($this->_queryID, $fieldnr);
  673. }
  674. $o = mysqli_fetch_field($this->_queryID);
  675. /* Properties of an ADOFieldObject as set by MetaColumns */
  676. $o->primary_key = $o->flags & MYSQLI_PRI_KEY_FLAG;
  677. $o->not_null = $o->flags & MYSQLI_NOT_NULL_FLAG;
  678. $o->auto_increment = $o->flags & MYSQLI_AUTO_INCREMENT_FLAG;
  679. $o->binary = $o->flags & MYSQLI_BINARY_FLAG;
  680. // $o->blob = $o->flags & MYSQLI_BLOB_FLAG; /* not returned by MetaColumns */
  681. $o->unsigned = $o->flags & MYSQLI_UNSIGNED_FLAG;
  682. return $o;
  683. }
  684. function &GetRowAssoc($upper = true)
  685. {
  686. if ($this->fetchMode == MYSQLI_ASSOC && !$upper)
  687. return $this->fields;
  688. $row =& ADORecordSet::GetRowAssoc($upper);
  689. return $row;
  690. }
  691. /* Use associative array to get fields array */
  692. function Fields($colname)
  693. {
  694. if ($this->fetchMode != MYSQLI_NUM)
  695. return @$this->fields[$colname];
  696. if (!$this->bind) {
  697. $this->bind = array();
  698. for ($i = 0; $i < $this->_numOfFields; $i++) {
  699. $o = $this->FetchField($i);
  700. $this->bind[strtoupper($o->name)] = $i;
  701. }
  702. }
  703. return $this->fields[$this->bind[strtoupper($colname)]];
  704. }
  705. function _seek($row)
  706. {
  707. if ($this->_numOfRows == 0)
  708. return false;
  709. if ($row < 0)
  710. return false;
  711. mysqli_data_seek($this->_queryID, $row);
  712. $this->EOF = false;
  713. return true;
  714. }
  715. // 10% speedup to move MoveNext to child class
  716. // This is the only implementation that works now (23-10-2003).
  717. // Other functions return no or the wrong results.
  718. function MoveNext()
  719. {
  720. if ($this->EOF) return false;
  721. $this->_currentRow++;
  722. $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode);
  723. if (is_array($this->fields)) return true;
  724. $this->EOF = true;
  725. return false;
  726. }
  727. function _fetch()
  728. {
  729. $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode);
  730. return is_array($this->fields);
  731. }
  732. function _close()
  733. {
  734. mysqli_free_result($this->_queryID);
  735. $this->_queryID = false;
  736. }
  737. /*
  738. 0 = MYSQLI_TYPE_DECIMAL
  739. 1 = MYSQLI_TYPE_CHAR
  740. 1 = MYSQLI_TYPE_TINY
  741. 2 = MYSQLI_TYPE_SHORT
  742. 3 = MYSQLI_TYPE_LONG
  743. 4 = MYSQLI_TYPE_FLOAT
  744. 5 = MYSQLI_TYPE_DOUBLE
  745. 6 = MYSQLI_TYPE_NULL
  746. 7 = MYSQLI_TYPE_TIMESTAMP
  747. 8 = MYSQLI_TYPE_LONGLONG
  748. 9 = MYSQLI_TYPE_INT24
  749. 10 = MYSQLI_TYPE_DATE
  750. 11 = MYSQLI_TYPE_TIME
  751. 12 = MYSQLI_TYPE_DATETIME
  752. 13 = MYSQLI_TYPE_YEAR
  753. 14 = MYSQLI_TYPE_NEWDATE
  754. 247 = MYSQLI_TYPE_ENUM
  755. 248 = MYSQLI_TYPE_SET
  756. 249 = MYSQLI_TYPE_TINY_BLOB
  757. 250 = MYSQLI_TYPE_MEDIUM_BLOB
  758. 251 = MYSQLI_TYPE_LONG_BLOB
  759. 252 = MYSQLI_TYPE_BLOB
  760. 253 = MYSQLI_TYPE_VAR_STRING
  761. 254 = MYSQLI_TYPE_STRING
  762. 255 = MYSQLI_TYPE_GEOMETRY
  763. */
  764. function MetaType($t, $len = -1, $fieldobj = false)
  765. {
  766. if (is_object($t)) {
  767. $fieldobj = $t;
  768. $t = $fieldobj->type;
  769. $len = $fieldobj->max_length;
  770. }
  771. $len = -1; // mysql max_length is not accurate
  772. switch (strtoupper($t)) {
  773. case 'STRING':
  774. case 'CHAR':
  775. case 'VARCHAR':
  776. case 'TINYBLOB':
  777. case 'TINYTEXT':
  778. case 'ENUM':
  779. case 'SET':
  780. case MYSQLI_TYPE_TINY_BLOB :
  781. case MYSQLI_TYPE_CHAR :
  782. case MYSQLI_TYPE_STRING :
  783. case MYSQLI_TYPE_ENUM :
  784. case MYSQLI_TYPE_SET :
  785. case 253 :
  786. if ($len <= $this->blobSize) return 'C';
  787. case 'TEXT':
  788. case 'LONGTEXT':
  789. case 'MEDIUMTEXT':
  790. return 'X';
  791. // php_mysql extension always returns 'blob' even if 'text'
  792. // so we have to check whether binary...
  793. case 'IMAGE':
  794. case 'LONGBLOB':
  795. case 'BLOB':
  796. case 'MEDIUMBLOB':
  797. case MYSQLI_TYPE_BLOB :
  798. case MYSQLI_TYPE_LONG_BLOB :
  799. case MYSQLI_TYPE_MEDIUM_BLOB :
  800. return !empty($fieldobj->binary) ? 'B' : 'X';
  801. case 'YEAR':
  802. case 'DATE':
  803. case MYSQLI_TYPE_DATE :
  804. case MYSQLI_TYPE_YEAR :
  805. return 'D';
  806. case 'TIME':
  807. case 'DATETIME':
  808. case 'TIMESTAMP':
  809. case MYSQLI_TYPE_DATETIME :
  810. case MYSQLI_TYPE_NEWDATE :
  811. case MYSQLI_TYPE_TIME :
  812. case MYSQLI_TYPE_TIMESTAMP :
  813. return 'T';
  814. case 'INT':
  815. case 'INTEGER':
  816. case 'BIGINT':
  817. case 'TINYINT':
  818. case 'MEDIUMINT':
  819. case 'SMALLINT':
  820. case MYSQLI_TYPE_INT24 :
  821. case MYSQLI_TYPE_LONG :
  822. case MYSQLI_TYPE_LONGLONG :
  823. case MYSQLI_TYPE_SHORT :
  824. case MYSQLI_TYPE_TINY :
  825. if (!empty($fieldobj->primary_key)) return 'R';
  826. return 'I';
  827. // Added floating-point types
  828. // Maybe not necessery.
  829. case 'FLOAT':
  830. case 'DOUBLE':
  831. // case 'DOUBLE PRECISION':
  832. case 'DECIMAL':
  833. case 'DEC':
  834. case 'FIXED':
  835. default:
  836. //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>";
  837. return 'N';
  838. }
  839. } // function
  840. } // rs class
  841. }
  842. ?>