implement DBName override for table names only

This commit is contained in:
Adam D. Ruppe 2022-10-15 13:50:01 -04:00
parent 2a964a4eef
commit ea9f199b29
1 changed files with 13 additions and 6 deletions

View File

@ -91,7 +91,7 @@ struct Serial {
string generateCreateTableFor(alias O)() { string generateCreateTableFor(alias O)() {
enum tableName = toTableName(O.stringof); enum tableName = tableNameFor!O(toTableName(O.stringof));
string sql = "CREATE TABLE " ~ tableName ~ " ("; string sql = "CREATE TABLE " ~ tableName ~ " (";
string postSql; string postSql;
bool outputtedPostSql = false; bool outputtedPostSql = false;
@ -198,7 +198,7 @@ string generateCreateTableFor(alias O)() {
} else } else
addPostSql("PRIMARY KEY(" ~ memberName ~ ")"); addPostSql("PRIMARY KEY(" ~ memberName ~ ")");
} else static if(is(attr == ForeignKey!(to, sqlPolicy), alias to, string sqlPolicy)) { } else static if(is(attr == ForeignKey!(to, sqlPolicy), alias to, string sqlPolicy)) {
string refTable = toTableName(__traits(parent, to).stringof); string refTable = tableNameFor!(__traits(parent, to))(toTableName(__traits(parent, to).stringof));
string refField = to.stringof; string refField = to.stringof;
addPostSql("FOREIGN KEY(" ~ memberName ~ ") REFERENCES "~refTable~"("~refField~(sqlPolicy.length ? ") " : ")") ~ sqlPolicy); addPostSql("FOREIGN KEY(" ~ memberName ~ ") REFERENCES "~refTable~"("~refField~(sqlPolicy.length ? ") " : ")") ~ sqlPolicy);
} }
@ -218,6 +218,13 @@ string generateCreateTableFor(alias O)() {
return sql; return sql;
} }
string tableNameFor(T)(string def) {
foreach(attr; __traits(getAttributes, T))
static if(is(typeof(attr) == DBName))
def = attr.name;
return def;
}
string toTableName(string t) { string toTableName(string t) {
return plural(50, beautify(t, '_', true)); return plural(50, beautify(t, '_', true));
} }
@ -299,7 +306,7 @@ void save(O)(ref O t, Database db) {
+/ +/
void insert(O)(ref O t, Database db) { void insert(O)(ref O t, Database db) {
auto builder = new InsertBuilder; auto builder = new InsertBuilder;
builder.setTable(toTableName(O.stringof)); builder.setTable(tableNameFor!O(toTableName(O.stringof)));
static foreach(memberName; __traits(allMembers, O)) {{ static foreach(memberName; __traits(allMembers, O)) {{
alias member = __traits(getMember, O, memberName); alias member = __traits(getMember, O, memberName);
@ -338,7 +345,7 @@ void insert(O)(ref O t, Database db) {
import std.conv; import std.conv;
version(dbgenerate_sqlite) { version(dbgenerate_sqlite) {
builder.execute(db); builder.execute(db);
foreach(row; db.query("SELECT max(id) FROM " ~ toTableName(O.stringof))) foreach(row; db.query("SELECT max(id) FROM " ~ tableNameFor!O(toTableName(O.stringof))))
t.id.value = to!int(row[0]); t.id.value = to!int(row[0]);
} else { } else {
static if (__traits(hasMember, O, "id")) static if (__traits(hasMember, O, "id"))
@ -382,7 +389,7 @@ auto find(alias T)(Database db, int id) {
// if it is unique, return an individual item. // if it is unique, return an individual item.
// if not, return the array // if not, return the array
foreach(record; db.query("SELECT * FROM " ~ toTableName(T.stringof) ~ " WHERE id = ?", id)) { foreach(record; db.query("SELECT * FROM " ~ tableNameFor!T(toTableName(T.stringof)) ~ " WHERE id = ?", id)) {
T t; T t;
populateFromDbRow(t, record); populateFromDbRow(t, record);
@ -609,7 +616,7 @@ struct QueryBuilderHelper(T) {
} }
QueryBuilderHelper!(T[]) from(T)() { QueryBuilderHelper!(T[]) from(T)() {
return QueryBuilderHelper!(T[])(toTableName(T.stringof)); return QueryBuilderHelper!(T[])(tableNameFor!T(toTableName(T.stringof)));
} }
/// ditto /// ditto