DB: add SQLite support

This commit is contained in:
2024-02-21 11:06:16 -03:00
parent 58028ca68e
commit a0b15f6526

View File

@@ -1,15 +1,18 @@
module db.db;
import std.json;
import slf4d;
import ddbc.core;
import hibernated.core;
import db.types;
enum DBConnector {
DB_MYSQL,
DB_PGSQL,
DB_SQLITE,
Mysql,
Postgres,
Sqlite,
}
struct DBSettings {
@@ -19,6 +22,39 @@ struct DBSettings {
string username;
string password;
string dbname;
static DBSettings fromJson(JSONValue cfg) {
DBSettings settings;
with (settings) {
dbname = cfg["dbName"].str;
switch (cfg["connector"].str) {
case "postgresql":
connector = DBConnector.Postgres;
break;
case "sqlite":
connector = DBConnector.Sqlite;
break;
default:
throw new Exception("Database connector `" ~ cfg["connector"].str ~ "` not supported");
}
if ("host" in cfg)
host = cfg["host"].str;
if ("port" in cfg)
port = cast(ushort) cfg["port"].integer;
if ("username" in cfg)
username = cfg["username"].str;
if ("password" in cfg)
username = cfg["password"].str;
}
return settings;
}
}
class DB {
@@ -40,7 +76,7 @@ class DB {
Dialect dialect;
switch (this.m_settings.connector) {
case DBConnector.DB_PGSQL:
case DBConnector.Postgres:
debugF!"Using PGSQL driver";
import ddbc.drivers.pgsqlddbc;
@@ -55,6 +91,22 @@ class DB {
dialect = new PGSQLDialect();
break;
case DBConnector.Sqlite:
debugF!"Using SQLite driver";
import ddbc.drivers.sqliteddbc;
driver = new SQLITEDriver();
url = this.m_settings.dbname;
static import std.file;
if (std.file.exists(url))
std.file.remove(url);
dialect = new SQLiteDialect();
break;
default:
throw new Exception("Database connector not supported (yet)");
}