summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Vendor/TSqliteMetaData.php
blob: c44d73cbe9372a2557fa31cf2dbe3a0fa687e81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
 * TSqliteMetaData class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2007 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.ActiveRecord.Vendor
 */

Prado::using('System.Data.ActiveRecord.Vendor.TDbMetaDataCommon');

/**
 * TSqliteMetaData specialized command builder for SQLite database.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.ActiveRecord.Vendor
 * @since 3.1
 */
class TSqliteMetaData extends TDbMetaDataCommon
{
	/**
	 * Build the SQL search string from the criteria object for Postgress database.
	 * @param TDbConnection database connection.
	 * @param TActiveRecordCriteria search criteria.
	 * @return string SQL search.
	 */
	protected function getSqlFromCriteria($conn, $criteria)
	{
		if($criteria===null) return '';
		$sql = '';
		if(($condition = $criteria->getCondition())!==null)
			$sql .= ' WHERE '.$condition;
		$orders=array();
		foreach($criteria->getOrdersBy() as $by=>$ordering)
			$orders[] = $this->getOrdering($by, $ordering);
		if(count($orders) > 0)
			$sql .= ' ORDER BY '.implode(', ', $orders);
		if(($limit = $criteria->getLimit())!==null)
		{
			$offset = $criteria->getOffset();
			$offset = $offset===null?0:intval($offset); //assume integer offset?
			$sql .= ' LIMIT '.$offset.', '.intval($limit); //assume integer limit?
		}
		return strlen($sql) > 0 ? $sql : '';
	}

	public function getSearchRegExpCriteria($fields, $keywords)
	{
		if(strlen(trim($keywords)) == 0) return '';
		$words = array();
		preg_match_all('/([a-zA-Z0-9-+]+)/', $keywords, $words);
		$result = array();
		foreach($fields as $field)
			$result[] = $this->getLikeCriteriaStr($this->getColumn($field)->getName(), $words[0]);
		return '('.implode(' OR ', $result).')';
	}

	protected function getLikeCriteriaStr($column, $words)
	{
		$result=array();
		foreach($words as $word)
			$result[] = "({$column} LIKE \"%{$word}%\")";
		return '('.implode(' AND ', $result).')';
	}

	/**
	 * Remove quote from the keys in the data.
	 * @param mixed record row
	 * @return array record row
	 */
	public function postQueryRow($row)
	{
		if(!is_array($row)) return $row;
		$result=array();
		foreach($row as $k=>$v)
			$result[str_replace('"','',$k)]=$v;
		return $result;
	}

	/**
	 * Remove quote from the keys in the data.
	 * @param mixed record row
	 * @return array record row
	 */
	public function postQuery($rows)
	{
		$data = array();
		foreach($rows as $k=>$v)
			$data[$k] = $this->postQueryRow($v);
		return $data;
	}
}

?>