Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/remove backtick query builder #407

Merged
merged 5 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/System/Database/MyModel/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function setter(string $key, $value): self
public function getter(string $key, $default = null)
{
if (array_key_exists($key, $this->stash)) {
throw new \Exception("Cant read this colum `{$key}`");
throw new \Exception("Cant read this column `{$key}`.");
}

return $this->first()[$key] ?? $default;
Expand Down Expand Up @@ -413,7 +413,7 @@ public function isClean(?string $column = null): bool
foreach (array_keys($this->columns) as $key) {
if (!array_key_exists($column, $this->columns[$key])
|| !array_key_exists($column, $this->fresh[$key])) {
throw new \Exception("Column `{$column}` is not in table `{$this->table_name}`");
throw new \Exception("Column {$column} is not in table `{$this->table_name}`.");
}

if (false === ($this->columns[$key][$column] === $this->fresh[$key][$column])) {
Expand Down Expand Up @@ -550,7 +550,7 @@ public function order(string $column_name, int $order_using = MyQuery::ORDER_ASC
{
$order = 0 === $order_using ? 'ASC' : 'DESC';
$belong_to ??= $this->table_name;
$this->sort_order = "ORDER BY `$belong_to`.`$column_name` $order";
$this->sort_order = "ORDER BY {$belong_to}.{$column_name} {$order}";

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System/Database/MyQuery/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function builder(): string
{
$where = $this->getWhere();

$this->_query = "DELETE FROM `$this->_table` $where";
$this->_query = "DELETE FROM {$this->_table} {$where}";

return $this->_query;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System/Database/MyQuery/InnerQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function isSubQuery(): bool

public function getAlias(): string
{
return "`{$this->table}`";
return $this->table;
}

/** @return Bind[] */
Expand Down
2 changes: 1 addition & 1 deletion src/System/Database/MyQuery/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function builder(): string
$stringBinds = implode(', ', $strings_binds);
$stringColumn = implode(', ', $columns);

$this->_query = "INSERT INTO `$this->_table` ($stringColumn) VALUES $stringBinds";
$this->_query = "INSERT INTO {$this->_table} ({$stringColumn}) VALUES {$stringBinds}";

return $this->_query;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System/Database/MyQuery/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected function splitGrupsFilters(array $group_filters): string
protected function splitFilters(array $filters): string
{
$query = [];
$table_name = null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
$table_name = null === $this->_sub_query ? $this->_table : $this->_sub_query->getAlias();
foreach ($filters['filters'] as $fieldName => $fieldValue) {
$value = $fieldValue['value'];
$comparation = $fieldValue['comparation'];
Expand Down
11 changes: 3 additions & 8 deletions src/System/Database/MyQuery/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ public function __construct($table_name, array $columns_name, MyPDO $PDO, ?array
$this->_binds = $table_name->getBind();
}

// defaul query
if (count($this->_column) > 1) {
$this->_column = array_map(fn ($e) => "`$e`", $this->_column);
}

$column = implode(', ', $columns_name);
$this->_query = $options['query'] ?? "SELECT {$column} FROM `{ $this->_sub_query}`";
$this->_query = $options['query'] ?? "SELECT {$column} FROM { $this->_sub_query}";
}

public function __toString()
Expand Down Expand Up @@ -171,8 +166,8 @@ public function limitOffset(int $limit, int $offset): self
public function order(string $column_name, int $order_using = MyQuery::ORDER_ASC, ?string $belong_to = null)
{
$order = 0 === $order_using ? 'ASC' : 'DESC';
$belong_to ??= null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
$this->_sort_order = "ORDER BY $belong_to.`$column_name` $order";
$belong_to ??= null === $this->_sub_query ? "{$this->_table}" : $this->_sub_query->getAlias();
$this->_sort_order = "ORDER BY $belong_to.$column_name $order";

return $this;
}
Expand Down
14 changes: 7 additions & 7 deletions src/System/Database/MyQuery/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public function info(): array
{
$this->PDO->query(
'SELECT
`COLUMN_NAME`,
`COLUMN_TYPE`,
`CHARACTER_SET_NAME`,
`COLLATION_NAME`,
`IS_NULLABLE`,
`ORDINAL_POSITION`,
`COLUMN_KEY`
COLUMN_NAME,
COLUMN_TYPE,
CHARACTER_SET_NAME,
COLLATION_NAME,
IS_NULLABLE,
ORDINAL_POSITION,
COLUMN_KEY
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
Expand Down
8 changes: 4 additions & 4 deletions src/System/Database/MyQuery/Traits/ConditionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public function where(string $where_condition, array $binder = [])
*/
public function between(string $column_name, $value_1, $value_2)
{
$table_name = null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
$table_name = null === $this->_sub_query ? $this->_table : $this->_sub_query->getAlias();

$this->where(
"({$table_name}.`{$column_name}` BETWEEN :b_start AND :b_end)",
"({$table_name}.{$column_name} BETWEEN :b_start AND :b_end)",
[]
);

Expand All @@ -105,10 +105,10 @@ public function in(string $column_name, $value)
$binder[] = [":in_$key", $bind];
}
$bindString = implode(', ', $binds);
$table_name = null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
$table_name = null === $this->_sub_query ? "{$this->_table}" : $this->_sub_query->getAlias();

$this->where(
"({$table_name}.`{$column_name}` IN ({$bindString}))",
"({$table_name}.{$column_name} IN ({$bindString}))",
$binder
);

Expand Down
4 changes: 2 additions & 2 deletions src/System/Database/MyQuery/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ protected function builder(): string
$setter = [];
foreach ($this->_binds as $bind) {
if ($bind->hasColumName()) {
$setter[] = '`' . $bind->getColumnName() . '` = ' . $bind->getBind();
$setter[] = $bind->getColumnName() . ' = ' . $bind->getBind();
}
}

// $binds = array_filter($setter);
$set_string = implode(', ', $setter);

$this->_query = "UPDATE `$this->_table` SET $set_string $where";
$this->_query = "UPDATE $this->_table SET $set_string $where";

return $this->_query;
}
Expand Down
4 changes: 2 additions & 2 deletions src/System/Database/MySchema/Table/Alter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function getRename(): array
$res = [];

foreach ($this->rename_columns as $old => $new) {
$res[] = "RENAME COLUMN `{$old}` TO `{$new}`";
$res[] = "RENAME COLUMN {$old} TO {$new}";
}

return $res;
Expand All @@ -112,7 +112,7 @@ private function getDrops(): array
$res = [];

foreach ($this->drop_columns as $drop) {
$res[] = "DROP COLUMN `{$drop}`";
$res[] = "DROP COLUMN {$drop}";
}

return $res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Constraint extends AttributesConstraint
{
public function after(string $column): self
{
$this->order = "AFTER `{$column}`";
$this->order = "AFTER {$column}";

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __toString()

private function query(): string
{
return '`' . $this->name . '` ' . $this->datatype;
return $this->name . ' ' . $this->datatype;
}

// number
Expand Down Expand Up @@ -141,7 +141,7 @@ public function enum(array $enums): Constraint

public function after(string $column): void
{
$this->datatype = "AFTER `{$column}`";
$this->datatype = "AFTER {$column}";
}

public function first(): void
Expand Down
2 changes: 1 addition & 1 deletion src/System/Database/MySchema/Table/Attributes/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __toString()

private function query(): string
{
return '`' . $this->name . '` ' . $this->datatype;
return $this->name . ' ' . $this->datatype;
}

// number
Expand Down
5 changes: 2 additions & 3 deletions src/System/Database/MySchema/Table/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function getPrimarykey(): array
return [''];
}

$primaryKeys = array_map(fn ($primaryKey) => "`{$primaryKey}`", $this->primaryKeys);
$primaryKeys = array_map(fn ($primaryKey) => $primaryKey, $this->primaryKeys);
$primaryKeys = implode(', ', $primaryKeys);

return ["PRIMARY KEY ({$primaryKeys})"];
Expand All @@ -139,8 +139,7 @@ private function getUnique(): array
return [''];
}

$uniques = array_map(fn ($uniques) => "`{$uniques}`", $this->uniques);
$uniques = implode(', ', $uniques);
$uniques = implode(', ', $this->uniques);

return ["UNIQUE ({$uniques})"];
}
Expand Down
10 changes: 5 additions & 5 deletions tests/DataBase/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ protected function createUserSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `users` (
`user` varchar(32) NOT NULL,
`password` varchar(500) NOT NULL,
`stat` int(2) NOT NULL,
PRIMARY KEY (`user`)
->query('CREATE TABLE users (
user varchar(32) NOT NULL,
password varchar(500) NOT NULL,
stat int(2) NOT NULL,
PRIMARY KEY (user)
)')
->execute();
}
Expand Down
22 changes: 11 additions & 11 deletions tests/DataBase/Model/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ private function createProfileSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `profiles` (
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`gender` varchar(10) NOT NULL,
PRIMARY KEY (`user`)
->query('CREATE TABLE profiles (
user varchar(32) NOT NULL,
name varchar(100) NOT NULL,
gender varchar(10) NOT NULL,
PRIMARY KEY (user)
)')
->execute();
}
Expand All @@ -63,12 +63,12 @@ private function createOrderSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `orders` (
`id` varchar(3) NOT NULL,
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`type` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
->query('CREATE TABLE orders (
id varchar(3) NOT NULL,
user varchar(32) NOT NULL,
name varchar(100) NOT NULL,
type varchar(30) NOT NULL,
PRIMARY KEY (id)
)')
->execute();
}
Expand Down
22 changes: 11 additions & 11 deletions tests/DataBase/Model/BaseMultyModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ private function createProfileSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `profiles` (
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`gender` varchar(10) NOT NULL,
PRIMARY KEY (`user`)
->query('CREATE TABLE profiles (
user varchar(32) NOT NULL,
name varchar(100) NOT NULL,
gender varchar(10) NOT NULL,
PRIMARY KEY (user)
)')
->execute();
}
Expand All @@ -75,12 +75,12 @@ private function createOrderSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `orders` (
`id` varchar(3) NOT NULL,
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`type` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
->query('CREATE TABLE orders (
id varchar(3) NOT NULL,
user varchar(32) NOT NULL,
name varchar(100) NOT NULL,
type varchar(30) NOT NULL,
PRIMARY KEY (id)
)')
->execute();
}
Expand Down
12 changes: 6 additions & 6 deletions tests/DataBase/Model/CostumeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ private function createProfileSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `profiles` (
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(3) NOT NULL,
PRIMARY KEY (`user`)
->query('CREATE TABLE profiles (
user varchar(32) NOT NULL,
name varchar(100) NOT NULL,
gender varchar(10) NOT NULL,
age int(3) NOT NULL,
PRIMARY KEY (user)
)')
->execute();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/DataBase/MySchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function itCanUpdateDatabaseTable()
public function itCanExecuteUsingRawQuery()
{
$schema = new MySchema($this->pdo_schema);
$raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN `user` varchar(20), ADD `status` int(3), DROP COLUMN `stat`');
$raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN user varchar(20), ADD status int(3), DROP COLUMN stat');

$this->assertTrue($raw->execute());
}
Expand Down
Loading
Loading