Skip to content

Commit

Permalink
Add upsert expressions for sqlsrv
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Mar 13, 2024
1 parent ef191d6 commit 213b6e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ protected function upsertCount(array $values): int
'value' => match ($driver = $this->connection()->getDriverName()) {
'mariadb', 'mysql' => new Expression('`value` + values(`value`)'),
'pgsql', 'sqlite' => new Expression('"pulse_aggregates"."value" + "excluded"."value"'),
'sqlsrv' => new Expression('value + laravel_source.value'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -205,6 +206,7 @@ protected function upsertMin(array $values): int
'mariadb', 'mysql' => new Expression('least(`value`, values(`value`))'),
'pgsql' => new Expression('least("pulse_aggregates"."value", "excluded"."value")'),
'sqlite' => new Expression('min("pulse_aggregates"."value", "excluded"."value")'),
'sqlsrv' => new Expression('least(value, laravel_source.value)'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -226,6 +228,7 @@ protected function upsertMax(array $values): int
'mariadb', 'mysql' => new Expression('greatest(`value`, values(`value`))'),
'pgsql' => new Expression('greatest("pulse_aggregates"."value", "excluded"."value")'),
'sqlite' => new Expression('max("pulse_aggregates"."value", "excluded"."value")'),
'sqlsrv' => new Expression('greatest(value, laravel_source.value)'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -246,6 +249,7 @@ protected function upsertSum(array $values): int
'value' => match ($driver = $this->connection()->getDriverName()) {
'mariadb', 'mysql' => new Expression('`value` + values(`value`)'),
'pgsql', 'sqlite' => new Expression('"pulse_aggregates"."value" + "excluded"."value"'),
'sqlsrv' => new Expression('value + laravel_source.value'),
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
},
]
Expand All @@ -271,6 +275,10 @@ protected function upsertAvg(array $values): int
'value' => new Expression('("pulse_aggregates"."value" * "pulse_aggregates"."count" + ("excluded"."value" * "excluded"."count")) / ("pulse_aggregates"."count" + "excluded"."count")'),
'count' => new Expression('"pulse_aggregates"."count" + "excluded"."count"'),
],
'sqlsrv' => [
'value' => new Expression('(value * count + (laravel_source.value * laravel_source.count)) / (count + laravel_source.count)'),
'count' => new Expression('count + laravel_source.count'),
],
default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
}
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
function keyHash(string $string): string
{
return match (DB::connection()->getDriverName()) {
'mariadb', 'mysql' => hex2bin(md5($string)),
'mariadb', 'mysql', 'sqlsrv' => hex2bin(md5($string)),
'pgsql' => Uuid::fromString(md5($string)),
'sqlite' => md5($string),
};
Expand Down

0 comments on commit 213b6e0

Please sign in to comment.