Skip to content

Commit

Permalink
🚿
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Aug 26, 2024
1 parent c116a57 commit 832d796
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 66 deletions.
39 changes: 24 additions & 15 deletions src/Query/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
*/
class CreateTable extends Statement implements Query, IfNotExists{

protected bool $temp = false;
protected string|null $primaryKey = null;
protected array $cols = [];
protected string|null $dir = null;
protected bool $temp = false;
protected string|null $primaryKey = null;
protected array $cols = [];
protected string|null $dir = null;

public function name(string $name):static{
return $this->setName($name);
Expand All @@ -42,7 +42,6 @@ public function ifNotExists():static{
return $this->setIfNotExists();
}

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

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{
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{

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

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

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

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

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

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

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

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

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

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

}
5 changes: 2 additions & 3 deletions src/Query/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function from(string $name):static{
return $this->setName($name);
}

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

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

/** @inheritdoc */
protected function getSQL():array{
return $this->dialect->delete($this->name, $this->_getWhere());
return $this->dialect->delete($this->name, $this->getWhere());
}

}
1 change: 0 additions & 1 deletion src/Query/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function values(iterable $values):static{
return $this;
}

/** @inheritdoc */
protected function getSQL():array{

if(empty($this->bindValues)){
Expand Down
8 changes: 0 additions & 8 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@

interface Query{

/**
* @param bool|null $multi
*
* @return string
*/
public function sql(bool|null $multi = null):string;

/**
* @param string|null $index
*
* @return \chillerlan\Database\Result|bool
* @throws \chillerlan\Database\Query\QueryException
*/
public function query(string|null $index = null);
Expand Down
15 changes: 7 additions & 8 deletions src/Query/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
class Select extends Statement implements Where, Limit, BindValues, Query, CachedQuery{

protected bool $distinct = false;
protected array $cols = [];
protected array $from = [];
protected array $orderby = [];
protected array $groupby = [];
protected bool $distinct = false;
protected array $cols = [];
protected array $from = [];
protected array $orderby = [];
protected array $groupby = [];

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

/** @inheritdoc */
protected function getSQL():array{

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

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

public function distinct():static{
Expand Down Expand Up @@ -102,7 +101,7 @@ public function count():int{
throw new QueryException('no FROM expression specified');
}

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

if($result instanceof ResultInterface && $result->count() > 0){
Expand Down
1 change: 0 additions & 1 deletion src/Query/ShowCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function name(string $name):static{
return $this->setName($name);
}

/** @inheritdoc */
protected function getSQL():array{
return $this->dialect->showCreateTable($this->name);
}
Expand Down
1 change: 0 additions & 1 deletion src/Query/ShowDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

class ShowDatabases extends Statement implements Query{

/** @inheritdoc */
protected function getSQL():array{
return $this->dialect->showDatabases(); // @todo? WHERE
}
Expand Down
3 changes: 1 addition & 2 deletions src/Query/ShowTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public function closeBracket():static{
return $this->setCloseBracket();
}

/** @inheritdoc */
protected function getSQL():array{
return $this->dialect->showTables($this->name, $this->pattern, $this->_getWhere());
return $this->dialect->showTables($this->name, $this->pattern, $this->getWhere());
}

public function pattern(string $pattern):static{
Expand Down
19 changes: 2 additions & 17 deletions src/Query/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function __construct(DriverInterface $db, Dialect $dialect, LoggerInterfa
$this->logger = $logger;
}

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

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

/** */
protected function setOnConflict(string $name, string|null $on_conflict = null, string|null $conflict_target = null):static{
$this->name = trim($name);
$on_conflict = trim(strtoupper($on_conflict ?? ''));
Expand All @@ -92,30 +90,24 @@ protected function setOnConflict(string $name, string|null $on_conflict = null,
return $this;
}

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

return $this;
}

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

return $this;
}

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

return $this;
}

/**
*
*/
protected function setWhere(mixed $val1, mixed $val2, string|null $operator = null, bool|null $bind = null, string|null $join = null):static{
$operator = $operator !== null ? strtoupper(trim($operator)) : '=';
$bind ??= true;
Expand All @@ -139,7 +131,7 @@ protected function setWhere(mixed $val1, mixed $val2, string|null $operator = nu
$this->bindValues = array_merge($this->bindValues, $val2);
}
else{
$where[] = $operator.'('.implode(',', array_map([$this->db, 'escape'], $val2)).')'; // @todo: quote
$where[] = $operator.'('.implode(',', array_map($this->db->escape(...), $val2)).')'; // @todo: quote
}

}
Expand Down Expand Up @@ -199,7 +191,6 @@ protected function setWhere(mixed $val1, mixed $val2, string|null $operator = nu
return $this;
}

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

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

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

return $this;
}

/** */
protected function _getWhere():string{
protected function getWhere():string{
$where = [];
$joinArgs = array_merge($this->joinArgs, ['(', ')']);

Expand Down Expand Up @@ -320,26 +309,22 @@ public function getBindValues():array{
return $this->bindValues;
}

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

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

return $this;
}

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

return $this;
}

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

Expand Down
1 change: 0 additions & 1 deletion src/Query/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function table(string $name):static{
return $this->setName($name);
}

/** @inheritdoc */
protected function getSQL():array{
return $this->dialect->truncate($this->name);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Query/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ public function offset(int $offset):static{
return $this->setOffset($offset);
}

/** @inheritdoc */
protected function getSQL():array{

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

return $this->dialect->update($this->name, $this->set, $this->_getWhere());
return $this->dialect->update($this->name, $this->set, $this->getWhere());
}

public function set(array $set, bool|null $bind = null):static{
Expand Down
7 changes: 0 additions & 7 deletions src/Query/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
*/
interface Where{

/**
* @param mixed $val1
* @param mixed $val2
* @param string|null $operator
* @param bool|null $bind
* @param string|null $join
*/
public function where(mixed $val1, mixed $val2 = null, string|null $operator = null, bool|null $bind = null, string|null $join = null):static;

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

0 comments on commit 832d796

Please sign in to comment.