Merge branch 'master' of github.com:adamdruppe/arsd

This commit is contained in:
Adam D. Ruppe 2022-11-15 12:24:50 -05:00
commit c982e8785e
1 changed files with 18 additions and 5 deletions

View File

@ -394,14 +394,27 @@ class RecordNotFoundException : Exception {
If you just give a type, it assumes the relevant index is "id".
+/
auto find(alias T)(Database db, int id) {
// FIXME: if T is an index, search by it.
static auto find(alias T)(Database db, int id) {
// FIXME:
// if it is unique, return an individual item.
// if not, return the array
static if (!is(T)) {
static const string fieldName = T.stringof;
alias FType = typeof(T); // field type
alias TType = __traits(parent, T); // Table type
}
else {
static const string fieldName = "id";
alias FType = int;
alias TType = T;
}
foreach(record; db.query("SELECT * FROM " ~ tableNameFor!T() ~ " WHERE id = ?", id)) {
T t;
static assert(is(FType : int),
TType.stringof ~ "." ~ fieldName ~ " should be an Integral field");
string q = "SELECT * FROM " ~ tableNameFor!TType() ~ " WHERE " ~ fieldName ~ " = ?";
foreach(record; db.query(q, id)) {
TType t;
populateFromDbRow(t, record);
return t;