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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Flow\PostgreSql\QueryBuilder\Delete;

use Flow\PostgreSql\Protobuf\AST\{Alias, DeleteStmt, Node, RangeVar, ResTarget};
use Flow\PostgreSql\QueryBuilder\AstToSql;
use Flow\PostgreSql\QueryBuilder\{AstToSql, QualifiedIdentifier};
use Flow\PostgreSql\QueryBuilder\Clause\WithClause;
use Flow\PostgreSql\QueryBuilder\Condition\{Condition, ConditionFactory};
use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException;
Expand Down Expand Up @@ -33,6 +33,7 @@
private function __construct(
private ?WithClause $with = null,
private ?string $table = null,
private ?string $schema = null,
private ?string $alias = null,
private array $using = [],
private ?Condition $where = null,
Expand All @@ -59,6 +60,12 @@ public static function fromAst(DeleteStmt $deleteStmt) : static
throw InvalidAstException::missingRequiredField('relname', 'RangeVar');
}

$schema = $relation->getSchemaname();

if ($schema === '') {
$schema = null;
}

$alias = $relation->getAlias();
$aliasName = $alias !== null ? $alias->getAliasname() : null;

Expand Down Expand Up @@ -125,6 +132,7 @@ public static function fromAst(DeleteStmt $deleteStmt) : static
return new self(
with: $withClause,
table: $tableName,
schema: $schema,
alias: $aliasName,
using: $using,
where: $whereCondition,
Expand All @@ -139,9 +147,12 @@ public static function with(WithClause $with) : DeleteFromStep

public function from(string $table, ?string $alias = null) : DeleteUsingStep
{
$identifier = QualifiedIdentifier::parse($table);

return new self(
with: $this->with,
table: $table,
table: $identifier->name(),
schema: $identifier->schema(),
alias: $alias,
using: $this->using,
where: $this->where,
Expand All @@ -154,6 +165,7 @@ public function returning(Expression ...$expressions) : DeleteFinalStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
using: $this->using,
where: $this->where,
Expand All @@ -166,6 +178,7 @@ public function returningAll() : DeleteFinalStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
using: $this->using,
where: $this->where,
Expand All @@ -186,6 +199,10 @@ public function toAst() : DeleteStmt
'inh' => true,
]);

if ($this->schema !== null) {
$rangeVar->setSchemaname($this->schema);
}

if ($this->alias !== null) {
$alias = new Alias([
'aliasname' => $this->alias,
Expand Down Expand Up @@ -242,6 +259,7 @@ public function using(TableReference ...$tables) : DeleteWhereStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
using: $tables,
where: $this->where,
Expand All @@ -254,6 +272,7 @@ public function where(Condition $condition) : DeleteReturningStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
using: $this->using,
where: $condition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private function __construct(
private ?string $schema = null,
private ?string $tableAlias = null,
private ?string $sourceTable = null,
private ?string $sourceSchema = null,
private ?SelectFinalStep $sourceSelect = null,
private ?string $sourceAlias = null,
private ?Condition $joinCondition = null,
Expand All @@ -52,6 +53,7 @@ public function addWhenClause(MergeWhenClauseData $clause) : MergeWhenStep
$this->schema,
$this->tableAlias,
$this->sourceTable,
$this->sourceSchema,
$this->sourceSelect,
$this->sourceAlias,
$this->joinCondition,
Expand All @@ -69,6 +71,7 @@ public function into(string $table, ?string $alias = null) : MergeUsingStep
$identifier->schema(),
$alias,
$this->sourceTable,
$this->sourceSchema,
$this->sourceSelect,
$this->sourceAlias,
$this->joinCondition,
Expand All @@ -84,6 +87,7 @@ public function on(Condition $condition) : MergeWhenStep
$this->schema,
$this->tableAlias,
$this->sourceTable,
$this->sourceSchema,
$this->sourceSelect,
$this->sourceAlias,
$condition,
Expand Down Expand Up @@ -148,6 +152,11 @@ public function toAst() : MergeStmt
'relname' => $this->sourceTable ?? '',
'inh' => true,
]);

if ($this->sourceSchema !== null) {
$sourceRangeVar->setSchemaname($this->sourceSchema);
}

$alias = new Alias();
$alias->setAliasname($this->sourceAlias);
$sourceRangeVar->setAlias($alias);
Expand Down Expand Up @@ -187,19 +196,23 @@ public function using(string|SelectFinalStep $source, string $alias) : MergeOnSt
$this->schema,
$this->tableAlias,
null,
null,
$source,
$alias,
$this->joinCondition,
$this->whenClauses,
);
}

$identifier = QualifiedIdentifier::parse($source);

return new self(
$this->with,
$this->table,
$this->schema,
$this->tableAlias,
$source,
$identifier->name(),
$identifier->schema(),
null,
$alias,
$this->joinCondition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Flow\PostgreSql\QueryBuilder\Update;

use Flow\PostgreSql\Protobuf\AST\{Alias, Node, RangeVar, ResTarget, UpdateStmt};
use Flow\PostgreSql\QueryBuilder\AstToSql;
use Flow\PostgreSql\QueryBuilder\{AstToSql, QualifiedIdentifier};
use Flow\PostgreSql\QueryBuilder\Clause\WithClause;
use Flow\PostgreSql\QueryBuilder\Condition\{Condition, ConditionFactory};
use Flow\PostgreSql\QueryBuilder\Exception\{InvalidAstException, InvalidExpressionException};
Expand All @@ -27,6 +27,7 @@
private function __construct(
private ?WithClause $with = null,
private ?string $table = null,
private ?string $schema = null,
private ?string $alias = null,
private array $assignments = [],
private array $from = [],
Expand Down Expand Up @@ -62,6 +63,12 @@ public static function fromAst(UpdateStmt $updateStmt) : static
throw InvalidAstException::missingRequiredField('relname', 'RangeVar');
}

$schema = $relation->getSchemaname();

if ($schema === '') {
$schema = null;
}

$alias = null;

if ($relation->hasAlias()) {
Expand Down Expand Up @@ -127,7 +134,7 @@ public static function fromAst(UpdateStmt $updateStmt) : static
}
}

return new self($with, $table, $alias, $assignments, $from, $where, $returning);
return new self($with, $table, $schema, $alias, $assignments, $from, $where, $returning);
}

