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

This commit is contained in:
zero 2007-04-12 08:28:04 +00:00
parent 1ea27b0635
commit a35fd84e0e
7 changed files with 380 additions and 313 deletions

View file

@ -125,7 +125,7 @@
} }
function actFinish() { function actFinish() {
if(!__DEBUG__) return; if(!__DEBUG__ || !$this->query) return;
$this->act_finish = getMicroTime(); $this->act_finish = getMicroTime();
$elapsed_time = $this->act_finish - $this->act_start; $elapsed_time = $this->act_finish - $this->act_start;
$GLOBALS['__db_elapsed_time__'] += $elapsed_time; $GLOBALS['__db_elapsed_time__'] += $elapsed_time;
@ -138,6 +138,7 @@
$str .= "\t Query Success\n"; $str .= "\t Query Success\n";
} }
$GLOBALS['__db_queries__'] .= $str; $GLOBALS['__db_queries__'] .= $str;
$this->query = null;
} }
/** /**

View file

@ -236,12 +236,12 @@
$auto_increment = $column->attrs->auto_increment; $auto_increment = $column->attrs->auto_increment;
$column_schema[] = sprintf('`%s` %s%s %s %s %s', $column_schema[] = sprintf('`%s` %s%s %s %s %s',
$name, $name,
$this->column_type[$type], $this->column_type[$type],
$size?'('.$size.')':'', $size?'('.$size.')':'',
$default?"default '".$default."'":'', $default?"default '".$default."'":'',
$notnull?'not null':'', $notnull?'not null':'',
$auto_increment?'auto_increment':'' $auto_increment?'auto_increment':''
); );
if($primary_key) $primary_list[] = $name; if($primary_key) $primary_list[] = $name;
@ -342,9 +342,11 @@
// 컬럼 정리 // 컬럼 정리
foreach($output->columns as $key => $val) { foreach($output->columns as $key => $val) {
if(!isset($val['value'])) continue;
$name = $val['name']; $name = $val['name'];
$value = $val['value']; $value = $val['value'];
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'"; if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
else $value = (int)$value;
$column_list[] = sprintf("`%s` = %s", $name, $value); $column_list[] = sprintf("`%s` = %s", $name, $value);
} }

View file

@ -351,9 +351,11 @@
// 컬럼 정리 // 컬럼 정리
foreach($output->columns as $key => $val) { foreach($output->columns as $key => $val) {
if(!isset($val['value'])) continue;
$name = $val['name']; $name = $val['name'];
$value = $val['value']; $value = $val['value'];
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'"; if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
else $value = (int)$value;
$column_list[] = sprintf("`%s` = %s", $name, $value); $column_list[] = sprintf("`%s` = %s", $name, $value);
} }

View file

@ -10,6 +10,9 @@
class DBSqlite2 extends DB { class DBSqlite2 extends DB {
/**
* DB를 이용하기 위한 정보
**/
var $database = NULL; ///< database var $database = NULL; ///< database
var $prefix = 'xe'; ///< 제로보드에서 사용할 테이블들의 prefix (한 DB에서 여러개의 제로보드 설치 가능) var $prefix = 'xe'; ///< 제로보드에서 사용할 테이블들의 prefix (한 DB에서 여러개의 제로보드 설치 가능)
@ -74,6 +77,14 @@
$this->is_connected = true; $this->is_connected = true;
} }
/**
* @brief DB접속 해제
**/
function close() {
if(!$this->isConnected()) return;
sqlite_close($this->fd);
}
/** /**
* @brief 트랜잭션 시작 * @brief 트랜잭션 시작
**/ **/
@ -100,16 +111,6 @@
$this->transaction_started = false; $this->transaction_started = false;
} }
/**
* @brief DB접속 해제
**/
function close() {
if(!$this->isConnected()) return;
sqlite_close($this->fd);
}
/** /**
* @brief 쿼리에서 입력되는 문자열 변수들의 quotation 조절 * @brief 쿼리에서 입력되는 문자열 변수들의 quotation 조절
**/ **/
@ -129,38 +130,21 @@
* return\n * return\n
**/ **/
function _query($query) { function _query($query) {
if(!$this->isConnected()) return false; if(!$this->isConnected()) return;
$this->query = $query; // 쿼리 시작을 알림
$this->actStart($query);
if(__DEBUG__) $query_start = getMicroTime();
$this->setError(0,'success');
// 쿼리 문 실행
$result = @sqlite_query($query, $this->fd); $result = @sqlite_query($query, $this->fd);
if(__DEBUG__) { // 오류 체크
$query_end = getMicroTime(); if(sqlite_last_error($this->fd)) $this->setError(sqlite_last_error($this->fd), sqlite_error_string(sqlite_last_error($this->fd)));
$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))); $this->actFinish();
if(__DEBUG__) { return $result;
$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;
} }
/** /**
@ -292,78 +276,89 @@
} }
/** /**
* @brief 테이블 삭제 * @brief 조건문 작성하여 return
**/ **/
function dropTable($target_name) { function getCondition($output) {
$query = sprintf('DROP TABLE %s%s;', $this->prefix, $this->addQuotes($target_name)); if(!$output->conditions) return;
$this->_query($query);
}
/** foreach($output->conditions as $key => $val) {
* @brief 테이블의 이름 변경 $sub_condition = '';
**/ foreach($val['condition'] as $k =>$v) {
function renameTable($source_name, $targe_name) { if(!$v['value']) continue;
$query = sprintf("ALTER TABLE %s%s RENAME TO %s%s;", $this->prefix, $this->addQuotes($source_name), $this->prefix, $this->addQuotes($targe_name));
$this->_query($query);
}
/** $name = $v['column'];
* @brief 테이블을 비움 $operation = $v['operation'];
**/ $value = $v['value'];
function truncateTable($target_name) { $type = $output->column_type[$name];
$query = sprintf("VACUUM %s%s;", $this->prefix, $this->addQuotes($target_name)); $pipe = $v['pipe'];
$this->_query($query);
}
/** $value = $this->getConditionValue($name, $value, $operation, $type);
* @brief 테이블 데이터 Dump $str = $this->getConditionPart($name, $value, $operation);
* if($sub_condition) $sub_condition .= ' '.$pipe.' ';
* @todo 아직 미구현 $sub_condition .= $str;
**/ }
function dumpTable($target_name) { if($sub_condition) {
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
$condition .= '('.$sub_condition.')';
}
}
if($condition) $condition = ' where '.$condition;
return $condition;
} }
/** /**
* @brief insertAct 처리 * @brief insertAct 처리
**/ **/
function _executeInsertAct($tables, $column, $pass_quotes) { function _executeInsertAct($output) {
$table = array_pop($tables); // 테이블 정리
foreach($output->tables as $key => $val) {
foreach($column as $key => $val) { $table_list[] = $this->prefix.$key;
$key_list[] = $key;
if(in_array($key, $pass_quotes)) $val_list[] = $this->addQuotes($val);
else {
if(!is_numeric($val)) $val_list[] = '\''.$this->addQuotes($val).'\'';
else $val_list[] = $this->addQuotes($val);
}
} }
$query = sprintf("INSERT INTO %s%s (%s) VALUES (%s);", $this->prefix, $table, implode(',',$key_list), implode(',', $val_list)); // 컬럼 정리
foreach($output->columns as $key => $val) {
$name = $val['name'];
$value = $val['value'];
if($output->column_type[$name]!='number') {
$value = "'".$this->addQuotes($value)."'";
if(!$value) $value = 'null';
} else {
if(!$value) $value = 0;
}
$column_list[] = $name;
$value_list[] = $value;
}
$query = sprintf("insert into %s (%s) values (%s);", implode(',',$table_list), implode(',',$column_list), implode(',', $value_list));
return $this->_query($query); return $this->_query($query);
} }
/** /**
* @brief updateAct 처리 * @brief updateAct 처리
**/ **/
function _executeUpdateAct($tables, $column, $args, $condition, $pass_quotes) { function _executeUpdateAct($output) {
$table = array_pop($tables); // 테이블 정리
foreach($output->tables as $key => $val) {
foreach($column as $key => $val) { $table_list[] = $this->prefix.$key;
// args에 아예 해당 key가 없으면 패스
if(!isset($args->{$key})) continue;
if(in_array($key, $pass_quotes)) $update_list[] = sprintf('%s = %s', $key, $this->addQuotes($val));
else {
if(!is_numeric($val)) $update_list[] = sprintf('%s = \'%s\'', $key, $this->addQuotes($val));
else $update_list[] = sprintf('%s = %s', $key, $this->addQuotes($val));
}
} }
if(!count($update_list)) return;
$update_query = implode(',',$update_list);
if($condition) $condition = ' WHERE '.$condition; // 컬럼 정리
foreach($output->columns as $key => $val) {
if(!isset($val['value'])) continue;
$name = $val['name'];
$value = $val['value'];
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
else $value = (int)$value;
$query = sprintf("UPDATE %s%s SET %s %s;", $this->prefix, $table, $update_query, $condition); $column_list[] = sprintf("%s = %s", $name, $value);
}
// 조건절 정리
$condition = $this->getCondition($output);
$query = sprintf("update %s set %s %s", implode(',',$table_list), implode(',',$column_list), $condition);
return $this->_query($query); return $this->_query($query);
} }
@ -371,12 +366,17 @@
/** /**
* @brief deleteAct 처리 * @brief deleteAct 처리
**/ **/
function _executeDeleteAct($tables, $condition, $pass_quotes) { function _executeDeleteAct($output) {
$table = array_pop($tables); // 테이블 정리
foreach($output->tables as $key => $val) {
$table_list[] = $this->prefix.$key;
}
if($condition) $condition = ' WHERE '.$condition; // 조건절 정리
$condition = $this->getCondition($output);
$query = sprintf("delete from %s %s", implode(',',$table_list), $condition);
$query = sprintf("DELETE FROM %s%s %s;", $this->prefix, $table, $condition);
return $this->_query($query); return $this->_query($query);
} }
@ -386,34 +386,44 @@
* select의 경우 특정 페이지의 목록을 가져오는 것을 편하게 하기 위해\n * select의 경우 특정 페이지의 목록을 가져오는 것을 편하게 하기 위해\n
* navigation이라는 method를 제공 * navigation이라는 method를 제공
**/ **/
function _executeSelectAct($tables, $column, $invert_columns, $condition, $navigation, $group_script, $pass_quotes) { function _executeSelectAct($output) {
if(!count($tables)) $table = $this->prefix.array_pop($tables); // 테이블 정리
else { $table_list = array();
foreach($tables as $key => $val) $table_list[] = sprintf('%s%s as %s', $this->prefix, $key, $val); foreach($output->tables as $key => $val) {
} $table_list[] = $this->prefix.$key.' as '.$val;
$table = implode(',',$table_list);
if(!$column) $columns = '*';
else {
foreach($invert_columns as $key => $val) {
$column_list[] = sprintf('%s as %s',$val, $key);
}
$columns = implode(',', $column_list);
} }
if($condition) $condition = ' WHERE '.$condition; if(!$output->columns) {
$columns = '*';
if($navigation->list_count) return $this->_getNavigationData($table, $columns, $condition, $navigation); } else {
$column_list = array();
$query = sprintf("SELECT %s FROM %s %s", $columns, $table, $condition); foreach($output->columns as $key => $val) {
$name = $val['name'];
$query .= ' '.$group_script; $alias = $val['alias'];
if($name == '*') {
if($navigation->index) { $column_list[] = '*';
foreach($navigation->index as $index_obj) { } elseif(strpos($name,'.')===false && strpos($name,'(')===false) {
$index_list[] = sprintf('%s %s', $index_obj[0], $index_obj[1]); if($alias) $column_list[] = sprintf('%s as %s', $name, $alias);
else $column_list[] = sprintf('%s',$name);
} else {
if($alias) $column_list[] = sprintf('%s as %s', $name, $alias);
else $column_list[] = sprintf('%s',$name);
}
} }
if(count($index_list)) $query .= ' ORDER BY '.implode(',',$index_list); $columns = implode(',',$column_list);
}
$condition = $this->getCondition($output);
if($output->list_count) return $this->_getNavigationData($table_list, $columns, $condition, $output);
$query = sprintf("select %s from %s %s", $columns, implode(',',$table_list), $condition);
if($output->order) {
foreach($output->order as $key => $val) {
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
}
if(count($index_list)) $query .= ' order by '.implode(',',$index_list);
} }
$result = $this->_query($query); $result = $this->_query($query);
@ -430,29 +440,40 @@
* *
* 그닥 좋지는 않은 구조이지만 편리하다.. -_-; * 그닥 좋지는 않은 구조이지만 편리하다.. -_-;
**/ **/
function _getNavigationData($table, $columns, $condition, $navigation) { function _getNavigationData($table_list, $columns, $condition, $output) {
require_once('./classes/page/PageHandler.class.php'); require_once('./classes/page/PageHandler.class.php');
// 전체 개수를 구함 // 전체 개수를 구함
$count_query = sprintf("select count(*) as count from %s %s", $table, $condition); $count_query = sprintf("select count(*) as count from %s %s", implode(',',$table_list), $condition);
$result = $this->_query($count_query); $result = $this->_query($count_query);
$count_output = $this->_fetch($result); $count_output = $this->_fetch($result);
$total_count = (int)$count_output->count; $total_count = (int)$count_output->count;
$list_count = $output->list_count['value'];
if(!$list_count) $list_count = 20;
$page_count = $output->page_count['value'];
if(!$page_count) $page_count = 10;
$page = $output->page->value;
if(!$page) $page = 1;
// 전체 페이지를 구함 // 전체 페이지를 구함
$total_page = (int)(($total_count-1)/$navigation->list_count) +1; $total_page = (int)(($total_count-1)/$list_count) +1;
// 페이지 변수를 체크 // 페이지 변수를 체크
if($navigation->page > $total_page) $page = $navigation->page; if($page > $total_page) $page = $total_page;
else $page = $navigation->page; $start_count = ($page-1)*$list_count;
$start_count = ($page-1)*$navigation->list_count;
foreach($navigation->index as $index_obj) { $query = sprintf("select %s from %s %s", $columns, implode(',',$table_list), $condition);
$index_list[] = sprintf('%s %s', $index_obj[0], $index_obj[1]);
if($output->order) {
foreach($output->order as $key => $val) {
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
}
if(count($index_list)) $query .= ' order by '.implode(',',$index_list);
} }
$index = implode(',',$index_list); $query = sprintf('%s limit %d, %d', $query, $start_count, $list_count);
$query = sprintf('SELECT %s FROM %s %s ORDER BY %s LIMIT %d, %d', $columns, $table, $condition, $index, $start_count, $navigation->list_count);
$result = $this->_query($query); $result = $this->_query($query);
if($this->isError()) { if($this->isError()) {
$buff = new Object(); $buff = new Object();
@ -461,19 +482,21 @@
$buff->page = 1; $buff->page = 1;
$buff->data = array(); $buff->data = array();
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $navigation->page_count); $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
return $buff; return $buff;
} }
$virtual_no = $total_count - ($page-1)*$navigation->list_count; if($result) {
$tmp_data = $this->_fetch($result); $virtual_no = $total_count - ($page-1)*$list_count;
if($tmp_data) { while($tmp = sqlite_fetch_array($result, SQLITE_ASSOC)) {
if(!is_array($tmp_data)) $tmp_data = array($tmp_data); unset($obj);
foreach($tmp_data as $tmp) { foreach($tmp as $key => $val) {
$data[$virtual_no--] = $tmp; $pos = strpos($key, '.');
if($pos) $key = substr($key, $pos+1);
$obj->{$key} = $val;
}
$data[$virtual_no--] = $obj;
} }
} else {
$data = null;
} }
$buff = new Object(); $buff = new Object();
@ -482,7 +505,7 @@
$buff->page = $page; $buff->page = $page;
$buff->data = $data; $buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $navigation->page_count); $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
return $buff; return $buff;
} }
} }

