Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit d9549f5

Browse files
committed
up
1 parent 7db372c commit d9549f5

File tree

1 file changed

+85
-10
lines changed

1 file changed

+85
-10
lines changed

src/Components/DatabaseClient.php

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,61 @@ public function __call($name, array $arguments)
188188
}
189189

190190
/**************************************************************************
191-
* basic method
191+
* extra methods
192192
*************************************************************************/
193193

194194
/**
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
198230
* @return array
199231
*/
200-
public function find($from, $wheres, array $options = [])
232+
public function find(string $from, $wheres = 1, $select = '*', array $options = [])
201233
{
202-
# code...
234+
203235
}
204236

205237
/**
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
209243
* @return array
210244
*/
211-
public function findAll($wheres, array $options = [])
245+
public function findAll(string $from, $wheres = 1, $select = '*', array $options = [])
212246
{
213247
# code...
214248
}
@@ -260,6 +294,42 @@ public function delete($statement, array $bindings = [])
260294
return $this->fetchAffected($statement, $bindings);
261295
}
262296

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+
263333
/********************************************************************************
264334
* fetch data methods
265335
*******************************************************************************/
@@ -749,6 +819,11 @@ public function handleWheres($wheres)
749819
return $query;
750820
}
751821

822+
public function handleFindOptions(array $options)
823+
{
824+
# code...
825+
}
826+
752827
/**
753828
* {@inheritdoc}
754829
*/

0 commit comments

Comments
 (0)