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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
module db.migrations.m_001_initial;
|
||||
|
||||
import db.migrations.migration;
|
||||
|
||||
Migration Initial = Migration(
|
||||
[
|
||||
`create table users(
|
||||
id uuid not null,
|
||||
name character varying(255),
|
||||
handle character varying(255),
|
||||
local boolean not null,
|
||||
ap_id character varying(255) not null,
|
||||
ap_privkey text,
|
||||
ap_pubkey text,
|
||||
ap_shared_inbox text,
|
||||
ap_inbox text,
|
||||
ap_outbox text,
|
||||
ap_followers_addr text,
|
||||
ap_following_addr text
|
||||
);`
|
||||
],
|
||||
[
|
||||
`drop table users;`
|
||||
],
|
||||
);
|
||||
@@ -1,31 +0,0 @@
|
||||
module db.migrations.migration;
|
||||
|
||||
import singletons;
|
||||
import db.migrations.m_001_initial;
|
||||
|
||||
struct Migration {
|
||||
string[] upStatements;
|
||||
string[] downStatements;
|
||||
|
||||
bool up() {
|
||||
int result;
|
||||
|
||||
foreach (string statement; upStatements)
|
||||
result |= Db.runUpdate(statement);
|
||||
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
bool down() {
|
||||
int result;
|
||||
|
||||
foreach (string statement; downStatements)
|
||||
result |= Db.runUpdate(statement);
|
||||
|
||||
return result == 0;
|
||||
}
|
||||
}
|
||||
|
||||
Migration[string] migrations = [
|
||||
"001_initial": Initial,
|
||||
];
|
||||
@@ -1,31 +0,0 @@
|
||||
module db.pgsql;
|
||||
|
||||
import db.db;
|
||||
|
||||
import ddbc.core;
|
||||
import ddbc.common;
|
||||
|
||||
import slf4d;
|
||||
|
||||
class PostgresDB : DB {
|
||||
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);
|
||||
}
|
||||
|
||||
debugF!"DB URL: %s"(url);
|
||||
|
||||
return new ConnectionPoolDataSourceImpl(driver, url, params);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,23 @@
|
||||
module db.types;
|
||||
|
||||
class User {
|
||||
string id;
|
||||
string name;
|
||||
string handle;
|
||||
bool local;
|
||||
import hibernated.core;
|
||||
|
||||
string ap_id;
|
||||
string ap_privkey;
|
||||
string ap_pubkey;
|
||||
string ap_shared_inbox;
|
||||
string ap_inbox;
|
||||
string ap_outbox;
|
||||
string ap_followers_addr;
|
||||
string ap_following_addr;
|
||||
@Entity
|
||||
@Table("users")
|
||||
class User {
|
||||
@Generator(UUID_GENERATOR)
|
||||
string id;
|
||||
|
||||
@Column string name;
|
||||
@Column string handle;
|
||||
@Column bool local;
|
||||
|
||||
@Column string ap_id;
|
||||
@Column string ap_privkey;
|
||||
@Column string ap_pubkey;
|
||||
@Column string ap_shared_inbox;
|
||||
@Column string ap_inbox;
|
||||
@Column string ap_outbox;
|
||||
@Column string ap_followers_addr;
|
||||
@Column string ap_following_addr;
|
||||
}
|
||||
|
||||
@@ -38,9 +38,11 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
Db = DB.getDB(dbSettings);
|
||||
Db = new DB(dbSettings);
|
||||
Db.connect();
|
||||
Db.runMigrations();
|
||||
|
||||
scope (exit)
|
||||
Db.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user