Skip to content
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
8 changes: 8 additions & 0 deletions packages/database/src/Config/DatabaseDialect.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public function tableNotFoundCode(): string
};
}

public function quoteIdentifier(string $identifier): string
{
return match ($this) {
self::MYSQL, self::SQLITE => sprintf('`%s`', $identifier),
self::POSTGRESQL => sprintf('"%s"', $identifier),
};
}

public function isTableNotFoundError(QueryWasInvalid $queryWasInvalid): bool
{
$pdoException = $queryWasInvalid->pdoException;
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function compile(): ImmutableString
}

if ($dialect === DatabaseDialect::POSTGRESQL) {
$sql = str_replace('`', '', $sql);
$sql = str_replace('`', '"', $sql);
}

return new ImmutableString($sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Tempest\Database\QueryStatements;

use Tempest\Database\Builder\TableDefinition;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\HasTrailingStatements;
use Tempest\Database\QueryStatement;
Expand Down Expand Up @@ -92,7 +91,7 @@ public function compile(DatabaseDialect $dialect): string
if ($this->statements !== []) {
return sprintf(
'ALTER TABLE %s %s;',
new TableDefinition($this->tableName),
$dialect->quoteIdentifier($this->tableName),
arr($this->statements)
->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' '))
->filter(fn (ImmutableString $line) => $line->isNotEmpty())
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/QueryStatements/BooleanStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function compile(DatabaseDialect $dialect): string
}

