From 27fde1f9ac4a9f145cc50514c470fe27de75885e Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Mon, 28 Apr 2025 14:17:59 +0200 Subject: [PATCH] feat(db): Add Postgres to SQL connection types + Refactor matching logic Implements pattern matching for easier identification of database types. Adds support for Postgres alongside existing Sqlite and MySQL types, enhancing flexibility for database connections. Updates enum to include Postgres type. --- TShockAPI/Extensions/DbExt.cs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/TShockAPI/Extensions/DbExt.cs b/TShockAPI/Extensions/DbExt.cs index b5a2c618..c6aa0ab9 100644 --- a/TShockAPI/Extensions/DbExt.cs +++ b/TShockAPI/Extensions/DbExt.cs @@ -20,6 +20,9 @@ using System; using System.Collections.Generic; using System.Data; using System.Diagnostics.CodeAnalysis; +using Microsoft.Data.Sqlite; +using MySql.Data.MySqlClient; +using Npgsql; namespace TShockAPI.DB { @@ -143,15 +146,13 @@ namespace TShockAPI.DB return clone; } - public static SqlType GetSqlType(this IDbConnection conn) + public static SqlType GetSqlType(this IDbConnection conn) => conn switch { - var name = conn.GetType().Name; - if (name == "SqliteConnection" || name == "SQLiteConnection") - return SqlType.Sqlite; - if (name == "MySqlConnection") - return SqlType.Mysql; - return SqlType.Unknown; - } + SqliteConnection => SqlType.Sqlite, + MySqlConnection => SqlType.Mysql, + NpgsqlConnection => SqlType.Postgres, + _ => SqlType.Unknown + }; private static readonly Dictionary> ReadFuncs = new Dictionary > @@ -267,7 +268,8 @@ namespace TShockAPI.DB { Unknown, Sqlite, - Mysql + Mysql, + Postgres } public class QueryResult : IDisposable