From 705163ee024426bd622d9722b7d18dd0e6920820 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 24 Apr 2007 07:22:28 +0000 Subject: Fixed scaffold search. But not safe yet. --- framework/Data/Common/TDbCommandBuilder.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'framework/Data/Common') diff --git a/framework/Data/Common/TDbCommandBuilder.php b/framework/Data/Common/TDbCommandBuilder.php index 3a08d890..440d579d 100644 --- a/framework/Data/Common/TDbCommandBuilder.php +++ b/framework/Data/Common/TDbCommandBuilder.php @@ -116,6 +116,28 @@ class TDbCommandBuilder extends TComponent return $sql; } + /** + * NOT SAFE YET! + */ + public function getSearchExpression($fields, $keywords) + { + if(strlen(trim($keywords)) == 0) return ''; + $words = preg_split('/\s/', preg_quote($keywords, '\'')); + $result = array(); + foreach($fields as $field) + { + $column = $this->getTableInfo()->getColumn($field)->getColumnName(); + $result[] = $this->getRegexpCriteriaStr($column, $words); + } + return '('.implode(' OR ', $result).')'; + } + + protected function getRegexpCriteriaStr($column, $words) + { + $regexp = implode('|', $words); + return "({$column} REGEXP '{$regexp}')"; + } + /** * Computes the SQL condition for search a set of column using regular expression * to match a string of keywords. The implementation should only uses columns -- cgit v1.2.3