return sprintf(
'`%s` BOOLEAN %s %s',
$this->name,
'%s BOOLEAN %s %s',
$dialect->quoteIdentifier($this->name),
$default !== null ? "DEFAULT {$default}" : '',
$this->nullable ? '' : 'NOT NULL',
);
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/QueryStatements/CharStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function __construct(
public function compile(DatabaseDialect $dialect): string
{
return sprintf(
'`%s` CHAR %s %s',
$this->name,
'%s CHAR %s %s',
$dialect->quoteIdentifier($this->name),
$this->default !== null ? "DEFAULT '{$this->default}'" : '',
$this->nullable ? '' : 'NOT NULL',
);
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/QueryStatements/CountStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function compile(DatabaseDialect $dialect): string

$query = arr([
sprintf('SELECT %s', $countField->compile($dialect)),
sprintf('FROM `%s`', $this->table->name),
sprintf('FROM %s', $dialect->quoteIdentifier($this->table->name)),
]);

if ($this->joins->isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Tempest\Database\QueryStatements;

use BackedEnum;
use Tempest\Database\Builder\TableDefinition;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Enums\DatabaseTextLength;
use Tempest\Database\HasTrailingStatements;
Expand Down Expand Up @@ -400,7 +399,7 @@ public function compile(DatabaseDialect $dialect): string
{
return sprintf(
'CREATE TABLE %s (%s);',
new TableDefinition($this->tableName),
$dialect->quoteIdentifier($this->tableName),
arr($this->statements)
// Remove BelongsTo for sqlLite as it does not support those queries
->filter(fn (QueryStatement $queryStatement) => ! ($dialect === DatabaseDialect::SQLITE && $queryStatement instanceof BelongsToStatement))
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/QueryStatements/DateStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function __construct(
public function compile(DatabaseDialect $dialect): string
{
return sprintf(
'`%s` DATE %s %s',
$this->name,
'%s DATE %s %s',
$dialect->quoteIdentifier($this->name),
$this->default !== null ? "DEFAULT '{$this->default}'" : '',
$this->nullable ? '' : 'NOT NULL',
);
Expand Down
10 changes: 6 additions & 4 deletions packages/database/src/QueryStatements/DatetimeStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ public function compile(DatabaseDialect $dialect): string
default => null,
};

$name = $dialect->quoteIdentifier($this->name);

return match ($dialect) {
DatabaseDialect::POSTGRESQL => sprintf(
'`%s` TIMESTAMP %s %s',
$this->name,
'%s TIMESTAMP %s %s',
$name,
$default !== null ? "DEFAULT {$default}" : '',
$this->nullable ? '' : 'NOT NULL',
),
default => sprintf(
'`%s` DATETIME %s %s',
$this->name,
'%s DATETIME %s %s',
$name,
$default !== null ? "DEFAULT {$default}" : '',
$this->nullable ? '' : 'NOT NULL',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function compile(DatabaseDialect $dialect): string
}

$query = arr([
sprintf('DELETE FROM `%s`', $this->table->name),
sprintf('DELETE FROM %s', $dialect->quoteIdentifier($this->table->name)),
]);

if ($this->where->isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function compile(DatabaseDialect $dialect): string

return match ($dialect) {
DatabaseDialect::MYSQL => sprintf(
'ALTER TABLE `%s` DROP CONSTRAINT %s',
$foreignTable,
'ALTER TABLE %s DROP CONSTRAINT %s',
$dialect->quoteIdentifier($foreignTable),
$constraintName,
),
default => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static function forModel(string $modelClass): self
public function compile(DatabaseDialect $dialect): string
{
return match ($dialect) {
DatabaseDialect::POSTGRESQL => sprintf('DROP TABLE IF EXISTS `%s` CASCADE', $this->tableName),
default => sprintf('DROP TABLE IF EXISTS `%s`', $this->tableName),
DatabaseDialect::POSTGRESQL => sprintf('DROP TABLE IF EXISTS %s CASCADE', $dialect->quoteIdentifier($this->tableName)),
default => sprintf('DROP TABLE IF EXISTS %s', $dialect->quoteIdentifier($this->tableName)),
};
}
}
14 changes: 8 additions & 6 deletions packages/database/src/QueryStatements/EnumStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,25 @@ public function compile(DatabaseDialect $dialect): string
$defaultValue = null;
}

$name = $dialect->quoteIdentifier($this->name);

return match ($dialect) {
DatabaseDialect::MYSQL => sprintf(
'`%s` ENUM(%s) %s %s',
$this->name,
'%s ENUM(%s) %s %s',
$name,
$cases->implode(', '),
$defaultValue !== null ? "DEFAULT '{$defaultValue}'" : '',
$this->nullable ? '' : 'NOT NULL',
),
DatabaseDialect::SQLITE => sprintf(
'`%s` TEXT %s %s',
$this->name,
'%s TEXT %s %s',
$name,
$defaultValue !== null ? "DEFAULT '{$defaultValue}'" : '',
$this->nullable ? '' : 'NOT NULL',
),
DatabaseDialect::POSTGRESQL => sprintf(
'"%s" "%s" %s %s',
$this->name,
'%s "%s" %s %s',
$name,
str($this->enumClass)->replace('\\\\', '_'),
$defaultValue !== null ? "DEFAULT ('{$defaultValue}')" : '',
$this->nullable ? '' : 'NOT NULL',
Expand Down
24 changes: 6 additions & 18 deletions packages/database/src/QueryStatements/FieldStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,18 @@ public function compile(DatabaseDialect $dialect): string
$aliasPrefix = $this->aliasPrefix ? "{$this->aliasPrefix}." : '';

if ($this->alias === true) {
$alias = sprintf(
'`%s%s`',
$aliasPrefix,
str_replace('`', '', $field),
);
$alias = $aliasPrefix . str_replace('`', '', $field);
} elseif ($this->alias) {
$alias = sprintf(
'`%s%s`',
$aliasPrefix,
$this->alias,
);
$alias = $aliasPrefix . $this->alias;
}
} else {
$alias = $parts[1];
$alias = trim($parts[1], '`" ');
}

$field = arr(explode('.', $field))
->map(fn (string $part) => trim($part, '` '))
->map(fn (string $part) => trim($part, '`" '))
->map(
function (string $part) use ($dialect) {
// Function calls are never wrapped in backticks.
if (str_contains($part, '(')) {
return $part;
}
Expand All @@ -58,7 +49,7 @@ function (string $part) use ($dialect) {
return $part;
}

return sprintf('`%s`', $part);
return $dialect->quoteIdentifier($part);
},
)
->implode('.');
Expand All @@ -67,10 +58,7 @@ function (string $part) use ($dialect) {
return $field;
}

return match ($dialect) {
DatabaseDialect::POSTGRESQL => sprintf('%s AS "%s"', $field, trim($alias, '`')),
default => sprintf('%s AS `%s`', $field, trim($alias, '`')),
};
return sprintf('%s AS %s', $field, $dialect->quoteIdentifier($alias));
}

public function withAliasPrefix(?string $prefix = null): self
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/QueryStatements/FloatStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function __construct(
public function compile(DatabaseDialect $dialect): string
{
return sprintf(
'`%s` FLOAT %s %s',
$this->name,
'%s FLOAT %s %s',
$dialect->quoteIdentifier($this->name),
$this->default !== null ? "DEFAULT {$this->default}" : '',
$this->nullable ? '' : 'NOT NULL',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function __construct(

public function compile(DatabaseDialect $dialect): string
{
return sprintf('`%s`', $this->name);
return $dialect->quoteIdentifier($this->name);
}
}
11 changes: 7 additions & 4 deletions packages/database/src/QueryStatements/IndexStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public function __construct(

public function compile(DatabaseDialect $dialect): string
{
$columns = arr($this->columns)->implode('`, `')->wrap('`', '`');
$columns = arr($this->columns)
->map($dialect->quoteIdentifier(...))
->implode(', ');

$indexName = str($this->tableName . ' ' . $columns->replace(',', '')->snake())->snake()->toString();
$rawColumns = arr($this->columns)->implode('_');
$indexName = str($this->tableName . '_' . $rawColumns)->snake()->toString();

$on = sprintf('`%s` (%s)', $this->tableName, $columns);
$on = sprintf('%s (%s)', $dialect->quoteIdentifier($this->tableName), $columns);

return sprintf('CREATE INDEX `%s` ON %s', $indexName, $on);
return sprintf('CREATE INDEX %s ON %s', $dialect->quoteIdentifier($indexName), $on);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function compile(DatabaseDialect $dialect): string
$sql = sprintf(
'INSERT INTO %s (%s) VALUES %s',
$this->table,
$columns->map(fn (string $column) => "`{$column}`")->implode(', '),
$columns->map($dialect->quoteIdentifier(...))->implode(', '),
$entryPlaceholders,
);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/database/src/QueryStatements/IntegerStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ public function __construct(

public function compile(DatabaseDialect $dialect): string
{
$name = $dialect->quoteIdentifier($this->name);

return match ($dialect) {
DatabaseDialect::SQLITE => sprintf(
'`%s` INTEGER %s %s %s',
$this->name,
'%s INTEGER %s %s %s',
$name,
$this->unsigned ? 'UNSIGNED' : '',
$this->default !== null ? "DEFAULT {$this->default}" : '',
$this->nullable ? '' : 'NOT NULL',
),
default => sprintf(
'`%s` %s %s %s %s',
$this->name,
'%s %s %s %s %s',
$name,
is_int($this->size) ? DatabaseIntegerSize::fromBytes($this->size)->toString() : $this->size->toString(),
$this->unsigned ? 'UNSIGNED' : '',
$this->default !== null ? "DEFAULT {$this->default}" : '',
Expand Down
14 changes: 8 additions & 6 deletions packages/database/src/QueryStatements/JsonStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ public function compile(DatabaseDialect $dialect): string
throw new DefaultValueWasInvalid($this->name, $this->default);
}

$name = $dialect->quoteIdentifier($this->name);

return match ($dialect) {
DatabaseDialect::MYSQL => sprintf(
'`%s` JSON %s',
$this->name,
'%s JSON %s',
$name,
$this->nullable ? '' : 'NOT NULL',
),
DatabaseDialect::SQLITE => sprintf(
'`%s` TEXT %s %s',
$this->name,
'%s TEXT %s %s',
$name,
$this->default !== null ? "DEFAULT '{$this->default}'" : '',
$this->nullable ? '' : 'NOT NULL',
),
DatabaseDialect::POSTGRESQL => sprintf(
'`%s` JSONB %s %s',
$this->name,
'%s JSONB %s %s',
$name,
$this->default !== null ? "DEFAULT ('{$this->default}')" : '',
$this->nullable ? '' : 'NOT NULL',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public function __construct(

public function compile(DatabaseDialect $dialect): string
{
$name = $dialect->quoteIdentifier($this->name);

return match ($dialect) {
DatabaseDialect::MYSQL => sprintf('`%s` INTEGER PRIMARY KEY AUTO_INCREMENT', $this->name),
DatabaseDialect::POSTGRESQL => sprintf('`%s` SERIAL PRIMARY KEY', $this->name),
DatabaseDialect::SQLITE => sprintf('`%s` INTEGER PRIMARY KEY AUTOINCREMENT', $this->name),
DatabaseDialect::MYSQL => "{$name} INTEGER PRIMARY KEY AUTO_INCREMENT",
DatabaseDialect::POSTGRESQL => "{$name} SERIAL PRIMARY KEY",
DatabaseDialect::SQLITE => "{$name} INTEGER PRIMARY KEY AUTOINCREMENT",
};
}
}
4 changes: 2 additions & 2 deletions packages/database/src/QueryStatements/SetStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function compile(DatabaseDialect $dialect): string

return match ($dialect) {
DatabaseDialect::MYSQL => sprintf(
'`%s` SET (%s) %s %s',
$this->name,
'%s SET (%s) %s %s',
$dialect->quoteIdentifier($this->name),
"'" . implode("', '", $this->values) . "'",
$this->default ? "DEFAULT '{$this->default}'" : '',
$this->nullable ? '' : 'NOT NULL',
Expand Down
10 changes: 6 additions & 4 deletions packages/database/src/QueryStatements/TextStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ public function __construct(

public function compile(DatabaseDialect $dialect): string
{
$name = $dialect->quoteIdentifier($this->name);

return match ($dialect) {
DatabaseDialect::MYSQL => sprintf(
'`%s` %s %s',
$this->name,
'%s %s %s',
$name,
$this->getSQLTypeDeclaration($this->length),
$this->nullable ? '' : 'NOT NULL',
),
default => sprintf(
'`%s` TEXT %s %s',
$this->name,
'%s TEXT %s %s',
$name,
$this->default !== null ? "DEFAULT '{$this->default}'" : '',
$this->nullable ? '' : 'NOT NULL',
),
Expand Down
Loading
Loading