Skip to content

Commit 832d796

Browse files
committed
🚿
1 parent c116a57 commit 832d796

12 files changed

+37
-66
lines changed

src/Query/CreateTable.php

+24-15
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
*/
2626
class CreateTable extends Statement implements Query, IfNotExists{
2727

28-
protected bool $temp = false;
29-
protected string|null $primaryKey = null;
30-
protected array $cols = [];
31-
protected string|null $dir = null;
28+
protected bool $temp = false;
29+
protected string|null $primaryKey = null;
30+
protected array $cols = [];
31+
protected string|null $dir = null;
3232

3333
public function name(string $name):static{
3434
return $this->setName($name);
@@ -42,7 +42,6 @@ public function ifNotExists():static{
4242
return $this->setIfNotExists();
4343
}
4444

45-
/** @inheritdoc */
4645
protected function getSQL():array{
4746
return $this->dialect->createTable($this->name, $this->cols, $this->primaryKey, $this->ifNotExists, $this->temp, $this->dir);
4847
}
@@ -60,7 +59,17 @@ public function primaryKey(string $field, string|null $dir = null):static{
6059
return $this;
6160
}
6261

63-
public function field(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):static{
62+
public function field(
63+
string $name,
64+
string $type,
65+
mixed $length = null,
66+
string|null $attribute = null,
67+
string|null $collation = null,
68+
bool|null $isNull = null,
69+
string|null $defaultType = null,
70+
mixed $defaultValue = null,
71+
string|null $extra = null,
72+
):static{
6473

6574
if(is_scalar($defaultValue) && $defaultType === null){
6675
$defaultType = 'USER_DEFINED';
@@ -78,39 +87,39 @@ public function enum(string $name, array $values, mixed $defaultValue = null, bo
7887
}
7988

8089
public function tinyint(string $name, int|null $length = null, mixed $defaultValue = null, bool|null $isNull = null, string|null $attribute = null):static{
81-
return $this->field($name, 'TINYINT', $length, $attribute, null, $isNull, null, $defaultValue);
90+
return $this->field(name: $name, type: 'TINYINT', length: $length, attribute: $attribute, isNull: $isNull, defaultValue: $defaultValue);
8291
}
8392

8493
public function int(string $name, int|null $length = null, mixed $defaultValue = null, bool|null $isNull = null, string|null $attribute = null):static{
85-
return $this->field($name, 'INT', $length, $attribute, null, $isNull, null, $defaultValue);
94+
return $this->field(name: $name, type: 'INT', length: $length, attribute: $attribute, isNull: $isNull, defaultValue: $defaultValue);
8695
}
8796

8897
public function bigint(string $name, int|null $length = null, mixed $defaultValue = null, bool|null $isNull = null, string|null $attribute = null):static{
89-
return $this->field($name, 'BIGINT', $length, $attribute, null, $isNull, null, $defaultValue);
98+
return $this->field(name: $name, type: 'BIGINT', length: $length, attribute: $attribute, isNull: $isNull, defaultValue: $defaultValue);
9099
}
91100

92101
public function varchar(string $name, int $length, mixed $defaultValue = null, bool|null $isNull = null):static{
93-
return $this->field($name, 'VARCHAR', $length, null, null, $isNull, null, $defaultValue);
102+
return $this->field(name: $name, type: 'VARCHAR', length: $length, isNull: $isNull, defaultValue: $defaultValue);
94103
}
95104

96105
public function decimal(string $name, string $length, mixed $defaultValue = null, bool|null $isNull = null):static{
97-
return $this->field($name, 'DECIMAL', $length, null, null, $isNull, null, $defaultValue);
106+
return $this->field(name: $name, type: 'DECIMAL', length: $length, isNull: $isNull, defaultValue: $defaultValue);
98107
}
99108

100109
public function tinytext(string $name, mixed $defaultValue = null, bool|null $isNull = null):static{
101-
return $this->field($name, 'TINYTEXT', null, null, null, $isNull, null, $defaultValue);
110+
return $this->field(name: $name, type: 'TINYTEXT', isNull: $isNull, defaultValue: $defaultValue);
102111
}
103112

104113
public function text(string $name, mixed $defaultValue = null, bool|null $isNull = null):static{
105-
return $this->field($name, 'TEXT', null, null, null, $isNull, null, $defaultValue);
114+
return $this->field(name: $name, type: 'TEXT', isNull: $isNull, defaultValue: $defaultValue);
106115
}
107116

108117
public function mediumtext(string $name, mixed $defaultValue = null, bool|null $isNull = null):static{
109-
return $this->field($name, 'MEDIUMTEXT', null, null, null, $isNull, null, $defaultValue);
118+
return $this->field(name: $name, type: 'MEDIUMTEXT', isNull: $isNull, defaultValue: $defaultValue);
110119
}
111120

112121
public function longtext(string $name, mixed $defaultValue = null, bool|null $isNull = null):static{
113-
return $this->field($name, 'LONGTEXT', null, null, null, $isNull, null, $defaultValue);
122+
return $this->field(name: $name, type: 'LONGTEXT', isNull: $isNull, defaultValue: $defaultValue);
114123
}
115124

116125
}

src/Query/Delete.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function from(string $name):static{
2424
return $this->setName($name);
2525
}
2626

27-
public function where($val1, $val2 = null, string|null $operator = null, bool|null $bind = null, string|null $join = null):static{
27+
public function where(mixed $val1, mixed $val2 = null, string|null $operator = null, bool|null $bind = null, string|null $join = null):static{
2828
return $this->setWhere($val1, $val2, $operator, $bind, $join);
2929
}
3030

@@ -44,9 +44,8 @@ public function offset(int $offset):static{
4444
return $this->setOffset($offset);
4545
}
4646

47-
/** @inheritdoc */
4847
protected function getSQL():array{
49-
return $this->dialect->delete($this->name, $this->_getWhere());
48+
return $this->dialect->delete($this->name, $this->getWhere());
5049
}
5150

5251
}

src/Query/Insert.php

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function values(iterable $values):static{
4242
return $this;
4343
}
4444

45-
/** @inheritdoc */
4645
protected function getSQL():array{
4746

4847
if(empty($this->bindValues)){

src/Query/Query.php

-8
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@
1313

1414
interface Query{
1515

16-
/**
17-
* @param bool|null $multi
18-
*
19-
* @return string
20-
*/
2116
public function sql(bool|null $multi = null):string;
2217

2318
/**
24-
* @param string|null $index
25-
*
26-
* @return \chillerlan\Database\Result|bool
2719
* @throws \chillerlan\Database\Query\QueryException
2820
*/
2921
public function query(string|null $index = null);

src/Query/Select.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
*/
2424
class Select extends Statement implements Where, Limit, BindValues, Query, CachedQuery{
2525

26-
protected bool $distinct = false;
27-
protected array $cols = [];
28-
protected array $from = [];
29-
protected array $orderby = [];
30-
protected array $groupby = [];
26+
protected bool $distinct = false;
27+
protected array $cols = [];
28+
protected array $from = [];
29+
protected array $orderby = [];
30+
protected array $groupby = [];
3131

3232
public function where(mixed $val1, mixed $val2 = null, string|null $operator = null, bool|null $bind = null, string|null $join = null):static{
3333
return $this->setWhere($val1, $val2, $operator, $bind, $join);
@@ -53,14 +53,13 @@ public function cached(int|null $ttl = null):static{
5353
return $this->setCached($ttl);
5454
}
5555

56-
/** @inheritdoc */
5756
protected function getSQL():array{
5857

5958
if(empty($this->from)){
6059
throw new QueryException('no FROM expression specified');
6160
}
6261

63-
return $this->dialect->select($this->cols, $this->from, $this->_getWhere(), $this->limit, $this->offset, $this->distinct, $this->groupby, $this->orderby);
62+
return $this->dialect->select($this->cols, $this->from, $this->getWhere(), $this->limit, $this->offset, $this->distinct, $this->groupby, $this->orderby);
6463
}
6564

6665
public function distinct():static{
@@ -102,7 +101,7 @@ public function count():int{
102101
throw new QueryException('no FROM expression specified');
103102
}
104103

105-
$sql = $this->dialect->selectCount($this->from, $this->_getWhere(), $this->distinct, $this->groupby);
104+
$sql = $this->dialect->selectCount($this->from, $this->getWhere(), $this->distinct, $this->groupby);
106105
$result = $this->db->prepared(implode(' ', $sql), $this->bindValues);
107106

108107
if($result instanceof ResultInterface && $result->count() > 0){

src/Query/ShowCreateTable.php

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function name(string $name):static{
1717
return $this->setName($name);
1818
}
1919

20-
/** @inheritdoc */
2120
protected function getSQL():array{
2221
return $this->dialect->showCreateTable($this->name);
2322
}

src/Query/ShowDatabases.php

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
class ShowDatabases extends Statement implements Query{
1515

16-
/** @inheritdoc */
1716
protected function getSQL():array{
1817
return $this->dialect->showDatabases(); // @todo? WHERE
1918
}

src/Query/ShowTables.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ public function closeBracket():static{
3333
return $this->setCloseBracket();
3434
}
3535

36-
/** @inheritdoc */
3736
protected function getSQL():array{
38-
return $this->dialect->showTables($this->name, $this->pattern, $this->_getWhere());
37+
return $this->dialect->showTables($this->name, $this->pattern, $this->getWhere());
3938
}
4039

4140
public function pattern(string $pattern):static{

src/Query/Statement.php

+2-17
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function __construct(DriverInterface $db, Dialect $dialect, LoggerInterfa
6060
$this->logger = $logger;
6161
}
6262

63-
/** */
6463
protected function setName(string $name):static{
6564
$this->name = trim($name);
6665

@@ -71,7 +70,6 @@ protected function setName(string $name):static{
7170
return $this;
7271
}
7372

74-
/** */
7573
protected function setOnConflict(string $name, string|null $on_conflict = null, string|null $conflict_target = null):static{
7674
$this->name = trim($name);
7775
$on_conflict = trim(strtoupper($on_conflict ?? ''));
@@ -92,30 +90,24 @@ protected function setOnConflict(string $name, string|null $on_conflict = null,
9290
return $this;
9391
}
9492

95-
/** */
9693
protected function setCharset(string $charset):static{
9794
$this->charset = trim($charset);
9895

9996
return $this;
10097
}
10198

102-
/** */
10399
protected function setIfExists():static{
104100
$this->ifExists = true;
105101

106102
return $this;
107103
}
108104

109-
/** */
110105
protected function setIfNotExists():static{
111106
$this->ifNotExists = true;
112107

113108
return $this;
114109
}
115110

116-
/**
117-
*
118-
*/
119111
protected function setWhere(mixed $val1, mixed $val2, string|null $operator = null, bool|null $bind = null, string|null $join = null):static{
120112
$operator = $operator !== null ? strtoupper(trim($operator)) : '=';
121113
$bind ??= true;
@@ -139,7 +131,7 @@ protected function setWhere(mixed $val1, mixed $val2, string|null $operator = nu
139131
$this->bindValues = array_merge($this->bindValues, $val2);
140132
}
141133
else{
142-
$where[] = $operator.'('.implode(',', array_map([$this->db, 'escape'], $val2)).')'; // @todo: quote
134+
$where[] = $operator.'('.implode(',', array_map($this->db->escape(...), $val2)).')'; // @todo: quote
143135
}
144136

145137
}
@@ -199,7 +191,6 @@ protected function setWhere(mixed $val1, mixed $val2, string|null $operator = nu
199191
return $this;
200192
}
201193

202-
/** */
203194
protected function setOpenBracket(string|null $join = null):static{
204195
$join = strtoupper(trim($join ?? ''));
205196

@@ -212,15 +203,13 @@ protected function setOpenBracket(string|null $join = null):static{
212203
return $this;
213204
}
214205

215-
/** */
216206
protected function setCloseBracket():static{
217207
$this->where[] = ')';
218208

219209
return $this;
220210
}
221211

222-
/** */
223-
protected function _getWhere():string{
212+
protected function getWhere():string{
224213
$where = [];
225214
$joinArgs = array_merge($this->joinArgs, ['(', ')']);
226215

@@ -320,26 +309,22 @@ public function getBindValues():array{
320309
return $this->bindValues;
321310
}
322311

323-
/** */
324312
protected function addBindValue(int|string $key, mixed $value):void{
325313
$this->bindValues[$key] = $value;
326314
}
327315

328-
/** */
329316
protected function setLimit(int $limit):static{
330317
$this->limit = max($limit, 0);
331318

332319
return $this;
333320
}
334321

335-
/** */
336322
protected function setOffset(int $offset):static{
337323
$this->offset = max($offset, 0);
338324

339325
return $this;
340326
}
341327

342-
/** */
343328
protected function setCached(int|null $ttl = null):static{
344329
$this->cached = true;
345330

src/Query/Truncate.php

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function table(string $name):static{
2222
return $this->setName($name);
2323
}
2424

25-
/** @inheritdoc */
2625
protected function getSQL():array{
2726
return $this->dialect->truncate($this->name);
2827
}

src/Query/Update.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ public function offset(int $offset):static{
4848
return $this->setOffset($offset);
4949
}
5050

51-
/** @inheritdoc */
5251
protected function getSQL():array{
5352

5453
if(empty($this->set)){
5554
throw new QueryException('no fields to update specified');
5655
}
5756

58-
return $this->dialect->update($this->name, $this->set, $this->_getWhere());
57+
return $this->dialect->update($this->name, $this->set, $this->getWhere());
5958
}
6059

6160
public function set(array $set, bool|null $bind = null):static{

src/Query/Where.php

-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
*/
1919
interface Where{
2020

21-
/**
22-
* @param mixed $val1
23-
* @param mixed $val2
24-
* @param string|null $operator
25-
* @param bool|null $bind
26-
* @param string|null $join
27-
*/
2821
public function where(mixed $val1, mixed $val2 = null, string|null $operator = null, bool|null $bind = null, string|null $join = null):static;
2922

3023
public function openBracket(string|null $join = null):static;

0 commit comments

Comments
 (0)