@@ -280,7 +280,7 @@ public function __call($name, array $arguments)
280280 * @param array $options
281281 * @return array
282282 */
283- public function find (string $ from , $ wheres = 1 , $ select = '* ' , array $ options = [])
283+ public function findOne (string $ from , $ wheres = 1 , $ select = '* ' , array $ options = [])
284284 {
285285 $ options ['select ' ] = $ this ->qns ($ select ?: '* ' );
286286 $ options ['from ' ] = $ this ->qn ($ from );
@@ -300,10 +300,14 @@ public function find(string $from, $wheres = 1, $select = '*', array $options =
300300 return $ this ->fetchObject ($ statement , $ bindings , $ class );
301301 }
302302
303- $ method = 'fetchOne ' ;
303+ $ method = 'fetchAssoc ' ;
304304
305- if (isset ($ options ['fetchType ' ]) && $ options ['fetchType ' ] === 'column ' ) {
306- $ method = 'fetchColumn ' ;
305+ if (isset ($ options ['fetchType ' ])) {
306+ if ($ options ['fetchType ' ] === 'column ' ) {
307+ $ method = 'fetchColumn ' ;
308+ } elseif ($ options ['fetchType ' ] === 'value ' ) {
309+ $ method = 'fetchValue ' ;
310+ }
307311 }
308312
309313 return $ this ->$ method ($ statement , $ bindings );
@@ -336,17 +340,24 @@ public function findAll(string $from, $wheres = 1, $select = '*', array $options
336340 return [$ statement , $ bindings ];
337341 }
338342
343+ $ indexKey = $ options ['indexKey ' ] ?? null ;
344+
339345 if ($ class = $ options ['class ' ] ?? null ) {
340- return $ this ->fetchObjects ($ statement , $ bindings , $ class );
346+ return $ this ->fetchObjects ($ statement , $ bindings , $ class, $ indexKey );
341347 }
342348
343- $ method = 'fetchAll ' ;
349+ $ method = 'fetchAssocs ' ;
344350
345- if (isset ($ options ['fetchType ' ]) && $ options ['fetchType ' ] === 'column ' ) {
346- $ method = 'fetchColumns ' ;
351+ if (isset ($ options ['fetchType ' ])) {
352+ if ($ options ['fetchType ' ] === 'column ' ) {
353+ // for get columns, indexKey is column number.
354+ $ method = 'fetchColumns ' ;
355+ } elseif ($ options ['fetchType ' ] === 'value ' ) {
356+ $ method = 'fetchValues ' ;
357+ }
347358 }
348359
349- return $ this ->$ method ($ statement , $ bindings );
360+ return $ this ->$ method ($ statement , $ bindings, $ indexKey );
350361 }
351362
352363 /**
@@ -506,11 +517,11 @@ public function fetchAffected($statement, array $bindings = [])
506517 /**
507518 * {@inheritdoc}
508519 */
509- public function fetchAssoc ($ statement , array $ bindings = [])
520+ public function fetchAssoc (string $ statement , array $ bindings = [])
510521 {
511522 return $ this ->fetchOne ($ statement , $ bindings );
512523 }
513- public function fetchOne ($ statement , array $ bindings = [])
524+ public function fetchOne (string $ statement , array $ bindings = [])
514525 {
515526 $ sth = $ this ->execute ($ statement , $ bindings );
516527 $ result = $ sth ->fetch (PDO ::FETCH_ASSOC );
@@ -521,16 +532,30 @@ public function fetchOne($statement, array $bindings = [])
521532 }
522533
523534 /**
524- * {@inheritdoc}
535+ * 从结果集中的下一行返回单独的一列
536+ *
537+ * @param string $statement
538+ * @param array $bindings
539+ * @param int $columnNum 你想从行里取回的列的索引数字(以0开始的索引)
540+ * @return void
525541 */
526- public function fetchColumn ($ statement , array $ bindings = [])
542+ public function fetchColumn (string $ statement , array $ bindings = [], int $ columnNum = 0 )
527543 {
528- return $ this ->fetchValue ($ statement , $ bindings );
544+ $ sth = $ this ->execute ($ statement , $ bindings );
545+ $ result = $ sth ->fetchColumn ($ columnNum );
546+
547+ $ this ->freeResource ($ sth );
548+
549+ return $ result ;
529550 }
530- public function fetchValue ($ statement , array $ bindings = [])
551+
552+ /**
553+ * @return array|bool
554+ */
555+ public function fetchValue (string $ statement , array $ bindings = [])
531556 {
532557 $ sth = $ this ->execute ($ statement , $ bindings );
533- $ result = $ sth ->fetchColumn ( );
558+ $ result = $ sth ->fetch ( PDO :: FETCH_NUM );
534559
535560 $ this ->freeResource ($ sth );
536561
@@ -540,7 +565,7 @@ public function fetchValue($statement, array $bindings = [])
540565 /**
541566 * {@inheritdoc}
542567 */
543- public function fetchObject ($ statement , array $ bindings = [], $ class = 'stdClass ' , array $ args = [])
568+ public function fetchObject (string $ statement , array $ bindings = [], $ class = 'stdClass ' , array $ args = [])
544569 {
545570 $ sth = $ this ->execute ($ statement , $ bindings );
546571
@@ -560,28 +585,43 @@ public function fetchObject($statement, array $bindings = [], $class = 'stdClass
560585 *******************************************************************************/
561586
562587 /**
563- * {@inheritdoc}
588+ * @param string $statement
589+ * @param array $bindings
590+ * @param string|int $indexKey
591+ * @param string $class a class name or fetch style name.
592+ * @return array
564593 */
565- public function fetchAll ($ statement , array $ bindings = [])
594+ public function fetchAll (string $ statement , array $ bindings = [], $ indexKey = null , $ class = ' assoc ' )
566595 {
567- $ sth = $ this ->execute ($ statement , $ bindings );
568- $ result = $ sth ->fetchAll (PDO ::FETCH_ASSOC );
596+ // $sth = $this->execute($statement, $bindings);
597+ // $result = $sth->fetchAll(PDO::FETCH_ASSOC);
569598
570- $ this ->freeResource ($ sth );
599+ if (strtolower ($ class ) === 'value ' ) {
600+ return $ this ->fetchValues ($ statement , $ bindings , $ indexKey );
601+ }
571602
572- return $ result ;
603+ if (strtolower ($ class ) === 'assoc ' ) {
604+ return $ this ->fetchAssocs ($ statement , $ bindings , $ indexKey );
605+ }
606+
607+ return $ this ->fetchObjects ($ statement , $ class , $ indexKey );
573608 }
574609
575610 /**
576611 * {@inheritdoc}
577612 */
578- public function fetchAssocs ($ statement , array $ bindings = [])
613+ public function fetchAssocs (string $ statement , array $ bindings = [], $ indexKey = null )
579614 {
580615 $ data = [];
581616 $ sth = $ this ->execute ($ statement , $ bindings );
582617
583618 while ($ row = $ sth ->fetch (PDO ::FETCH_ASSOC )) {
584- $ data [current ($ row )] = $ row ;
619+ if ($ indexKey ) {
620+ $ data [$ row [$ indexKey ]] = $ row ;
621+ } else {
622+ $ data [] = $ row ;
623+ }
624+ // $data[current($row)] = $row;
585625 }
586626
587627 $ this ->freeResource ($ sth );
@@ -592,49 +632,66 @@ public function fetchAssocs($statement, array $bindings = [])
592632 /**
593633 * {@inheritdoc}
594634 */
595- public function fetchColumns ( $ statement , array $ bindings = [])
635+ public function fetchValues ( string $ statement , array $ bindings = [], $ indexKey = null )
596636 {
637+ $ data = [];
597638 $ sth = $ this ->execute ($ statement , $ bindings );
598- $ column = $ sth ->fetchAll (PDO ::FETCH_COLUMN , 0 );
639+
640+ while ($ row = $ sth ->fetch (PDO ::FETCH_NUM )) {
641+ if ($ indexKey ) {
642+ $ data [$ row [$ indexKey ]] = $ row ;
643+ } else {
644+ $ data [] = $ row ;
645+ }
646+ }
599647
600648 $ this ->freeResource ($ sth );
601649
602- return $ column ;
650+ return $ data ;
603651 }
604652
605653 /**
606654 * {@inheritdoc}
607655 */
608- public function fetchGroups ( $ statement , array $ bindings = [], $ style = PDO :: FETCH_COLUMN )
656+ public function fetchColumns ( string $ statement , array $ bindings = [], int $ columnNum = 0 )
609657 {
610658 $ sth = $ this ->execute ($ statement , $ bindings );
611- $ group = $ sth ->fetchAll (PDO ::FETCH_GROUP | $ style );
659+ $ column = $ sth ->fetchAll (PDO ::FETCH_COLUMN , $ columnNum );
612660
613661 $ this ->freeResource ($ sth );
614662
615- return $ group ;
663+ return $ column ;
616664 }
617665
618666 /**
619667 * {@inheritdoc}
620668 */
621- public function fetchObjects ($ statement , array $ bindings = [], $ class = 'stdClass ' , array $ args = [])
669+ public function fetchObjects (string $ statement , array $ bindings = [], $ class = 'stdClass ' , $ indexKey = null , array $ args = [])
622670 {
671+ $ data = [];
623672 $ sth = $ this ->execute ($ statement , $ bindings );
624673
625- if (!empty ($ args )) {
626- $ result = $ sth ->fetchAll (PDO ::FETCH_CLASS , $ class , $ args );
627- } else {
628- $ result = $ sth ->fetchAll (PDO ::FETCH_CLASS , $ class );
674+ // if (!empty($args)) {
675+ // $result = $sth->fetchAll(PDO::FETCH_CLASS, $class, $args);
676+ // } else {
677+ // $result = $sth->fetchAll(PDO::FETCH_CLASS, $class);
678+ // }
679+
680+ while ($ row = $ sth ->fetchObject ($ class , $ args )) {
681+ if ($ indexKey ) {
682+ $ data [$ row ->$ indexKey ] = $ row ;
683+ } else {
684+ $ data [] = $ row ;
685+ }
629686 }
630687
631688 $ this ->freeResource ($ sth );
632689
633- return $ result ;
690+ return $ data ;
634691 }
635692
636693 /**
637- * 每行调用一次函数
694+ * 每行调用一次函数. 将每行的列值作为参数传递给指定的函数,并返回调用函数后的结果。
638695 *
639696 * @param string $statement
640697 * @param array $bindings
@@ -648,11 +705,10 @@ public function fetchObjects($statement, array $bindings = [], $class = 'stdClas
648705 *
649706 * @return void
650707 */
651- public function fetchFuns ($ statement , array $ bindings = [], callable $ func )
708+ public function fetchFuns (callable $ func , string $ statement , array $ bindings = [])
652709 {
653710 $ sth = $ this ->execute ($ statement , $ bindings );
654-
655- $ result = $ sth ->fetchAll (PDO ::FETCH_KEY_PAIR );
711+ $ result = $ sth ->fetchAll (PDO ::FETCH_FUNC , $ func );
656712
657713 $ this ->freeResource ($ sth );
658714
@@ -662,7 +718,20 @@ public function fetchFuns($statement, array $bindings = [], callable $func)
662718 /**
663719 * {@inheritdoc}
664720 */
665- public function fetchPairs ($ statement , array $ bindings = [])
721+ public function fetchGroups (string $ statement , array $ bindings = [], $ style = PDO ::FETCH_COLUMN )
722+ {
723+ $ sth = $ this ->execute ($ statement , $ bindings );
724+ $ group = $ sth ->fetchAll (PDO ::FETCH_GROUP | $ style );
725+
726+ $ this ->freeResource ($ sth );
727+
728+ return $ group ;
729+ }
730+
731+ /**
732+ * {@inheritdoc}
733+ */
734+ public function fetchPairs (string $ statement , array $ bindings = [])
666735 {
667736 $ sth = $ this ->execute ($ statement , $ bindings );
668737
@@ -734,12 +803,32 @@ public function yieldAll($statement, array $bindings = [])
734803 * @param array $bindings
735804 * @return \Generator
736805 */
737- public function yieldColumn ($ statement , array $ bindings = [])
806+ public function yieldValue ($ statement , array $ bindings = [])
738807 {
739808 $ sth = $ this ->execute ($ statement , $ bindings );
740809
741810 while ($ row = $ sth ->fetch (PDO ::FETCH_NUM )) {
742- yield $ row [0 ];
811+ yield $ row ;
812+ }
813+
814+ $ this ->freeResource ($ sth );
815+ }
816+
817+ /**
818+ * @param string $statement
819+ * @param array $bindings
820+ * @return \Generator
821+ */
822+ public function yieldColumn ($ statement , array $ bindings = [], int $ columnNum = 0 )
823+ {
824+ $ sth = $ this ->execute ($ statement , $ bindings );
825+
826+ // while ($row = $sth->fetch(PDO::FETCH_NUM)) {
827+ // yield $row[0];
828+ // }
829+
830+ while ($ colValue = $ sth ->fetchColumn ($ columnNum )) {
831+ yield $ colValue ;
743832 }
744833
745834 $ this ->freeResource ($ sth );
@@ -794,7 +883,6 @@ public function execute($statement, array $params = [])
794883 $ this ->fire (self ::BEFORE_EXECUTE , [$ statement , $ params , 'execute ' ]);
795884
796885 $ sth = $ this ->prepareWithBindings ($ statement , $ params );
797-
798886 $ sth ->execute ();
799887
800888 // trigger after event
@@ -811,6 +899,7 @@ public function execute($statement, array $params = [])
811899 public function prepareWithBindings ($ statement , array $ params = [])
812900 {
813901 $ this ->connect ();
902+ $ statement = $ this ->replaceTablePrefix ($ statement );
814903
815904 // if there are no values to bind ...
816905 if (!$ params ) {
@@ -1267,7 +1356,7 @@ public function exec($statement)
12671356 // trigger before event
12681357 $ this ->fire (self ::BEFORE_EXECUTE , [$ statement , 'exec ' ]);
12691358
1270- $ affected = $ this ->pdo ->exec ($ statement );
1359+ $ affected = $ this ->pdo ->exec ($ this -> replaceTablePrefix ( $ statement) );
12711360
12721361 // trigger after event
12731362 $ this ->fire (self ::AFTER_EXECUTE , [$ statement , 'exec ' ]);
@@ -1286,7 +1375,7 @@ public function query($statement, ...$fetch)
12861375 // trigger before event
12871376 $ this ->fire (self ::BEFORE_EXECUTE , [$ statement , 'query ' ]);
12881377
1289- $ sth = $ this ->pdo ->query ($ statement , ...$ fetch );
1378+ $ sth = $ this ->pdo ->query ($ this -> replaceTablePrefix ( $ statement) , ...$ fetch );
12901379
12911380 // trigger after event
12921381 $ this ->fire (self ::AFTER_EXECUTE , [$ statement , 'query ' ]);
@@ -1304,7 +1393,7 @@ public function prepare($statement, array $options = [])
13041393 $ this ->connect ();
13051394 $ this ->log ($ statement , $ options );
13061395
1307- return $ this ->pdo ->prepare ($ statement , $ options );
1396+ return $ this ->pdo ->prepare ($ this -> replaceTablePrefix ( $ statement) , $ options );
13081397 }
13091398
13101399 /**
0 commit comments