From 1ae09931b2572d9c3067c99f841a60cb3330b3f3 Mon Sep 17 00:00:00 2001 From: wei <> Date: Thu, 3 May 2007 00:48:04 +0000 Subject: Update active relations docs --- .../Data/Common/Mssql/TMssqlCommandBuilder.php | 6 +-- framework/Data/Common/Mssql/TMssqlTableInfo.php | 1 - .../Data/Common/Sqlite/TSqliteCommandBuilder.php | 48 ++++++++++++++++++++++ framework/Data/Common/Sqlite/TSqliteMetaData.php | 2 +- framework/Data/Common/Sqlite/TSqliteTableInfo.php | 9 ++++ framework/Data/Common/TDbCommandBuilder.php | 39 +++++++++--------- 6 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 framework/Data/Common/Sqlite/TSqliteCommandBuilder.php (limited to 'framework/Data/Common') diff --git a/framework/Data/Common/Mssql/TMssqlCommandBuilder.php b/framework/Data/Common/Mssql/TMssqlCommandBuilder.php index b22c08a5..3de6aa5e 100644 --- a/framework/Data/Common/Mssql/TMssqlCommandBuilder.php +++ b/framework/Data/Common/Mssql/TMssqlCommandBuilder.php @@ -1,6 +1,6 @@ * @link http://www.pradosoft.com/ @@ -13,8 +13,8 @@ Prado::using('System.Data.Common.TDbCommandBuilder'); /** - * TDbCommandBuilder provides basic methods to create query commands for tables - * giving by {@link setTableInfo TableInfo} the property. + * TMssqlCommandBuilder provides specifics methods to create limit/offset query commands + * for MSSQL servers. * * @author Wei Zhuo * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ diff --git a/framework/Data/Common/Mssql/TMssqlTableInfo.php b/framework/Data/Common/Mssql/TMssqlTableInfo.php index e408440b..ee9c500b 100644 --- a/framework/Data/Common/Mssql/TMssqlTableInfo.php +++ b/framework/Data/Common/Mssql/TMssqlTableInfo.php @@ -60,7 +60,6 @@ class TMssqlTableInfo extends TDbTableInfo Prado::using('System.Data.Common.Mssql.TMssqlCommandBuilder'); return new TMssqlCommandBuilder($connection,$this); } - } ?> \ No newline at end of file diff --git a/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php b/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php new file mode 100644 index 00000000..9f1253c9 --- /dev/null +++ b/framework/Data/Common/Sqlite/TSqliteCommandBuilder.php @@ -0,0 +1,48 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2007 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ + * @package System.Data.Common + */ + +Prado::using('System.Data.Common.TDbCommandBuilder'); + +/** + * TSqliteCommandBuilder provides specifics methods to create limit/offset query commands + * for Sqlite database. + * + * @author Wei Zhuo + * @version $Id: TDbCommandBuilder.php 1863 2007-04-12 12:43:49Z wei $ + * @package System.Data.Common + * @since 3.1 + */ +class TSqliteCommandBuilder extends TDbCommandBuilder +{ + /** + * Alters the sql to apply $limit and $offset. + * @param string SQL query string. + * @param integer maximum number of rows, -1 to ignore limit. + * @param integer row offset, -1 to ignore offset. + * @return string SQL with limit and offset. + */ + public function applyLimitOffset($sql, $limit=-1, $offset=-1) + { + $limit = $limit!==null ? intval($limit) : -1; + $offset = $offset!==null ? intval($offset) : -1; + if($limit > 0 || $offset > 0) + { + $limitStr = ' LIMIT '.$limit; + $offsetStr = $offset >= 0 ? ' OFFSET '.$offset : ''; + return $sql.$limitStr.$offsetStr; + } + else + return $sql; + } +} + +?> \ No newline at end of file diff --git a/framework/Data/Common/Sqlite/TSqliteMetaData.php b/framework/Data/Common/Sqlite/TSqliteMetaData.php index f9b49488..6c6ff232 100644 --- a/framework/Data/Common/Sqlite/TSqliteMetaData.php +++ b/framework/Data/Common/Sqlite/TSqliteMetaData.php @@ -49,7 +49,7 @@ class TSqliteMetaData extends TDbMetaData if($column->getIsPrimaryKey()) $primary[] = $col['name']; } - $info['TableName'] = $tableName; + $info['TableName'] = $table; if($this->getIsView($tableName)) $info['IsView'] = true; if(count($columns)===0) diff --git a/framework/Data/Common/Sqlite/TSqliteTableInfo.php b/framework/Data/Common/Sqlite/TSqliteTableInfo.php index 1581c3cc..e0bcb484 100644 --- a/framework/Data/Common/Sqlite/TSqliteTableInfo.php +++ b/framework/Data/Common/Sqlite/TSqliteTableInfo.php @@ -26,6 +26,15 @@ Prado::using('System.Data.Common.Sqlite.TSqliteTableColumn'); */ class TSqliteTableInfo extends TDbTableInfo { + /** + * @param TDbConnection database connection. + * @return TDbCommandBuilder new command builder + */ + public function createCommandBuilder($connection) + { + Prado::using('System.Data.Common.Sqlite.TSqliteCommandBuilder'); + return new TSqliteCommandBuilder($connection,$this); + } } ?> \ No newline at end of file diff --git a/framework/Data/Common/TDbCommandBuilder.php b/framework/Data/Common/TDbCommandBuilder.php index 440d579d..3535100f 100644 --- a/framework/Data/Common/TDbCommandBuilder.php +++ b/framework/Data/Common/TDbCommandBuilder.php @@ -117,37 +117,38 @@ class TDbCommandBuilder extends TComponent } /** - * NOT SAFE YET! + * Computes the SQL condition for search a set of column using regular expression + * (or LIKE, depending on database implementation) to match a string of + * keywords (default matches all keywords). + * @param array list of column id for potential search condition. + * @param string string of keywords + * @return string SQL search condition matching on a set of columns. */ public function getSearchExpression($fields, $keywords) { if(strlen(trim($keywords)) == 0) return ''; - $words = preg_split('/\s/', preg_quote($keywords, '\'')); - $result = array(); + $words = preg_split('/\s/u', $keywords); + $conditions = array(); foreach($fields as $field) { $column = $this->getTableInfo()->getColumn($field)->getColumnName(); - $result[] = $this->getRegexpCriteriaStr($column, $words); + $conditions[] = $this->getSearchCondition($column, $words); } - return '('.implode(' OR ', $result).')'; - } - - protected function getRegexpCriteriaStr($column, $words) - { - $regexp = implode('|', $words); - return "({$column} REGEXP '{$regexp}')"; + return '('.implode(' OR ', $conditions).')'; } /** - * 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 - * that permit regular expression matching. This method should be implemented in - * database specific command builder classes. - * @param array list of column id for potential search condition. - * @param string string of keywords - * @return string SQL condition for regular expression matching on a set of columns. + * @param string column name. + * @param array keywords + * @return string search condition for all words in one column. */ - //abstract public function createRegExpSearch($columnIds, $keywords); + protected function getSearchCondition($column, $words) + { + $conditions=array(); + foreach($words as $word) + $conditions[] = $column.' LIKE '.$this->getDbConnection()->quoteString('%'.$word.'%'); + return '('.implode(' AND ', $conditions).')'; + } /** * Appends the $where condition to the string "SELECT * FROM tableName WHERE ". -- cgit v1.2.3