-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #362 from keboola/erik-PST-2398-split2
[output-mapping] delete where - part2 - filters values_from_set, values_from_workspace
- Loading branch information
Showing
13 changed files
with
1,102 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\OutputMapping\Storage; | ||
|
||
use Keboola\OutputMapping\Mapping\MappingFromConfigurationDeleteWhere; | ||
use Keboola\OutputMapping\Mapping\MappingFromConfigurationDeleteWhereFilterFromSet; | ||
use Keboola\OutputMapping\Mapping\MappingFromConfigurationDeleteWhereFilterFromWorkspace; | ||
|
||
class DeleteTableRowsOptionsFactory | ||
{ | ||
public static function createFromLegacyDeleteWhereColumn( | ||
string $column, | ||
string $operator, | ||
array $values, | ||
): array { | ||
return [ | ||
'whereColumn' => $column, | ||
'whereOperator' => $operator, | ||
'whereValues' => $values, | ||
]; | ||
} | ||
|
||
public static function createFromDeleteWhere( | ||
MappingFromConfigurationDeleteWhere $deleteWhere, | ||
): ?array { | ||
$options = []; | ||
|
||
if ($deleteWhere->getChangedSince()) { | ||
$options['changedSince'] = $deleteWhere->getChangedSince(); | ||
} | ||
|
||
if ($deleteWhere->getChangedUntil()) { | ||
$options['changedUntil'] = $deleteWhere->getChangedUntil(); | ||
} | ||
|
||
$whereFilters = self::createWhereFilters($deleteWhere); | ||
if ($whereFilters !== []) { | ||
$options['whereFilters'] = $whereFilters; | ||
} | ||
|
||
return $options === [] ? null : $options; | ||
} | ||
|
||
private static function createWhereFilters(MappingFromConfigurationDeleteWhere $deleteWhere): array | ||
{ | ||
if (!$deleteWhere->getWhereFilters()) { | ||
return []; | ||
} | ||
|
||
$whereFilters = []; | ||
foreach ($deleteWhere->getWhereFilters() as $deleteFilter) { | ||
$whereFilter = [ | ||
'column' => $deleteFilter->getColumn(), | ||
'operator' => $deleteFilter->getOperator(), | ||
]; | ||
|
||
if ($deleteFilter instanceof MappingFromConfigurationDeleteWhereFilterFromSet) { | ||
$whereFilter['values'] = $deleteFilter->getValues(); | ||
$whereFilters[] = $whereFilter; | ||
} | ||
if ($deleteFilter instanceof MappingFromConfigurationDeleteWhereFilterFromWorkspace) { | ||
$whereFilter['valuesByTableInWorkspace'] = [ | ||
'workspaceId' => $deleteFilter->getWorkspaceId(), | ||
'table' => $deleteFilter->getWorkspaceTable(), | ||
'column' => $deleteFilter->getWorkspaceColumn() ?: $deleteFilter->getColumn(), | ||
]; | ||
$whereFilters[] = $whereFilter; | ||
} | ||
} | ||
|
||
return $whereFilters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\OutputMapping\Writer\Helper; | ||
|
||
class DeleteWhereHelper | ||
{ | ||
private static function hasWhereFilters(array $deleteWhere): bool | ||
{ | ||
return isset($deleteWhere['where_filters']) && is_array($deleteWhere['where_filters']); | ||
} | ||
|
||
private static function isSourceWorkspaceIdNeeded(array $whereFilter): bool | ||
{ | ||
return isset($whereFilter['values_from_workspace']) | ||
&& empty($whereFilter['values_from_workspace']['workspace_id']); | ||
} | ||
|
||
public static function addWorkspaceIdToValuesFromWorkspaceIfMissing(array $deleteWhere, string $workspaceId): array | ||
{ | ||
if (self::hasWhereFilters($deleteWhere)) { | ||
$deleteWhere['where_filters'] = array_map( | ||
function (array $whereFilter) use ($workspaceId): array { | ||
if (self::isSourceWorkspaceIdNeeded($whereFilter)) { | ||
$whereFilter['values_from_workspace']['workspace_id'] = $workspaceId; | ||
} | ||
return $whereFilter; | ||
}, | ||
$deleteWhere['where_filters'], | ||
); | ||
} | ||
|
||
return $deleteWhere; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.