DB: start using ORM
This commit is contained in:
@@ -3,6 +3,8 @@ module db.db;
|
||||
import slf4d;
|
||||
|
||||
import ddbc.core;
|
||||
import hibernated.core;
|
||||
import db.types;
|
||||
|
||||
enum DBConnector {
|
||||
DB_MYSQL,
|
||||
@@ -21,68 +23,60 @@ struct DBSettings {
|
||||
|
||||
class DB {
|
||||
protected DBSettings m_settings;
|
||||
protected EntityMetaData m_schema;
|
||||
protected DataSource m_ds;
|
||||
protected Connection m_conn;
|
||||
protected Statement m_stmt = null;
|
||||
protected SessionFactory m_factory;
|
||||
protected Session m_session;
|
||||
|
||||
this(DBSettings settings) {
|
||||
this.m_settings = settings;
|
||||
this.m_schema = new SchemaInfoImpl!(User);
|
||||
}
|
||||
|
||||
static DB getDB(DBSettings settings) {
|
||||
switch (settings.connector) {
|
||||
case DBConnector.DB_PGSQL:
|
||||
import db.pgsql;
|
||||
|
||||
debugF!"DB type is PostgreSQL";
|
||||
return new 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();
|
||||
this.m_conn.setAutoCommit(false);
|
||||
}
|
||||
Driver driver;
|
||||
string url;
|
||||
string[string] params;
|
||||
Dialect dialect;
|
||||
|
||||
int runUpdate(string statement) {
|
||||
if (this.m_stmt is null)
|
||||
this.m_stmt = this.m_conn.createStatement();
|
||||
switch (this.m_settings.connector) {
|
||||
case DBConnector.DB_PGSQL:
|
||||
debugF!"Using PGSQL driver";
|
||||
|
||||
debugF!"Running update: %s"(statement);
|
||||
import ddbc.drivers.pgsqlddbc;
|
||||
|
||||
return this.m_stmt.executeUpdate(statement);
|
||||
}
|
||||
driver = new PGSQLDriver();
|
||||
|
||||
ResultSet runQuery(string statement) {
|
||||
if (this.m_stmt is null)
|
||||
this.m_stmt = this.m_conn.createStatement();
|
||||
with (this.m_settings) {
|
||||
url = PGSQLDriver.generateUrl(host, port, dbname);
|
||||
params = PGSQLDriver.setUserAndPassword(username, password);
|
||||
}
|
||||
|
||||
debugF!"Running query: %s"(statement);
|
||||
dialect = new PGSQLDialect();
|
||||
break;
|
||||
|
||||
return this.m_stmt.executeQuery(statement);
|
||||
}
|
||||
default:
|
||||
throw new Exception("Database connector not supported (yet)");
|
||||
}
|
||||
|
||||
void runMigrations() {
|
||||
ResultSet rs = this.runQuery(
|
||||
`select * from information_schema.tables where table_name = 'migrations';`,
|
||||
);
|
||||
debugF!"Connecting to DB with URL: %s"(url);
|
||||
|
||||
bool exists = rs.next();
|
||||
int ret = 0;
|
||||
this.m_ds = new ConnectionPoolDataSourceImpl(driver, url, params);
|
||||
this.m_factory = new SessionFactoryImpl(this.m_schema, dialect, this.m_ds);
|
||||
|
||||
if (exists == false) {
|
||||
ret |= this.runUpdate(
|
||||
`create table migrations(
|
||||
id serial,
|
||||
name character varying
|
||||
)`,
|
||||
);
|
||||
{ // Create schema if necessary
|
||||
Connection conn = this.m_ds.getConnection();
|
||||
scope (exit)
|
||||
conn.close();
|
||||
this.m_factory.getDBMetaData().updateDBSchema(conn, false, true);
|
||||
}
|
||||
}
|
||||
|
||||
void close() {
|
||||
if (this.m_session && this.m_session.isOpen())
|
||||
this.m_session.close();
|
||||
|
||||
if (this.m_factory && this.m_factory.isClosed() == false)
|
||||
this.m_factory.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user