Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Jan 22, 2024
1 parent 429fb9a commit b2c7c0b
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 64 deletions.
41 changes: 32 additions & 9 deletions src/Fluent/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,36 @@ public function rows(array $rows): Query


/**
* @param mixed ...$params
* @param string|list<string>|NULL $columnsOrConstraint
* @param string|Complex|Db\Sql|NULL $where
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function onConflict($columnsOrConstraint = NULL, $where = NULL): Query
{
return $this->createQuery()->onConflict($columnsOrConstraint, $where);
}


/**
* @param array<int|string, string|Db\Sql> $set
* @param string|Complex|Db\Sql|NULL $where
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function onConflict(string $targetAndAction, ...$params): Query
public function doUpdate(array $set, $where = NULL): Query
{
return $this->createQuery()->onConflict($targetAndAction, ...$params);
return $this->createQuery()->doUpdate($set, $where);
}


/**
* @return QueryExecute
* @throws Exceptions\QueryException
*/
public function doNothing(): Query
{
return $this->createQuery()->doNothing();
}


Expand Down Expand Up @@ -438,25 +461,25 @@ public function using($dataSource, ?string $alias = NULL, $onCondition = NULL):

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


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


Expand Down
33 changes: 27 additions & 6 deletions src/Fluent/Exceptions/QueryBuilderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class QueryBuilderException extends Exception
public const BAD_PARAMS_COUNT = 8;
public const NO_CORRESPONDING_TABLE = 9;
public const SELECT_ALL_COLUMNS_CANT_BE_COMBINED_WITH_CONCRETE_COLUMN_FOR_INSERT_SELECT_WITH_COLUMN_DETECTION = 10;
public const NO_USING = 11;
public const NO_WHEN = 12;
public const MERGE_NO_USING = 11;
public const MERGE_NO_WHEN = 12;
public const ON_CONFLICT_NO_DO = 13;
public const ON_CONFLICT_DO_WITHOUT_DEFINITION = 14;
public const ON_CONFLICT_DO_UPDATE_SET_SINGLE_COLUMN_CAN_BE_ONLY_STRING = 15;


public static function badQueryType(string $type): self
Expand Down Expand Up @@ -81,15 +84,33 @@ public static function selectAllColumnsCantBeCombinedWithConcreteColumnForInsert
}


public static function noUsing(): self
public static function mergeNoUsing(): self
{
return new self('No USING for MERGE.', self::NO_USING);
return new self('There is missing USING statement for MERGE command.', self::MERGE_NO_USING);
}


public static function noWhen(): self
public static function mergeNoWhen(): self
{
return new self('No WHEN for MERGE.', self::NO_WHEN);
return new self('There must be at least one WHEN statement for MERGE command.', self::MERGE_NO_WHEN);
}


public static function onConflictNoDo(): self
{
return new self('There is missing DO statement for ON CONFLICT clause.', self::ON_CONFLICT_NO_DO);
}


public static function onConflictDoWithoutDefinition(): self
{
return new self('There is existing DO statement but ON CONFLICT clause is missing.', self::ON_CONFLICT_DO_WITHOUT_DEFINITION);
}


public static function onConflictDoUpdateSetSingleColumnCanBeOnlyString(): self
{
return new self('ON CONFLICT UPDATE SET array value without alias (string key) must be only string.', self::ON_CONFLICT_DO_UPDATE_SET_SINGLE_COLUMN_CAN_BE_ONLY_STRING);
}

}
13 changes: 10 additions & 3 deletions src/Fluent/Exceptions/QueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class QueryException extends Exception
public const PARAM_MUST_BE_SCALAR_OR_ENUM_OR_EXPRESSION = 5;
public const CANT_UPDATE_QUERY_AFTER_EXECUTE = 6;
public const YOU_MUST_EXECUTE_QUERY_BEFORE_THAT = 7;
public const ONLY_ONE_USING = 8;
public const MERGE_ONLY_ONE_USING = 8;
public const ON_CONFLICT_WHERE_NOT_FOR_CONSTRAINT = 9;


public static function onlyOneMainTable(): self
Expand Down Expand Up @@ -59,9 +60,15 @@ public static function youMustExecuteQueryBeforeThat(): self
}


public static function onlyOneUsing(): self
public static function mergeOnlyOneUsing(): self
{
return new self('USING can be set only once.', self::ONLY_ONE_USING);
return new self('USING can be set only once.', self::MERGE_ONLY_ONE_USING);
}


public static function onConflictWhereNotForConstraint(): self
{
return new self('ON CONFLICT with constraint can\'t have a WHERE clause.', self::ON_CONFLICT_WHERE_NOT_FOR_CONSTRAINT);
}

}
71 changes: 59 additions & 12 deletions src/Fluent/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Query implements Sql
private const COMBINE_INTERSECT = 'INTERSECT';
private const COMBINE_EXCEPT = 'EXCEPT';

