Skip to content

Commit

Permalink
Merge pull request #30 from tab-engineer/master
Browse files Browse the repository at this point in the history
http-message等优化
  • Loading branch information
bbuugg authored Aug 12, 2022
2 parents 0f59707 + 2bcbecc commit 8933ddc
Show file tree
Hide file tree
Showing 55 changed files with 240 additions and 157 deletions.
2 changes: 1 addition & 1 deletion src/database/publish/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

use Max\Database\Connectors\AutoConnector;
use Max\Database\Connector\AutoConnector;
use Max\Database\DatabaseConfig;

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Connectors;
namespace Max\Database\Connector;

use Max\Database\Contracts\ConnectorInterface;
use Max\Database\Contract\ConnectorInterface;
use Max\Database\DatabaseConfig;

class AutoConnector implements ConnectorInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Connectors;
namespace Max\Database\Connector;

use ArrayObject;
use Max\Database\Contracts\ConnectorInterface;
use Max\Database\Contract\ConnectorInterface;
use Max\Database\DatabaseConfig;
use PDO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Connectors;
namespace Max\Database\Connector;

use ArrayObject;
use Max\Context\Context;
use Max\Database\Context\Connection;
use Max\Database\Contracts\ConnectorInterface;
use Max\Database\Contracts\PoolInterface;
use Max\Database\Contract\ConnectorInterface;
use Max\Database\Contract\PoolInterface;
use Max\Database\DatabaseConfig;
use PDO;
use Swoole\Coroutine\Channel;
Expand Down
2 changes: 1 addition & 1 deletion src/database/src/Context/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Max\Database\Context;

use ArrayObject;
use Max\Database\Connectors\PoolConnector;
use Max\Database\Connector\PoolConnector;
use Throwable;

class Connection extends ArrayObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Contracts;
namespace Max\Database\Contract;

use Max\Database\DatabaseConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Contracts;
namespace Max\Database\Contract;

interface PoolInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Contracts;
namespace Max\Database\Contract;

interface QueryInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/database/src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Max\Database\Eloquent;

use Max\Database\Collection;
use Max\Database\Exceptions\ModelNotFoundException;
use Max\Database\Exception\ModelNotFoundException;
use Max\Database\Query\Builder as QueryBuilder;
use PDO;
use Throwable;
Expand Down
2 changes: 1 addition & 1 deletion src/database/src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use JsonSerializable;
use Max\Database\Collection;
use Max\Database\Eloquent\Traits\Relations;
use Max\Database\Exceptions\ModelNotFoundException;
use Max\Database\Exception\ModelNotFoundException;
use Max\Database\Manager;
use Max\Database\Query\Expression;
use Max\Utils\Arr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Eloquent\Relations;
namespace Max\Database\Eloquent\Relation;

use Max\Database\Eloquent\Builder;
use Max\Database\Eloquent\Model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Eloquent\Relations;
namespace Max\Database\Eloquent\Relation;

class HasOne implements \JsonSerializable
{
Expand Down
2 changes: 1 addition & 1 deletion src/database/src/Eloquent/Traits/Relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Max\Database\Eloquent\Traits;

use Max\Database\Eloquent\Model;
use Max\Database\Eloquent\Relations\HasMany;
use Max\Database\Eloquent\Relation\HasMany;

trait Relations
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Events;
namespace Max\Database\Event;

class QueryExecuted
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Exceptions;
namespace Max\Database\Exception;

use Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Exceptions;
namespace Max\Database\Exception;

class PoolException extends \Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Database\Exceptions;
namespace Max\Database\Exception;

class QueryException extends \Exception
{
Expand Down
31 changes: 14 additions & 17 deletions src/database/src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Max\Database;

use ArrayObject;
use InvalidArgumentException;
use Max\Config\Contract\ConfigInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
Expand All @@ -26,18 +25,19 @@ class Manager
*/
protected string $defaultConnection;

protected ArrayObject $connections;
protected array $connections = [];

/**
* @var array|mixed
*/
protected array $config = [];

public function __construct(ConfigInterface $config, protected ?EventDispatcherInterface $eventDispatcher = null)
{
public function __construct(
ConfigInterface $config,
protected ?EventDispatcherInterface $eventDispatcher = null
) {
$config = $config->get('database');
$this->defaultConnection = $config['default'];
$this->connections = new ArrayObject();
$this->config = $config['connections'] ?? [];
}

Expand All @@ -49,22 +49,19 @@ public function __call(string $name, array $arguments)
return $this->connection($this->defaultConnection)->{$name}(...$arguments);
}

/**
* @return Query
*/
public function connection(?string $name = null)
public function connection(?string $name = null): Query
{
$name ??= $this->defaultConnection;
if (! $this->connections->offsetExists($name)) {
if (! isset($this->config[$name])) {
if (!isset($this->connections[$name])) {
if (!isset($this->config[$name])) {
throw new InvalidArgumentException('没有相关数据库连接');
}
$config = $this->config[$name];
$connector = $config['connector'];
$options = $config['options'];
$options['name'] = $name;
$this->connections->offsetSet($name, new $connector(new DatabaseConfig($options)));
$config = $this->config[$name];
$connector = $config['connector'];
$options = $config['options'];
$options['name'] = $name;
$this->connections[$name] = new $connector(new DatabaseConfig($options));
}
return new Query($this->connections->offsetGet($name), $this->eventDispatcher);
return new Query($this->connections[$name], $this->eventDispatcher);
}
}
6 changes: 3 additions & 3 deletions src/database/src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace Max\Database;

use Closure;
use Max\Database\Contracts\ConnectorInterface;
use Max\Database\Contracts\QueryInterface;
use Max\Database\Events\QueryExecuted;
use Max\Database\Contract\ConnectorInterface;
use Max\Database\Contract\QueryInterface;
use Max\Database\Event\QueryExecuted;
use Max\Database\Query\Builder;
use PDO;
use PDOException;
Expand Down
34 changes: 15 additions & 19 deletions src/database/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Max\Database\Query;

use Max\Database\Collection;
use Max\Database\Contracts\QueryInterface;
use Max\Database\Contract\QueryInterface;
use Max\Utils\Traits\Conditionable;
use PDO;

Expand Down Expand Up @@ -62,8 +62,9 @@ class Builder
*/
protected array $column;

public function __construct(protected QueryInterface $query)
{
public function __construct(
protected QueryInterface $query
) {
}

/**
Expand All @@ -78,12 +79,7 @@ public function from(string $table, $alias = null): static
return $this;
}

/**
* @param $value
*
* @return $this
*/
public function where(string $column, $value, string $operator = '='): static
public function where(string $column, mixed $value, string $operator = '='): static
{
$this->where[] = [$column, $operator, '?'];
$this->addBindings($value);
Expand Down Expand Up @@ -127,7 +123,7 @@ public function whereLike($column, $value): static
*/
public function whereIn(string $column, array $in): static
{
if (! empty($in)) {
if (!empty($in)) {
$this->addBindings($in);
$this->where[] = [$column, 'IN', sprintf('(%s)', rtrim(str_repeat('?, ', count($in)), ' ,'))];
}
Expand Down Expand Up @@ -345,7 +341,7 @@ public function avg($column): int

public function exists(): bool
{
return (bool) $this->query->statement(
return (bool)$this->query->statement(
sprintf('SELECT EXISTS(%s) AS MAX_EXIST', $this->toSql()),
$this->bindings
)->fetchColumn();
Expand Down Expand Up @@ -385,7 +381,7 @@ public function insert(array $record): int
$this->bindings,
);

return (int) $this->query->getPdo()->lastInsertId();
return (int)$this->query->getPdo()->lastInsertId();
}

public function insertMany(array $records): mixed
Expand All @@ -402,7 +398,7 @@ public function insertMany(array $records): mixed

public function insertAll(array $data): array
{
return array_map(fn ($item) => $this->insert($item), $data);
return array_map(fn($item) => $this->insert($item), $data);
}

public function update(array $data): int
Expand All @@ -415,7 +411,7 @@ public function generateSelectQuery(): string
$query = 'SELECT ';
foreach (static::$clause as $value) {
$compiler = 'compile' . ucfirst($value);
if (! empty($this->{$value})) {
if (!empty($this->{$value})) {
$query .= $this->{$compiler}($this);
}
}
Expand Down Expand Up @@ -469,15 +465,15 @@ protected function addBindings($value): void

protected function aggregate(string $expression): int
{
return (int) $this->query->statement(
$this->toSql((array) ($expression . ' AS AGGREGATE')),
return (int)$this->query->statement(
$this->toSql((array)($expression . ' AS AGGREGATE')),
$this->bindings
)->fetchColumn();
}

protected function compileJoin(): string
{
$joins = array_map(function (Join $item) {
$joins = array_map(function(Join $item) {
$alias = $item->alias ? 'AS ' . $item->alias : '';
$on = $item->on ? ('ON ' . implode(' ', $item->on)) : '';
return ' ' . $item->league . ' ' . $item->table . ' ' . $alias . ' ' . $on;
Expand Down Expand Up @@ -517,7 +513,7 @@ protected function compileOffset(): string

protected function compileOrder(): string
{
$orderBy = array_map(fn ($item) => $item[0] instanceof Expression ? $item[0]->__toString() : implode(' ', $item), $this->order);
$orderBy = array_map(fn($item) => $item[0] instanceof Expression ? $item[0]->__toString() : implode(' ', $item), $this->order);
return ' ORDER BY ' . implode(', ', $orderBy);
}

Expand All @@ -528,7 +524,7 @@ protected function compileGroup(): string

protected function compileHaving(): string
{
$having = array_map(fn ($item) => implode(' ', $item), $this->having);
$having = array_map(fn($item) => implode(' ', $item), $this->having);

return ' HAVING ' . implode(' AND ', $having);
}
Expand Down
13 changes: 3 additions & 10 deletions src/database/src/Query/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

class Expression
{
protected string $expression;

public function __construct(string $expression)
{
$this->expression = $expression;
public function __construct(
public string $expression
) {
}

/**
Expand All @@ -27,9 +25,4 @@ public function __toString()
{
return $this->expression;
}

public function getExpression(): string
{
return $this->expression;
}
}
Loading

0 comments on commit 8933ddc

Please sign in to comment.