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

This commit is contained in:
zero 2007-04-11 12:46:57 +00:00
parent 60ebbe4e02
commit 94c12ff8bf
6 changed files with 386 additions and 409 deletions

View file

@ -15,6 +15,20 @@
class DB {
/**
* @brief 조건문에서 조건을 등호로 표시하는 변수
**/
var $cond_operation = array(
'equal' => '=',
'more' => '>=',
'excess' => '>',
'less' => '<=',
'below' => '<',
'notequal' => '!=',
'notnull' => 'is not null',
'null' => 'is null',
);
var $fd = NULL; ///< connector resource or file description
var $result = NULL; ///< result
@ -23,7 +37,7 @@
var $errstr = ''; ///< 에러 발생시 에러 메세지
var $query = ''; ///< 가장 최근에 수행된 query string
var $transaction_started = false;
var $transaction_started = false; ///< 트랙잭션 처리 flag
var $is_connected = false; ///< DB에 접속이 되었는지에 대한 flag
@ -135,7 +149,7 @@
if(!$query_id) return new Object(-1, 'msg_invalid_queryid');
list($module, $id) = explode('.',$query_id);
if(!$module||!$id) return new Object(-1, 'msg_invalid_queryid');
if(!$module || !$id) return new Object(-1, 'msg_invalid_queryid');
$xml_file = sprintf('./modules/%s/queries/%s.xml', $module, $id);
if(!file_exists($xml_file)) return new Object(-1, 'msg_invalid_queryid');
@ -159,31 +173,33 @@
**/
function _executeQuery($cache_file, $source_args, $query_id) {
global $lang;
debugPrint($query_id);
if(!file_exists($cache_file)) return new Object(-1, 'msg_invalid_queryid');
if($source_args) $args = clone($source_args);
$output = include($cache_file);
if( (is_a($output, 'Object')||is_subclass_of($output,'Object'))&&!$output->toBool()) return $output;
// action값에 따라서 쿼리 생성으로 돌입
switch($action) {
switch($output->action) {
case 'insert' :
$output = $this->_executeInsertAct($tables, $column, $pass_quotes);
$output = $this->_executeInsertAct($output);
break;
case 'update' :
$output = $this->_executeUpdateAct($tables, $column, $args, $condition, $pass_quotes);
$output = $this->_executeUpdateAct($output);
break;
case 'delete' :
$output = $this->_executeDeleteAct($tables, $condition, $pass_quotes);
$output = $this->_executeDeleteAct($output);
break;
case 'select' :
$output = $this->_executeSelectAct($tables, $column, $invert_columns, $condition, $navigation, $group_script, $pass_quotes);
$output = $this->_executeSelectAct($output);
break;
}
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();
}
@ -191,13 +207,9 @@
/**
* @brief $val을 $filter_type으로 검사
**/
function _checkFilter($key, $val, $filter_type, $minlength, $maxlength) {
function _checkFilter($key, $val, $filter_type) {
global $lang;
$length = strlen($val);
if($minlength && $length < $minlength) return new Object(-1, sprintf($lang->filter->outofrange, $lang->{$key}?$lang->{$key}:$key));
if($maxlength && $length > $maxlength) return new Object(-1, sprintf($lang->filter->outofrange, $lang->{$key}?$lang->{$key}:$key));
switch($filter_type) {
case 'email' :
case 'email_adderss' :
@ -225,30 +237,41 @@
}
/**
* @brief 조건문들 정리
* @brief 이름, , operation으로 조건절 작성
**/
function _combineCondition($cond_group, $group_pipe) {
if(!is_array($cond_group)) return;
$cond_query = '';
foreach($cond_group as $group_idx => $group) {
if(!is_array($group)) continue;
$buff = '';
foreach($group as $key => $val) {
$pipe = key($val);
$cond = array_pop($val);
if($buff) $buff .= ' '.$pipe.' '.$cond;
else $buff = $cond;
}
$g_pipe = $group_pipe[$group_idx];
if(!$g_pipe) $g_pipe = 'and';
if($cond_query) $cond_query .= sprintf(' %s ( %s )', $g_pipe, $buff);
else $cond_query = '('.$buff.')';
function getConditionPart($name, $value, $operation) {
switch($operation) {
case 'equal' :
if(!$value) return;
return $name.' = '.$value;
break;
case 'more' :
if(!$value) return;
return $name.' >= '.$value;
break;
case 'excess' :
if(!$value) return;
return $name.' > '.$value;
break;
case 'less' :
if(!$value) return;
return $name.' <= '.$value;
break;
case 'below' :
if(!$value) return;
return $name.' < '.$value;
break;
case 'notequal' :
if(!$value) return;
return $name.' != '.$value;
break;
case 'notnull' :
return $name.' is not null';
break;
case 'null' :
return $name.' is null';
break;
}
return $cond_query;
}
}
?>

View file

@ -284,71 +284,81 @@
}
/**
* @brief 테이블 삭제
* @brief 조건문 작성하여 return
**/
function dropTable($target_name) {
$query = sprintf('drop table `%s%s`;', $this->prefix, $this->addQuotes($target_name));
$this->_query($query);
}
function getCondition($output) {
if(!$output->conditions) return;
/**
* @brief 테이블의 이름 변경
**/
function renameTable($source_name, $targe_name) {
$query = sprintf("alter table `%s%s` rename `%s%s`;", $this->prefix, $this->addQuotes($source_name), $this->prefix, $this->addQuotes($targe_name));
$this->_query($query);
}
foreach($output->conditions as $key => $val) {
$sub_condition = '';
foreach($val as $k =>$v) {
if(!$v->value) continue;
$name = $v->column;
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($v->value)."'";
else $value = $v->value;
$operation = $v->operation;
/**
* @brief 테이블을 비움
**/
function truncateTable($target_name) {
$query = sprintf("truncate table `%s%s`;", $this->prefix, $this->addQuotes($target_name));
$this->_query($query);
}
$str = $this->getConditionPart($name, $value, $operation);
if($sub_condition) $sub_condition .= ' '.$v->pipe.' ';
$sub_condition .= $str;
}
if($sub_condition) {
if($condition && $val['pipe']) $condition .= ' '.$val['pipe'].' ';
$condition .= '('.$sub_condition.')';
}
}
/**
* @brief 테이블 데이터 Dump
*
* @todo 아직 미구현
**/
function dumpTable($target_name) {
if($condition) $condition = ' where '.$condition;
return "";
}
/**
* @brief insertAct 처리
**/
function _executeInsertAct($tables, $column, $pass_quotes) {
$table = array_pop($tables);
foreach($column as $key => $val) {
$key_list[] = $key;
if(in_array($key, $pass_quotes)) $val_list[] = $this->addQuotes($val);
else $val_list[] = '\''.$this->addQuotes($val).'\'';
function _executeInsertAct($output) {
// 테이블 정리
foreach($output->tables as $key => $val) {
$table_list[] = '`'.$this->prefix.$key.'`';
}
$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';
$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);
}
/**
* @brief updateAct 처리
**/
function _executeUpdateAct($tables, $column, $args, $condition, $pass_quotes) {
$table = array_pop($tables);
foreach($column as $key => $val) {
// args에 아예 해당 key가 없으면 패스
if(!isset($args->{$key})) continue;
if(in_array($key, $pass_quotes)) $update_list[] = sprintf('`%s` = %s', $key, $this->addQuotes($val));
else $update_list[] = sprintf('`%s` = \'%s\'', $key, $this->addQuotes($val));
function _executeUpdateAct($output) {
// 테이블 정리
foreach($output->tables as $key => $val) {
$table_list[] = '`'.$this->prefix.$key.'`';
}
if(!count($update_list)) return;
$update_query = implode(',',$update_list);
if($condition) $condition = ' where '.$condition;
// 컬럼 정리
foreach($output->columns as $key => $val) {
$name = $val['name'];
$value = $val['value'];
if($output->column_type[$name]!='number') $value = "'".$this->addQuotes($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);
}
@ -356,12 +366,17 @@
/**
* @brief deleteAct 처리
**/
function _executeDeleteAct($tables, $condition, $pass_quotes) {
$table = array_pop($tables);
function _executeDeleteAct($output) {
// 테이블 정리
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);
}
@ -371,32 +386,31 @@
* select의 경우 특정 페이지의 목록을 가져오는 것을 편하게 하기 위해\n
* navigation이라는 method를 제공
**/
function _executeSelectAct($tables, $column, $invert_columns, $condition, $navigation, $group_script, $pass_quotes) {
if(!count($tables)) $table = $this->prefix.array_pop($tables);
else {
foreach($tables as $key => $val) $table_list[] = sprintf('%s%s as %s', $this->prefix, $key, $val);
function _executeSelectAct($output) {
// 테이블 정리
$table_list = array();
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);
if($val) $column_list[] = sprintf('%s as %s',$val, $key);
else $column_list[] = sprintf('%s',$val);
}
$columns = implode(',', $column_list);
}
if($condition) $condition = ' where '.$condition;
$condition = $this->getCondition($output);
if($navigation->list_count) return $this->_getNavigationData($table, $columns, $condition, $navigation);
$query = sprintf("select %s from %s %s", $columns, implode(',',$table_list), $condition);
$query = sprintf("select %s from %s %s", $columns, $table, $condition);
if($output->list_count) return $this->_getNavigationData($table_list, $columns, $condition, $output);
$query .= ' '.$group_script;
if($navigation->index) {
foreach($navigation->index as $index_obj) {
$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', $key, $val);
}
if(count($index_list)) $query .= ' order by '.implode(',',$index_list);
}
@ -415,11 +429,11 @@
*
* 그닥 좋지는 않은 구조이지만 편리하다.. -_-;
**/
function _getNavigationData($table, $columns, $condition, $navigation) {
function _getNavigationData($table_list, $columns, $condition, $output) {
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);
$count_output = $this->_fetch($result);
$total_count = (int)$count_output->count;
@ -432,12 +446,17 @@
else $page = $navigation->page;
$start_count = ($page-1)*$navigation->list_count;
foreach($navigation->index as $index_obj) {
$index_list[] = sprintf('%s %s', $index_obj[0], $index_obj[1]);
$query = sprintf("select %s from %s %s", implode(',',$columns), implode(',',$table_list), $condition);
if($output->order) {
foreach($output->order as $key => $val) {
$index_list[] = sprintf('%s %s', $index_obj[0], $index_obj[1]);
}
if(count($index_list)) $query .= ' order by '.implode(',',$index_list);
}
$index = implode(',',$index_list);
$query = sprintf('select %s from %s %s order by %s limit %d, %d', $columns, $table, $condition, $index, $start_count, $navigation->list_count);
$query .= sprintf('%s limit %d, %d', $query, $start_count, $output->list_count['value']);
$result = $this->_query($query);
if($this->isError()) {
$buff = new Object();