Skip to content

Commit dd9dd46

Browse files
committed
🛀 driver & dialect cleanup
1 parent d89083b commit dd9dd46

27 files changed

+538
-903
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"php": "^8.2",
2626
"ext-json": "*",
2727
"ext-mbstring": "*",
28+
"ext-sodium": "*",
2829
"chillerlan/php-settings-container": "^3.2.1",
2930
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0",
3031
"psr/log": "^1.1 || ^2.0 || ^3.0"

src/Database.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,56 @@
1616

1717
class Database extends DatabaseAbstract implements DriverInterface{
1818

19-
/**
20-
* @inheritdoc
21-
* @codeCoverageIgnore
22-
*/
2319
public function getDBResource():mixed{
2420
return $this->driver->getDBResource();
2521
}
2622

27-
/** @inheritdoc */
2823
public function connect():DriverInterface{
2924
$this->driver->connect();
3025

3126
return $this;
3227
}
3328

34-
/** @inheritdoc */
3529
public function disconnect():bool{
3630
return $this->driver->disconnect();
3731
}
3832

39-
/** @inheritdoc */
4033
public function getClientInfo():string{
4134
return $this->driver->getClientInfo();
4235
}
4336

44-
/** @inheritdoc */
4537
public function getServerInfo():string{
4638
return $this->driver->getServerInfo();
4739
}
4840

49-
/** @inheritdoc */
50-
public function escape(mixed $data = null):mixed{
41+
public function escape(bool|float|int|string|null $data = null):float|int|string{
5142
return $this->driver->escape($data);
5243
}
5344

54-
/** @inheritdoc */
5545
public function raw(string $sql, string|null $index = null, bool|null $assoc = null):Result{
5646
return $this->driver->raw($sql, $index, $assoc);
5747
}
5848

59-
/** @inheritdoc */
6049
public function rawCached(string $sql, string|null $index = null, bool|null $assoc = null, int|null $ttl = null):Result{
6150
return $this->driver->rawCached($sql, $index, $assoc, $ttl);
6251
}
6352

64-
/** @inheritdoc */
6553
public function prepared(string $sql, array|null $values = null, string|null $index = null, bool|null $assoc = null):Result{
6654
return $this->driver->prepared($sql, $values, $index, $assoc);
6755
}
6856

69-
/** @inheritdoc */
7057
public function preparedCached(string $sql, array|null $values = null, string|null $index = null, bool|null $assoc = null, int|null $ttl = null):Result{
7158
return $this->driver->preparedCached($sql, $values, $index, $assoc, $ttl);
7259
}
7360

74-
/** @inheritdoc */
7561
public function multi(string $sql, array $values):bool{
7662
return $this->driver->multi($sql, $values);
7763
}
7864

79-
/** @inheritdoc */
8065
public function multiCallback(string $sql, array $data, Closure $callback):bool{
8166
return $this->driver->multiCallback($sql, $data, $callback);
8267
}
8368

84-
/** @inheritdoc */
8569
public function getDialect():Dialect{
8670
return $this->dialect;
8771
}

src/DatabaseAbstract.php

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
use chillerlan\Database\Drivers\DriverInterface;
1515
use chillerlan\Database\Query\{Alter, Create, Delete, Drop, Insert, QueryException, Select, Show, Statement, Truncate, Update};
1616
use chillerlan\Settings\SettingsContainerInterface;
17-
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
17+
use Psr\Log\{LoggerInterface, NullLogger};
1818
use Psr\SimpleCache\CacheInterface;
19-
2019
use function strtolower;
2120

2221
/**
@@ -30,8 +29,7 @@
3029
* @property \chillerlan\Database\Query\Truncate $truncate
3130
* @property \chillerlan\Database\Query\Update $update
3231
*/
33-
abstract class DatabaseAbstract implements LoggerAwareInterface{
34-
use LoggerAwareTrait;
32+
abstract class DatabaseAbstract{
3533

3634
protected const STATEMENTS = [
3735
'alter' => Alter::class,
@@ -44,26 +42,28 @@ abstract class DatabaseAbstract implements LoggerAwareInterface{
4442
'truncate' => Truncate::class,
4543
'update' => Update::class,
4644
];
47-
/** @var \chillerlan\Database\DatabaseOptions */
48-
protected SettingsContainerInterface $options;
49-
protected CacheInterface|null $cache = null;
50-
protected DriverInterface $driver;
51-
protected Dialect $dialect;
45+
46+
protected SettingsContainerInterface|DatabaseOptions $options;
47+
protected CacheInterface|null $cache = null;
48+
protected LoggerInterface $logger;
49+
protected DriverInterface $driver;
50+
protected Dialect $dialect;
5251

5352
/**
5453
* Database constructor.
5554
*
5655
* @throws \chillerlan\Database\DatabaseException
5756
*/
58-
public function __construct(SettingsContainerInterface $options, CacheInterface|null $cache = null, LoggerInterface|null $logger = null){
57+
public function __construct(
58+
SettingsContainerInterface|DatabaseOptions $options,
59+
CacheInterface|null $cache = null,
60+
LoggerInterface $logger = new NullLogger,
61+
){
5962
$this->options = $options;
6063
$this->cache = $cache;
64+
$this->logger = $logger;
6165

62-
// set a default logger
63-
$this->logger = $logger ?? new NullLogger;
64-
/** @phan-suppress-next-line PhanTypeExpectedObjectOrClassName */
65-
$this->driver = new $this->options->driver($this->options, $this->cache, $this->logger);
66-
66+
$this->driver = new ($this->options->driver)($this->options, $this->cache, $this->logger);
6767
$this->dialect = $this->driver->getDialect();
6868
}
6969

@@ -74,34 +74,25 @@ public function __get(string $name):Statement{
7474
$name = strtolower($name);
7575

7676
if(isset($this::STATEMENTS[$name])){
77-
$statement = $this::STATEMENTS[$name];
78-
79-
return new $statement($this->driver, $this->dialect, $this->logger);
77+
return new ($this::STATEMENTS[$name])($this->driver, $this->dialect, $this->logger);
8078
}
8179

8280
throw new QueryException('invalid statement');
8381
}
8482

85-
/**
86-
* @codeCoverageIgnore
87-
*/
8883
public function __destruct(){
8984
$this->driver->disconnect();
9085
}
9186

92-
/**
93-
* @return \chillerlan\Database\Drivers\DriverInterface
94-
*/
9587
public function getDriver():DriverInterface{
9688
return $this->driver;
9789
}
9890

99-
/**
100-
*
101-
*/
102-
public function setLogger(LoggerInterface $logger):void{
91+
public function setLogger(LoggerInterface $logger):static{
10392
$this->logger = $logger;
10493
$this->driver->setLogger($logger);
94+
95+
return $this;
10596
}
10697

10798
}

src/DatabaseOptions.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,6 @@
1212

1313
use chillerlan\Settings\SettingsContainerAbstract;
1414

15-
/**
16-
* @property string $driver
17-
* @property string $host
18-
* @property int $port
19-
* @property string $socket
20-
* @property string $database
21-
* @property string $username
22-
* @property string $password
23-
* @property bool $use_ssl
24-
* @property string $ssl_key
25-
* @property string $ssl_cert
26-
* @property string $ssl_ca
27-
* @property string $ssl_capath
28-
* @property string $ssl_cipher
29-
* @property int $mysqli_timeout
30-
* @property string $mysql_charset
31-
* @property string $pgsql_charset
32-
* @property string $odbc_driver
33-
* @property string $convert_encoding_src
34-
* @property string $convert_encoding_dest
35-
* @property int $mssql_timeout
36-
* @property string $mssql_charset
37-
* @property bool $mssql_encrypt
38-
* @property string $firebird_encoding
39-
* @property string $cachekey_hash_algo
40-
* @property string $storage_path
41-
*/
4215
class DatabaseOptions extends SettingsContainerAbstract{
4316
use DatabaseOptionsTrait;
4417
}

src/DatabaseOptionsTrait.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@
1010

1111
namespace chillerlan\Database;
1212

13+
/**
14+
* @property string $driver
15+
* @property string $host
16+
* @property int $port
17+
* @property string $socket
18+
* @property string $database
19+
* @property string $username
20+
* @property string $password
21+
* @property bool $use_ssl
22+
* @property string $ssl_key
23+
* @property string $ssl_cert
24+
* @property string $ssl_ca
25+
* @property string $ssl_capath
26+
* @property string $ssl_cipher
27+
* @property int $mysqli_timeout
28+
* @property string $mysql_charset
29+
* @property string $pgsql_charset
30+
* @property string $odbc_driver
31+
* @property string $convert_encoding_src
32+
* @property string $convert_encoding_dest
33+
* @property int $mssql_timeout
34+
* @property string $mssql_charset
35+
* @property bool $mssql_encrypt
36+
* @property string $firebird_encoding
37+
* @property string $cachekey_hash_algo
38+
* @property string $storage_path
39+
*/
1340
trait DatabaseOptionsTrait{
1441

1542
/**
@@ -48,7 +75,7 @@ trait DatabaseOptionsTrait{
4875
protected string|null $password = null;
4976

5077
/**
51-
* Indicates whether the connection should use SSL or not
78+
* Indicates whether the connection should use SSL
5279
*/
5380
protected bool $use_ssl = false;
5481

@@ -100,7 +127,7 @@ trait DatabaseOptionsTrait{
100127
protected string|null $odbc_driver = null;
101128

102129
/**
103-
* atabase result encoding
130+
* Database result encoding
104131
*
105132
* @see \mb_convert_encoding()
106133
*/

src/Dialects/Dialect.php

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ interface Dialect{
2020
public function quote(string $str):string;
2121

2222
/**
23-
* @param array $cols
24-
* @param array $from
25-
* @param string|null $where
26-
* @param mixed $limit
27-
* @param mixed $offset
28-
* @param bool|null $distinct
29-
* @param array|null $groupby
30-
* @param array|null $orderby
3123
*
32-
* @return array
3324
*/
34-
public function select(array $cols, array $from, string|null $where = null, mixed $limit = null, mixed $offset = null, bool|null $distinct = null, array|null $groupby = null, array|null $orderby = null):array;
25+
public function select(
26+
array $cols,
27+
array $from,
28+
string|null $where = null,
29+
mixed $limit = null,
30+
mixed $offset = null,
31+
bool|null $distinct = null,
32+
array|null $groupby = null,
33+
array|null $orderby = null,
34+
):array;
3535

3636
/**
3737
*
@@ -49,14 +49,24 @@ public function from(array $expressions):array;
4949
public function orderby(array $expressions):array;
5050

5151
/**
52-
*
52+
* @todo: offset?
5353
*/
54-
public function selectCount(array $from, string|null $where = null, bool|null $distinct = null, array|null $groupby = null):array; // @todo: offset?
54+
public function selectCount(
55+
array $from,
56+
string|null $where = null,
57+
bool|null $distinct = null,
58+
array|null $groupby = null,
59+
):array;
5560

5661
/**
5762
*
5863
*/
59-
public function insert(string $table, array $fields, string|null $onConflict = null, string|null $conflictTarget = null):array;
64+
public function insert(
65+
string $table,
66+
array $fields,
67+
string|null $onConflict = null,
68+
string|null $conflictTarget = null,
69+
):array;
6070

6171
/**
6272
*
@@ -76,7 +86,14 @@ public function createDatabase(string $dbname, bool|null $ifNotExists = null, st
7686
/**
7787
*
7888
*/
79-
public function createTable(string $table, array $cols, string|null $primaryKey = null, bool|null $ifNotExists = null, bool|null $temp = null, string|null $dir = null):array;
89+
public function createTable(
90+
string $table,
91+
array $cols,
92+
string|null $primaryKey = null,
93+
bool|null $ifNotExists = null,
94+
bool|null $temp = null,
95+
string|null $dir = null,
96+
):array;
8097

8198
/**
8299
*
@@ -96,15 +113,20 @@ public function truncate(string $table):array;
96113
/**
97114
*
98115
*/
99-
public function fieldspec(string $name, string $type, mixed $length = null, string|null $attribute = null, string|null $collation = null, bool|null $isNull = null, string|null $defaultType = null, mixed $defaultValue = null, string|null $extra = null):string;
116+
public function fieldspec(
117+
string $name,
118+
string $type,
119+
mixed $length = null,
120+
string|null $attribute = null,
121+
string|null $collation = null,
122+
bool|null $isNull = null,
123+
string|null $defaultType = null,
124+
mixed $defaultValue = null,
125+
string|null $extra = null,
126+
):string;
100127

101128
/**
102-
* @param string $name
103-
* @param array $values
104-
* @param mixed $defaultValue
105-
* @param bool|null $isNull
106129
*
107-
* @return string
108130
*/
109131
public function enum(string $name, array $values, mixed $defaultValue = null, bool|null $isNull = null):string;
110132

0 commit comments

Comments
 (0)