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

Commit dc15021

Browse files
committed
update
1 parent 405669f commit dc15021

File tree

1 file changed

+91
-45
lines changed

1 file changed

+91
-45
lines changed

src/Components/DatabaseClient.php

Lines changed: 91 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ public function __call($name, array $arguments)
264264

265265
/*
266266
data load type, in :
267-
'a className' -- return object, instanceof the class`
268-
'array' -- return array, only [ 'value' ]
267+
'object' -- return object, instanceof the 'class'
268+
'column' -- return array, only [ 'value' ]
269269
'assoc' -- return array, Contain [ 'column' => 'value']
270270
*/
271271
'fetchType' => 'assoc',
272+
'class' => null, // a className. when 'fetchType' eq 'object'
272273
];
273274

274275
/**
@@ -281,7 +282,7 @@ public function __call($name, array $arguments)
281282
*/
282283
public function find(string $from, $wheres = 1, $select = '*', array $options = [])
283284
{
284-
$options['select'] = $this->qns($select);
285+
$options['select'] = $this->qns($select ?: '*');
285286
$options['from'] = $this->qn($from);
286287

287288
list($where, $bindings) = $this->handleWheres($wheres);
@@ -295,7 +296,17 @@ public function find(string $from, $wheres = 1, $select = '*', array $options =
295296
return [$statement, $bindings];
296297
}
297298

298-
return $this->fetchOne($statement, $bindings);
299+
if ($class = $options['class'] ?? null) {
300+
return $this->fetchObject($statement, $bindings, $class);
301+
}
302+
303+
$method = 'fetchOne';
304+
305+
if (isset($options['fetchType']) && $options['fetchType'] === 'column') {
306+
$method = 'fetchColumn';
307+
}
308+
309+
return $this->$method($statement, $bindings);
299310
}
300311

301312
/**
@@ -308,7 +319,7 @@ public function find(string $from, $wheres = 1, $select = '*', array $options =
308319
*/
309320
public function findAll(string $from, $wheres = 1, $select = '*', array $options = [])
310321
{
311-
$options['select'] = $this->qns($select);
322+
$options['select'] = $this->qns($select ?: '*');
312323
$options['from'] = $this->qn($from);
313324

314325
list($where, $bindings) = $this->handleWheres($wheres);
@@ -325,18 +336,32 @@ public function findAll(string $from, $wheres = 1, $select = '*', array $options
325336
return [$statement, $bindings];
326337
}
327338

328-
return $this->fetchAll($statement, $bindings);
339+
if ($class = $options['class'] ?? null) {
340+
return $this->fetchObjects($statement, $bindings, $class);
341+
}
342+
343+
$method = 'fetchAll';
344+
345+
if (isset($options['fetchType']) && $options['fetchType'] === 'column') {
346+
$method = 'fetchColumns';
347+
}
348+
349+
return $this->$method($statement, $bindings);
329350
}
330351