public static function with(WithClause $with) : UpdateTableStep
Expand All @@ -140,6 +147,7 @@ public function from(TableReference ...$tables) : UpdateWhereStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
assignments: $this->assignments,
from: $tables,
Expand All @@ -153,6 +161,7 @@ public function returning(Expression ...$expressions) : UpdateFinalStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
assignments: $this->assignments,
from: $this->from,
Expand All @@ -171,6 +180,7 @@ public function set(string $column, Expression $value) : UpdateSetStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
assignments: [...$this->assignments, $column => $value],
from: $this->from,
Expand All @@ -184,6 +194,7 @@ public function setAll(array $assignments) : UpdateFromStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
assignments: [...$this->assignments, ...$assignments],
from: $this->from,
Expand All @@ -206,6 +217,10 @@ public function toAst() : UpdateStmt

$rangeVar = new RangeVar(['relname' => $this->table, 'inh' => true]);

if ($this->schema !== null) {
$rangeVar->setSchemaname($this->schema);
}

if ($this->alias !== null) {
$aliasProto = new Alias(['aliasname' => $this->alias]);
$rangeVar->setAlias($aliasProto);
Expand Down Expand Up @@ -271,9 +286,12 @@ public function toAst() : UpdateStmt

public function update(string $table, ?string $alias = null) : UpdateSetStep
{
$identifier = QualifiedIdentifier::parse($table);

return new self(
with: $this->with,
table: $table,
table: $identifier->name(),
schema: $identifier->schema(),
alias: $alias,
assignments: $this->assignments,
from: $this->from,
Expand All @@ -287,6 +305,7 @@ public function where(Condition $condition) : UpdateReturningStep
return new self(
with: $this->with,
table: $this->table,
schema: $this->schema,
alias: $this->alias,
assignments: $this->assignments,
from: $this->from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

final class DeleteDatabaseTest extends DatabaseTestCase
{
private const SCHEMA_NAME = 'flow_postgres_test_delete_schema';

private const SCHEMA_TABLE = 'flow_postgres_schema_logs';

private const TABLE_ARCHIVE = 'flow_postgres_log_archive';

private const TABLE_LOGS = 'flow_postgres_logs';
Expand Down Expand Up @@ -79,6 +83,7 @@ protected function tearDown() : void
{
$this->dropTableIfExists(self::TABLE_ARCHIVE);
$this->dropTableIfExists(self::TABLE_LOGS);
$this->execute('DROP SCHEMA IF EXISTS ' . self::SCHEMA_NAME . ' CASCADE');

parent::tearDown();
}
Expand Down Expand Up @@ -159,6 +164,46 @@ public function test_delete_with_returning_all() : void
self::assertArrayHasKey('created_at', $row);
}

public function test_delete_with_schema_qualified_table() : void
{
$this->execute('CREATE SCHEMA IF NOT EXISTS ' . self::SCHEMA_NAME);

$this->execute(
create()->table(self::SCHEMA_TABLE, self::SCHEMA_NAME)
->column(column('id', data_type_serial()))
->column(column('level', data_type_varchar(20))->notNull())
->column(column('message', data_type_text()))
->constraint(primary_key('id'))
->toSql()
);

$this->execute(
insert()
->into(self::SCHEMA_NAME . '.' . self::SCHEMA_TABLE)
->columns('level', 'message')
->values(literal('DEBUG'), literal('Test message'))
->values(literal('INFO'), literal('Info message'))
->toSql()
);

$query = delete()
->from(self::SCHEMA_NAME . '.' . self::SCHEMA_TABLE)
->where(eq(col('level'), literal('DEBUG')));

$result = $this->execute($query->toSql());

self::assertNotFalse($result);
self::assertSame(1, $this->affectedRows($result));

$check = $this->execute(
select(agg_count(star())->as('cnt'))
->from(table(self::SCHEMA_TABLE, self::SCHEMA_NAME))
->toSql()
);
$row = $this->fetchOne($check);
self::assertSame('1', $row['cnt']);
}

public function test_delete_with_using() : void
{
$query = delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

final class InsertDatabaseTest extends DatabaseTestCase
{
private const SCHEMA_NAME = 'flow_postgres_test_insert_schema';

private const SCHEMA_TABLE = 'flow_postgres_schema_products';

private const TABLE_PRODUCTS = 'flow_postgres_products';

protected function setUp() : void
Expand All @@ -48,6 +52,7 @@ protected function setUp() : void
protected function tearDown() : void
{
$this->dropTableIfExists(self::TABLE_PRODUCTS);
$this->execute('DROP SCHEMA IF EXISTS ' . self::SCHEMA_NAME . ' CASCADE');

parent::tearDown();
}
Expand Down Expand Up @@ -189,4 +194,37 @@ public function test_insert_with_returning_all() : void
self::assertArrayHasKey('price', $row);
self::assertArrayHasKey('stock', $row);
}

public function test_insert_with_schema_qualified_table() : void
{
$this->execute('CREATE SCHEMA IF NOT EXISTS ' . self::SCHEMA_NAME);

$this->execute(
create()->table(self::SCHEMA_TABLE, self::SCHEMA_NAME)
->column(column('id', data_type_serial()))
->column(column('sku', data_type_varchar(50))->notNull())
->column(column('name', data_type_varchar(100))->notNull())
->constraint(primary_key('id'))
->toSql()
);

$query = insert()
->into(self::SCHEMA_NAME . '.' . self::SCHEMA_TABLE)
->columns('sku', 'name')
->values(literal('SCHEMA-SKU'), literal('Schema Product'));

$result = $this->execute($query->toSql());

self::assertNotFalse($result);
self::assertSame(1, $this->affectedRows($result));

$check = $this->execute(
select(col('name'))
->from(table(self::SCHEMA_TABLE, self::SCHEMA_NAME))
->where(eq(col('sku'), literal('SCHEMA-SKU')))
->toSql()
);
$row = $this->fetchOne($check);
self::assertSame('Schema Product', $row['name']);
}
}
Loading
Loading