@@ -20,162 +20,162 @@ |
||
20 | 20 | */ |
21 | 21 | class ActiveRecordQuery |
22 | 22 | { |
23 | - private $table; |
|
24 | - |
|
25 | - private $hooks; |
|
26 | - |
|
27 | - private $results; |
|
28 | - |
|
29 | - private $query; |
|
30 | - |
|
31 | - private $whereExpression = null; |
|
32 | - |
|
33 | - private $clauses = []; |
|
34 | - |
|
35 | - public function __construct(AbstractActiveRecord $instance, $table, Array $additionalWhereClauses) |
|
36 | - { |
|
37 | - $this->table = $table; |
|
38 | - $this->query = new Query($instance->getPdo(), $table); |
|
39 | - $this->type = $instance; |
|
40 | - $this->clauses = $additionalWhereClauses; |
|
41 | - } |
|
42 | - |
|
43 | - private function execute() |
|
44 | - { |
|
45 | - $clauses = $this->clauses; |
|
46 | - |
|
47 | - // Optionally add user concatenated where expression |
|
48 | - if ($this->whereExpression !== null) |
|
49 | - { |
|
50 | - $clauses[] = $this->whereExpression; |
|
51 | - } |
|
52 | - |
|
53 | - // Construct where clause |
|
54 | - if (count($clauses) == 1) |
|
55 | - { |
|
56 | - $this->query->where($clauses[0]); |
|
57 | - } else if (count($clauses) >= 2) |
|
58 | - { |
|
59 | - $rest = array_slice($clauses, 1); |
|
60 | - $this->query->where(Query::And($clauses[0], ...$rest)); |
|
61 | - } |
|
62 | - |
|
63 | - $this->query->select(); |
|
64 | - |
|
65 | - $this->results = $this->query->execute(); |
|
66 | - |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - public function fetchAll() |
|
71 | - { |
|
72 | - try { |
|
73 | - // TODO: Should execute call be explicit? |
|
74 | - $this->execute(); |
|
75 | - |
|
76 | - $typedResults = []; |
|
77 | - |
|
78 | - $entries = $this->results->fetchAll(); |
|
79 | - if ($entries === false) { |
|
80 | - throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
|
81 | - } |
|
82 | - |
|
83 | - foreach ($entries as $entry) { |
|
84 | - $typedEntry = clone $this->type; |
|
85 | - $typedEntry->fill($entry); |
|
86 | - $typedResults[] = $typedEntry; |
|
87 | - } |
|
88 | - |
|
89 | - return $typedResults; |
|
90 | - } catch (\PDOException $e) { |
|
91 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - public function fetch() |
|
96 | - { |
|
97 | - try { |
|
98 | - $this->execute(); |
|
99 | - |
|
100 | - $typedResult = clone $this->type; |
|
101 | - |
|
102 | - $entry = $this->results->fetch(); |
|
103 | - if ($entry === false) { |
|
104 | - throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
|
105 | - } |
|
106 | - |
|
107 | - $typedResult->fill($entry); |
|
108 | - |
|
109 | - return $typedResult; |
|
110 | - } catch (\PDOException $e) { |
|
111 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
112 | - } |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * Set the where condition |
|
118 | - * |
|
119 | - * @param QueryExpression $expression the query expression |
|
120 | - * @return $this |
|
121 | - * @see https://en.wikipedia.org/wiki/SQL#Operators |
|
122 | - * @see https://en.wikipedia.org/wiki/Where_(SQL) |
|
123 | - */ |
|
124 | - public function where(QueryExpression $expression) |
|
125 | - { |
|
126 | - $this->whereExpression = $expression; |
|
127 | - return $this; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Set an additional group by. |
|
132 | - * |
|
133 | - * @param string $column |
|
134 | - * @return $this |
|
135 | - * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
136 | - */ |
|
137 | - public function groupBy($column) |
|
138 | - { |
|
139 | - $this->query->groupBy($column); |
|
140 | - return $this; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Set an additional order condition. |
|
145 | - * |
|
146 | - * @param string $column |
|
147 | - * @param string|null $order |
|
148 | - * @return $this |
|
149 | - * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
150 | - * @see https://en.wikipedia.org/wiki/Order_by |
|
151 | - */ |
|
152 | - public function orderBy($column, $order = null) |
|
153 | - { |
|
154 | - $this->query->orderBy($column, $order); |
|
155 | - return $this; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Set the limit. |
|
160 | - * |
|
161 | - * @param mixed $limit |
|
162 | - * @return $this |
|
163 | - */ |
|
164 | - public function limit($limit) |
|
165 | - { |
|
166 | - $this->query->limit($limit); |
|
167 | - return $this; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Set the offset. |
|
172 | - * |
|
173 | - * @param mixed $offset |
|
174 | - * @return $this |
|
175 | - */ |
|
176 | - public function offset($offset) |
|
177 | - { |
|
178 | - $this->query->offset($offset); |
|
179 | - return $this; |
|
180 | - } |
|
23 | + private $table; |
|
24 | + |
|
25 | + private $hooks; |
|
26 | + |
|
27 | + private $results; |
|
28 | + |
|
29 | + private $query; |
|
30 | + |
|
31 | + private $whereExpression = null; |
|
32 | + |
|
33 | + private $clauses = []; |
|
34 | + |
|
35 | + public function __construct(AbstractActiveRecord $instance, $table, Array $additionalWhereClauses) |
|
36 | + { |
|
37 | + $this->table = $table; |
|
38 | + $this->query = new Query($instance->getPdo(), $table); |
|
39 | + $this->type = $instance; |
|
40 | + $this->clauses = $additionalWhereClauses; |
|
41 | + } |
|
42 | + |
|
43 | + private function execute() |
|
44 | + { |
|
45 | + $clauses = $this->clauses; |
|
46 | + |
|
47 | + // Optionally add user concatenated where expression |
|
48 | + if ($this->whereExpression !== null) |
|
49 | + { |
|
50 | + $clauses[] = $this->whereExpression; |
|
51 | + } |
|
52 | + |
|
53 | + // Construct where clause |
|
54 | + if (count($clauses) == 1) |
|
55 | + { |
|
56 | + $this->query->where($clauses[0]); |
|
57 | + } else if (count($clauses) >= 2) |
|
58 | + { |
|
59 | + $rest = array_slice($clauses, 1); |
|
60 | + $this->query->where(Query::And($clauses[0], ...$rest)); |
|
61 | + } |
|
62 | + |
|
63 | + $this->query->select(); |
|
64 | + |
|
65 | + $this->results = $this->query->execute(); |
|
66 | + |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + public function fetchAll() |
|
71 | + { |
|
72 | + try { |
|
73 | + // TODO: Should execute call be explicit? |
|
74 | + $this->execute(); |
|
75 | + |
|
76 | + $typedResults = []; |
|
77 | + |
|
78 | + $entries = $this->results->fetchAll(); |
|
79 | + if ($entries === false) { |
|
80 | + throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
|
81 | + } |
|
82 | + |
|
83 | + foreach ($entries as $entry) { |
|
84 | + $typedEntry = clone $this->type; |
|
85 | + $typedEntry->fill($entry); |
|
86 | + $typedResults[] = $typedEntry; |
|
87 | + } |
|
88 | + |
|
89 | + return $typedResults; |
|
90 | + } catch (\PDOException $e) { |
|
91 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + public function fetch() |
|
96 | + { |
|
97 | + try { |
|
98 | + $this->execute(); |
|
99 | + |
|
100 | + $typedResult = clone $this->type; |
|
101 | + |
|
102 | + $entry = $this->results->fetch(); |
|
103 | + if ($entry === false) { |
|
104 | + throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
|
105 | + } |
|
106 | + |
|
107 | + $typedResult->fill($entry); |
|
108 | + |
|
109 | + return $typedResult; |
|
110 | + } catch (\PDOException $e) { |
|
111 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
112 | + } |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * Set the where condition |
|
118 | + * |
|
119 | + * @param QueryExpression $expression the query expression |
|
120 | + * @return $this |
|
121 | + * @see https://en.wikipedia.org/wiki/SQL#Operators |
|
122 | + * @see https://en.wikipedia.org/wiki/Where_(SQL) |
|
123 | + */ |
|
124 | + public function where(QueryExpression $expression) |
|
125 | + { |
|
126 | + $this->whereExpression = $expression; |
|
127 | + return $this; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Set an additional group by. |
|
132 | + * |
|
133 | + * @param string $column |
|
134 | + * @return $this |
|
135 | + * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
136 | + */ |
|
137 | + public function groupBy($column) |
|
138 | + { |
|
139 | + $this->query->groupBy($column); |
|
140 | + return $this; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Set an additional order condition. |
|
145 | + * |
|
146 | + * @param string $column |
|
147 | + * @param string|null $order |
|
148 | + * @return $this |
|
149 | + * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
150 | + * @see https://en.wikipedia.org/wiki/Order_by |
|
151 | + */ |
|
152 | + public function orderBy($column, $order = null) |
|
153 | + { |
|
154 | + $this->query->orderBy($column, $order); |
|
155 | + return $this; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Set the limit. |
|
160 | + * |
|
161 | + * @param mixed $limit |
|
162 | + * @return $this |
|
163 | + */ |
|
164 | + public function limit($limit) |
|
165 | + { |
|
166 | + $this->query->limit($limit); |
|
167 | + return $this; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Set the offset. |
|
172 | + * |
|
173 | + * @param mixed $offset |
|
174 | + * @return $this |
|
175 | + */ |
|
176 | + public function offset($offset) |
|
177 | + { |
|
178 | + $this->query->offset($offset); |
|
179 | + return $this; |
|
180 | + } |
|
181 | 181 | } |
@@ -57,7 +57,7 @@ |
||
57 | 57 | } else if (count($clauses) >= 2) |
58 | 58 | { |
59 | 59 | $rest = array_slice($clauses, 1); |
60 | - $this->query->where(Query::And($clauses[0], ...$rest)); |
|
60 | + $this->query->where(Query:: And ($clauses[0], ...$rest)); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $this->query->select(); |
@@ -18,70 +18,70 @@ |
||
18 | 18 | */ |
19 | 19 | interface ActiveRecordInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * Returns this active record after creating an entry with the records attributes. |
|
23 | - * |
|
24 | - * @return $this |
|
25 | - * @throws ActiveRecordException on failure. |
|
26 | - */ |
|
27 | - public function create(); |
|
21 | + /** |
|
22 | + * Returns this active record after creating an entry with the records attributes. |
|
23 | + * |
|
24 | + * @return $this |
|
25 | + * @throws ActiveRecordException on failure. |
|
26 | + */ |
|
27 | + public function create(); |
|
28 | 28 | |
29 | - /** |
|
30 | - * Returns this active record after reading the attributes from the entry with the given identifier. |
|
31 | - * |
|
32 | - * @param mixed $id |
|
33 | - * @return $this |
|
34 | - * @throws ActiveRecordException on failure. |
|
35 | - */ |
|
36 | - public function read($id); |
|
29 | + /** |
|
30 | + * Returns this active record after reading the attributes from the entry with the given identifier. |
|
31 | + * |
|
32 | + * @param mixed $id |
|
33 | + * @return $this |
|
34 | + * @throws ActiveRecordException on failure. |
|
35 | + */ |
|
36 | + public function read($id); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Returns this active record after updating the attributes to the corresponding entry. |
|
40 | - * |
|
41 | - * @return $this |
|
42 | - * @throws ActiveRecordException on failure. |
|
43 | - */ |
|
44 | - public function update(); |
|
38 | + /** |
|
39 | + * Returns this active record after updating the attributes to the corresponding entry. |
|
40 | + * |
|
41 | + * @return $this |
|
42 | + * @throws ActiveRecordException on failure. |
|
43 | + */ |
|
44 | + public function update(); |
|
45 | 45 | |
46 | - /** |
|
47 | - * Returns this record after deleting the corresponding entry. |
|
48 | - * |
|
49 | - * @return $this |
|
50 | - * @throws ActiveRecordException on failure. |
|
51 | - */ |
|
52 | - public function delete(); |
|
46 | + /** |
|
47 | + * Returns this record after deleting the corresponding entry. |
|
48 | + * |
|
49 | + * @return $this |
|
50 | + * @throws ActiveRecordException on failure. |
|
51 | + */ |
|
52 | + public function delete(); |
|
53 | 53 | |
54 | - /** |
|
55 | - * Returns this record after synchronizing it with the corresponding entry. |
|
56 | - * A new entry is created if this active record does not have a corresponding entry. |
|
57 | - * |
|
58 | - * @return $this |
|
59 | - * @throws ActiveRecordException on failure. |
|
60 | - */ |
|
61 | - public function sync(); |
|
54 | + /** |
|
55 | + * Returns this record after synchronizing it with the corresponding entry. |
|
56 | + * A new entry is created if this active record does not have a corresponding entry. |
|
57 | + * |
|
58 | + * @return $this |
|
59 | + * @throws ActiveRecordException on failure. |
|
60 | + */ |
|
61 | + public function sync(); |
|
62 | 62 | |
63 | - /** |
|
64 | - * Returns true if this active record has a corresponding entry. |
|
65 | - * |
|
66 | - * @return bool true if this active record has a corresponding entry. |
|
67 | - */ |
|
68 | - public function exists(); |
|
63 | + /** |
|
64 | + * Returns true if this active record has a corresponding entry. |
|
65 | + * |
|
66 | + * @return bool true if this active record has a corresponding entry. |
|
67 | + */ |
|
68 | + public function exists(); |
|
69 | 69 | |
70 | - /** |
|
71 | - * Returns this record after filling it with the given attributes. |
|
72 | - * |
|
73 | - * @param array $attributes = [] |
|
74 | - * @return $this |
|
75 | - * @throws ActiveRecordException on failure. |
|
76 | - */ |
|
77 | - public function fill(array $attributes); |
|
70 | + /** |
|
71 | + * Returns this record after filling it with the given attributes. |
|
72 | + * |
|
73 | + * @param array $attributes = [] |
|
74 | + * @return $this |
|
75 | + * @throws ActiveRecordException on failure. |
|
76 | + */ |
|
77 | + public function fill(array $attributes); |
|
78 | 78 | |
79 | - /** |
|
80 | - * Returns the records with the given where, order by, limit and offset clauses. |
|
81 | - * |
|
82 | - * @param array $excludedTraits |
|
83 | - * @return ActiveRecordQuery the query representing the current search. |
|
84 | - * @throws ActiveRecordException on failure. |
|
85 | - */ |
|
86 | - public function search(Array $excludedTraits); |
|
79 | + /** |
|
80 | + * Returns the records with the given where, order by, limit and offset clauses. |
|
81 | + * |
|
82 | + * @param array $excludedTraits |
|
83 | + * @return ActiveRecordQuery the query representing the current search. |
|
84 | + * @throws ActiveRecordException on failure. |
|
85 | + */ |
|
86 | + public function search(Array $excludedTraits); |
|
87 | 87 | } |