Add basic PGSQL connection
This commit is contained in:
50
source/db/db.d
Normal file
50
source/db/db.d
Normal file
@@ -0,0 +1,50 @@
|
||||
module db.db;
|
||||
|
||||
import slf4d;
|
||||
|
||||
import ddbc.core;
|
||||
|
||||
enum DBConnector {
|
||||
DB_MYSQL,
|
||||
DB_PGSQL,
|
||||
DB_SQLITE,
|
||||
}
|
||||
|
||||
struct DBSettings {
|
||||
DBConnector connector;
|
||||
string host;
|
||||
ushort port;
|
||||
string username;
|
||||
string password;
|
||||
string dbname;
|
||||
}
|
||||
|
||||
class DB {
|
||||
protected DBSettings m_settings;
|
||||
protected DataSource m_ds;
|
||||
protected Connection m_conn;
|
||||
|
||||
shared this(DBSettings settings) {
|
||||
this.m_settings = settings;
|
||||
}
|
||||
|
||||
static shared(DB) getDB(DBSettings settings) {
|
||||
switch (settings.connector) {
|
||||
case DBConnector.DB_PGSQL:
|
||||
import db.pgsql;
|
||||
|
||||
debugF!"DB type is PostgreSQL";
|
||||
return new shared PostgresDB(settings);
|
||||
|
||||
default:
|
||||
throw new Exception("DB not supported");
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract DataSource getDataSource();
|
||||
|
||||
void connect() {
|
||||
this.m_ds = this.getDataSource();
|
||||
this.m_conn = this.m_ds.getConnection();
|
||||
}
|
||||
}
|
||||
27
source/db/pgsql.d
Normal file
27
source/db/pgsql.d
Normal file
@@ -0,0 +1,27 @@
|
||||
module db.pgsql;
|
||||
|
||||
import db.db;
|
||||
|
||||
import ddbc.core;
|
||||
import ddbc.common;
|
||||
|
||||
class PostgresDB : DB {
|
||||
shared this(Args...)(auto ref Args args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
protected override DataSource getDataSource() {
|
||||
import ddbc.drivers.pgsqlddbc;
|
||||
|
||||
Driver driver = new PGSQLDriver();
|
||||
string url;
|
||||
string[string] params;
|
||||
|
||||
with (this.m_settings) {
|
||||
url = PGSQLDriver.generateUrl(host, port, dbname);
|
||||
params = PGSQLDriver.setUserAndPassword(username, password);
|
||||
}
|
||||
|
||||
return new ConnectionPoolDataSourceImpl(driver, url, params);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user