1 | <?php |
||
42 | class Query extends Component implements QueryInterface |
||
43 | { |
||
44 | use QueryTrait; |
||
45 | |||
46 | const SEARCH_SCOPE_SUB = 'ldap_search'; |
||
47 | const SEARCH_SCOPE_ONE = 'ldap_list'; |
||
48 | const SEARCH_SCOPE_BASE = 'ldap_read'; |
||
49 | |||
50 | /** |
||
51 | * @var string the scope of search |
||
52 | * The search scope: |
||
53 | * Query::SEARCH_SCOPE_SUB searches the complete subtree including the $baseDn node. This is the default value. |
||
54 | * Query::SEARCH_SCOPE_ONE restricts search to one level below $baseDn. |
||
55 | * Query::SEARCH_SCOPE_BASE restricts search to the $baseDn itself; this can be used to efficiently retrieve a single entry by its DN. |
||
56 | */ |
||
57 | public $scope = self::SEARCH_SCOPE_SUB; |
||
58 | |||
59 | /** |
||
60 | * @var array the columns being selected. For example, `['id', 'name']`. |
||
61 | * This is used to construct the SEARCH function in a LDAP statement. If not set, it means selecting all columns. |
||
62 | * @see select() |
||
63 | */ |
||
64 | public $select; |
||
65 | |||
66 | /** |
||
67 | * @var string The search filter. Format is described in the LDAP documentation. |
||
68 | * @see http://www.faqs.org/rfcs/rfc4515.html |
||
69 | */ |
||
70 | public $filter; |
||
71 | |||
72 | /** |
||
73 | * Creates a LDAP command that can be used to execute this query. |
||
74 | * @param Connection $db the database connection. |
||
75 | * If this parameter is not given, the `db` application component will be used. |
||
76 | * @return DataReader |
||
77 | */ |
||
78 | public function execute($db = null) |
||
102 | |||
103 | /** |
||
104 | * Executes the query and returns all results as an array. |
||
105 | * @param Connection $db the database connection. |
||
106 | * If this parameter is not given, the `db` application component will be used. |
||
107 | * @return array the query results. If the query results in nothing, an empty array will be returned. |
||
108 | */ |
||
109 | public function all($db = null) |
||
110 | { |
||
111 | /** @var $result DataReader */ |
||
112 | $result = $this->execute($db); |
||
113 | return $this->populate($result->toArray()); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Converts the raw query results into the format as specified by this query. |
||
118 | * This method is internally used to convert the data fetched from database |
||
119 | * into the format as required by this query. |
||
120 | * @param array $rows the raw query result from database |
||
121 | * @return array the converted query result |
||
122 | */ |
||
123 | public function populate($rows) |
||
139 | |||
140 | /** |
||
141 | * Executes the query and returns a single row of result. |
||
142 | * @param Connection $db the database connection. |
||
143 | * If this parameter is not given, the `db` application component will be used. |
||
144 | * @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query |
||
145 | * results in nothing. |
||
146 | */ |
||
147 | public function one($db = null) |
||
153 | |||
154 | /** |
||
155 | * Returns the number of entries in a search. |
||
156 | * @param string $q do not use |
||
157 | * @param Connection $db the database connection |
||
158 | * If this parameter is not given (or null), the `db` application component will be used. |
||
159 | * @return integer number of entries. |
||
160 | */ |
||
161 | public function count($q = null, $db = NULL) |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Returns a value indicating whether the query result contains any row of data. |
||
170 | * @param Connection $db the database connection. |
||
171 | * If this parameter is not given, the `db` application component will be used. |
||
172 | * @return boolean whether the query result contains any row of entries. |
||
173 | */ |
||
174 | public function exists($db = null) |
||
179 | |||
180 | /** |
||
181 | * Sets the SELECT part of the query. |
||
182 | * @param string|array $columns the columns to be selected. |
||
183 | * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
||
184 | * |
||
185 | * ```php |
||
186 | * $query->addSelect(['cn, mail'])->one(); |
||
187 | * ``` |
||
188 | * |
||
189 | * @return $this the query object itself |
||
190 | */ |
||
191 | public function select($columns) |
||
199 | |||
200 | /** |
||
201 | * Add more columns to the select part of the query. |
||
202 | * |
||
203 | * ```php |
||
204 | * $query->addSelect(['cn, mail'])->one(); |
||
205 | * ``` |
||
206 | * |
||
207 | * @param string|array|Expression $columns the columns to add to the select. See [[select()]] for more |
||
208 | * details about the format of this parameter. |
||
209 | * @return $this the query object itself |
||
210 | * @see select() |
||
211 | */ |
||
212 | public function addSelect($columns) |
||
224 | |||
225 | /** |
||
226 | * Adds a filtering condition for a specific column and allow the user to choose a filter operator. |
||
227 | * |
||
228 | * It adds an additional WHERE condition for the given field and determines the comparison operator |
||
229 | * based on the first few characters of the given value. |
||
230 | * The condition is added in the same way as in [[andFilterWhere]] so [[isEmpty()|empty values]] are ignored. |
||
231 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
232 | * |
||
233 | * The comparison operator is intelligently determined based on the first few characters in the given value. |
||
234 | * In particular, it recognizes the following operators if they appear as the leading characters in the given value: |
||
235 | * |
||
236 | * - `<`: the column must be less than the given value. |
||
237 | * - `>`: the column must be greater than the given value. |
||
238 | * - `<=`: the column must be less than or equal to the given value. |
||
239 | * - `>=`: the column must be greater than or equal to the given value. |
||
240 | * - `~=`: the column must approximate the given value. |
||
241 | * - `=`: the column must be equal to the given value. |
||
242 | * - If none of the above operators is detected, the `$defaultOperator` will be used. |
||
243 | * |
||
244 | * @param string $name the column name. |
||
245 | * @param string $value the column value optionally prepended with the comparison operator. |
||
246 | * @param string $defaultOperator The operator to use, when no operator is given in `$value`. |
||
247 | * Defaults to `=`, performing an exact match. |
||
248 | * @return $this The query object itself |
||
249 | */ |
||
250 | public function andFilterCompare($name, $value, $defaultOperator = '=') |
||
260 | |||
261 | /** |
||
262 | * Creates a new Query object and copies its property values from an existing one. |
||
263 | * The properties being copies are the ones to be used by query builders. |
||
264 | * @param Query $from the source query object |
||
265 | * @return Query the new Query object |
||
266 | */ |
||
267 | public static function create(Query $from) |
||
278 | } |
||
279 |