From 24304f2e3c1edbca0c43f5afc057e0f1673a74d4 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 7 Aug 2011 23:37:56 -0400 Subject: [PATCH] make query data object easier to use with custom stuff --- database.d | 8 ++++++++ mysql.d | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/database.d b/database.d index b8c8268..cb32884 100644 --- a/database.d +++ b/database.d @@ -231,6 +231,8 @@ enum UpdateOrInsertMode { AlwaysInsert } + +// BIG FIXME: this should really use prepared statements int updateOrInsert(Database db, string table, string[string] values, string where, UpdateOrInsertMode mode = UpdateOrInsertMode.CheckForMe, string key = "id") { bool insert = false; @@ -401,6 +403,12 @@ string fixupSqlForDataObjectUse(string sql) { */ +mixin template DataObjectConstructors() { + this(Database db, string[string] res, Tuple!(string, string)[string] mappings) { + super(db, res, mappings); + } +} + string yield(string what) { return `if(auto result = dg(`~what~`)) return result;`; } import std.typecons; diff --git a/mysql.d b/mysql.d index 4f8c282..b862267 100644 --- a/mysql.d +++ b/mysql.d @@ -300,12 +300,12 @@ class MySql : Database { - ResultByDataObject queryDataObject(T...)(string sql, T t) { + ResultByDataObject!R queryDataObject(R = DataObject, T...)(string sql, T t) { // modify sql for the best data object grabbing sql = fixupSqlForDataObjectUse(sql); auto magic = query(sql, t); - return ResultByDataObject(cast(MySqlResult) magic, this); + return ResultByDataObject!R(cast(MySqlResult) magic, this); } @@ -539,7 +539,7 @@ class MySql : Database { MYSQL* mysql; } -struct ResultByDataObject { +struct ResultByDataObject(ObjType) if (is(ObjType : DataObject)) { this(MySqlResult r, MySql mysql) { result = r; auto fields = r.fields(); @@ -560,8 +560,8 @@ struct ResultByDataObject { ulong length() { return result.length; } bool empty() { return result.empty; } void popFront() { result.popFront(); } - DataObject front() { - return new DataObject(mysql, result.front.toAA, mappings); + ObjType front() { + return new ObjType(mysql, result.front.toAA, mappings); } // would it be good to add a new() method? would be valid even if empty // it'd just fill in the ID's at random and allow you to do the rest