View file

@ -8,14 +8,20 @@
class DBSqlite3_pdo extends DB { class DBSqlite3_pdo extends DB {
/**
* DB를 이용하기 위한 정보
**/
var $database = NULL; ///< database
var $prefix = 'xe'; ///< 제로보드에서 사용할 테이블들의 prefix (한 DB에서 여러개의 제로보드 설치 가능)
/**
* PDO 사용시 필요한 변수들
**/
var $handler = NULL; var $handler = NULL;
var $stmt = NULL; var $stmt = NULL;
var $bind_idx = 0; var $bind_idx = 0;
var $bind_vars = array(); var $bind_vars = array();
var $database = NULL; ///< database
var $prefix = 'xe'; ///< 제로보드에서 사용할 테이블들의 prefix (한 DB에서 여러개의 제로보드 설치 가능)
/** /**
* @brief sqlite3 에서 사용될 column type * @brief sqlite3 에서 사용될 column type
* *
@ -79,6 +85,14 @@
$this->is_connected = true; $this->is_connected = true;
} }
/**
* @brief DB접속 해제
**/
function close() {
if(!$this->isConnected()) return;
$this->commit();
}
/** /**
* @brief 트랜잭션 시작 * @brief 트랜잭션 시작
**/ **/
@ -105,20 +119,12 @@
$this->transaction_started = false; $this->transaction_started = false;
} }
/**
* @brief DB접속 해제
**/
function close() {
if(!$this->isConnected()) return;
}
/** /**
* @brief 쿼리에서 입력되는 문자열 변수들의 quotation 조절 * @brief 쿼리에서 입력되는 문자열 변수들의 quotation 조절
**/ **/
function addQuotes($string) { function addQuotes($string) {
if(get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string)); if(get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
if(!is_numeric($string)) $string = str_replace("'","''", $string); if(!is_numeric($string)) $string = str_replace("'","''",$string);
return $string; return $string;
} }
@ -128,22 +134,15 @@
function _prepare($query) { function _prepare($query) {
if(!$this->isConnected()) return; if(!$this->isConnected()) return;
$this->query = $query; // 쿼리 시작을 알림
$this->setError(0,'success'); $this->actStart($query);
$this->stmt = $this->handler->prepare($query); $this->stmt = $this->handler->prepare($query);
if($this->handler->errorCode() != '00000') { if($this->handler->errorCode() != '00000') {
$this->setError($this->handler->errorCode(), print_r($this->handler->errorInfo(),true)); $this->setError($this->handler->errorCode(), print_r($this->handler->errorInfo(),true));
$this->actFinish();
if(__DEBUG__) {
$GLOBALS['__db_queries__'] .= sprintf("\t%02d. %s\n\t Fail : %s\n\t\t %s\n", ++$GLOBALS['__dbcnt'], $this->query, $this->errno, $this->errstr);
}
unset($this->stmt);
return;
} }
$this->bind_idx = 0; $this->bind_idx = 0;
$this->bind_vars = array(); $this->bind_vars = array();
} }
@ -165,46 +164,25 @@
function _execute() { function _execute() {
if(!$this->isConnected() || !$this->stmt) return; if(!$this->isConnected() || !$this->stmt) return;
if(__DEBUG__) $query_start = getMicroTime();
$this->stmt->execute(); $this->stmt->execute();
if(__DEBUG__) { if($this->stmt->errorCode() === '00000') {
$query_end = getMicroTime(); $output = null;
$elapsed_time = $query_end - $query_start; while($tmp = $this->stmt->fetch(PDO::FETCH_ASSOC)) {
$GLOBALS['__db_elapsed_time__'] += $elapsed_time; unset($obj);
} foreach($tmp as $key => $val) {
$pos = strpos($key, '.');
$this->bind_idx = 0; if($pos) $key = substr($key, $pos+1);
$this->bind_vars = 0; $obj->{$key} = $val;
if($this->stmt->errorCode() != '00000') { }
$output[] = $obj;
}
} else {
$this->setError($this->stmt->errorCode(),print_r($this->stmt->errorInfo(),true)); $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);
foreach($tmp as $key => $val) {
$pos = strpos($key, '.');
if($pos) $key = substr($key, $pos+1);
$obj->{$key} = $val;
}
$output[] = $obj;
} }
$this->stmt = null; $this->stmt = null;
$this->actFinish();
if(is_array($output) && count($output)==1) return $output[0]; if(is_array($output) && count($output)==1) return $output[0];
return $output; return $output;
@ -325,99 +303,115 @@
} }
/** /**
* @brief 테이블 삭제 * @brief 조건문 작성하여 return
**/ **/
function dropTable($target_name) { function getCondition($output) {
$query = sprintf('DROP TABLE %s%s;', $this->prefix, $this->addQuotes($target_name)); if(!$output->conditions) return;
$this->_prepare($query);
$this->_execute();
}
/** foreach($output->conditions as $key => $val) {
* @brief 테이블의 이름 변경 $sub_condition = '';
**/ foreach($val['condition'] as $k =>$v) {
function renameTable($source_name, $targe_name) { if(!$v['value']) continue;
$query = sprintf("ALTER TABLE %s%s RENAME TO %s%s;", $this->prefix, $this->addQuotes($source_name), $this->prefix, $this->addQuotes($targe_name));
$this->_prepare($query);
$this->_execute();
}
/** $name = $v['column'];
* @brief 테이블을 비움 $operation = $v['operation'];
**/ $value = $v['value'];
function truncateTable($target_name) { $type = $output->column_type[$name];
$query = sprintf("VACUUM %s%s;", $this->prefix, $this->addQuotes($target_name)); $pipe = $v['pipe'];
$this->_prepare($query);
$this->_execute();
}
/** $value = $this->getConditionValue($name, $value, $operation, $type);
* @brief 테이블 데이터 Dump $str = $this->getConditionPart($name, $value, $operation);
* if($sub_condition) $sub_condition .= ' '.$pipe.' ';
* @todo 아직 미구현 $sub_condition .= $str;
**/ }
function dumpTable($target_name) { if($sub_condition) {
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
$condition .= '('.$sub_condition.')';
}
}
if($condition) $condition = ' where '.$condition;
return $condition;
} }
/** /**
* @brief insertAct 처리 * @brief insertAct 처리
**/ **/
function _executeInsertAct($tables, $column, $pass_quotes) { function _executeInsertAct($output) {
$table = array_pop($tables); // 테이블 정리
foreach($output->tables as $key => $val) {
$table_list[] = $this->prefix.$key;
}
// 컬럼 정리
foreach($output->columns as $key => $val) {
$name = $val['name'];
$value = $val['value'];
$key_list[] = $name;
if($output->column_type[$name]!='number') $val_list[] = $this->addQuotes($value);
else $val_list[] = (int)$value;
foreach($column as $key => $val) {
$key_list[] = $key;
$val_list[] = $this->addQuotes($val);
$prepare_list[] = '?'; $prepare_list[] = '?';
} }
$query = sprintf("INSERT INTO %s%s (%s) VALUES (%s);", $this->prefix, $table, implode(',',$key_list), implode(',',$prepare_list)); $query = sprintf("INSERT INTO %s (%s) VALUES (%s);", implode(',',$table_list), implode(',',$key_list), implode(',',$prepare_list));
$this->_prepare($query); $this->_prepare($query);
$val_count = count($val_list); $val_count = count($val_list);
for($i=0;$i<$val_count;$i++) $this->_bind($val_list[$i]); for($i=0;$i<$val_count;$i++) $this->_bind($val_list[$i]);
$this->_execute(); return $this->_execute();
return $this->isError();
} }
/** /**
* @brief updateAct 처리 * @brief updateAct 처리
**/ **/
function _executeUpdateAct($tables, $column, $args, $condition, $pass_quotes) { function _executeUpdateAct($output) {
$table = array_pop($tables); // 테이블 정리
foreach($output->tables as $key => $val) {
foreach($column as $key => $val) { $table_list[] = $this->prefix.$key;
// args에 아예 해당 key가 없으면 패스
if(!isset($args->{$key})) continue;
$val = $this->addQuotes($val);
if(is_numeric($val) || in_array($key, $pass_quotes)) $update_list[] = sprintf('`%s` = %s', $key, $val);
else $update_list[] = sprintf('`%s` = \'%s\'', $key, $this->addQuotes($val));
} }
if(!count($update_list)) return;
$update_query = implode(',',$update_list); // 컬럼 정리
if($condition) $condition = ' where '.$condition; foreach($output->columns as $key => $val) {
$query = sprintf("update `%s%s` set %s %s;", $this->prefix, $table, $update_query, $condition); if(!isset($val['value'])) continue;
$name = $val['name'];
$value = $val['value'];
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($value)."'";
else $value = (int)$value;
$column_list[] = sprintf("%s = %s", $name, $value);
}
// 조건절 정리
$condition = $this->getCondition($output);
$query = sprintf("update %s set %s %s", implode(',',$table_list), implode(',',$column_list), $condition);
$this->_prepare($query); $this->_prepare($query);
$this->_execute(); return $this->_execute();
return $this->isError();
} }
/** /**
* @brief deleteAct 처리 * @brief deleteAct 처리
**/ **/
function _executeDeleteAct($tables, $condition, $pass_quotes) { function _executeDeleteAct($output) {
$table = array_pop($tables); // 테이블 정리
foreach($output->tables as $key => $val) {
$table_list[] = $this->prefix.$key;
}
if($condition) $condition = ' WHERE '.$condition; // 조건절 정리
$condition = $this->getCondition($output);
$query = sprintf("delete from %s %s", implode(',',$table_list), $condition);
$query = sprintf("DELETE FROM %s%s %s;", $this->prefix, $table, $condition);
$this->_prepare($query); $this->_prepare($query);
$this->_execute(); return $this->_execute();
return $this->isError();
} }
/** /**
@ -426,34 +420,44 @@
* select의 경우 특정 페이지의 목록을 가져오는 것을 편하게 하기 위해\n * select의 경우 특정 페이지의 목록을 가져오는 것을 편하게 하기 위해\n
* navigation이라는 method를 제공 * navigation이라는 method를 제공
**/ **/
function _executeSelectAct($tables, $column, $invert_columns, $condition, $navigation, $group_script, $pass_quotes) { function _executeSelectAct($output) {
if(!count($tables)) $table = $this->prefix.array_pop($tables); // 테이블 정리
else { $table_list = array();
foreach($tables as $key => $val) $table_list[] = sprintf('%s%s as %s', $this->prefix, $key, $val); foreach($output->tables as $key => $val) {
} $table_list[] = $this->prefix.$key.' as '.$val;
$table = implode(',',$table_list);
if(!$column) $columns = '*';
else {
foreach($invert_columns as $key => $val) {
$column_list[] = sprintf('%s as %s',$val, $key);
}
$columns = implode(',', $column_list);
} }
if($condition) $condition = ' WHERE '.$condition; if(!$output->columns) {
$columns = '*';
if($navigation->list_count) return $this->_getNavigationData($table, $columns, $condition, $navigation); } else {
$column_list = array();
$query = sprintf("SELECT %s FROM %s %s", $columns, $table, $condition); foreach($output->columns as $key => $val) {
$name = $val['name'];
$query .= ' '.$group_script; $alias = $val['alias'];
if($name == '*') {
if($navigation->index) { $column_list[] = '*';
foreach($navigation->index as $index_obj) { } elseif(strpos($name,'.')===false && strpos($name,'(')===false) {
$index_list[] = sprintf('%s %s', $index_obj[0], $index_obj[1]); if($alias) $column_list[] = sprintf('%s as %s', $name, $alias);
else $column_list[] = sprintf('%s',$name);
} else {
if($alias) $column_list[] = sprintf('%s as %s', $name, $alias);
else $column_list[] = sprintf('%s',$name);
}
} }
if(count($index_list)) $query .= ' ORDER BY '.implode(',',$index_list); $columns = implode(',',$column_list);
}
$condition = $this->getCondition($output);
if($output->list_count) return $this->_getNavigationData($table_list, $columns, $condition, $output);
$query = sprintf("select %s from %s %s", $columns, implode(',',$table_list), $condition);
if($output->order) {
foreach($output->order as $key => $val) {
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
}
if(count($index_list)) $query .= ' order by '.implode(',',$index_list);
} }
$this->_prepare($query); $this->_prepare($query);
@ -470,58 +474,86 @@
* *
* 그닥 좋지는 않은 구조이지만 편리하다.. -_-; * 그닥 좋지는 않은 구조이지만 편리하다.. -_-;
**/ **/
function _getNavigationData($table, $columns, $condition, $navigation) { function _getNavigationData($table_list, $columns, $condition, $output) {
require_once('./classes/page/PageHandler.class.php'); require_once('./classes/page/PageHandler.class.php');
// 전체 개수를 구함 // 전체 개수를 구함
$count_query = sprintf("select count(*) as count from %s %s", $table, $condition); $count_query = sprintf("select count(*) as count from %s %s", implode(',',$table_list), $condition);
$this->_prepare($count_query); $this->_prepare($count_query);
$count_output = $this->_execute(); $count_output = $this->_execute();
$total_count = (int)$count_output->count; $total_count = (int)$count_output->count;
$list_count = $output->list_count['value'];
if(!$list_count) $list_count = 20;
$page_count = $output->page_count['value'];
if(!$page_count) $page_count = 10;
$page = $output->page->value;
if(!$page) $page = 1;
// 전체 페이지를 구함 // 전체 페이지를 구함
$total_page = (int)(($total_count-1)/$navigation->list_count) +1; $total_page = (int)(($total_count-1)/$list_count) +1;
// 페이지 변수를 체크 // 페이지 변수를 체크
if($navigation->page > $total_page) $page = $navigation->page; if($page > $total_page) $page = $total_page;
else $page = $navigation->page; $start_count = ($page-1)*$list_count;
$start_count = ($page-1)*$navigation->list_count;
foreach($navigation->index as $index_obj) { $query = sprintf("select %s from %s %s", $columns, implode(',',$table_list), $condition);
$index_list[] = sprintf('%s %s', $index_obj[0], $index_obj[1]);
if($output->order) {
foreach($output->order as $key => $val) {
$index_list[] = sprintf('%s %s', $val[0], $val[1]);
}
if(count($index_list)) $query .= ' order by '.implode(',',$index_list);
} }
$index = implode(',',$index_list); // return 결과물 생성
$query = sprintf('SELECT %s FROM %s %s ORDER BY %s LIMIT %d, %d', $columns, $table, $condition, $index, $start_count, $navigation->list_count); $buff = new Object();
$buff->total_count = 0;
$buff->total_page = 0;
$buff->page = 1;
$buff->data = array();
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
// 쿼리 실행
$query = sprintf('%s limit %d, %d', $query, $start_count, $list_count);
$this->_prepare($query); $this->_prepare($query);
$tmp_data = $this->_execute();
if($this->isError()) { if($this->isError()) {
$buff = new Object(); $this->setError($this->handler->errorCode(), print_r($this->handler->errorInfo(),true));
$buff->total_count = 0; $this->actFinish();
$buff->total_page = 0;
$buff->page = 1;
$buff->data = array();
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $navigation->page_count);
return $buff; return $buff;
} }
$virtual_no = $total_count - ($page-1)*$navigation->list_count; $this->stmt->execute();
if($tmp_data) {
if(!is_array($tmp_data)) $tmp_data = array($tmp_data); if($this->stmt->errorCode() != '00000') {
foreach($tmp_data as $tmp) { $this->setError($this->stmt->errorCode(), print_r($this->stmt->errorInfo(),true));
$data[$virtual_no--] = $tmp; $this->actFinish();
} return $buff;
} else {
$data = null;
} }
$output = null;
$virtual_no = $total_count - ($page-1)*$list_count;
while($tmp = $this->stmt->fetch(PDO::FETCH_ASSOC)) {
unset($obj);
foreach($tmp as $key => $val) {
$pos = strpos($key, '.');
if($pos) $key = substr($key, $pos+1);
$obj->{$key} = $val;
}
$data[$virtual_no--] = $obj;
}
$this->stmt = null;
$this->actFinish();
$buff = new Object(); $buff = new Object();
$buff->total_count = $total_count; $buff->total_count = $total_count;
$buff->total_page = $total_page; $buff->total_page = $total_page;
$buff->page = $page; $buff->page = $page;
$buff->data = $data; $buff->data = $data;
$buff->page_navigation = new PageHandler($total_count, $total_page, $page, $navigation->page_count); $buff->page_navigation = new PageHandler($total_count, $total_page, $page, $page_count);
return $buff; return $buff;
} }
} }

