From 213b6e0ef154cc515ea8072e5aa6c8c90a5e3100 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Mar 2024 13:28:29 +1000 Subject: [PATCH] Add upsert expressions for sqlsrv --- src/Storage/DatabaseStorage.php | 8 ++++++++ tests/Pest.php | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Storage/DatabaseStorage.php b/src/Storage/DatabaseStorage.php index 995c6d08..9f6194af 100644 --- a/src/Storage/DatabaseStorage.php +++ b/src/Storage/DatabaseStorage.php @@ -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}]"), }, ] @@ -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}]"), }, ] @@ -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}]"), }, ] @@ -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}]"), }, ] @@ -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}]"), } ); diff --git a/tests/Pest.php b/tests/Pest.php index 3e6cd13b..de117370 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -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), };