Skip to content

Commit 1508483

Browse files
Refactor/remove backtick query builder (#407)
* refactor: remove backticks from column names in SQL queries for consistency * refactor: remove backticks from column names in database schema for consistency * refactor: improve string formatting and remove unnecessary backticks in query classes * refactor: use string interpolation * refactor: use string interpolation --------- Co-authored-by: anggerpradana <75710827+anggerpradana@users.noreply.github.com>
1 parent e8f833a commit 1508483

35 files changed

+204
-210
lines changed

src/System/Database/MyModel/Model.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function setter(string $key, $value): self
180180
public function getter(string $key, $default = null)
181181
{
182182
if (array_key_exists($key, $this->stash)) {
183-
throw new \Exception("Cant read this colum `{$key}`");
183+
throw new \Exception("Cant read this column `{$key}`.");
184184
}
185185

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

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

555555
return $this;
556556
}

src/System/Database/MyQuery/Delete.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function builder(): string
2828
{
2929
$where = $this->getWhere();
3030

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

3333
return $this->_query;
3434
}

src/System/Database/MyQuery/InnerQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function isSubQuery(): bool
1717

1818
public function getAlias(): string
1919
{
20-
return "`{$this->table}`";
20+
return $this->table;
2121
}
2222

2323
/** @return Bind[] */

src/System/Database/MyQuery/Insert.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function builder(): string
7777
$stringBinds = implode(', ', $strings_binds);
7878
$stringColumn = implode(', ', $columns);
7979

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

8282
return $this->_query;
8383
}

