mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-30 07:42:11 +09:00
Fixed a few MSSQL bugs - related to array query arguments and increment columns.
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8632 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
6edd5f03a7
commit
b3c75ac4db
15 changed files with 411 additions and 276 deletions
|
|
@ -320,12 +320,12 @@
|
|||
require_once(_XE_PATH_.'classes/db/queryparts/limit/Limit.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/Query.class.php');
|
||||
require_once(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
|
||||
|
||||
|
||||
|
||||
|
||||
$output = include($cache_file);
|
||||
|
||||
if( (is_a($output, 'Object') || is_subclass_of($output, 'Object')) && !$output->toBool()) return $output;
|
||||
|
||||
|
||||
// execute appropriate query
|
||||
switch($output->getAction()) {
|
||||
case 'insert' :
|
||||
|
|
@ -346,7 +346,7 @@
|
|||
$output = $this->_executeSelectAct($output);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if($this->isError()) $output = $this->getError();
|
||||
else if(!is_a($output, 'Object') && !is_subclass_of($output, 'Object')) $output = new Object();
|
||||
$output->add('_query', $this->query);
|
||||
|
|
@ -458,76 +458,76 @@
|
|||
$query = sprintf("drop table %s%s", $this->prefix, $table_name);
|
||||
$this->_query($query);
|
||||
}
|
||||
|
||||
function getSelectSql($query, $with_values = true){
|
||||
|
||||
function getSelectSql($query, $with_values = true){
|
||||
$select = $query->getSelectString($with_values);
|
||||
if($select == '') return new Object(-1, "Invalid query");
|
||||
$select = 'SELECT ' .$select;
|
||||
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$from = ' FROM '.$from;
|
||||
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
|
||||
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
|
||||
|
||||
|
||||
$limit = $query->getLimitString();
|
||||
if($limit != '') $limit = ' LIMIT ' . $limit;
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
|
||||
}
|
||||
}
|
||||
|
||||
function getDeleteSql($query, $with_values = true){
|
||||
$sql = 'DELETE ';
|
||||
|
||||
// TODO Add support for deleting based on alias, for both simple FROM and multi table join FROM clause
|
||||
$tables = $query->getTables();
|
||||
|
||||
|
||||
$sql .= $tables[0]->getAlias();
|
||||
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$sql .= ' FROM '.$from;
|
||||
|
||||
$sql .= ' FROM '.$from;
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $sql .= ' WHERE ' . $where;
|
||||
|
||||
if($where != '') $sql .= ' WHERE ' . $where;
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
function getUpdateSql($query, $with_values = true){
|
||||
$columnsList = $query->getSelectString();
|
||||
$columnsList = $query->getSelectString($with_values);
|
||||
if($columnsList == '') return new Object(-1, "Invalid query");
|
||||
|
||||
|
||||
$tableName = $query->getFirstTableName();
|
||||
if($tableName == '') return new Object(-1, "Invalid query");
|
||||
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
|
||||
return "UPDATE $tableName SET $columnsList ".$where;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getInsertSql($query, $with_values = true){
|
||||
$tableName = $query->getFirstTableName();
|
||||
$values = $query->getInsertString($with_values);
|
||||
|
||||
|
||||
return "INSERT INTO $tableName \n $values";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// HACK This is needed because on installation, the XmlQueryParer is used without any configured database
|
||||
// TODO Change this or make sure the query cache files created before db.config exists are deleted
|
||||
function getParser(){
|
||||
return new DBParser('"');
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TO BE REMOVED - Used for query compare
|
||||
/**
|
||||
* @brief returns type of column
|
||||
|
|
@ -560,7 +560,7 @@
|
|||
if(strpos($value, ',') === false && strpos($value, '(') === false) return (int)$value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
if(!is_array($value) && strpos($name, '.') !== false && strpos($value, '.') !== false) {
|
||||
list($table_name, $column_name) = explode('.', $value);
|
||||
if($column_type[$column_name]) return $value;
|
||||
|
|
@ -713,6 +713,6 @@
|
|||
}
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
var $prefix = 'xe'; // / <prefix of XE tables(One more XE can be installed on a single DB)
|
||||
var $param = array();
|
||||
var $comment_syntax = '/* %s */';
|
||||
|
||||
|
||||
/**
|
||||
* @brief column type used in mssql
|
||||
*
|
||||
|
|
@ -42,7 +42,7 @@
|
|||
$this->_setDBInfo();
|
||||
$this->_connect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief create an instance of this class
|
||||
*/
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
$this->password = $db_info->db_password;
|
||||
$this->database = $db_info->db_database;
|
||||
$this->prefix = $db_info->db_table_prefix;
|
||||
|
||||
|
||||
if(!substr($this->prefix,-1)!='_') $this->prefix .= '_';
|
||||
}
|
||||
|
||||
|
|
@ -85,10 +85,10 @@
|
|||
//sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
|
||||
//sqlsrv_configure( 'LogSubsystems', SQLSRV_LOG_SYSTEM_ALL );
|
||||
|
||||
$this->conn = sqlsrv_connect( $this->hostname,
|
||||
$this->conn = sqlsrv_connect( $this->hostname,
|
||||
array( 'Database' => $this->database,'UID'=>$this->userid,'PWD'=>$this->password ));
|
||||
|
||||
|
||||
|
||||
// Check connections
|
||||
if($this->conn){
|
||||
$this->is_connected = true;
|
||||
|
|
@ -103,7 +103,7 @@
|
|||
**/
|
||||
function close() {
|
||||
if($this->is_connected == false) return;
|
||||
|
||||
|
||||
$this->commit();
|
||||
sqlsrv_close($this->conn);
|
||||
$this->conn = null;
|
||||
|
|
@ -116,7 +116,7 @@
|
|||
function addQuotes($string) {
|
||||
if(version_compare(PHP_VERSION, "5.9.0", "<") && get_magic_quotes_gpc()) $string = stripslashes(str_replace("\\","\\\\",$string));
|
||||
//if(!is_numeric($string)) $string = str_replace("'","''",$string);
|
||||
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
|
|
@ -126,7 +126,7 @@
|
|||
function begin() {
|
||||
if($this->is_connected == false || $this->transaction_started) return;
|
||||
if(sqlsrv_begin_transaction( $this->conn ) === false) return;
|
||||
|
||||
|
||||
$this->transaction_started = true;
|
||||
}
|
||||
|
||||
|
|
@ -135,7 +135,7 @@
|
|||
**/
|
||||
function rollback() {
|
||||
if($this->is_connected == false || !$this->transaction_started) return;
|
||||
|
||||
|
||||
$this->transaction_started = false;
|
||||
sqlsrv_rollback( $this->conn );
|
||||
}
|
||||
|
|
@ -145,8 +145,8 @@
|
|||
**/
|
||||
function commit($force = false) {
|
||||
if(!$force && ($this->is_connected == false || !$this->transaction_started)) return;
|
||||
|
||||
$this->transaction_started = false;
|
||||
|
||||
$this->transaction_started = false;
|
||||
sqlsrv_commit( $this->conn );
|
||||
}
|
||||
|
||||
|
|
@ -159,25 +159,37 @@
|
|||
* object if a row returned \n
|
||||
* return\n
|
||||
**/
|
||||
|
||||
// TODO Support array arguments in sql server
|
||||
/*
|
||||
* $query_emp="select name from employee where id in (?,?,?)";
|
||||
$params_emp= Array(1,2,3);
|
||||
$res_emp = sqlsrv_query($conn, $query_emp, $params_emp);
|
||||
*
|
||||
*/
|
||||
|
||||
function _query($query) {
|
||||
if($this->is_connected == false || !$query) return;
|
||||
|
||||
$_param = array();
|
||||
|
||||
|
||||
if(count($this->param)){
|
||||
foreach($this->param as $k => $o){
|
||||
if($o->getType() == 'number'){
|
||||
$_param[] = $o->getUnescapedValue();
|
||||
$value = $o->getUnescapedValue();
|
||||
if(is_array($value)) $_param = array_merge($_param, $value);
|
||||
else $_param[] = $o->getUnescapedValue();
|
||||
}else{
|
||||
// TODO treat arrays here too
|
||||
$value = $o->getUnescapedValue();
|
||||
$_param[] = array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('utf-8'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Notify to start a query execution
|
||||
$this->actStart($query);
|
||||
|
||||
|
||||
// Run the query statement
|
||||
$result = false;
|
||||
if(count($_param)){
|
||||
|
|
@ -186,9 +198,9 @@
|
|||
$result = @sqlsrv_query($this->conn, $query);
|
||||
}
|
||||
// Error Check
|
||||
|
||||
|
||||
if(!$result) $this->setError(print_r(sqlsrv_errors(),true));
|
||||
|
||||
|
||||
// Notify to complete a query execution
|
||||
$this->actFinish();
|
||||
$this->param = array();
|
||||
|
|
@ -201,16 +213,16 @@
|
|||
**/
|
||||
function _fetch($result, $arrayIndexEndValue = NULL) {
|
||||
if(!$this->isConnected() || $this->isError() || !$result) return;
|
||||
|
||||
|
||||
$c = sqlsrv_num_fields($result);
|
||||
$m = null;
|
||||
$output = array();
|
||||
|
||||
|
||||
while(sqlsrv_fetch($result)){
|
||||
if(!$m) $m = sqlsrv_field_metadata($result);
|
||||
unset($row);
|
||||
for($i=0;$i<$c;$i++){
|
||||
$row->{$m[$i]['Name']} = sqlsrv_get_field( $result, $i, SQLSRV_PHPTYPE_STRING( 'utf-8' ));
|
||||
$row->{$m[$i]['Name']} = sqlsrv_get_field( $result, $i, SQLSRV_PHPTYPE_STRING( 'utf-8' ));
|
||||
}
|
||||
if($arrayIndexEndValue) $output[$arrayIndexEndValue--] = $row;
|
||||
else $output[] = $row;
|
||||
|
|
@ -230,12 +242,12 @@
|
|||
function getNextSequence() {
|
||||
$query = sprintf("insert into %ssequence (seq) values (ident_incr('%ssequence'))", $this->prefix, $this->prefix);
|
||||
$this->_query($query);
|
||||
|
||||
|
||||
$query = sprintf("select ident_current('%ssequence')+1 as sequence", $this->prefix);
|
||||
$result = $this->_query($query);
|
||||
$tmp = $this->_fetch($result);
|
||||
|
||||
|
||||
|
||||
return $tmp->sequence;
|
||||
}
|
||||
|
||||
|
|
@ -244,9 +256,9 @@
|
|||
**/
|
||||
function isTableExists($target_name) {
|
||||
$query = sprintf("select name from sysobjects where name = '%s%s' and xtype='U'", $this->prefix, $this->addQuotes($target_name));
|
||||
$result = $this->_query($query);
|
||||
$result = $this->_query($query);
|
||||
$tmp = $this->_fetch($result);
|
||||
|
||||
|
||||
if(!$tmp) return false;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -391,11 +403,11 @@
|
|||
if($unique) $unique_list[$unique][] = $name;
|
||||
else if($index) $index_list[$index][] = $name;
|
||||
}
|
||||
|
||||
|
||||
$schema = sprintf('create table [%s] (xe_seq int identity(1,1),%s%s)', $this->addQuotes($table_name), "\n", implode($column_schema,",\n"));
|
||||
$output = $this->_query($schema);
|
||||
if(!$output) return false;
|
||||
|
||||
|
||||
if(count($unique_list)) {
|
||||
foreach($unique_list as $key => $val) {
|
||||
$query = sprintf("create unique index %s on %s (%s);", $key, $table_name, '['.implode('],[',$val).']');
|
||||
|
|
@ -413,13 +425,13 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Handle the insertAct
|
||||
**/
|
||||
// TODO Lookup _filterNumber against sql injection - see if it is still needed and how to integrate
|
||||
function _executeInsertAct($queryObject) {
|
||||
$query = $this->getInsertSql($queryObject);
|
||||
$query = $this->getInsertSql($queryObject, false);
|
||||
$this->param = $queryObject->getArguments();
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
|
@ -428,7 +440,7 @@
|
|||
* @brief Handle updateAct
|
||||
**/
|
||||
function _executeUpdateAct($queryObject) {
|
||||
$query = $this->getUpdateSql($queryObject);
|
||||
$query = $this->getUpdateSql($queryObject, false);
|
||||
$this->param = $queryObject->getArguments();
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
|
@ -437,47 +449,47 @@
|
|||
* @brief Handle deleteAct
|
||||
**/
|
||||
function _executeDeleteAct($queryObject) {
|
||||
$query = $this->getDeleteSql($queryObject);
|
||||
$query = $this->getDeleteSql($queryObject, false);
|
||||
$this->param = $queryObject->getArguments();
|
||||
return $this->_query($query);
|
||||
}
|
||||
|
||||
function getSelectSql($query){
|
||||
$with_value = false;
|
||||
|
||||
|
||||
//$limitOffset = $query->getLimit()->getOffset();
|
||||
//if($limitOffset)
|
||||
// TODO Implement Limit with offset with subquery
|
||||
$limit = '';$limitCount = '';
|
||||
if($query->getLimit())
|
||||
$limitCount = $query->getLimit()->getLimit();
|
||||
if($limitCount != '') $limit = 'SELECT TOP ' . $limitCount;
|
||||
|
||||
if($limitCount != '') $limit = 'SELECT TOP ' . $limitCount;
|
||||
|
||||
$select = $query->getSelectString($with_values);
|
||||
if($select == '') return new Object(-1, "Invalid query");
|
||||
if($limit != '')
|
||||
$select = $limit.' '.$select;
|
||||
else
|
||||
$select = 'SELECT ' .$select;
|
||||
|
||||
|
||||
$from = $query->getFromString($with_values);
|
||||
if($from == '') return new Object(-1, "Invalid query");
|
||||
$from = ' FROM '.$from;
|
||||
|
||||
|
||||
$where = $query->getWhereString($with_values);
|
||||
if($where != '') $where = ' WHERE ' . $where;
|
||||
|
||||
|
||||
$groupBy = $query->getGroupByString();
|
||||
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
|
||||
|
||||
|
||||
$orderBy = $query->getOrderByString();
|
||||
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
|
||||
|
||||
|
||||
|
||||
|
||||
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Handle selectAct
|
||||
*
|
||||
|
|
@ -486,21 +498,21 @@
|
|||
**/
|
||||
function _executeSelectAct($queryObject) {
|
||||
$query = $this->getSelectSql($queryObject);
|
||||
|
||||
|
||||
// TODO Decide if we continue to pass parameters like this
|
||||
$this->param = $queryObject->getArguments();
|
||||
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
|
||||
if ($this->isError ()) return $this->queryError($queryObject);
|
||||
else return $this->queryPageLimit($queryObject, $result);
|
||||
$query .= (__DEBUG_QUERY__&1 && $output->query_id)?sprintf(' '.$this->comment_syntax,$this->query_id):'';
|
||||
$result = $this->_query($query);
|
||||
|
||||
if ($this->isError ()) return $this->queryError($queryObject);
|
||||
else return $this->queryPageLimit($queryObject, $result);
|
||||
}
|
||||
|
||||
function getParser(){
|
||||
return new DBParser("[", "]");
|
||||
}
|
||||
|
||||
|
||||
function queryError($queryObject){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()){
|
||||
$buff = new Object ();
|
||||
|
|
@ -510,10 +522,10 @@
|
|||
$buff->data = array ();
|
||||
$buff->page_navigation = new PageHandler (/*$total_count*/0, /*$total_page*/1, /*$page*/1, /*$page_count*/10);//default page handler values
|
||||
return $buff;
|
||||
}else
|
||||
}else
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
function queryPageLimit($queryObject, $result){
|
||||
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
|
||||
// Total count
|
||||
|
|
@ -526,12 +538,12 @@
|
|||
$result_count = $this->_query($count_query);
|
||||
$count_output = $this->_fetch($result_count);
|
||||
$total_count = (int)$count_output->count;
|
||||
|
||||
|
||||
// Total pages
|
||||
if ($total_count) {
|
||||
$total_page = (int) (($total_count - 1) / $queryObject->getLimit()->list_count) + 1;
|
||||
} else $total_page = 1;
|
||||
|
||||
|
||||
$virtual_no = $total_count - ($queryObject->getLimit()->page - 1) * $queryObject->getLimit()->list_count;
|
||||
$data = $this->_fetch($result, $virtual_no);
|
||||
|
||||
|
|
@ -540,15 +552,15 @@
|
|||
$buff->total_page = $total_page;
|
||||
$buff->page = $queryObject->getLimit()->page;
|
||||
$buff->data = $data;
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page, $queryObject->getLimit()->page_count);
|
||||
$buff->page_navigation = new PageHandler($total_count, $total_page, $queryObject->getLimit()->page, $queryObject->getLimit()->page_count);
|
||||
}else{
|
||||
$data = $this->_fetch($result);
|
||||
$buff = new Object ();
|
||||
$buff->data = $data;
|
||||
$buff->data = $data;
|
||||
}
|
||||
return $buff;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return new DBMssql;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
<?php
|
||||
|
||||
class Condition {
|
||||
var $column_name;
|
||||
var $argument;
|
||||
var $operation;
|
||||
var $pipe;
|
||||
|
||||
|
||||
var $_value;
|
||||
|
||||
|
||||
function Condition($column_name, $argument, $operation, $pipe = ""){
|
||||
$this->column_name = $column_name;
|
||||
$this->argument = $argument;
|
||||
|
|
@ -17,40 +17,50 @@
|
|||
$this->_value = $argument->getValue();
|
||||
else if(is_a($this->argument, 'Subquery'))
|
||||
$this->_value = $argument->toString();
|
||||
else
|
||||
else
|
||||
$this->_value = $argument;
|
||||
}
|
||||
|
||||
|
||||
function hasArgument(){
|
||||
return is_a($this->argument, 'Argument');
|
||||
}
|
||||
|
||||
|
||||
function getArgument(){
|
||||
if($this->hasArgument()) return $this->argument;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
function toString($withValue = true){
|
||||
if(!$this->show()) return '';
|
||||
if($withValue)
|
||||
return $this->toStringWithValue();
|
||||
return $this->toStringWithoutValue();
|
||||
}
|
||||
|
||||
|
||||
function toStringWithoutValue(){
|
||||
if($this->hasArgument())
|
||||
return $this->pipe . ' ' . $this->getConditionPart("?");
|
||||
if($this->hasArgument()){
|
||||
$value = $this->argument->getUnescapedValue();
|
||||
|
||||
if(is_array($value)){
|
||||
$q = '';
|
||||
foreach ($value as $v) $q .= '?,';
|
||||
if($q !== '') $q = substr($q, 0, -1);
|
||||
$q = '(' . $q . ')';
|
||||
}
|
||||
else $q = '?';
|
||||
return $this->pipe . ' ' . $this->getConditionPart($q);
|
||||
}
|
||||
else return $this->toString();
|
||||
}
|
||||
|
||||
|
||||
function toStringWithValue(){
|
||||
return $this->pipe . ' ' . $this->getConditionPart($this->_value);
|
||||
}
|
||||
|
||||
|
||||
function setPipe($pipe){
|
||||
$this->pipe = $pipe;
|
||||
}
|
||||
|
||||
|
||||
function show(){
|
||||
if($this->hasArgument() && !$this->argument->isValid()) return false;
|
||||
if($this->hasArgument() && ($this->_value === '\'\'')) return false;
|
||||
|
|
@ -75,14 +85,14 @@
|
|||
if(!is_array($this->_value)) return false;
|
||||
if(count($this->_value)!=2) return false;
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
function getConditionPart($value) {
|
||||
$name = $this->column_name;
|
||||
$operation = $this->operation;
|
||||
|
||||
$operation = $this->operation;
|
||||
|
||||
switch($operation) {
|
||||
case 'equal' :
|
||||
return $name.' = '.$value;
|
||||
|
|
@ -123,7 +133,7 @@
|
|||
return $name.' between ' . $value[0] . ' and ' . $value[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -1,46 +1,52 @@
|
|||
<?php
|
||||
<?php
|
||||
/**
|
||||
* @class UpdateExpression
|
||||
* @author Arnia Software
|
||||
* @brief
|
||||
* @brief
|
||||
*
|
||||
*/
|
||||
|
||||
class UpdateExpression extends Expression {
|
||||
var $argument;
|
||||
|
||||
|
||||
function UpdateExpression($column_name, $argument){
|
||||
parent::Expression($column_name);
|
||||
$this->argument = $argument;
|
||||
}
|
||||
|
||||
|
||||
function getExpression($with_value = true){
|
||||
if($with_value)
|
||||
return $this->getExpressionWithValue();
|
||||
return $this->getExpressionWithoutValue();
|
||||
}
|
||||
|
||||
|
||||
function getExpressionWithValue(){
|
||||
$value = $this->argument->getValue();
|
||||
$operation = $this->argument->getColumnOperation();
|
||||
if(isset($operation))
|
||||
return "$this->column_name = $this->column_name $operation $value";
|
||||
return "$this->column_name = $value";
|
||||
}
|
||||
|
||||
|
||||
function getExpressionWithoutValue(){
|
||||
$operation = $this->argument->getColumnOperation();
|
||||
if(isset($operation))
|
||||
return "$this->column_name = $this->column_name $operation ?";
|
||||
return "$this->column_name = ?";
|
||||
}
|
||||
|
||||
|
||||
function getValue(){
|
||||
// TODO Escape value according to column type instead of variable type
|
||||
$value = $this->argument->getValue();
|
||||
if(!is_numeric($value)) return "'".$value."'";
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
function show(){
|
||||
if(!$this->argument->getValue()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
function getArgument(){
|
||||
return $this->argument;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,52 +1,62 @@
|
|||
<?php
|
||||
<?php
|
||||
|
||||
class Argument {
|
||||
var $value;
|
||||
var $name;
|
||||
var $type;
|
||||
|
||||
|
||||
var $isValid;
|
||||
var $errorMessage;
|
||||
|
||||
|
||||
var $column_operation;
|
||||
|
||||
function Argument($name, $value){
|
||||
$this->value = $value;
|
||||
$this->name = $name;
|
||||
$this->name = $name;
|
||||
$this->isValid = true;
|
||||
}
|
||||
|
||||
|
||||
function getType(){
|
||||
if(isset($this->type)) return $this->type;
|
||||
if(is_string($this->value)) return 'column_name';
|
||||
return 'number';
|
||||
}
|
||||
|
||||
|
||||
function setColumnType($value){
|
||||
$this->type = $value;
|
||||
}
|
||||
|
||||
|
||||
function setColumnOperation($operation){
|
||||
$this->column_operation = $operation;
|
||||
}
|
||||
|
||||
function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
||||
function getValue(){
|
||||
$value = $this->escapeValue($this->value);
|
||||
return $this->toString($value);
|
||||
}
|
||||
|
||||
function getColumnOperation(){
|
||||
return $this->column_operation;
|
||||
}
|
||||
|
||||
function getUnescapedValue(){
|
||||
return $this->toString($this->value);
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
|
||||
function toString($value){
|
||||
if(is_array($value)) return '('.implode(',', $value).')';
|
||||
return $value;
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
function escapeValue($value){
|
||||
if($this->getType() == 'column_name'){
|
||||
$dbParser = XmlQueryParser::getDBParser();
|
||||
return $dbParser->parseExpression($value);
|
||||
}
|
||||
return $dbParser->parseExpression($value);
|
||||
}
|
||||
if(!isset($value)) return null;
|
||||
if(in_array($this->type, array('date', 'varchar', 'char','text', 'bigtext'))){
|
||||
if(!is_array($value))
|
||||
|
|
@ -57,32 +67,32 @@
|
|||
$value[$i] = $this->_escapeStringValue($value[$i]);
|
||||
//$value[$i] = '\''.$value[$i].'\'';
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function _escapeStringValue($value){
|
||||
$db = &DB::getInstance();
|
||||
$value = $db->addQuotes($value);
|
||||
$value = $db->addQuotes($value);
|
||||
return '\''.$value.'\'';
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function isValid(){
|
||||
return $this->isValid;
|
||||
}
|
||||
|
||||
|
||||
function getErrorMessage(){
|
||||
return $this->errorMessage;
|
||||
}
|
||||
|
||||
|
||||
function ensureDefaultValue($default_value){
|
||||
if(!isset($this->value) || $this->value == '')
|
||||
if(!isset($this->value) || $this->value == '')
|
||||
$this->value = $default_value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function checkFilter($filter_type){
|
||||
if(isset($this->value) && $this->value != ''){
|
||||
$val = $this->value;
|
||||
|
|
@ -90,7 +100,7 @@
|
|||
switch($filter_type) {
|
||||
case 'email' :
|
||||
case 'email_address' :
|
||||
if(!preg_match('/^[_0-9a-z-]+(\.[_0-9a-z-]+)*@[0-9a-z-]+(\.[0-9a-z-]+)*$/is', $val)) {
|
||||
if(!preg_match('/^[_0-9a-z-]+(\.[_0-9a-z-]+)*@[0-9a-z-]+(\.[0-9a-z-]+)*$/is', $val)) {
|
||||
$this->isValid = false;
|
||||
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_email, $lang->{$key} ? $lang->{$key} : $key));
|
||||
}
|
||||
|
|
@ -111,7 +121,7 @@
|
|||
case 'number' :
|
||||
case 'numbers' :
|
||||
if(is_array($val)) $val = join(',', $val);
|
||||
if(!preg_match('/^(-?)[0-9]+(,\-?[0-9]+)*$/is', $val)){
|
||||
if(!preg_match('/^(-?)[0-9]+(,\-?[0-9]+)*$/is', $val)){
|
||||
$this->isValid = false;
|
||||
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_number, $lang->{$key} ? $lang->{$key} : $key));
|
||||
}
|
||||
|
|
@ -128,10 +138,10 @@
|
|||
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha_number, $lang->{$key} ? $lang->{$key} : $key));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function checkMaxLength($length){
|
||||
if($this->value && (strlen($this->value) > $length)){
|
||||
$this->isValid = false;
|
||||
|
|
@ -139,15 +149,15 @@
|
|||
$this->errorMessage = new Object(-1, $lang->filter->outofrange, $lang->{$key} ? $lang->{$key} : $key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function checkMinLength($length){
|
||||
if($this->value && (strlen($this->value) > $length)){
|
||||
$this->isValid = false;
|
||||
$key = $this->name;
|
||||
$this->errorMessage = new Object(-1, $lang->filter->outofrange, $lang->{$key} ? $lang->{$key} : $key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function checkNotNull(){
|
||||
if(!isset($this->value)){
|
||||
$this->isValid = false;
|
||||
|
|
|
|||
|
|
@ -1,35 +1,46 @@
|
|||
<?php
|
||||
<?php
|
||||
|
||||
class DefaultValue {
|
||||
var $column_name;
|
||||
var $value;
|
||||
var $is_sequence = false;
|
||||
|
||||
var $is_operation = false;
|
||||
var $operation = '';
|
||||
|
||||
function DefaultValue($column_name, $value){
|
||||
$this->column_name = $column_name;
|
||||
$dbParser = &XmlQueryParser::getDBParser();
|
||||
$this->column_name = $dbParser->parseColumnName($column_name);
|
||||
$this->value = $value;
|
||||
$this->value = $this->_setValue();
|
||||
}
|
||||
|
||||
|
||||
function isString(){
|
||||
$str_pos = strpos($this->value, '(');
|
||||
if($str_pos===false) return true;
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
function isSequence(){
|
||||
return $this->is_sequence;
|
||||
}
|
||||
|
||||
|
||||
function isOperation(){
|
||||
return $this->is_operation;
|
||||
}
|
||||
|
||||
function getOperation(){
|
||||
return $this->operation;
|
||||
}
|
||||
|
||||
function _setValue(){
|
||||
if(!isset($this->value)) return;
|
||||
|
||||
|
||||
// If value contains comma separated values and does not contain paranthesis
|
||||
// -> default value is an array
|
||||
if(strpos($this->value, ',') !== false && strpos($this->value, '(') === false) {
|
||||
return sprintf('array(%s)', $this->value);
|
||||
}
|
||||
|
||||
|
||||
$str_pos = strpos($this->value, '(');
|
||||
// // TODO Replace this with parseExpression
|
||||
if($str_pos===false) return '\''.$this->value.'\'';
|
||||
|
|
@ -37,7 +48,7 @@
|
|||
|
||||
$func_name = substr($this->value, 0, $str_pos);
|
||||
$args = substr($this->value, $str_pos+1, strlen($value)-1);
|
||||
|
||||
|
||||
switch($func_name) {
|
||||
case 'ipaddress' :
|
||||
$val = '$_SERVER[\'REMOTE_ADDR\']';
|
||||
|
|
@ -54,25 +65,30 @@
|
|||
break;
|
||||
case 'plus' :
|
||||
$args = abs($args);
|
||||
// TODO Make sure column name is escaped
|
||||
$val = sprintf('"%s+%d"', $this->column_name, $args);
|
||||
$this->is_operation = true;
|
||||
$this->operation = '+';
|
||||
$val = sprintf('%d', $args);
|
||||
break;
|
||||
case 'minus' :
|
||||
$args = abs($args);
|
||||
$val = sprintf('"%s-%d"', $this->column_name, $args);
|
||||
break;
|
||||
$this->is_operation = true;
|
||||
$this->operation = '-';
|
||||
$val = sprintf('%d', $args);
|
||||
break;
|
||||
case 'multiply' :
|
||||
$args = intval($args);
|
||||
$val = sprintf('"%s*%d"', $this->column_name, $args);
|
||||
$this->is_operation = true;
|
||||
$this->operation = '*';
|
||||
$val = sprintf('%d', $args);
|
||||
break;
|
||||
default :
|
||||
$val = '\'' . $this->value . '\'';
|
||||
//$val = $this->value;
|
||||
}
|
||||
|
||||
return $val;
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
|
||||
function toString(){
|
||||
return $this->value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<?php
|
||||
<?php
|
||||
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/DefaultValue.class.php');
|
||||
|
||||
class QueryArgumentValidator {
|
||||
|
|
@ -10,54 +10,59 @@
|
|||
var $max_length;
|
||||
|
||||
var $validator_string;
|
||||
|
||||
|
||||
var $argument;
|
||||
|
||||
|
||||
function QueryArgumentValidator($tag, $argument){
|
||||
$this->argument = $argument;
|
||||
$this->argument_name = $this->argument->getArgumentName();
|
||||
|
||||
|
||||
$this->default_value = $tag->attrs->default;
|
||||
$this->notnull = $tag->attrs->notnull;
|
||||
$this->filter = $tag->attrs->filter;
|
||||
$this->min_length = $tag->attrs->min_length;
|
||||
$this->max_length = $tag->attrs->max_length;
|
||||
$this->max_length = $tag->attrs->max_length;
|
||||
}
|
||||
|
||||
|
||||
function toString(){
|
||||
$validator = '';
|
||||
if(isset($this->default_value)){
|
||||
$this->default_value = new DefaultValue($this->argument_name, $this->default_value);
|
||||
if($this->default_value->isSequence())
|
||||
$validator .= '$db = &DB::getInstance(); $sequence = $db->getNextSequence(); ';
|
||||
if($this->default_value->isOperation())
|
||||
$validator .= sprintf("$%s_argument->setColumnOperation('%s');\n"
|
||||
, $this->argument_name
|
||||
, $this->default_value->getOperation()
|
||||
);
|
||||
$validator .= sprintf("$%s_argument->ensureDefaultValue(%s);\n"
|
||||
, $this->argument_name
|
||||
, $this->default_value->toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
if($this->notnull){
|
||||
$validator .= sprintf("$%s_argument->checkNotNull();\n"
|
||||
, $this->argument_name
|
||||
);
|
||||
);
|
||||
}
|
||||
if($this->filter){
|
||||
$validator .= sprintf("$%s_argument->checkFilter('%s');\n"
|
||||
, $this->argument_name
|
||||
, $this->filter
|
||||
);
|
||||
);
|
||||
}
|
||||
if($this->min_length){
|
||||
$validator .= sprintf("$%s_argument->checkMinLength(%s);\n"
|
||||
, $this->argument_name
|
||||
, $this->min_length
|
||||
);
|
||||
);
|
||||
}
|
||||
if($this->max_length){
|
||||
$validator .= sprintf("$%s_argument->checkMaxLength(%s);\n"
|
||||
, $this->argument_name
|
||||
, $this->max_length
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
return $validator;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue