mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-23 12:22:15 +09:00
Updated query classes in order to support prepared statements - added support for parameter binding. Added unit tests for mssql select using new prepared statement syntax.
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8511 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
32fe8750e9
commit
6f17aa5759
11 changed files with 93 additions and 16 deletions
|
|
@ -11,12 +11,8 @@
|
|||
var $orderby;
|
||||
var $limit;
|
||||
|
||||
var $arguments = array();
|
||||
|
||||
function addArgument($argument){
|
||||
$this->arguments[] = $argument;
|
||||
}
|
||||
|
||||
var $arguments = null;
|
||||
|
||||
function setQueryId($queryID){
|
||||
$this->queryID = $queryID;
|
||||
}
|
||||
|
|
@ -202,6 +198,35 @@
|
|||
function getFirstTableName(){
|
||||
return $this->tables[0]->getName();
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
if(!isset($this->arguments)){
|
||||
$this->arguments = array();
|
||||
|
||||
// Column arguments
|
||||
foreach($this->columns as $column){
|
||||
if($column->show()){
|
||||
$arg = $column->getArgument();
|
||||
if($arg) $this->arguments[] = $arg;
|
||||
}
|
||||
}
|
||||
|
||||
// Condition arguments
|
||||
if(count($this->conditions) > 0)
|
||||
foreach($this->conditions as $conditionGroup){
|
||||
$args = $conditionGroup->getArguments();
|
||||
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
|
||||
}
|
||||
|
||||
// Navigation arguments
|
||||
if(count($this->orderby) > 0)
|
||||
foreach($this->orderby as $order){
|
||||
$args = $order->getArguments();
|
||||
if(count($args) > 0) $this->arguments = array_merge($this->arguments, $args);
|
||||
}
|
||||
}
|
||||
return $this->arguments;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,11 @@
|
|||
return is_a($this->argument, 'Argument');
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
if($this->hasArgument()) return $this->argument;
|
||||
return null;
|
||||
}
|
||||
|
||||
function toString($withValue = true){
|
||||
if($withValue)
|
||||
return $this->toStringWithValue();
|
||||
|
|
|
|||
|
|
@ -31,5 +31,16 @@
|
|||
|
||||
return $group;
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
$args = array();
|
||||
foreach($this->conditions as $condition){
|
||||
if($condition->show()){
|
||||
$arg = $condition->getArgument();
|
||||
if($arg) $args[] = $arg;
|
||||
}
|
||||
}
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -26,6 +26,10 @@
|
|||
if(!isset($value)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return $this->argument;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -28,5 +28,9 @@
|
|||
function show() {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -12,5 +12,9 @@
|
|||
function StarExpression(){
|
||||
parent::SelectExpression("*");
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -40,6 +40,10 @@
|
|||
if(!$this->argument->getValue()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function getArgument(){
|
||||
return $this->argument;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,14 @@
|
|||
$result .= is_a($this->sort_order, 'Argument') ? $this->sort_order->getValue() : $this->sort_order;
|
||||
return $result;
|
||||
}
|
||||
|
||||
function getArguments(){
|
||||
$args = array();
|
||||
if(is_a($this->column_name, 'Argument'))
|
||||
$args[]= $this->column_name;
|
||||
if(is_a($this->sort_order, 'Argument'))
|
||||
$args[] = $this->sort_order;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -109,6 +109,7 @@ class QueryParser {
|
|||
|
||||
$this->setTableColumnTypes($tables);
|
||||
|
||||
// TODO Check if this work with arguments in join clause
|
||||
$arguments = array();
|
||||
if($columns)
|
||||
$arguments = array_merge($arguments, $columns->getArguments());
|
||||
|
|
@ -122,7 +123,6 @@ class QueryParser {
|
|||
$prebuff .= sprintf("$%s_argument->setColumnType('%s');\n"
|
||||
, $argument->getArgumentName()
|
||||
, $this->column_type[$this->getQueryId()][$argument->getColumnName()] );
|
||||
$prebuff .= sprintf('$query->addArgument($%s_argument);', $argument->getArgumentName());
|
||||
}
|
||||
}
|
||||
$prebuff .= "\n";
|
||||
|
|
|
|||
|
|
@ -14,6 +14,10 @@
|
|||
$this->isValid = true;
|
||||
}
|
||||
|
||||
function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
function getValue(){
|
||||
if(is_array($this->value)) return implode(',', $this->value);
|
||||
return $this->value;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
class SelectXmlTest_Mssql extends PHPUnit_Framework_TestCase {
|
||||
|
||||
function _test($xml_file, $argsString, $expected){
|
||||
function _test($xml_file, $argsString, $expected, $expectedArgs = NULL){
|
||||
$tester = new QueryTester();
|
||||
$outputString = $tester->getNewParserOutputString($xml_file, '[', $argsString, 'mssql');
|
||||
//echo $outputString;
|
||||
|
|
@ -14,7 +14,8 @@
|
|||
}else {
|
||||
$db = &DB::getInstance('mssql');
|
||||
$querySql = $db->getSelectSql($output);
|
||||
|
||||
$queryArguments = $output->getArguments();
|
||||
|
||||
// Remove whitespaces, tabs and all
|
||||
$querySql = Helper::cleanQuery($querySql);
|
||||
$expected = Helper::cleanQuery($expected);
|
||||
|
|
@ -22,13 +23,20 @@
|
|||
|
||||
// Test
|
||||
$this->assertEquals($expected, $querySql);
|
||||
|
||||
// Test query arguments
|
||||
$argCount = count($expectedArgs);
|
||||
for($i = 0; $i < $argCount; $i++){
|
||||
//echo "$i: $expectedArgs[$i] vs $queryArguments[$i]->getValue()";
|
||||
$this->assertEquals($expectedArgs[$i], $queryArguments[$i]->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
function testSelectStar(){
|
||||
$xml_file = _XE_PATH_ . "modules/module/queries/getAdminId.xml";
|
||||
$argsString = '$args->module_srl = 10;';
|
||||
$expected = 'SELECT * FROM [xe_module_admins] as [module_admins] , [xe_member] as [member] WHERE [module_srl] = ? and [member].[member_srl] = [module_admins].[member_srl]';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array(10));
|
||||
}
|
||||
|
||||
function testRquiredParameter(){
|
||||
|
|
@ -59,7 +67,7 @@
|
|||
on [module_categories].[module_category_srl] = [modules].[module_category_srl]
|
||||
WHERE [modules].[site_srl] = ?
|
||||
ORDER BY [modules].[module] asc, [module_categories].[title] asc, [modules].[mid] asc';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array(0));
|
||||
}
|
||||
|
||||
function test_module_getSiteInfo(){
|
||||
|
|
@ -92,7 +100,7 @@
|
|||
FROM [xe_sites] as [sites]
|
||||
left join [xe_modules] as [modules] on [modules].[module_srl] = [sites].[index_module_srl]
|
||||
WHERE [sites].[site_srl] = ? ';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array(0));
|
||||
}
|
||||
|
||||
function test_addon_getAddonInfo(){
|
||||
|
|
@ -101,7 +109,7 @@
|
|||
$expected = 'SELECT *
|
||||
FROM [xe_addons] as [addons]
|
||||
WHERE [addon] = ? ';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array("'captcha'"));
|
||||
}
|
||||
|
||||
function test_addon_getAddons(){
|
||||
|
|
@ -129,7 +137,7 @@
|
|||
WHERE [regdate] >= ?
|
||||
GROUP BY substr([regdate],1,8)
|
||||
ORDER BY substr([regdate],1,8) asc';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array("'20110411'"));
|
||||
}
|
||||
|
||||
function test_member_getAutoLogin(){
|
||||
|
|
@ -141,7 +149,7 @@
|
|||
FROM [xe_member] as [member] , [xe_member_autologin] as [member_autologin]
|
||||
WHERE [member_autologin].[autologin_key] = ?
|
||||
and [member].[member_srl] = [member_autologin].[member_srl]';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array("'10'"));
|
||||
}
|
||||
|
||||
function test_opage_getOpageList(){
|
||||
|
|
@ -152,7 +160,7 @@
|
|||
FROM [xe_modules] as [modules]
|
||||
WHERE [module] = ? and ([browser_title] like ?)
|
||||
ORDER BY [module_srl] desc';
|
||||
$this->_test($xml_file, $argsString, $expected);
|
||||
$this->_test($xml_file, $argsString, $expected, array("'opage'", "'%yuhuu%'"));
|
||||
}
|
||||
|
||||
// TODO Something fishy about this query - to be investigated
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue