From de7676fc988d29881fff97acab3031783ae1115e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 26 May 2023 16:59:32 +0200 Subject: [PATCH] Implement byteaA postgres migration --- src/Db/DbPropertiesFactory.php | 2 +- src/Db/PostgresArray.php | 38 ++++++++++++++++++++++++++++++++++ src/Db/SqlArray.php | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Db/DbPropertiesFactory.php b/src/Db/DbPropertiesFactory.php index 9dcc518bb..8497b7e49 100644 --- a/src/Db/DbPropertiesFactory.php +++ b/src/Db/DbPropertiesFactory.php @@ -61,7 +61,7 @@ final class DbPropertiesFactory $class .= '\\MysqlArray'; break; case $dbSettings instanceof Postgres: - $class .= '\\PostgresArrayBytea'; + $class .= '\\PostgresArray'; break; case $dbSettings instanceof Redis: $class .= '\\RedisArray'; diff --git a/src/Db/PostgresArray.php b/src/Db/PostgresArray.php index e17c958b2..27aa19b55 100644 --- a/src/Db/PostgresArray.php +++ b/src/Db/PostgresArray.php @@ -21,6 +21,44 @@ use danog\MadelineProto\Settings\Database\SerializerType; */ class PostgresArray extends PostgresArrayBytea { + /** + * Prepare statements. + * + * @param SqlArray::SQL_* $type + */ + protected function getSqlQuery(int $type): string + { + switch ($type) { + case SqlArray::SQL_GET: + return "SELECT value FROM \"{$this->table}\" WHERE key = :index"; + case SqlArray::SQL_SET: + return " + INSERT INTO \"{$this->table}\" + (key,value) + VALUES (:index, :value) + ON CONFLICT (key) DO UPDATE SET value = :value + "; + case SqlArray::SQL_UNSET: + return " + DELETE FROM \"{$this->table}\" + WHERE key = :index + "; + case SqlArray::SQL_COUNT: + return " + SELECT count(key) as count FROM \"{$this->table}\" + "; + case SqlArray::SQL_ITERATE: + return " + SELECT key, value FROM \"{$this->table}\" + "; + case SqlArray::SQL_CLEAR: + return " + DELETE FROM \"{$this->table}\" + "; + } + throw new Exception("An invalid statement type $type was provided!"); + } + protected function setSerializer(SerializerType $serializer): void { $this->serializer = match ($serializer) { diff --git a/src/Db/SqlArray.php b/src/Db/SqlArray.php index 9a6fafdf5..1618f2598 100644 --- a/src/Db/SqlArray.php +++ b/src/Db/SqlArray.php @@ -94,7 +94,7 @@ abstract class SqlArray extends DriverArray $this->setCache($key, $value); - $result = $this->execute( + $this->execute( $this->queries[self::SQL_SET], [ 'index' => $key,