src/System/Database/MyQuery/Query.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ protected function splitGrupsFilters(array $group_filters): string
177177
protected function splitFilters(array $filters): string
178178
{
179179
$query = [];
180-
$table_name = null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
180+
$table_name = null === $this->_sub_query ? $this->_table : $this->_sub_query->getAlias();
181181
foreach ($filters['filters'] as $fieldName => $fieldValue) {
182182
$value = $fieldValue['value'];
183183
$comparation = $fieldValue['comparation'];

src/System/Database/MyQuery/Select.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ public function __construct($table_name, array $columns_name, MyPDO $PDO, ?array
3434
$this->_binds = $table_name->getBind();
3535
}
3636

37-
// defaul query
38-
if (count($this->_column) > 1) {
39-
$this->_column = array_map(fn ($e) => "`$e`", $this->_column);
40-
}
41-
4237
$column = implode(', ', $columns_name);
43-
$this->_query = $options['query'] ?? "SELECT {$column} FROM `{ $this->_sub_query}`";
38+
$this->_query = $options['query'] ?? "SELECT {$column} FROM { $this->_sub_query}";
4439
}
4540

4641
public function __toString()
@@ -171,8 +166,8 @@ public function limitOffset(int $limit, int $offset): self
171166
public function order(string $column_name, int $order_using = MyQuery::ORDER_ASC, ?string $belong_to = null)
172167
{
173168
$order = 0 === $order_using ? 'ASC' : 'DESC';
174-
$belong_to ??= null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
175-
$this->_sort_order = "ORDER BY $belong_to.`$column_name` $order";
169+
$belong_to ??= null === $this->_sub_query ? "{$this->_table}" : $this->_sub_query->getAlias();
170+
$this->_sort_order = "ORDER BY $belong_to.$column_name $order";
176171

177172
return $this;
178173
}

src/System/Database/MyQuery/Table.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public function info(): array
8282
{
8383
$this->PDO->query(
8484
'SELECT
85-
`COLUMN_NAME`,
86-
`COLUMN_TYPE`,
87-
`CHARACTER_SET_NAME`,
88-
`COLLATION_NAME`,
89-
`IS_NULLABLE`,
90-
`ORDINAL_POSITION`,
91-
`COLUMN_KEY`
85+
COLUMN_NAME,
86+
COLUMN_TYPE,
87+
CHARACTER_SET_NAME,
88+
COLLATION_NAME,
89+
IS_NULLABLE,
90+
ORDINAL_POSITION,
91+
COLUMN_KEY
9292
FROM
9393
INFORMATION_SCHEMA.COLUMNS
9494
WHERE

src/System/Database/MyQuery/Traits/ConditionTrait.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public function where(string $where_condition, array $binder = [])
7575
*/
7676
public function between(string $column_name, $value_1, $value_2)
7777
{
78-
$table_name = null === $this->_sub_query ? "`{$this->_table}`" : $this->_sub_query->getAlias();
78+
$table_name = null === $this->_sub_query ? $this->_table : $this->_sub_query->getAlias();
7979

8080
$this->where(
81-
"({$table_name}.`{$column_name}` BETWEEN :b_start AND :b_end)",
81+
"({$table_name}.{$column_name} BETWEEN :b_start AND :b_end)",
8282
[]
8383
);
8484

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

110110
$this->where(
111-
"({$table_name}.`{$column_name}` IN ({$bindString}))",
111+
"({$table_name}.{$column_name} IN ({$bindString}))",
112112
$binder
113113
);
114114

src/System/Database/MyQuery/Update.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ protected function builder(): string
6262
$setter = [];
6363
foreach ($this->_binds as $bind) {
6464
if ($bind->hasColumName()) {
65-
$setter[] = '`' . $bind->getColumnName() . '` = ' . $bind->getBind();
65+
$setter[] = $bind->getColumnName() . ' = ' . $bind->getBind();
6666
}
6767
}
6868

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

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

7474
return $this->_query;
7575
}

src/System/Database/MySchema/Table/Alter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private function getRename(): array
8888
$res = [];
8989

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

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

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

118118
return $res;

src/System/Database/MySchema/Table/Attributes/Alter/Constraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Constraint extends AttributesConstraint
1010
{
1111
public function after(string $column): self
1212
{
13-
$this->order = "AFTER `{$column}`";
13+
$this->order = "AFTER {$column}";
1414

1515
return $this;
1616
}

src/System/Database/MySchema/Table/Attributes/Alter/DataType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __toString()
2424

2525
private function query(): string
2626
{
27-
return '`' . $this->name . '` ' . $this->datatype;
27+
return $this->name . ' ' . $this->datatype;
2828
}
2929

3030
// number
@@ -141,7 +141,7 @@ public function enum(array $enums): Constraint
141141

142142
public function after(string $column): void
143143
{
144-
$this->datatype = "AFTER `{$column}`";
144+
$this->datatype = "AFTER {$column}";
145145
}
146146

147147
public function first(): void

src/System/Database/MySchema/Table/Attributes/DataType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __toString()
2424

2525
private function query(): string
2626
{
27-
return '`' . $this->name . '` ' . $this->datatype;
27+
return $this->name . ' ' . $this->datatype;
2828
}
2929

3030
// number

src/System/Database/MySchema/Table/Create.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function getPrimarykey(): array
126126
return [''];
127127
}
128128

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

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

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

145144
return ["UNIQUE ({$uniques})"];
146145
}

tests/DataBase/BaseConnection.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ protected function createUserSchema(): bool
4343
{
4444
return $this
4545
->pdo
46-
->query('CREATE TABLE `users` (
47-
`user` varchar(32) NOT NULL,
48-
`password` varchar(500) NOT NULL,
49-
`stat` int(2) NOT NULL,
50-
PRIMARY KEY (`user`)
46+
->query('CREATE TABLE users (
47+
user varchar(32) NOT NULL,
48+
password varchar(500) NOT NULL,
49+
stat int(2) NOT NULL,
50+
PRIMARY KEY (user)
5151
)')
5252
->execute();
5353
}

tests/DataBase/Model/BaseModelTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ private function createProfileSchema(): bool
4343
{
4444
return $this
4545
->pdo
46-
->query('CREATE TABLE `profiles` (
47-
`user` varchar(32) NOT NULL,
48-
`name` varchar(100) NOT NULL,
49-
`gender` varchar(10) NOT NULL,
50-
PRIMARY KEY (`user`)
46+
->query('CREATE TABLE profiles (
47+
user varchar(32) NOT NULL,
48+
name varchar(100) NOT NULL,
49+
gender varchar(10) NOT NULL,
50+
PRIMARY KEY (user)
5151
)')
5252
->execute();
5353
}
@@ -63,12 +63,12 @@ private function createOrderSchema(): bool
6363
{
6464
return $this
6565
->pdo
66-
->query('CREATE TABLE `orders` (
67-
`id` varchar(3) NOT NULL,
68-
`user` varchar(32) NOT NULL,
69-
`name` varchar(100) NOT NULL,
70-
`type` varchar(30) NOT NULL,
71-
PRIMARY KEY (`id`)
66+
->query('CREATE TABLE orders (
67+
id varchar(3) NOT NULL,
68+
user varchar(32) NOT NULL,
69+
name varchar(100) NOT NULL,
70+
type varchar(30) NOT NULL,
71+
PRIMARY KEY (id)
7272
)')
7373
->execute();
7474
}

tests/DataBase/Model/BaseMultyModelTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ private function createProfileSchema(): bool
5555
{
5656
return $this
5757
->pdo
58-
->query('CREATE TABLE `profiles` (
59-
`user` varchar(32) NOT NULL,
60-
`name` varchar(100) NOT NULL,
61-
`gender` varchar(10) NOT NULL,
62-
PRIMARY KEY (`user`)
58+
->query('CREATE TABLE profiles (
59+
user varchar(32) NOT NULL,
60+
name varchar(100) NOT NULL,
61+
gender varchar(10) NOT NULL,
62+
PRIMARY KEY (user)
6363
)')
6464
->execute();
6565
}
@@ -75,12 +75,12 @@ private function createOrderSchema(): bool
7575
{
7676
return $this
7777
->pdo
78-
->query('CREATE TABLE `orders` (
79-
`id` varchar(3) NOT NULL,
80-
`user` varchar(32) NOT NULL,
81-
`name` varchar(100) NOT NULL,
82-
`type` varchar(30) NOT NULL,
83-
PRIMARY KEY (`id`)
78+
->query('CREATE TABLE orders (
79+
id varchar(3) NOT NULL,
80+
user varchar(32) NOT NULL,
81+
name varchar(100) NOT NULL,
82+
type varchar(30) NOT NULL,
83+
PRIMARY KEY (id)
8484
)')
8585
->execute();
8686
}

tests/DataBase/Model/CostumeModelTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ private function createProfileSchema(): bool
5454
{
5555
return $this
5656
->pdo
57-
->query('CREATE TABLE `profiles` (
58-
`user` varchar(32) NOT NULL,
59-
`name` varchar(100) NOT NULL,
60-
`gender` varchar(10) NOT NULL,
61-
`age` int(3) NOT NULL,
62-
PRIMARY KEY (`user`)
57+
->query('CREATE TABLE profiles (
58+
user varchar(32) NOT NULL,
59+
name varchar(100) NOT NULL,
60+
gender varchar(10) NOT NULL,
61+
age int(3) NOT NULL,
62+
PRIMARY KEY (user)
6363
)')
6464
->execute();
6565
}

tests/DataBase/MySchemaTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function itCanUpdateDatabaseTable()
3535
public function itCanExecuteUsingRawQuery()
3636
{
3737
$schema = new MySchema($this->pdo_schema);
38-
$raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN `user` varchar(20), ADD `status` int(3), DROP COLUMN `stat`');
38+
$raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN user varchar(20), ADD status int(3), DROP COLUMN stat');
3939

4040
$this->assertTrue($raw->execute());
4141
}

0 commit comments

Comments
 (0)