Skip to content

Commit

Permalink
Merge - With [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Jan 1, 2024
1 parent aaa7ca7 commit 10d8ab9
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Fluent/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,50 @@ public function delete(?string $from = NULL, ?string $alias = NULL): Query
}


/**
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function merge(?string $into = NULL, ?string $alias = NULL): Query
{
return $this->createQuery()->merge($into, $alias);
}


/**
* @param array<string, mixed>|string|Query|Db\Sql $dataSource values, table or query
* @param string|Complex|Db\Sql|NULL $onCondition
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function using($dataSource, ?string $alias = NULL, $onCondition = NULL): Query
{
return $this->createQuery()->using($dataSource, $alias, $onCondition);
}


/**
* @param string|Complex|Db\Sql|NULL $onCondition
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function whenMatched(string $then, $onCondition = NULL): Query
{
return $this->createQuery()->whenMatched($then, $onCondition);
}


/**
* @param string|Complex|Db\Sql|NULL $onCondition
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function whenNotMatched(string $then, $onCondition = NULL): Query
{
return $this->createQuery()->whenNotMatched($then, $onCondition);
}


/**
* @param array<int|string, string|int|Query|Db\Sql> $returning
* @return QueryExecute
Expand Down
64 changes: 64 additions & 0 deletions src/Fluent/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Query implements Sql
public const QUERY_INSERT = 'insert';
public const QUERY_UPDATE = 'update';
public const QUERY_DELETE = 'delete';
public const QUERY_MERGE = 'merge';
public const QUERY_TRUNCATE = 'truncate';

public const PARAM_SELECT = 'select';
Expand All @@ -32,6 +33,7 @@ class Query implements Sql
public const PARAM_RETURNING = 'returning';
public const PARAM_DATA = 'data';
public const PARAM_ROWS = 'rows';
public const PARAM_MERGE = 'merge';
public const PARAM_WITH = 'with';
public const PARAM_PREFIX = 'prefix';
public const PARAM_SUFFIX = 'suffix';
Expand All @@ -51,6 +53,10 @@ class Query implements Sql
private const COMBINE_INTERSECT = 'INTERSECT';
private const COMBINE_EXCEPT = 'EXCEPT';

public const MERGE_USING_DATA_SOURCE = 'using-data-source';
public const MERGE_USING_ALIAS = 'using-alias';
public const MERGE_WHEN = 'when';

public const WITH_QUERIES = 'queries';
public const WITH_QUERIES_SUFFIX = 'queries-suffix';
public const WITH_QUERIES_NOT_MATERIALIZED = 'queries-not-materialized';
Expand Down Expand Up @@ -78,6 +84,11 @@ class Query implements Sql
self::PARAM_RETURNING => [],
self::PARAM_DATA => [],
self::PARAM_ROWS => [],
self::PARAM_MERGE => [
self::MERGE_USING_DATA_SOURCE => NULL,
self::MERGE_USING_ALIAS => [],
self::MERGE_WHEN => [],
],
self::PARAM_WITH => [
self::WITH_QUERIES => [],
self::WITH_QUERIES_SUFFIX => [],
Expand Down Expand Up @@ -642,6 +653,59 @@ public function delete(?string $from = NULL, ?string $alias = NULL): self
}


/**
* @return static
* @throws Exceptions\QueryException
*/
public function merge(?string $into = NULL, ?string $alias = NULL): self
{
$this->resetQuery();

$this->queryType = self::QUERY_DELETE;

if ($into !== NULL) {
$this->table($into, $alias);
}

return $this;
}


/**
* @param array<string, mixed>|string|self|Db\Sql $dataSource values, table or query
* @param string|Complex|Db\Sql|NULL $onCondition
* @return static
* @throws Exceptions\QueryException
*/
public function using($dataSource, ?string $alias = NULL, $onCondition = NULL): self
{
$this->resetQuery();
$this->queryType = self::QUERY_MERGE;
$this->params[self::MERGE_USING_DATA_SOURCE] = $dataSource;
return $this;
}


/**
* @param string|Complex|Db\Sql|NULL $onCondition
* @return static
* @throws Exceptions\QueryException
*/
public function whenMatched(string $then, $onCondition = NULL): self
{
}


/**
* @param string|Complex|Db\Sql|NULL $onCondition
* @return static
* @throws Exceptions\QueryException
*/
public function whenNotMatched(string $then, $onCondition = NULL): self
{
}


/**
* @param array<int|string, string|int|self|Db\Sql> $returning
* @return static
Expand Down
20 changes: 20 additions & 0 deletions src/Fluent/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ function set(array $data): Query;
function delete(?string $from = NULL, ?string $alias = NULL): Query;


function merge(?string $into = NULL, ?string $alias = NULL): Query;


/**
* @param array<string, mixed>|string|Query|Db\Sql $dataSource values, table or query
* @param string|Complex|Db\Sql|NULL $onCondition
*/
function using($dataSource, ?string $alias = NULL, $onCondition = NULL): Query;

/**
* @param string|Complex|Db\Sql|NULL $onCondition
*/
function whenMatched(string $then, $onCondition = NULL): Query;

/**
* @param string|Complex|Db\Sql|NULL $onCondition
*/
function whenNotMatched(string $then, $onCondition = NULL): Query;


/**
* @param array<int|string, string|int|Query|Db\Sql> $returning
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/QueryExecuteFetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@
final class QueryExecuteFetchTest extends TestCase
{

public function testMerge(): void
{
$this->connection->query('
CREATE TABLE characters (
id int not null,
first character varying(50),
last character varying(50));
');
$query = $this->connection->query('
merge into characters
using (values(?::integer, ?, ?)) as incoming(id,first,last)
on characters.id = incoming.id
when not matched then
insert (id, first, last) values (incoming.id, incoming.first, incoming.last)
when matched then
update set first = incoming.first, last = incoming.last;
', 4, 'darth', 'vader');

Tester\Assert::same(1, $query->getAffectedRows());

$query->free();
}


public function testFetch(): void
{
$this->connection->query('
Expand Down

0 comments on commit 10d8ab9

Please sign in to comment.