public const INSERT_ONCONFLICT_COLUMNS_OR_CONSTRAINT = 'columns-or-constraint';
public const INSERT_ONCONFLICT_WHERE = 'where';
public const INSERT_ONCONFLICT_DO = 'do';
public const INSERT_ONCONFLICT_DO_WHERE = 'do-where';

public const MERGE_WHEN_MATCHED = 'when-matched';
public const MERGE_WHEN_NOT_MATCHED = 'when-not-matched';

Expand Down Expand Up @@ -83,7 +88,12 @@ class Query implements Sql
self::PARAM_OFFSET => NULL,
self::PARAM_COMBINE_QUERIES => [],
self::PARAM_INSERT_COLUMNS => [],
self::PARAM_INSERT_ONCONFLICT => NULL,
self::PARAM_INSERT_ONCONFLICT => [
self::INSERT_ONCONFLICT_COLUMNS_OR_CONSTRAINT => NULL,
self::INSERT_ONCONFLICT_WHERE => NULL,
self::INSERT_ONCONFLICT_DO => NULL,
self::INSERT_ONCONFLICT_DO_WHERE => NULL,
],
self::PARAM_RETURNING => [],
self::PARAM_DATA => [],
self::PARAM_ROWS => [],
Expand Down Expand Up @@ -302,7 +312,7 @@ private function addTable(string $type, $name, ?string $alias, $onCondition = NU
$this->checkAlias($name, $alias);

if (in_array($type, [self::TABLE_TYPE_MAIN, self::TABLE_TYPE_USING], TRUE) && ($this->params[self::PARAM_TABLE_TYPES][$type] !== NULL)) {
throw ($type === self::TABLE_TYPE_MAIN) ? Exceptions\QueryException::onlyOneMainTable() : Exceptions\QueryException::onlyOneUsing();
throw ($type === self::TABLE_TYPE_MAIN) ? Exceptions\QueryException::onlyOneMainTable() : Exceptions\QueryException::mergeOnlyOneUsing();
}

if ($alias === NULL) {
Expand Down Expand Up @@ -629,16 +639,53 @@ public function rows(array $rows): self


/**
* @param mixed ...$params
* @param string|list<string>|NULL $columnsOrConstraint
* @param string|Complex|Db\Sql|NULL $where
* @return static
* @throws Exceptions\QueryException
*/
public function onConflict(string $targetAndAction, ...$params): Query
public function onConflict($columnsOrConstraint = NULL, $where = NULL): self
{
$this->resetQuery();

\array_unshift($params, $targetAndAction);
$this->params[self::PARAM_INSERT_ONCONFLICT] = $params;
if (is_string($columnsOrConstraint) && ($where !== NULL)) {
throw Exceptions\QueryException::onConflictWhereNotForConstraint();
}

$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_COLUMNS_OR_CONSTRAINT] = $columnsOrConstraint ?? FALSE;
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_WHERE] = $where === NULL ? NULL : Complex::createAnd()->add($where);

return $this;
}


/**
* @param array<int|string, string|Db\Sql> $set
* @param string|Complex|Db\Sql|NULL $where
* @return static
* @throws Exceptions\QueryException
*/
public function doUpdate(array $set, $where = NULL): self
{
$this->resetQuery();

$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO] = $set;
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO_WHERE] = $where === NULL ? NULL : Complex::createAnd()->add($where);

return $this;
}


/**
* @return static
* @throws Exceptions\QueryException
*/
public function doNothing(): self
{
$this->resetQuery();

$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO] = FALSE;
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO_WHERE] = NULL;

return $this;
}
Expand Down Expand Up @@ -743,18 +790,18 @@ public function using($dataSource, ?string $alias = NULL, $onCondition = NULL):

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

$this->params[self::PARAM_MERGE][] = [
self::MERGE_WHEN_MATCHED,
$then,
$onCondition === NULL ? NULL : Complex::createAnd()->add($onCondition),
$condition === NULL ? NULL : Complex::createAnd()->add($condition),
];

return $this;
Expand All @@ -763,18 +810,18 @@ public function whenMatched($then, $onCondition = NULL): self

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

$this->params[self::PARAM_MERGE][] = [
self::MERGE_WHEN_NOT_MATCHED,
$then,
$onCondition === NULL ? NULL : Complex::createAnd()->add($onCondition),
$condition === NULL ? NULL : Complex::createAnd()->add($condition),
];

return $this;
Expand Down
Loading

0 comments on commit b2c7c0b

Please sign in to comment.