From 868a541816b9613fd1a2bf01902cdf28e68bab26 Mon Sep 17 00:00:00 2001 From: qixinzhi Date: Wed, 18 Jun 2025 01:44:13 +0000 Subject: [PATCH] * [misc] add quote for getCondition. --- lib/sqlparser/sqlparser.class.php | 8 +++++--- module/bi/model.php | 4 ++-- module/bi/test/lib/bi.unittest.class.php | 4 ++-- module/bi/test/model/getcondition.php | 12 ++++++------ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/sqlparser/sqlparser.class.php b/lib/sqlparser/sqlparser.class.php index 7a7e3be8f0..42dc06199c 100644 --- a/lib/sqlparser/sqlparser.class.php +++ b/lib/sqlparser/sqlparser.class.php @@ -301,7 +301,7 @@ class sqlparser * @access public * @return object */ - public function getCondition($tableA = null, $columnA = null, $operator = '', $tableB = null, $columnB = null, $group = 1) + public function getCondition($tableA = null, $columnA = null, $operator = '', $tableB = null, $columnB = null, $group = 1, $quote = true) { if(is_array($tableA)) return call_user_func_array(array($this, 'getCondition'), $tableA); @@ -323,8 +323,10 @@ class sqlparser /* 如果tableB为空,那么columnB是值,需要trim("'")。*/ if(empty($tableB)) $columnB = $this->trimExpr($columnB, "'"); - $exprA = empty($tableA) ? "`$columnA`" : "`$tableA`.`$columnA`"; - $exprB = empty($tableB) ? "'$columnB'" : "`$tableB`.`$columnB`"; + $columnB = empty($tableB) && $quote ? "'$columnB'" : $columnB; + $exprA = empty($tableA) ? "`$columnA`" : "`$tableA`.`$columnA`"; + $exprB = empty($tableB) ? $columnB : "`$tableB`.`$columnB`"; + $operator = strtoupper($operator); $expr = "$exprA $operator $exprB"; diff --git a/module/bi/model.php b/module/bi/model.php index bc1115cf7a..a7bcfe5cfe 100644 --- a/module/bi/model.php +++ b/module/bi/model.php @@ -1463,12 +1463,12 @@ class biModel extends model * @access public * @return object */ - public function getCondition(mixed $tableA = null, mixed $columnA = null, string $operator = '', mixed $tableB = null, mixed $columnB = null, int $group = 1): object + public function getCondition(mixed $tableA = null, mixed $columnA = null, string $operator = '', mixed $tableB = null, mixed $columnB = null, int $group = 1, bool $quote = true): object { $this->app->loadClass('sqlparser', true); $parser = new sqlparser(null); - return $parser->getCondition($tableA, $columnA, $operator, $tableB, $columnB, $group); + return $parser->getCondition($tableA, $columnA, $operator, $tableB, $columnB, $group, $quote); } /** diff --git a/module/bi/test/lib/bi.unittest.class.php b/module/bi/test/lib/bi.unittest.class.php index 0ea469698d..7560494f2a 100644 --- a/module/bi/test/lib/bi.unittest.class.php +++ b/module/bi/test/lib/bi.unittest.class.php @@ -60,9 +60,9 @@ class biTest * @access public * @return string */ - public function getConditionTest(mixed $tableA = null, mixed $columnA = null, string $operator = '', mixed $tableB = null, mixed $columnB = null, int $group = 1): string + public function getConditionTest(mixed $tableA = null, mixed $columnA = null, string $operator = '', mixed $tableB = null, mixed $columnB = null, int $group = 1, bool $quote = true): string { - $condition = $this->objectModel->getCondition($tableA, $columnA, $operator, $tableB, $columnB, $group); + $condition = $this->objectModel->getCondition($tableA, $columnA, $operator, $tableB, $columnB, $group, $quote); return $condition->build($condition); } diff --git a/module/bi/test/model/getcondition.php b/module/bi/test/model/getcondition.php index b69ea3dd98..f6370f5dc8 100755 --- a/module/bi/test/model/getcondition.php +++ b/module/bi/test/model/getcondition.php @@ -24,14 +24,14 @@ cid=1 */ $bi = new biTest(); -r($bi->getConditionTest(null, 'deleted', '=', null, "'0'")) && p('') && e("`deleted` = '0'"); // 测试 `deleted` = '0' -r($bi->getConditionTest('t1', 'deleted', '=', null, "'0'")) && p('') && e("`t1`.`deleted` = '0'"); // 测试 `t1`.`deleted` = '0' +r($bi->getConditionTest(null, 'deleted', '=', null, "0")) && p('') && e("`deleted` = '0'"); // 测试 `deleted` = '0' +r($bi->getConditionTest('t1', 'deleted', '=', null, "0")) && p('') && e("`t1`.`deleted` = '0'"); // 测试 `t1`.`deleted` = '0' r($bi->getConditionTest('t1', 'project', '=', 't2', 'id')) && p('') && e("`t1`.`project` = `t2`.`id`"); // 测试 `t1`.`project` = `t2`.`id` r($bi->getConditionTest('`t1`', '`project`', '=', 't2', 'id')) && p('') && e("`t1`.`project` = `t2`.`id`"); // 测试 `t1`.`project` = `t2`.`id` r($bi->getConditionTest('`t1`', '`project` ', '=', 't2', 'id')) && p('') && e("`t1`.`project` = `t2`.`id`"); // 测试 `t1`.`project` = `t2`.`id` -r($bi->getConditionTest('t1', 'type', 'in', null, "('sprint', 'stage', 'kanban')")) && p('') && e("`t1`.`type` IN ('sprint', 'stage', 'kanban')"); // 测试 `t1`.`type` IN ('sprint', 'stage', 'kanban') -r($bi->getConditionTest('t1', 'type', 'not in', null, "('sprint', 'stage', 'kanban')")) && p('') && e("`t1`.`type` NOT IN ('sprint', 'stage', 'kanban')"); // 测试 `t1`.`type` NOT IN ('sprint', 'stage', 'kanban') +r($bi->getConditionTest('t1', 'type', 'in', null, "('sprint', 'stage', 'kanban')", 1, false)) && p('') && e("`t1`.`type` IN ('sprint', 'stage', 'kanban')"); // 测试 `t1`.`type` IN ('sprint', 'stage', 'kanban') +r($bi->getConditionTest('t1', 'type', 'not in', null, "('sprint', 'stage', 'kanban')", 1, false)) && p('') && e("`t1`.`type` NOT IN ('sprint', 'stage', 'kanban')"); // 测试 `t1`.`type` NOT IN ('sprint', 'stage', 'kanban') -r($bi->getConditionTest('t1', 'name', 'is', null, 'not null')) && p('') && e("`t1`.`name` IS not null"); // 测试 `t1`.`name` IS not null -r($bi->getConditionTest('t1', 'name', 'is', null, 'null')) && p('') && e("`t1`.`name` IS null"); // 测试 `t1`.`name` IS not null`t1`.`name` IS null \ No newline at end of file +r($bi->getConditionTest('t1', 'name', 'is', null, 'not null', 1, false)) && p('') && e("`t1`.`name` IS not null"); // 测试 `t1`.`name` IS not null +r($bi->getConditionTest('t1', 'name', 'is', null, 'null', 1, false)) && p('') && e("`t1`.`name` IS null"); // 测试 `t1`.`name` IS not null`t1`.`name` IS null \ No newline at end of file