git-svn-id: http://xe-core.googlecode.com/svn/trunk@1079 201d5d3c-b55e-5fd7-737f-ddc643e51545

This commit is contained in:
zero 2007-04-10 08:49:56 +00:00
parent 2a13d510fb
commit 0e53c321df
12 changed files with 140 additions and 61 deletions

View file

@ -102,15 +102,13 @@
function setError($errno, $errstr) {
$this->errno = $errno;
$this->errstr = $errstr;
if(__DEBUG__ && $this->errno!=0) debugPrint(sprintf("Query Fail\t#%05d : %s - %s\n\t\t%s", $GLOBALS['__dbcnt'], $errno, $errstr, $this->query));
}
/**
* @brief 접속되었는지 return
**/
function isConnected() {
return $this->is_connected;
return $this->is_connected?true:false;
}
/**
@ -164,8 +162,6 @@
if(!file_exists($cache_file)) return new Object(-1, 'msg_invalid_queryid');
if(__DEBUG__) $query_start = getMicroTime();
if($source_args) $args = clone($source_args);
$output = include($cache_file);
@ -187,14 +183,7 @@
break;
}
if(__DEBUG__) {
$query_end = getMicroTime();
$elapsed_time = $query_end - $query_start;
$GLOBALS['__db_elapsed_time__'] += $elapsed_time;
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.4f sec)\n\t %s\n", ++$GLOBALS['__dbcnt'], $query_id, $elapsed_time, $this->query);
}
if($this->errno!=0) return new Object($this->errno, $this->errstr);
if($this->errno!=0) return new Object($this->errno, $this->errstr);
if(is_a($output, 'Object') || is_subclass_of($output, 'Object')) return $output;
return new Object();
}

View file

@ -117,17 +117,35 @@
**/
function _query($query) {
if(!$this->isConnected()) return;
$this->query = $query;
if(__DEBUG__) $query_start = getMicroTime();
$this->setError(0,'success');
$result = @mysql_query($query, $this->fd);
if(__DEBUG__) {
$query_end = getMicroTime();
$elapsed_time = $query_end - $query_start;
$GLOBALS['__db_elapsed_time__'] += $elapsed_time;
}
if(mysql_error()) {
$this->setError(mysql_errno(), mysql_error());
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n\t Fail : %d\n\t\t %s\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time, $this->errno, $this->errstr);
}
return;
}
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time);
}
return $result;
}

View file

@ -12,8 +12,6 @@
class DBMysqli extends DB {
var $handler = null;
var $hostname = '127.0.0.1'; ///< hostname
var $userid = NULL; ///< user id
var $password = NULL; ///< password
@ -73,14 +71,16 @@
if(!$this->hostname || !$this->userid || !$this->password || !$this->database) return;
// 접속시도
$this->handler = new mysqli($this->hostname, $this->userid, $this->password, $this->database);
$this->fd = @mysqli_connect($this->hostname, $this->userid, $this->password, $this->database);
// 접속체크
if(mysqli_connect_error()) $this->is_connected = false;
else $this->is_connected = true;
if(mysqli_connect_error()) {
$this->setError(-1, mysqli_connect_error());
return $this->is_connected = false;
}
// mysql의 경우 utf8임을 지정
$this->handler->query("SET NAMES 'utf8'");
$this->is_connected = true;
$this->_query("SET NAMES 'utf8'");
}
/**
@ -88,15 +88,16 @@
**/
function close() {
if(!$this->isConnected()) return;
$this->handler->close();
@mysqli_close();
}
/**
* @brief 쿼리에서 입력되는 문자열 변수들의 quotation 조절
**/
function addQuotes($string) {
if(!$this->fd) return $string;
if(get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(!is_numeric($string)) $string = $this->handler->real_escape_string($string);
if(!is_numeric($string)) $string = mysqli_escape_string($this->fd, $string);
return $string;
}
@ -111,17 +112,36 @@
**/
function _query($query) {
if(!$this->isConnected()) return;
$this->query = $query;
if(__DEBUG__) $query_start = getMicroTime();
$this->setError(0,'success');
$result = $this->handler->query($query);
$result = mysqli_query($this->fd,$query);
if(__DEBUG__) {
$query_end = getMicroTime();
$elapsed_time = $query_end - $query_start;
$GLOBALS['__db_elapsed_time__'] += $elapsed_time;
}
if(mysqli_errno($this->fd)) {
$this->setError(mysqli_errno($this->fd), mysqli_error($this->fd));
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n\t Fail : %d\n\t\t %s\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time, $this->errno, $this->errstr);
}
if($this->handler->error) {
$this->setError($this->handler->errno, $this->handler->error);
return;
}
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time);
}
return $result;
}
@ -129,18 +149,27 @@
* @brief 트랜잭션 시작
**/
function begin() {
if(!$this->is_connected || $this->transaction_started) return;
$this->_query('begin');
$this->transaction_started = true;
}
/**
* @brief 롤백
**/
function rollback() {
if(!$this->is_connected || !$this->transaction_started) return;
$this->_query('rollback');
$this->transaction_started = false;
}
/**
* @brief 커밋
**/
function commit() {
if(!$this->is_connected || !$this->transaction_started) return;
$this->_query('commit');
$this->transaction_started = false;
}
/**
@ -149,7 +178,7 @@
function _fetch($result) {
if($this->isError() || !$result) return;
while($tmp = $result->fetch_object()) {
while($tmp = mysqli_fetch_object($result)) {
$output[] = $tmp;
}
if(count($output)==1) return $output[0];
@ -162,7 +191,7 @@
function getNextSequence() {
$query = sprintf("insert into `%ssequence` (seq) values ('')", $this->prefix);
$this->_query($query);
return $this->handler->insert_id;
return mysqli_insert_id($this->fd);
}
/**
@ -254,7 +283,7 @@
}
}
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"), "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci");
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"), "type=innodb CHARACTER SET utf8 COLLATE utf8_general_ci");
$output = $this->_query($schema);
if(!$output) return false;

View file

@ -65,7 +65,6 @@
// 데이터 베이스 파일 접속 시도
$this->fd = sqlite_open($this->database, 0666, &$error);
if(!file_exists($this->database) || $error) {
//$this->setError(-1,'permission denied to access database');
$this->setError(-1,$error);
$this->is_connected = false;
return;
@ -133,14 +132,33 @@
if(!$this->isConnected()) return false;
$this->query = $query;
if(__DEBUG__) $query_start = getMicroTime();
$this->setError(0,'success');
$result = @sqlite_query($query, $this->fd);
if(__DEBUG__) {
$query_end = getMicroTime();
$elapsed_time = $query_end - $query_start;
$GLOBALS['__db_elapsed_time__'] += $elapsed_time;
}
if(sqlite_last_error($this->fd)) {
$this->setError(sqlite_last_error($this->fd), sqlite_error_string(sqlite_last_error($this->fd)));
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n\t Fail : %d\n\t\t %s\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time, $this->errno, $this->errstr);
}
return false;
}
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time);
}
if($result) return $result;
return true;
}
@ -260,15 +278,15 @@
if(count($unique_list)) {
foreach($unique_list as $key => $val) {
$query = sprintf('CREATE UNIQUE INDEX IF NOT EXISTS %s (%s)', $key, implode(',',$val));
$this->_query($schema);
$query = sprintf('CREATE UNIQUE INDEX %s_%s ON %s (%s)', $this->addQuotes($table_name), $key, $this->addQuotes($table_name), implode(',',$val));
$this->_query($query);
}
}
if(count($unique_list)) {
foreach($unique_list as $key => $val) {
$query = sprintf('CREATE INDEX IF NOT EXISTS %s (%s)', $key, implode(',',$val));
$this->_query($schema);
if(count($index_list)) {
foreach($index_list as $key => $val) {
$query = sprintf('CREATE INDEX %s_%s ON %s (%s)', $this->addQuotes($table_name), $key, $this->addQuotes($table_name), implode(',',$val));
$this->_query($query);
}
}
}

View file

@ -13,8 +13,6 @@
var $bind_idx = 0;
var $bind_vars = array();
var $debugDetail = false;
var $database = NULL; ///< database
var $prefix = 'zb'; ///< 제로보드에서 사용할 테이블들의 prefix (한 DB에서 여러개의 제로보드 설치 가능)
@ -155,19 +153,35 @@
function _execute() {
if(!$this->isConnected() || !$this->stmt) return;
if(__DEBUG__) $query_start = getMicroTime();
$this->stmt->execute();
if($this->debugDetail && $this->stmt->errorCode()!='00000') debugPrint($this->query."\n".$this->stmt->errorCode()." : ".print_r($this->stmt->errorInfo(),true)."\n".print_r($this->bind_vars,true));
if(__DEBUG__) {
$query_end = getMicroTime();
$elapsed_time = $query_end - $query_start;
$GLOBALS['__db_elapsed_time__'] += $elapsed_time;
}
$this->bind_idx = 0;
$this->bind_vars = 0;
if($this->stmt->errorCode()!='00000') {
$this->setError($this->stmt->errorCode(),print_r($this->stmt->errorInfo(),true));
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n\t Fail : %d\n\t\t %s\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time, $this->errno, $this->errstr);
}
$this->stmt = null;
return false;
}
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s (%0.6f sec)\n", ++$GLOBALS['__dbcnt'], $this->query, $elapsed_time);
}
$output = null;
while($tmp = $this->stmt->fetch(PDO::FETCH_ASSOC)) {
unset($obj);