forked from yii2tech/filedb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.php
More file actions
136 lines (124 loc) · 3.88 KB
/
Copy pathQuery.php
File metadata and controls
136 lines (124 loc) · 3.88 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\filedb;
use yii\base\Component;
use yii\db\QueryInterface;
use yii\db\QueryTrait;
/**
* Query represents the data file search inquiry.
*
* For example:
*
* ```php
* $query = new Query();
* // compose the query
* $query->from('status')
* ->where(['type' => 'public'])
* ->limit(10);
* // build and execute the query
* $rows = $query->all();
* ```
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class Query extends Component implements QueryInterface
{
use QueryTrait;
/**
* @var string data file name to be selected from.
* @see from()
*/
public $from;
/**
* Executes the query and returns all results as an array.
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public function all($db = null)
{
$rows = $this->fetchData($db);
return $this->populate($rows);
}
/**
* Executes the query and returns a single row of result.
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query
* results in nothing.
*/
public function one($db = null)
{
$rows = $this->fetchData($db);
return empty($rows) ? false : reset($rows);
}
/**
* Returns the number of records.
* @param string $q the COUNT expression. Defaults to '*'.
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return integer number of records.
*/
public function count($q = '*', $db = null)
{
$data = $this->fetchData($db);
return count($data);
}
/**
* Returns a value indicating whether the query result contains any row of data.
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return boolean whether the query result contains any row of data.
*/
public function exists($db = null)
{
$data = $this->fetchData($db);
return !empty($data);
}
/**
* Sets data file name to be selected from.
* @param string $name data file name.
* @return $this the query object itself
*/
public function from($name)
{
$this->from = $name;
return $this;
}
/**
* @param Connection $db
* @return array[]
*/
protected function fetchData($db)
{
return $db->getQueryProcessor()->process($this);
}
/**
* Converts the raw query results into the format as specified by this query.
* This method is internally used to convert the data fetched from database
* into the format as required by this query.
* @param array $rows the raw query result from database
* @return array the converted query result
*/
public function populate($rows)
{
if ($this->indexBy === null) {
return array_values($rows); // reset storage internal keys
}
$result = [];
foreach ($rows as $row) {
if (is_string($this->indexBy)) {
$key = $row[$this->indexBy];
} else {
$key = call_user_func($this->indexBy, $row);
}
$result[$key] = $row;
}
return $result;
}
}