331352
/**
332-
* Run a statement for insert a row
353+
* Run a statement for insert a row
333354
* @param string $statement
334355
* @param array $data <column => value>
335356
* @param array $options
336357
* @return int
337358
*/
338359
public function insert(string $from, array $data, array $options = [])
339360
{
361+
if (!$data) {
362+
throw new \RuntimeException('The data inserted into the database cannot be empty');
363+
}
364+
340365
list($statement, $bindings) = $this->compileInsert($from, $data);
341366

342367
if (isset($options['dumpSql'])) {
@@ -350,7 +375,7 @@ public function insert(string $from, array $data, array $options = [])
350375
}
351376

352377
/**
353-
* Run a statement for insert multi row
378+
* Run a statement for insert multi row
354379
* @param string $statement
355380
* @param array $data <column => value>
356381
* @param array $options
@@ -399,6 +424,10 @@ public function update(string $from, $wheres, array $values, array $options = []
399424
*/
400425
public function delete(string $from, $wheres, array $options = [])
401426
{
427+
if (!$wheres) {
428+
throw new \RuntimeException('Safety considerations, where conditions can not be empty');
429+
}
430+
402431
list($where, $bindings) = $this->handleWheres($wheres);
403432

404433
$options['from'] = $this->qn($from);
@@ -470,14 +499,22 @@ public function fetchAffected($statement, array $bindings = [])
470499
return $affected;
471500
}
472501

502+
/********************************************************************************
503+
* fetch a row methods
504+
*******************************************************************************/
505+
473506
/**
474507
* {@inheritdoc}
475508
*/
476-
public function fetchAll($statement, array $bindings = [])
509+
public function fetchAssoc($statement, array $bindings = [])
510+
{
511+
return $this->fetchOne($statement, $bindings);
512+
}
513+
public function fetchOne($statement, array $bindings = [])
477514
{
478515
$sth = $this->execute($statement, $bindings);
479516

480-
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
517+
$result = $sth->fetch(PDO::FETCH_ASSOC);
481518

482519
$this->freeResource($sth);
483520

@@ -487,105 +524,113 @@ public function fetchAll($statement, array $bindings = [])
487524
/**
488525
* {@inheritdoc}
489526
*/
490-
public function fetchAssoc($statement, array $bindings = [])
527+
public function fetchColumn($statement, array $bindings = [])
528+
{
529+
return $this->fetchValue($statement, $bindings);
530+
}
531+
public function fetchValue($statement, array $bindings = [])
491532
{
492-
$data = [];
493533
$sth = $this->execute($statement, $bindings);
494534

495-
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
496-
$data[current($row)] = $row;
497-
}
535+
$result = $sth->fetchColumn();
498536

499537
$this->freeResource($sth);
500538

501-
return $data;
539+
return $result;
502540
}
503541

504542
/**
505543
* {@inheritdoc}
506544
*/
507-
public function fetchColumn($statement, array $bindings = [])
545+
public function fetchObject($statement, array $bindings = [], $class = 'stdClass', array $args = [])
508546
{
509547
$sth = $this->execute($statement, $bindings);
510548

511-
$column = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
549+
if (!empty($args)) {
550+
$result = $sth->fetchObject($class, $args);
551+
} else {
552+
$result = $sth->fetchObject($class);
553+
}
512554

513555
$this->freeResource($sth);
514556

515-
return $column;
557+
return $result;
516558
}
517559

560+
/********************************************************************************
561+
* fetch multi rows methods
562+
*******************************************************************************/
563+
518564
/**
519565
* {@inheritdoc}
520566
*/
521-
public function fetchGroup($statement, array $bindings = [], $style = PDO::FETCH_COLUMN)
567+
public function fetchAll($statement, array $bindings = [])
522568
{
523569
$sth = $this->execute($statement, $bindings);
524570

525-
$group = $sth->fetchAll(PDO::FETCH_GROUP | $style);
571+
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
572+
526573
$this->freeResource($sth);
527574

528-
return $group;
575+
return $result;
529576
}
530577

531578
/**
532579
* {@inheritdoc}
533580
*/
534-
public function fetchObject($statement, array $bindings = [], $class = 'stdClass', array $args = [])
581+
public function fetchAssocs($statement, array $bindings = [])
535582
{
583+
$data = [];
536584
$sth = $this->execute($statement, $bindings);
537585

538-
if (!empty($args)) {
539-
$result = $sth->fetchObject($class, $args);
540-
} else {
541-
$result = $sth->fetchObject($class);
586+
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
587+
$data[current($row)] = $row;
542588
}
543589

544590
$this->freeResource($sth);
545591

546-
return $result;
592+
return $data;
547593
}
548594

549595
/**
550596
* {@inheritdoc}
551597
*/
552-
public function fetchObjects($statement, array $bindings = [], $class = 'stdClass', array $args = [])
598+
public function fetchColumns($statement, array $bindings = [])
553599
{
554600
$sth = $this->execute($statement, $bindings);
555601

556-
if (!empty($args)) {
557-
$result = $sth->fetchAll(PDO::FETCH_CLASS, $class, $args);
558-
} else {
559-
$result = $sth->fetchAll(PDO::FETCH_CLASS, $class);
560-
}
602+
$column = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
561603

562604
$this->freeResource($sth);
563605

564-
return $result;
606+
return $column;
565607
}
566608

567609
/**
568610
* {@inheritdoc}
569611
*/
570-
public function fetchOne($statement, array $bindings = [])
612+
public function fetchGroups($statement, array $bindings = [], $style = PDO::FETCH_COLUMN)
571613
{
572614
$sth = $this->execute($statement, $bindings);
573615

574-
$result = $sth->fetch(PDO::FETCH_ASSOC);
575-
616+
$group = $sth->fetchAll(PDO::FETCH_GROUP | $style);
576617
$this->freeResource($sth);
577618

578-
return $result;
619+
return $group;
579620
}
580621

581622
/**
582623
* {@inheritdoc}
583624
*/
584-
public function fetchPairs($statement, array $bindings = [])
625+
public function fetchObjects($statement, array $bindings = [], $class = 'stdClass', array $args = [])
585626
{
586627
$sth = $this->execute($statement, $bindings);
587628

588-
$result = $sth->fetchAll(PDO::FETCH_KEY_PAIR);
629+
if (!empty($args)) {
630+
$result = $sth->fetchAll(PDO::FETCH_CLASS, $class, $args);
631+
} else {
632+
$result = $sth->fetchAll(PDO::FETCH_CLASS, $class);
633+
}
589634

590635
$this->freeResource($sth);
591636

@@ -595,17 +640,18 @@ public function fetchPairs($statement, array $bindings = [])
595640
/**
596641
* {@inheritdoc}
597642
*/
598-
public function fetchValue($statement, array $bindings = [])
643+
public function fetchPairs($statement, array $bindings = [])
599644
{
600645
$sth = $this->execute($statement, $bindings);
601646

602-
$result = $sth->fetchColumn();
647+
$result = $sth->fetchAll(PDO::FETCH_KEY_PAIR);
603648

604649
$this->freeResource($sth);
605650

606651
return $result;
607652
}
608653

654+
609655
/********************************************************************************
610656
* Generator methods
611657
*******************************************************************************/
@@ -858,7 +904,7 @@ public function ping()
858904
* $result = $db->findAll('user', [
859905
* 'userId' => 23, // ==> 'AND `userId` = 23'
860906
* 'title' => 'test', // value will auto add quote, equal to "AND title = 'test'"
861-
*
907+
*
862908
* ['publishTime', '>', '0'], // ==> 'AND `publishTime` > 0'
863909
* ['createdAt', '<=', 1345665427, 'OR'], // ==> 'OR `createdAt` <= 1345665427'
864910
* ['id', 'IN' ,[4,5,56]], // ==> '`id` IN ('4','5','56')'
@@ -999,7 +1045,7 @@ public function compileDelete(array $options)
9991045
{
10001046
return 'DELETE ' . $this->compileNodes(self::DELETE_NODES, $options);;
10011047
}
1002-
1048+
10031049
/**
10041050
* @param array $commandNodes
10051051
* @param array $data

0 commit comments

Comments
 (0)