Refactor for Amp v3

This commit is contained in:
Aaron Piotrowski 2020-10-10 21:37:06 -05:00
parent 66d4ab1018
commit c17f93fb9a
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
10 changed files with 38 additions and 68 deletions

View File

@ -11,14 +11,14 @@
"homepage": "http://amphp.org", "homepage": "http://amphp.org",
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": ">=7.1", "php": ">=8",
"amphp/amp": "dev-streams as 2.6" "amphp/amp": "^3-dev"
}, },
"require-dev": { "require-dev": {
"amphp/php-cs-fixer-config": "dev-master", "amphp/php-cs-fixer-config": "dev-master",
"amphp/phpunit-util": "^1.1", "amphp/phpunit-util": "^2-dev",
"phpunit/phpunit": "^9 | ^8 | ^7", "phpunit/phpunit": "^9",
"vimeo/psalm": "^3.11@dev" "psalm/phar": "^3.11@dev"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -12,20 +12,15 @@ abstract class ConnectionConfig
'dbname' => 'db', 'dbname' => 'db',
]; ];
/** @var string */ private string $host;
private $host;
/** @var int */ private int $port;
private $port;
/** @var string|null */ private ?string $user;
private $user;
/** @var string|null */ private ?string $password;
private $password;
/** @var string|null */ private ?string $database;
private $database;
/** /**
* Parses a connection string into an array of keys and values given. * Parses a connection string into an array of keys and values given.
@ -64,7 +59,7 @@ abstract class ConnectionConfig
return $values; return $values;
} }
public function __construct(string $host, int $port, string $user = null, string $password = null, string $database = null) public function __construct(string $host, int $port, ?string $user = null, ?string $password = null, ?string $database = null)
{ {
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;

View File

@ -2,14 +2,10 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
interface Connector interface Connector
{ {
/** /**
* @param ConnectionConfig $config * @param ConnectionConfig $config
*
* @return Promise<Link>
*/ */
public function connect(ConnectionConfig $config): Promise; public function connect(ConnectionConfig $config): Link;
} }

View File

@ -2,43 +2,41 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
interface Executor extends TransientResource interface Executor extends TransientResource
{ {
/** /**
* @param string $sql SQL query to execute. * @param string $sql SQL query to execute.
* *
* @return Promise<Result> * @return Result
* *
* @throws FailureException If the operation fails due to unexpected condition. * @throws FailureException If the operation fails due to unexpected condition.
* @throws ConnectionException If the connection to the database is lost. * @throws ConnectionException If the connection to the database is lost.
* @throws QueryError If the operation fails due to an error in the query (such as a syntax error). * @throws QueryError If the operation fails due to an error in the query (such as a syntax error).
*/ */
public function query(string $sql): Promise; public function query(string $sql): Result;
/** /**
* @param string $sql SQL query to prepare. * @param string $sql SQL query to prepare.
* *
* @return Promise<Statement> * @return Statement
* *
* @throws FailureException If the operation fails due to unexpected condition. * @throws FailureException If the operation fails due to unexpected condition.
* @throws ConnectionException If the connection to the database is lost. * @throws ConnectionException If the connection to the database is lost.
* @throws QueryError If the operation fails due to an error in the query (such as a syntax error). * @throws QueryError If the operation fails due to an error in the query (such as a syntax error).
*/ */
public function prepare(string $sql): Promise; public function prepare(string $sql): Statement;
/** /**
* @param string $sql SQL query to prepare and execute. * @param string $sql SQL query to prepare and execute.
* @param mixed[] $params Query parameters. * @param mixed[] $params Query parameters.
* *
* @return Promise<Result> * @return Result
* *
* @throws FailureException If the operation fails due to unexpected condition. * @throws FailureException If the operation fails due to unexpected condition.
* @throws ConnectionException If the connection to the database is lost. * @throws ConnectionException If the connection to the database is lost.
* @throws QueryError If the operation fails due to an error in the query (such as a syntax error). * @throws QueryError If the operation fails due to an error in the query (such as a syntax error).
*/ */
public function execute(string $sql, array $params = []): Promise; public function execute(string $sql, array $params = []): Result;
/** /**
* Closes the executor. No further queries may be performed. * Closes the executor. No further queries may be performed.

View File

@ -2,8 +2,6 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
interface Link extends Executor interface Link extends Executor
{ {
/** /**
@ -11,7 +9,7 @@ interface Link extends Executor
* *
* @param int $isolation Transaction isolation level. * @param int $isolation Transaction isolation level.
* *
* @return Promise<Transaction> * @return Transaction
*/ */
public function beginTransaction(int $isolation = Transaction::ISOLATION_COMMITTED): Promise; public function beginTransaction(int $isolation = Transaction::ISOLATION_COMMITTED): Transaction;
} }

View File

