@@ -188,27 +188,61 @@ public function __call($name, array $arguments)
188
188
}
189
189
190
190
/**************************************************************************
191
- * basic method
191
+ * extra methods
192
192
*************************************************************************/
193
193
194
194
/**
195
- * Run a select statement
196
- * @param string $statement
197
- * @param array $bindings
195
+ * @var array
196
+ */
197
+ protected static $ queryNodes = [
198
+ 'select ' => '* ' , // string: 'id, name' array: ['id', 'name']
199
+ 'from ' => '' ,
200
+ 'join ' => '' , // [$table, $condition, $type]
201
+
202
+ 'having ' => '' , // [$conditions, $glue = 'AND']
203
+ 'group ' => '' , // 'id, type'
204
+ 'order ' => '' , // 'created ASC' OR ['created ASC', 'publish DESC']
205
+ 'limit ' => 1 , // 10 OR [2, 10]
206
+ ];
207
+
208
+ /**
209
+ * @var array
210
+ */
211
+ protected static $ queryOptions = [
212
+ /* data index column. */
213
+ 'indexKey ' => null ,
214
+
215
+ /*
216
+ data load type, in :
217
+ 'a className' -- return object, instanceof the class`
218
+ 'array' -- return array, only [ 'value' ]
219
+ 'assoc' -- return array, Contain [ 'column' => 'value']
220
+ */
221
+ 'loadType ' => 'assoc ' ,
222
+ ];
223
+
224
+ /**
225
+ * Run a select statement, fetch one
226
+ * @param string $from
227
+ * @param array|string $wheres
228
+ * @param string|array $select
229
+ * @param array $options
198
230
* @return array
199
231
*/
200
- public function find ($ from , $ wheres , array $ options = [])
232
+ public function find (string $ from , $ wheres = 1 , $ select = ' * ' , array $ options = [])
201
233
{
202
- # code...
234
+
203
235
}
204
236
205
237
/**
206
- * Run a select statement
207
- * @param string $statement
208
- * @param array $bindings
238
+ * Run a select statement, fetch all
239
+ * @param string $from
240
+ * @param array|string $wheres
241
+ * @param string|array $select
242
+ * @param array $options
209
243
* @return array
210
244
*/
211
- public function findAll ($ wheres , array $ options = [])
245
+ public function findAll (string $ from , $ wheres = 1 , $ select = ' * ' , array $ options = [])
212
246
{
213
247
# code...
214
248
}
@@ -260,6 +294,42 @@ public function delete($statement, array $bindings = [])
260
294
return $ this ->fetchAffected ($ statement , $ bindings );
261
295
}
262
296
297
+ /**
298
+ * count
299
+ * ```
300
+ * $db->count();
301
+ * ```
302
+ * @param string $table
303
+ * @param array|string $wheres
304
+ * @return int
305
+ */
306
+ public function count (string $ table , $ wheres )
307
+ {
308
+ list ($ where , $ bindings ) = $ this ->handleWheres ($ wheres );
309
+ $ sql = "SELECT COUNT(*) AS total FROM {$ table } WHERE {$ where }" ;
310
+
311
+ $ result = $ this ->fetchObject ($ sql , $ bindings );
312
+
313
+ return $ result ? (int )$ result ->total : 0 ;
314
+ }
315
+
316
+ /**
317
+ * exists
318
+ * ```
319
+ * $db->exists();
320
+ * // SQL: select exists(select * from `table` where (`phone` = 152xxx)) as `exists`;
321
+ * ```
322
+ * @return int
323
+ */
324
+ public function exists ($ statement , array $ bindings = [])
325
+ {
326
+ $ sql = sprintf ('SELECT EXISTS(%s) AS `exists` ' , $ sql );
327
+
328
+ $ result = $ this ->fetchObject ($ sql , $ bindings );
329
+
330
+ return $ result ? $ result ->exists : 0 ;
331
+ }
332
+
263
333
/********************************************************************************
264
334
* fetch data methods
265
335
*******************************************************************************/
@@ -749,6 +819,11 @@ public function handleWheres($wheres)
749
819
return $ query ;
750
820
}
751
821
822
+ public function handleFindOptions (array $ options )
823
+ {
824
+ # code...
825
+ }
826
+
752
827
/**
753
828
* {@inheritdoc}
754
829
*/
0 commit comments