44
55use App \ApiJson \Entity \ConditionEntity ;
66use App \ApiJson \Interface \QueryInterface ;
7+ use App \Event \ApiJson \MysqlQueryAfter ;
78use Hyperf \Database \Query \Builder ;
89use Hyperf \DbConnection \Db ;
10+ use Hyperf \Utils \ApplicationContext ;
11+ use PDO ;
12+ use Psr \EventDispatcher \EventDispatcherInterface ;
913
1014class MysqlQuery implements QueryInterface
1115{
@@ -23,6 +27,11 @@ public function __construct(protected string $tableName, protected ConditionEnti
2327 $ this ->db = Db::table ($ tableName );
2428 }
2529
30+ public function getDb (): Builder
31+ {
32+ return $ this ->db ;
33+ }
34+
2635 /**
2736 * @param string $primaryKey
2837 */
@@ -39,14 +48,20 @@ public function getPrimaryKey(): string
3948 return $ this ->primaryKey ;
4049 }
4150
42- public function getDb (): Builder
51+ public function all (): array
4352 {
44- return $ this ->db ;
45- }
53+ $ this ->buildQuery ();
4654
47- public function query (): array
48- {
49- return $ this ->db ->get ()->all ();
55+ $ pdo = $ this ->db ->getConnection ()->getReadPdo (); //为了实现自动解析Json 找不到Hyperf的能提供的能力 则手动拿PDO处理
56+
57+ $ statement = $ pdo ->prepare ($ this ->toSql ());
58+ $ statement ->execute ($ this ->getBindings ());
59+ $ result = $ statement ->fetchAll (PDO ::FETCH_ASSOC );
60+
61+ $ event = new MysqlQueryAfter ($ result , $ statement , $ this ->toSql (), $ this ->getBindings ()); //这可能并不是很好的写法 待暂无其他思路去实现
62+ ApplicationContext::getContainer ()->get (EventDispatcherInterface::class)->dispatch ($ event );
63+
64+ return $ event ->result ;
5065 }
5166
5267 public function count ($ columns = '* ' ): int
@@ -55,7 +70,13 @@ public function count($columns = '*'): int
5570 return $ this ->db ->count ();
5671 }
5772
58- public function insertGetId (array $ values , $ sequence = null ): int
73+ public function toSql (): string
74+ {
75+ $ this ->buildQuery ();
76+ return $ this ->db ->toSql ();
77+ }
78+
79+ public function insert (array $ values , $ sequence = null ): int
5980 {
6081 $ this ->build = true ;
6182 return $ this ->db ->insertGetId ($ values , $ sequence );
@@ -75,18 +96,6 @@ public function delete($id = null): bool
7596 return $ this ->db ->delete ($ id );
7697 }
7798
78- public function all (): array
79- {
80- $ this ->buildQuery ();
81- return $ this ->db ->get ()->all ();
82- }
83-
84- public function toSql (): string
85- {
86- $ this ->buildQuery ();
87- return $ this ->db ->toSql ();
88- }
89-
9099 public function getBindings (): array
91100 {
92101 return $ this ->db ->getBindings ();
0 commit comments