Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/ApiJson/Interface/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getPrimaryKey(): string;

public function count($columns = '*'): int;

public function insertGetId(array $values, $sequence = null): int;
public function insert(array $values, $sequence = null): int;

public function update(array $values): bool;

Expand All @@ -27,6 +27,4 @@ public function toSql();
public function getBindings();

public function getDb();

public function query();
}
47 changes: 28 additions & 19 deletions app/ApiJson/Model/MysqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use App\ApiJson\Entity\ConditionEntity;
use App\ApiJson\Interface\QueryInterface;
use App\Event\ApiJson\MysqlQueryAfter;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\ApplicationContext;
use PDO;
use Psr\EventDispatcher\EventDispatcherInterface;

class MysqlQuery implements QueryInterface
{
Expand All @@ -23,6 +27,11 @@ public function __construct(protected string $tableName, protected ConditionEnti
$this->db = Db::table($tableName);
}

public function getDb(): Builder
{
return $this->db;
}

/**
* @param string $primaryKey
*/
Expand All @@ -39,14 +48,20 @@ public function getPrimaryKey(): string
return $this->primaryKey;
}

public function getDb(): Builder
public function all(): array
{
return $this->db;
}
$this->buildQuery();

public function query(): array
{
return $this->db->get()->all();
$pdo = $this->db->getConnection()->getReadPdo(); //为了实现自动解析Json 找不到Hyperf的能提供的能力 则手动拿PDO处理

$statement = $pdo->prepare($this->toSql());
$statement->execute($this->getBindings());
$result = $statement->fetchAll(PDO::FETCH_ASSOC);

$event = new MysqlQueryAfter($result, $statement, $this->toSql(), $this->getBindings()); //这可能并不是很好的写法 待暂无其他思路去实现
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);

return $event->result;
}

public function count($columns = '*'): int
Expand All @@ -55,7 +70,13 @@ public function count($columns = '*'): int
return $this->db->count();
}

public function insertGetId(array $values, $sequence = null): int
public function toSql(): string
{
$this->buildQuery();
return $this->db->toSql();
}

public function insert(array $values, $sequence = null): int
{
$this->build = true;
return $this->db->insertGetId($values, $sequence);
Expand All @@ -75,18 +96,6 @@ public function delete($id = null): bool
return $this->db->delete($id);
}

public function all(): array
{
$this->buildQuery();
return $this->db->get()->all();
}

public function toSql(): string
{
$this->buildQuery();
return $this->db->toSql();
}

public function getBindings(): array
{
return $this->db->getBindings();
Expand Down
15 changes: 15 additions & 0 deletions app/Event/ApiJson/MysqlQueryAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Event\ApiJson;

use PDOStatement;

/**
* 查询完毕的处理
*/
class MysqlQueryAfter
{
public function __construct(public array $result, public PDOStatement $statement, protected string $sql, protected array $bindings)
{
}
}
33 changes: 16 additions & 17 deletions app/Listener/QueryResultTryToJsonListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace App\Listener;

use App\Event\ApiJson\QueryExecuteAfter;
use App\Event\ApiJson\MysqlQueryAfter;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

Expand All @@ -15,29 +15,28 @@ class QueryResultTryToJsonListener implements ListenerInterface
public function listen(): array
{
return [
QueryExecuteAfter::class,
MysqlQueryAfter::class,
];
}

public function process(object $event)
{
if (!$event instanceof QueryExecuteAfter) return;
if ($event->method != 'GET') return;
$event->result = $this->toJson($event->result);
}

private function toJson(array $result): array
{
foreach ($result as $key => $value) {
if (is_array($value)) {
$result[$key] = $this->toJson($value);
if (!$event instanceof MysqlQueryAfter) return;
$columnCount = count(array_keys(current($event->result)));
$columnMeta = [];
for ($i = 0; $i <= $columnCount; $i++) {
$meta = $event->statement->getColumnMeta($i);
if ($meta) {
$columnMeta[$meta['name']] = $meta;
}
if (!is_string($value)) continue;
$jsonData = json_decode($value, true);
if (is_array($jsonData)) {
$result[$key] = $jsonData;
}

foreach(array_filter($columnMeta, function ($item) {
return !isset($item['native_type']) && in_array('blob', $item['flags']);
}, ARRAY_FILTER_USE_BOTH) as $item) {
for ($i = 0; $i < count($event->result); $i++) {
$event->result[$i][$item['name']] = json_decode($event->result[$i][$item['name']], true);
}
}
return $result;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-pdo": "*",
"hyperf/cache": "~2.2.0",
"hyperf/command": "~2.2.0",
"hyperf/config": "~2.2.0",
Expand All @@ -39,7 +40,6 @@
},
"suggest": {
"ext-openssl": "Required to use HTTPS.",
"ext-pdo": "Required to use MySQL Client.",
"ext-pdo_mysql": "Required to use MySQL Client.",
"ext-redis": "Required to use Redis Client."
},
Expand Down
2 changes: 1 addition & 1 deletion config/autoload/databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
return [
'default' => [
'driver' => env('DB_DRIVER', 'mysql'),
'driver' => env('DB_DRIVER', 'pdo'),
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'hyperf'),
'port' => env('DB_PORT', 3306),
Expand Down
2 changes: 1 addition & 1 deletion test/Cases/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public function testWhereIdSubQuery()
"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
"http://common.cnblogs.com/images/icon_weibo_24.png"
],
"date" =>"2017-02-01 11:21:50"
"date" =>"2017-02-01 19:21:50"
]
], $result);
}
Expand Down