123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818 |
- <?php
-
- if (! defined("_ADODB_MYSQLI_LAYER")) {
- define("_ADODB_MYSQLI_LAYER", 1 );
-
- global $ADODB_EXTENSION; $ADODB_EXTENSION = false;
- class ADODB_mysqli extends ADOConnection {
- var $databaseType = 'mysqli';
- var $dataProvider = 'native';
- var $hasInsertID = true;
- var $hasAffectedRows = true;
- var $metaTablesSQL = "SHOW TABLES";
- var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
- var $fmtTimeStamp = "'Y-m-d H:i:s'";
- var $hasLimit = true;
- var $hasMoveFirst = true;
- var $hasGenID = true;
- var $isoDates = true;
- var $sysDate = 'CURDATE()';
- var $sysTimeStamp = 'NOW()';
- var $hasTransactions = false;
- var $forceNewConnect = false;
- var $poorAffectedRows = true;
- var $clientFlags = 0;
- var $substr = "substring";
- var $port = false;
- var $socket = false;
- var $_bindInputArray = false;
- var $nameQuote = '`';
-
- function ADODB_mysqli()
- {
- if(!extension_loaded("mysqli"))
- trigger_error("You must have the mysqli extension installed.", E_USER_ERROR);
-
- }
-
-
-
-
- function _connect($argHostname = NULL,
- $argUsername = NULL,
- $argPassword = NULL,
- $argDatabasename = NULL, $persist=false)
- {
- $this->_connectionID = @mysqli_init();
-
- if (is_null($this->_connectionID)) {
-
- if ($this->debug)
- ADOConnection::outp("mysqli_init() failed : " . $this->ErrorMsg());
- return false;
- }
-
-
-
- if (mysqli_real_connect($this->_connectionID,
- $argHostname,
- $argUsername,
- $argPassword,
- $argDatabasename,
- $this->port,
- $this->socket,
- $this->clientFlags))
- {
- if ($argDatabasename) return $this->SelectDB($argDatabasename);
-
-
- return true;
- }
- else {
- if ($this->debug)
- ADOConnection::outp("Could't connect : " . $this->ErrorMsg());
- return false;
- }
- }
-
-
-
- function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
- {
- return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, true);
- }
-
-
-
- function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
- {
- $this->forceNewConnect = true;
- return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
- }
-
- function IfNull( $field, $ifNull )
- {
- return " IFNULL($field, $ifNull) ";
- }
-
- function ServerInfo()
- {
- $arr['description'] = $this->GetOne("select version()");
- $arr['version'] = ADOConnection::_findvers($arr['description']);
- return $arr;
- }
-
-
- function BeginTrans()
- {
- if ($this->transOff) return true;
- $this->transCnt += 1;
- $this->Execute('SET AUTOCOMMIT=0');
- $this->Execute('BEGIN');
- return true;
- }
-
- function CommitTrans($ok=true)
- {
- if ($this->transOff) return true;
- if (!$ok) return $this->RollbackTrans();
-
- if ($this->transCnt) $this->transCnt -= 1;
- $this->Execute('COMMIT');
- $this->Execute('SET AUTOCOMMIT=1');
- return true;
- }
-
- function RollbackTrans()
- {
- if ($this->transOff) return true;
- if ($this->transCnt) $this->transCnt -= 1;
- $this->Execute('ROLLBACK');
- $this->Execute('SET AUTOCOMMIT=1');
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
- function qstr($s, $magic_quotes = false)
- {
- if (!$magic_quotes) {
- if (PHP_VERSION >= 5)
- return "'" . mysqli_real_escape_string($this->_connectionID, $s) . "'";
-
- if ($this->replaceQuote[0] == '\\')
- $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
- return "'".str_replace("'",$this->replaceQuote,$s)."'";
- }
-
- $s = str_replace('\\"','"',$s);
- return "'$s'";
- }
-
- function _insertid()
- {
- $result = @mysqli_insert_id($this->_connectionID);
- if ($result == -1){
- if ($this->debug) ADOConnection::outp("mysqli_insert_id() failed : " . $this->ErrorMsg());
- }
- return $result;
- }
-
-
- function _affectedrows()
- {
- $result = @mysqli_affected_rows($this->_connectionID);
- if ($result == -1) {
- if ($this->debug) ADOConnection::outp("mysqli_affected_rows() failed : " . $this->ErrorMsg());
- }
- return $result;
- }
-
-
-
- var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
- var $_genSeqSQL = "create table %s (id int not null)";
- var $_genSeq2SQL = "insert into %s values (%s)";
- var $_dropSeqSQL = "drop table %s";
-
- function CreateSequence($seqname='adodbseq',$startID=1)
- {
- if (empty($this->_genSeqSQL)) return false;
- $u = strtoupper($seqname);
-
- $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
- if (!$ok) return false;
- return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
- }
-
- function GenID($seqname='adodbseq',$startID=1)
- {
-
- if (!$this->hasGenID) return false;
-
- $getnext = sprintf($this->_genIDSQL,$seqname);
- $holdtransOK = $this->_transOK;
- $rs = @$this->Execute($getnext);
- if (!$rs) {
- if ($holdtransOK) $this->_transOK = true;
- $u = strtoupper($seqname);
- $this->Execute(sprintf($this->_genSeqSQL,$seqname));
- $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
- $rs = $this->Execute($getnext);
- }
- $this->genID = mysqli_insert_id($this->_connectionID);
-
- if ($rs) $rs->Close();
-
- return $this->genID;
- }
-
- function &MetaDatabases()
- {
- $query = "SHOW DATABASES";
- $ret =& $this->Execute($query);
- return $ret;
- }
-
- function &MetaIndexes ($table, $primary = FALSE)
- {
-
- global $ADODB_FETCH_MODE;
-
- $save = $ADODB_FETCH_MODE;
- $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
- if ($this->fetchMode !== FALSE) {
- $savem = $this->SetFetchMode(FALSE);
- }
-
-
- $rs = $this->Execute(sprintf('SHOW INDEXES FROM %s',$table));
-
-
- if (isset($savem)) {
- $this->SetFetchMode($savem);
- }
- $ADODB_FETCH_MODE = $save;
-
- if (!is_object($rs)) {
- return FALSE;
- }
-
- $indexes = array ();
-
-
- while ($row = $rs->FetchRow()) {
- if ($primary == FALSE AND $row[2] == 'PRIMARY') {
- continue;
- }
-
- if (!isset($indexes[$row[2]])) {
- $indexes[$row[2]] = array(
- 'unique' => ($row[1] == 0),
- 'columns' => array()
- );
- }
-
- $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
- }
-
-
- foreach ( array_keys ($indexes) as $index )
- {
- ksort ($indexes[$index]['columns']);
- }
-
- return $indexes;
- }
-
-
- function SQLDate($fmt, $col=false)
- {
- if (!$col) $col = $this->sysTimeStamp;
- $s = 'DATE_FORMAT('.$col.",'";
- $concat = false;
- $len = strlen($fmt);
- for ($i=0; $i < $len; $i++) {
- $ch = $fmt[$i];
- switch($ch) {
- case 'Y':
- case 'y':
- $s .= '%Y';
- break;
- case 'Q':
- case 'q':
- $s .= "'),Quarter($col)";
-
- if ($len > $i+1) $s .= ",DATE_FORMAT($col,'";
- else $s .= ",('";
- $concat = true;
- break;
- case 'M':
- $s .= '%b';
- break;
-
- case 'm':
- $s .= '%m';
- break;
- case 'D':
- case 'd':
- $s .= '%d';
- break;
-
- case 'H':
- $s .= '%H';
- break;
-
- case 'h':
- $s .= '%I';
- break;
-
- case 'i':
- $s .= '%i';
- break;
-
- case 's':
- $s .= '%s';
- break;
-
- case 'a':
- case 'A':
- $s .= '%p';
- break;
-
- default:
-
- if ($ch == '\\') {
- $i++;
- $ch = substr($fmt,$i,1);
- }
- $s .= $ch;
- break;
- }
- }
- $s.="')";
- if ($concat) $s = "CONCAT($s)";
- return $s;
- }
-
-
-
- function Concat()
- {
- $s = "";
- $arr = func_get_args();
-
-
- $s = implode(',',$arr);
- if (strlen($s) > 0) return "CONCAT($s)";
- else return '';
- }
-
-
- function OffsetDate($dayFraction,$date=false)
- {
- if (!$date)
- $date = $this->sysDate;
- return "from_unixtime(unix_timestamp($date)+($dayFraction)*24*3600)";
- }
-
-
- function &MetaColumns($table)
- {
- if (!$this->metaColumnsSQL)
- return false;
-
- global $ADODB_FETCH_MODE;
- $save = $ADODB_FETCH_MODE;
- $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
- if ($this->fetchMode !== false)
- $savem = $this->SetFetchMode(false);
- $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
- if (isset($savem)) $this->SetFetchMode($savem);
- $ADODB_FETCH_MODE = $save;
- if (!is_object($rs))
- return false;
-
- $retarr = array();
- while (!$rs->EOF) {
- $fld = new ADOFieldObject();
- $fld->name = $rs->fields[0];
- $type = $rs->fields[1];
-
-
- $fld->scale = null;
- if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
- $fld->type = $query_array[1];
- $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
- $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
- } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
- $fld->type = $query_array[1];
- $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
- } else {
- $fld->type = $type;
- $fld->max_length = -1;
- }
- $fld->not_null = ($rs->fields[2] != 'YES');
- $fld->primary_key = ($rs->fields[3] == 'PRI');
- $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
- $fld->binary = (strpos($type,'blob') !== false);
- $fld->unsigned = (strpos($type,'unsigned') !== false);
- if (!$fld->binary) {
- $d = $rs->fields[4];
- if ($d != '' && $d != 'NULL') {
- $fld->has_default = true;
- $fld->default_value = $d;
- } else {
- $fld->has_default = false;
- }
- }
-
- if ($save == ADODB_FETCH_NUM) {
- $retarr[] = $fld;
- } else {
- $retarr[strtoupper($fld->name)] = $fld;
- }
- $rs->MoveNext();
- }
-
- $rs->Close();
- return $retarr;
- }
-
-
- function SelectDB($dbName)
- {
- $this->databaseName = $dbName;
- if ($this->_connectionID) {
- $result = @mysqli_select_db($this->_connectionID, $dbName);
- if (!$result) {
- ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->ErrorMsg());
- }
- return $result;
- }
- return false;
- }
-
-
- function &SelectLimit($sql,
- $nrows = -1,
- $offset = -1,
- $inputarr = false,
- $arg3 = false,
- $secs = 0)
- {
- $offsetStr = ($offset >= 0) ? "$offset," : '';
-
- if ($secs)
- $rs =& $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
- else
- $rs =& $this->Execute($sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
-
- return $rs;
- }
-
-
- function Prepare($sql)
- {
- return $sql;
-
- $stmt = $this->_connectionID->prepare($sql);
- if (!$stmt) {
- echo $this->ErrorMsg();
- return $sql;
- }
- return array($sql,$stmt);
- }
-
-
-
- function _query($sql, $inputarr)
- {
- global $ADODB_COUNTRECS;
-
- if (is_array($sql)) {
- $stmt = $sql[1];
- $a = '';
- foreach($inputarr as $k => $v) {
- if (is_string($v)) $a .= 's';
- else if (is_integer($v)) $a .= 'i';
- else $a .= 'd';
- }
-
- $fnarr =& array_merge( array($stmt,$a) , $inputarr);
- $ret = call_user_func_array('mysqli_stmt_bind_param',$fnarr);
- $ret = mysqli_stmt_execute($stmt);
- return $ret;
- }
- if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) {
- if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->ErrorMsg());
- return false;
- }
-
- return $mysql_res;
- }
-
- function ErrorMsg()
- {
- if (empty($this->_connectionID))
- $this->_errorMsg = @mysqli_error();
- else
- $this->_errorMsg = @mysqli_error($this->_connectionID);
- return $this->_errorMsg;
- }
-
-
- function ErrorNo()
- {
- if (empty($this->_connectionID))
- return @mysqli_errno();
- else
- return @mysqli_errno($this->_connectionID);
- }
-
-
- function _close()
- {
- @mysqli_close($this->_connectionID);
- $this->_connectionID = false;
- }
-
- function CharMax()
- {
- return 255;
- }
-
-
- function TextMax()
- {
- return 4294967295;
- }
- }
-
- class ADORecordSet_mysqli extends ADORecordSet{
-
- var $databaseType = "mysqli";
- var $canSeek = true;
-
- function ADORecordSet_mysqli($queryID, $mode = false)
- {
- if ($mode === false)
- {
- global $ADODB_FETCH_MODE;
- $mode = $ADODB_FETCH_MODE;
- }
-
- switch ($mode)
- {
- case ADODB_FETCH_NUM:
- $this->fetchMode = MYSQLI_NUM;
- break;
- case ADODB_FETCH_ASSOC:
- $this->fetchMode = MYSQLI_ASSOC;
- break;
- case ADODB_FETCH_DEFAULT:
- case ADODB_FETCH_BOTH:
- default:
- $this->fetchMode = MYSQLI_BOTH;
- break;
- }
- $this->adodbFetchMode = $mode;
- $this->ADORecordSet($queryID);
- }
-
- function _initrs()
- {
- global $ADODB_COUNTRECS;
-
- $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1;
- $this->_numOfFields = @mysqli_num_fields($this->_queryID);
- }
-
- function &FetchField($fieldOffset = -1)
- {
- $fieldnr = $fieldOffset;
- if ($fieldOffset != -1) {
- $fieldOffset = mysqli_field_seek($this->_queryID, $fieldnr);
- }
- $o = mysqli_fetch_field($this->_queryID);
- return $o;
- }
- function &GetRowAssoc($upper = true)
- {
- if ($this->fetchMode == MYSQLI_ASSOC && !$upper)
- return $this->fields;
- $row =& ADORecordSet::GetRowAssoc($upper);
- return $row;
- }
-
-
- function Fields($colname)
- {
- if ($this->fetchMode != MYSQLI_NUM)
- return @$this->fields[$colname];
-
- if (!$this->bind) {
- $this->bind = array();
- for ($i = 0; $i < $this->_numOfFields; $i++) {
- $o = $this->FetchField($i);
- $this->bind[strtoupper($o->name)] = $i;
- }
- }
- return $this->fields[$this->bind[strtoupper($colname)]];
- }
-
- function _seek($row)
- {
- if ($this->_numOfRows == 0)
- return false;
- if ($row < 0)
- return false;
- mysqli_data_seek($this->_queryID, $row);
- $this->EOF = false;
- return true;
- }
-
-
-
-
- function MoveNext()
- {
- if ($this->EOF) return false;
- $this->_currentRow++;
- $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode);
-
- if (is_array($this->fields)) return true;
- $this->EOF = true;
- return false;
- }
-
- function _fetch()
- {
- $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode);
- return is_array($this->fields);
- }
-
- function _close()
- {
- mysqli_free_result($this->_queryID);
- $this->_queryID = false;
- }
-
- function MetaType($t, $len = -1, $fieldobj = false)
- {
- if (is_object($t)) {
- $fieldobj = $t;
- $t = $fieldobj->type;
- $len = $fieldobj->max_length;
- }
-
-
- $len = -1;
- switch (strtoupper($t)) {
-
-
- case MYSQLI_TYPE_TINY_BLOB :
- case MYSQLI_TYPE_CHAR :
- case MYSQLI_TYPE_STRING :
- case MYSQLI_TYPE_ENUM :
- case MYSQLI_TYPE_SET :
- case 253 :
- if ($len <= $this->blobSize) return 'C';
-
-
- return 'X';
-
-
-
-
-
-
- case MYSQLI_TYPE_BLOB :
- case MYSQLI_TYPE_LONG_BLOB :
- case MYSQLI_TYPE_MEDIUM_BLOB :
-
- return !empty($fieldobj->binary) ? 'B' : 'X';
-
- case MYSQLI_TYPE_DATE :
- case MYSQLI_TYPE_YEAR :
-
- return 'D';
-
-
-
- case MYSQLI_TYPE_DATETIME :
- case MYSQLI_TYPE_NEWDATE :
- case MYSQLI_TYPE_TIME :
- case MYSQLI_TYPE_TIMESTAMP :
-
- return 'T';
-
-
- case MYSQLI_TYPE_INT24 :
- case MYSQLI_TYPE_LONG :
- case MYSQLI_TYPE_LONGLONG :
- case MYSQLI_TYPE_SHORT :
- case MYSQLI_TYPE_TINY :
-
- if (!empty($fieldobj->primary_key)) return 'R';
-
- return 'I';
-
-
- default:
- if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>";
- return 'N';
- }
- }
-
- }
-
- }
- ?>
|