Prepared statements - if argument is not given as array (eg. for IN clauses) even though it should be, convert it to an array.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8633 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-07-25 16:47:22 +00:00
parent b3c75ac4db
commit 39c2c004c2
6 changed files with 118 additions and 185 deletions

View file

@ -64,6 +64,7 @@
function show(){ function show(){
if($this->hasArgument() && !$this->argument->isValid()) return false; if($this->hasArgument() && !$this->argument->isValid()) return false;
if($this->hasArgument() && ($this->_value === '\'\'')) return false; if($this->hasArgument() && ($this->_value === '\'\'')) return false;
if(is_array($this->_value) && count($this->_value) === 1 && $this->_value[0] === '') return false;
switch($this->operation) { switch($this->operation) {
case 'equal' : case 'equal' :
case 'more' : case 'more' :

View file

@ -97,48 +97,48 @@
if(isset($this->value) && $this->value != ''){ if(isset($this->value) && $this->value != ''){
$val = $this->value; $val = $this->value;
$key = $this->name; $key = $this->name;
switch($filter_type) { switch($filter_type) {
case 'email' : case 'email' :
case 'email_address' : 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->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_email, $lang->{$key} ? $lang->{$key} : $key)); $this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_email, $lang->{$key} ? $lang->{$key} : $key));
} }
break; break;
case 'homepage' : case 'homepage' :
if(!preg_match('/^(http|https)+(:\/\/)+[0-9a-z_-]+\.[^ ]+$/is', $val)) { if(!preg_match('/^(http|https)+(:\/\/)+[0-9a-z_-]+\.[^ ]+$/is', $val)) {
$this->isValid = false; $this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_homepage, $lang->{$key} ? $lang->{$key} : $key)); $this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_homepage, $lang->{$key} ? $lang->{$key} : $key));
} }
break; break;
case 'userid' : case 'userid' :
case 'user_id' : case 'user_id' :
if(!preg_match('/^[a-zA-Z]+([_0-9a-zA-Z]+)*$/is', $val)) { if(!preg_match('/^[a-zA-Z]+([_0-9a-zA-Z]+)*$/is', $val)) {
$this->isValid = false; $this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_userid, $lang->{$key} ? $lang->{$key} : $key)); $this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_userid, $lang->{$key} ? $lang->{$key} : $key));
} }
break; break;
case 'number' : case 'number' :
case 'numbers' : case 'numbers' :
if(is_array($val)) $val = join(',', $val); 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->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_number, $lang->{$key} ? $lang->{$key} : $key)); $this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_number, $lang->{$key} ? $lang->{$key} : $key));
} }
break; break;
case 'alpha' : case 'alpha' :
if(!preg_match('/^[a-z]+$/is', $val)) { if(!preg_match('/^[a-z]+$/is', $val)) {
$this->isValid = false; $this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha, $lang->{$key} ? $lang->{$key} : $key)); $this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha, $lang->{$key} ? $lang->{$key} : $key));
} }
break; break;
case 'alpha_number' : case 'alpha_number' :
if(!preg_match('/^[0-9a-z]+$/is', $val)) { if(!preg_match('/^[0-9a-z]+$/is', $val)) {
$this->isValid = false; $this->isValid = false;
$this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha_number, $lang->{$key} ? $lang->{$key} : $key)); $this->errorMessage = new Object(-1, sprintf($lang->filter->invalid_alpha_number, $lang->{$key} ? $lang->{$key} : $key));
} }
break; break;
} }
} }
} }

View file

@ -5,6 +5,9 @@
function ConditionArgument($name, $value, $operation){ function ConditionArgument($name, $value, $operation){
if(isset($value) && in_array($operation, array('in', 'not in', 'between')) && !is_array($value)){
$value = explode(',', $value);
}
parent::Argument($name, $value); parent::Argument($name, $value);
$this->operation = $operation; $this->operation = $operation;
@ -35,85 +38,6 @@
if(!is_array($value)) $this->value = array($value); if(!is_array($value)) $this->value = array($value);
break; break;
} }
/*
//if(!in_array($operation,array('in','notin','between')) && is_array($value)){
// $value = join(',', $value);
//}
// Daca operatia nu este in, notin, between si coloana e de tip numeric
// daca valoarea e array -> concatenare
// daca valoarea nu e array si nici nu contine paranteze (nu e functie) -> return (int)
// altfel return valoare
// if(!in_array($operation,array('in','notin','between')) && $type == 'number') {
// if(is_array($value)){
// $value = join(',',$value);
// }
// 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;
// }
switch($operation) {
case 'like_prefix' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
$value = $value.'%';
break;
case 'like_tail' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
$value = '%'.$value;
break;
case 'like' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
$value = '%'.$value.'%';
break;
// case 'notin' :
// if(is_array($value))
// {
// $value = $this->addQuotesArray($value);
// if($type=='number') return join(',',$value);
// else return "'". join("','",$value)."'";
// }
// else
// {
// return $value;
// }
// break;
// case 'in' :
// if(is_array($value))
// {
// $value = $this->addQuotesArray($value);
// if($type=='number') return join(',',$value);
// else return "'". join("','",$value)."'";
// }
// else
// {
// return $value;
// }
// break;
// case 'between' :
// if(!is_array($value)) $value = array($value);
// $value = $this->addQuotesArray($value);
// if($type!='number')
// {
// foreach($value as $k=>$v)
// {
// $value[$k] = "'".$v."'";
// }
// }
//return $value;
break;
default:
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
}
$this->value = $value;
//return "'".$this->addQuotes($value)."'";
*/
} }
function getType(){ function getType(){

View file

@ -62,7 +62,7 @@
, "unique_visitor" , "unique_visitor"
, "pageview") , "pageview")
values values
(0 ('.date("YmdHis").'
, 0 , 0
, 0)'; , 0)';
$this->_test($xml_file, $argsString, $expected); $this->_test($xml_file, $argsString, $expected);

View file

@ -27,7 +27,7 @@
$args->s_member_srl = 4; $args->s_member_srl = 4;
$output = executeQuery('document.getDocumentList', $args); $output = executeQuery('document.getDocumentList', $args);
$this->assertEquals(0, $output->error, $output->message); $this->assertEquals(0, $output->error, $output->message . PHP_EOL . $output->variables["_query"]);
} }
function test_syndication_getDocumentList(){ function test_syndication_getDocumentList(){

View file

@ -145,6 +145,14 @@
$this->_test($xml_file, $argsString, $expected, array("25")); $this->_test($xml_file, $argsString, $expected, array("25"));
} }
function test_module_getModuleSites(){
$xml_file = _XE_PATH_ . "modules/module/queries/getModuleSites.xml";
//$argsString = '$args->module_srls = array(67, 65);';
$argsString = '$args->module_srls = "67, 65";';
$expected = 'SELECT [modules].[module_srl] as [module_srl], [sites].[domain] as [domain] FROM [xe_modules] as [modules] , [xe_sites] as [sites] WHERE [modules].[module_srl] in (?,?) and [sites].[site_srl] = [modules].[site_srl]';
$this->_test($xml_file, $argsString, $expected, array("67", "65"));
}
// TODO Something fishy about this query - to be investigated // TODO Something fishy about this query - to be investigated