@ -2,14 +2,12 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
interface Pool extends Link interface Pool extends Link
{ {
/** /**
* @return Promise<Link> * @return Link
*/ */
public function extractConnection(): Promise; public function extractConnection(): Link;
/** /**
* @return int Total number of active connections in the pool. * @return int Total number of active connections in the pool.

View File

@ -5,7 +5,7 @@ namespace Amp\Sql;
class QueryError extends \Error class QueryError extends \Error
{ {
/** @var string */ /** @var string */
protected $query = ""; protected string $query = "";
public function __construct(string $message, string $query = "", \Throwable $previous = null) public function __construct(string $message, string $query = "", \Throwable $previous = null)
{ {

View File

@ -2,7 +2,6 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
use Amp\Pipeline; use Amp\Pipeline;
interface Result extends Pipeline interface Result extends Pipeline
@ -11,17 +10,17 @@ interface Result extends Pipeline
* Promise returned resolves with a map (associative array) of column-names to column-values for each row in the * Promise returned resolves with a map (associative array) of column-names to column-values for each row in the
* result set. The promise resolves with null when no more rows remain. * result set. The promise resolves with null when no more rows remain.
* *
* @return Promise<array<string, mixed>|null> * @return array<string, mixed>|null
*/ */
public function continue(): Promise; public function continue(): ?array;
/** /**
* Resolves with a new instance of Result if another result is available after this result. Resolves with null if * Resolves with a new instance of Result if another result is available after this result. Resolves with null if
* no further results are available. * no further results are available.
* *
* @return Promise<Result|null> * @return Result|null
*/ */
public function getNextResult(): Promise; public function getNextResult(): ?Result;
/** /**
* Returns the number of rows affected or returned by the query if applicable or null if the number of rows is * Returns the number of rows affected or returned by the query if applicable or null if the number of rows is

View File

@ -2,16 +2,14 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
interface Statement extends TransientResource interface Statement extends TransientResource
{ {
/** /**
* @param mixed[] $params * @param mixed[] $params
* *
* @return Promise<Result> * @return Result
*/ */
public function execute(array $params = []): Promise; public function execute(array $params = []): Result;
/** /**
* @return string The SQL string used to prepare the statement. * @return string The SQL string used to prepare the statement.

View File

@ -2,14 +2,12 @@
namespace Amp\Sql; namespace Amp\Sql;
use Amp\Promise;
interface Transaction extends Executor interface Transaction extends Executor
{ {
const ISOLATION_UNCOMMITTED = 0; public const ISOLATION_UNCOMMITTED = 0;
const ISOLATION_COMMITTED = 1; public const ISOLATION_COMMITTED = 1;
const ISOLATION_REPEATABLE = 2; public const ISOLATION_REPEATABLE = 2;
const ISOLATION_SERIALIZABLE = 4; public const ISOLATION_SERIALIZABLE = 4;
/** /**
* @return int * @return int
@ -24,51 +22,41 @@ interface Transaction extends Executor
/** /**
* Commits the transaction and makes it inactive. * Commits the transaction and makes it inactive.
* *
* @return Promise<Result>
*
* @throws TransactionError If the transaction has been committed or rolled back. * @throws TransactionError If the transaction has been committed or rolled back.
*/ */
public function commit(): Promise; public function commit(): void;
/** /**
* Rolls back the transaction and makes it inactive. * Rolls back the transaction and makes it inactive.
* *
* @return Promise<Result>
*
* @throws TransactionError If the transaction has been committed or rolled back. * @throws TransactionError If the transaction has been committed or rolled back.
*/ */
public function rollback(): Promise; public function rollback(): void;
/** /**
* Creates a savepoint with the given identifier. * Creates a savepoint with the given identifier.
* *
* @param string $identifier Savepoint identifier. * @param string $identifier Savepoint identifier.
* *
* @return Promise<Result>
*
* @throws TransactionError If the transaction has been committed or rolled back. * @throws TransactionError If the transaction has been committed or rolled back.
*/ */
public function createSavepoint(string $identifier): Promise; public function createSavepoint(string $identifier): void;
/** /**
* Rolls back to the savepoint with the given identifier. * Rolls back to the savepoint with the given identifier.
* *
* @param string $identifier Savepoint identifier. * @param string $identifier Savepoint identifier.
* *
* @return Promise<Result>
*
* @throws TransactionError If the transaction has been committed or rolled back. * @throws TransactionError If the transaction has been committed or rolled back.
*/ */
public function rollbackTo(string $identifier): Promise; public function rollbackTo(string $identifier): void;
/** /**
* Releases the savepoint with the given identifier. * Releases the savepoint with the given identifier.
* *
* @param string $identifier Savepoint identifier. * @param string $identifier Savepoint identifier.
* *
* @return Promise<Result>
*
* @throws TransactionError If the transaction has been committed or rolled back. * @throws TransactionError If the transaction has been committed or rolled back.
*/ */
public function releaseSavepoint(string $identifier): Promise; public function releaseSavepoint(string $identifier): void;
} }