View file

@ -96,7 +96,7 @@
function _toXmlDoc(&$oModule) { function _toXmlDoc(&$oModule) {
$xmlDoc = "<response>\n"; $xmlDoc = "<response>\n";
$xmlDoc .= sprintf("<error>%s</error>\n",$oModule->getError()); $xmlDoc .= sprintf("<error>%s</error>\n",$oModule->getError());
$xmlDoc .= sprintf("<message>%s</message>\n",$oModule->getMessage()); $xmlDoc .= sprintf("<message>%s</message>\n",str_replace(array('<','>','&'),array('&lt;','&gt;','&amp;'),$oModule->getMessage()));
$variables = $oModule->getVariables(); $variables = $oModule->getVariables();

View file

@ -166,13 +166,17 @@
foreach($output->columns as $key => $val) { foreach($output->columns as $key => $val) {
$val['default'] = $this->getDefault($val['name'], $val['default']); $val['default'] = $this->getDefault($val['name'], $val['default']);
if($val['var'] && strpos($val['var'],'.')===false) { if($val['var'] && strpos($val['var'],'.')===false) {
$buff .= sprintf('array("name"=>"%s", "alias"=>"%s", "value"=>$args->%s?$args->%s:%s),%s', $val['name'], $val['alias'], $val['var'], $val['var'], $val['default'] ,"\n");
if($val['default']) $buff .= sprintf('array("name"=>"%s", "alias"=>"%s", "value"=>$args->%s?$args->%s:%s),%s', $val['name'], $val['alias'], $val['var'], $val['var'], $val['default'] ,"\n");
else $buff .= sprintf('array("name"=>"%s", "alias"=>"%s", "value"=>$args->%s),%s', $val['name'], $val['alias'], $val['var'], "\n");
if($val['default']) $default_list[$val['var']] = $val['default']; if($val['default']) $default_list[$val['var']] = $val['default'];
if($val['notnull']) $notnull_list[] = $val['var']; if($val['notnull']) $notnull_list[] = $val['var'];
if($val['minlength']) $minlength_list[$val['var']] = $val['minlength']; if($val['minlength']) $minlength_list[$val['var']] = $val['minlength'];
if($val['maxlength']) $maxlength_list[$val['var']] = $val['maxlength']; if($val['maxlength']) $maxlength_list[$val['var']] = $val['maxlength'];
} else { } else {
$buff .= sprintf('array("name"=>"%s", "alias"=>"%s", "value"=>%s),%s', $val['name'], $val['alias'], $val['default'] ,"\n"); if($val['default']) $buff .= sprintf('array("name"=>"%s", "alias"=>"%s", "value"=>%s),%s', $val['name'], $val['alias'], $val['default'] ,"\n");
else $buff .= sprintf('array("name"=>"%s", "alias"=>"%s",),%s', $val['name'], $val['alias'], "\n");
} }
} }
$buff .= ' );'."\n"; $buff .= ' );'."\n";
@ -189,12 +193,14 @@
if(strpos($v->var,".")===false) { if(strpos($v->var,".")===false) {
if($v->default) $default_list[$v->var] = $v->default; if($v->default) $default_list[$v->var] = $v->default;
if($v->filter) $filter_list[] = $v; if($v->filter) $filter_list[] = $v;
$buff .= sprintf('array("column"=>"%s", "value"=>$args->%s?$args->%s:%s,"pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->var, $v->var, $v->default, $v->pipe, $v->operation, "\n"); if($v->default) $buff .= sprintf('array("column"=>"%s", "value"=>$args->%s?$args->%s:%s,"pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->var, $v->var, $v->default, $v->pipe, $v->operation, "\n");
else $buff .= sprintf('array("column"=>"%s", "value"=>$args->%s,"pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->var, $v->pipe, $v->operation, "\n");
} else { } else {
$buff .= sprintf('array("column"=>"%s", "value"=>"%s","pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->var, $v->pipe, $v->operation, "\n"); $buff .= sprintf('array("column"=>"%s", "value"=>"%s","pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->var, $v->pipe, $v->operation, "\n");
} }
} else { } else {
$buff .= sprintf('array("column"=>"%s", "value"=>%s,"pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->default ,$v->pipe, $v->operation,"\n"); if($v->default) $buff .= sprintf('array("column"=>"%s", "value"=>%s,"pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->default ,$v->pipe, $v->operation,"\n");
else $buff .= sprintf('array("column"=>"%s", "pipe"=>"%s","operation"=>"%s",),%s', $v->column, $v->pipe, $v->operation,"\n");
} }
} }
$buff .= ')),'."\n"; $buff .= ')),'."\n";
@ -278,6 +284,7 @@
* @brief column, condition등의 key에 default 값을 세팅 * @brief column, condition등의 key에 default 값을 세팅
**/ **/
function getDefault($name, $value) { function getDefault($name, $value) {
if(!$value) return;
$str_pos = strpos($value, '('); $str_pos = strpos($value, '(');
if($str_pos===false) return '"'.$value.'"'; if($str_pos===false) return '"'.$value.'"';