mirror of
https://github.com/danog/sql.git
synced 2024-11-30 04:29:18 +01:00
Refactor for Amp v3
This commit is contained in:
parent
66d4ab1018
commit
c17f93fb9a
@ -11,14 +11,14 @@
|
||||
"homepage": "http://amphp.org",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=7.1",
|
||||
"amphp/amp": "dev-streams as 2.6"
|
||||
"php": ">=8",
|
||||
"amphp/amp": "^3-dev"
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/php-cs-fixer-config": "dev-master",
|
||||
"amphp/phpunit-util": "^1.1",
|
||||
"phpunit/phpunit": "^9 | ^8 | ^7",
|
||||
"vimeo/psalm": "^3.11@dev"
|
||||
"amphp/phpunit-util": "^2-dev",
|
||||
"phpunit/phpunit": "^9",
|
||||
"psalm/phar": "^3.11@dev"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
@ -12,20 +12,15 @@ abstract class ConnectionConfig
|
||||
'dbname' => 'db',
|
||||
];
|
||||
|
||||
/** @var string */
|
||||
private $host;
|
||||
private string $host;
|
||||
|
||||
/** @var int */
|
||||
private $port;
|
||||
private int $port;
|
||||
|
||||
/** @var string|null */
|
||||
private $user;
|
||||
private ?string $user;
|
||||
|
||||
/** @var string|null */
|
||||
private $password;
|
||||
private ?string $password;
|
||||
|
||||
/** @var string|null */
|
||||
private $database;
|
||||
private ?string $database;
|
||||
|
||||
/**
|
||||
* Parses a connection string into an array of keys and values given.
|
||||
@ -64,7 +59,7 @@ abstract class ConnectionConfig
|
||||
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->port = $port;
|
||||
|
@ -2,14 +2,10 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
interface Connector
|
||||
{
|
||||
/**
|
||||
* @param ConnectionConfig $config
|
||||
*
|
||||
* @return Promise<Link>
|
||||
*/
|
||||
public function connect(ConnectionConfig $config): Promise;
|
||||
public function connect(ConnectionConfig $config): Link;
|
||||
}
|
||||
|
@ -2,43 +2,41 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
interface Executor extends TransientResource
|
||||
{
|
||||
/**
|
||||
* @param string $sql SQL query to execute.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
* @return Result
|
||||
*
|
||||
* @throws FailureException If the operation fails due to unexpected condition.
|
||||
* @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).
|
||||
*/
|
||||
public function query(string $sql): Promise;
|
||||
public function query(string $sql): Result;
|
||||
|
||||
/**
|
||||
* @param string $sql SQL query to prepare.
|
||||
*
|
||||
* @return Promise<Statement>
|
||||
* @return Statement
|
||||
*
|
||||
* @throws FailureException If the operation fails due to unexpected condition.
|
||||
* @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).
|
||||
*/
|
||||
public function prepare(string $sql): Promise;
|
||||
public function prepare(string $sql): Statement;
|
||||
|
||||
/**
|
||||
* @param string $sql SQL query to prepare and execute.
|
||||
* @param mixed[] $params Query parameters.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
* @return Result
|
||||
*
|
||||
* @throws FailureException If the operation fails due to unexpected condition.
|
||||
* @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).
|
||||
*/
|
||||
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.
|
||||
|
@ -2,8 +2,6 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
interface Link extends Executor
|
||||
{
|
||||
/**
|
||||
@ -11,7 +9,7 @@ interface Link extends Executor
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
@ -2,14 +2,12 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
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.
|
||||
|
@ -5,7 +5,7 @@ namespace Amp\Sql;
|
||||
class QueryError extends \Error
|
||||
{
|
||||
/** @var string */
|
||||
protected $query = "";
|
||||
protected string $query = "";
|
||||
|
||||
public function __construct(string $message, string $query = "", \Throwable $previous = null)
|
||||
{
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
use Amp\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
|
||||
* 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
|
||||
* 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
|
||||
|
@ -2,16 +2,14 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
interface Statement extends TransientResource
|
||||
{
|
||||
/**
|
||||
* @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.
|
||||
|
@ -2,14 +2,12 @@
|
||||
|
||||
namespace Amp\Sql;
|
||||
|
||||
use Amp\Promise;
|
||||
|
||||
interface Transaction extends Executor
|
||||
{
|
||||
const ISOLATION_UNCOMMITTED = 0;
|
||||
const ISOLATION_COMMITTED = 1;
|
||||
const ISOLATION_REPEATABLE = 2;
|
||||
const ISOLATION_SERIALIZABLE = 4;
|
||||
public const ISOLATION_UNCOMMITTED = 0;
|
||||
public const ISOLATION_COMMITTED = 1;
|
||||
public const ISOLATION_REPEATABLE = 2;
|
||||
public const ISOLATION_SERIALIZABLE = 4;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
@ -24,51 +22,41 @@ interface Transaction extends Executor
|
||||
/**
|
||||
* Commits the transaction and makes it inactive.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @param string $identifier Savepoint identifier.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @param string $identifier Savepoint identifier.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @param string $identifier Savepoint identifier.
|
||||
*
|
||||
* @return Promise<Result>
|
||||
*
|
||||
* @throws TransactionError If the transaction has been committed or rolled back.
|
||||
*/
|
||||
public function releaseSavepoint(string $identifier): Promise;
|
||||
public function releaseSavepoint(string $identifier): void;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user