mirror of https://github.com/adamdruppe/arsd.git
make query data object easier to use with custom stuff
This commit is contained in:
parent
d8e5da4ea2
commit
24304f2e3c
|
@ -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;
|
||||
|
|
10
mysql.d
10
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
|
||||
|
|
Loading…
Reference in New Issue