<?php
namespace core\dzz;
use DB;
class Tpsqli extends Tpdb{
public function __construct($config=''){
if ( !extension_loaded('mysqli') ) {
echo "不支持mysqli";
exit;
}
if(!empty($config)) {
$this->config = $config;
if(empty($this->config['params'])) {
$this->config['params'] = '';
}
}
}
public function connect($config='',$linkNum=0) {
$this->linkID[$linkNum] =DB::linknum();
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
$this->linkID[$linkNum] = new \mysqli($config['hostname'],$config['username'],$config['password'],$config['database'],$config['hostport']?intval($config['hostport']):3306);
if (mysqli_connect_errno()){
echo "数据库连接错误";exit }
$dbVersion = $this->linkID[$linkNum]->server_version;
$this->linkID[$linkNum]->query("SET NAMES '".C('DB_CHARSET')."'");
if($dbVersion >'5.0.1'){
$this->linkID[$linkNum]->query("SET sql_mode=''");
}
$this->connected = true;
if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
}
return $this->linkID[$linkNum];
}
public function free() {
$this->queryID->free_result();
$this->queryID = null;
}
public function query($str) {
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
if ( $this->queryID ) $this->free();
$this->queryID = $this->_linkID->query($str);
if( $this->_linkID->more_results() ){
while (($res = $this->_linkID->next_result()) != NULL) {
$res->free_result();
}
}
$this->debug();
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = $this->queryID->num_rows;
$this->numCols = $this->queryID->field_count;
return $this->getAll();
}
}
public function execute($str) {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
if ( $this->queryID ) $this->free();
$result = $this->_linkID->query($str);
$this->debug();
if ( false === $result ) {
$this->error();
return false;
} else {
$this->numRows = $this->_linkID->affected_rows;
$this->lastInsID = $this->_linkID->insert_id;
return $this->numRows;
}
}
public function startTrans() {
$this->initConnect(true);
if ($this->transTimes == 0) {
$this->_linkID->autocommit(false);
}
$this->transTimes++;
return ;
}
public function commit() {
if ($this->transTimes > 0) {
$result = $this->_linkID->commit();
$this->_linkID->autocommit( true);
$this->transTimes = 0;
if(!$result){
$this->error();
return false;
}
}
return true;
}
public function rollback() {
if ($this->transTimes > 0) {
$result = $this->_linkID->rollback();
$this->_linkID->autocommit( true);
$this->transTimes = 0;
if(!$result){
$this->error();
return false;
}
}
return true;
}
private function getAll() {
$result = array();
if($this->numRows>0) {
for($i=0;$i<$this->numRows ;$i++ ){
$result[$i] = $this->queryID->fetch_assoc();
}
$this->queryID->data_seek(0);
}
return $result;
}
public function getFields($tableName) {
$result = $this->query('SHOW COLUMNS FROM '.$this->parseKey($tableName));
$info = array();
if($result) {
foreach ($result as $key => $val) {
$info[$val['Field']] = array(
'name' => $val['Field'],
'type' => $val['Type'],
'notnull' => (bool) ($val['Null'] === ''), 'default' => $val['Default'],
'primary' => (strtolower($val['Key']) == 'pri'),
'autoinc' => (strtolower($val['Extra']) == 'auto_increment'),
);
}
}
return $info;
}
public function getTables($dbName='') {
$sql = !empty($dbName)?'SHOW TABLES FROM '.$dbName:'SHOW TABLES ';
$result = $this->query($sql);
$info = array();
if($result) {
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
}
return $info;
}
public function replace($data,$options=array()) {
foreach ($data as $key=>$val){
$value = $this->parseValue($val);
if(is_scalar($value)) { $values[] = $value;
$fields[] = $this->parseKey($key);
}
}
$sql = 'REPLACE INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')';
return $this->execute($sql);
}
public function insertAll($datas,$options=array(),$replace=false) {
if(!is_array($datas[0])) return false;
$fields = array_keys($datas[0]);
array_walk($fields, array($this, 'parseKey'));
$values = array();
foreach ($datas as $data){
$value = array();
foreach ($data as $key=>$val){
$val = $this->parseValue($val);
if(is_scalar($val)) { $value[] = $val;
}
}
$values[] = '('.implode(',', $value).')';
}
$sql = ($replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES '.implode(',',$values);
return $this->execute($sql);
}
public function close() {
if ($this->_linkID){
$this->_linkID->close();
}
$this->_linkID = null;
}
public function error() {
$this->error = $this->_linkID->errno.':'.$this->_linkID->error;
if('' != $this->queryStr){
$this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
}
return $this->error;
}
public function escapeString($str) {
if($this->_linkID) {
return $this->_linkID->real_escape_string($str);
}else{
return addslashes($str);
}
}
protected function parseKey(&$key) {
$key = trim($key);
if(!preg_match('/[,\'\"\*\(\)`.\s]/',$key)) {
$key = '`'.$key.'`';
}
return $key;
}
}