1 module uim.sql.tables.insert; 2 3 import uim.sql; 4 5 class DSQLInsert : DSQLStatement { 6 7 this() { } 8 this(string table, string[string] values = null) { this(); from(table).values(values); } 9 this(string table = "", string names = "", string values = "") { this(); from(table).values(names, values); } 10 11 protected string _table; 12 auto from(string name) { return table(name); } 13 auto table(string name) { 14 if (name.length > 0) _table = name; 15 return this; 16 } 17 18 protected string _columns; 19 protected string _values; 20 auto columns(string names) { 21 _columns = names; 22 return this; 23 } 24 auto values(string values) { 25 _values = values; 26 return this; 27 } 28 29 auto values(string[string] vals) { 30 auto keys = vals.keys; 31 string[] v = new string[keys.length]; 32 foreach(i, key; keys) v[i] = vals[key]; 33 34 values(keys.join(","), v.join(",")); 35 return this; 36 } 37 auto values(string names, string values) { 38 _columns = names; 39 _values = values; 40 return this; 41 } 42 43 override string toSQL() { 44 auto sql = "INSERT INTO "~_table~" ("~_columns~") VALUES("~_values~")"; 45 return sql; 46 } 47 } 48 auto SQLInsert() { return new DSQLInsert; } 49 auto SQLInsert(string table, string[string] values) { return new DSQLInsert(table, values); } 50 auto SQLInsert(string table = "", string names = "", string values = "") { return new DSQLInsert(table, names, values); } 51 52 unittest { 53 writeln("Testing ", __MODULE__); 54 55 assert(SQLInsert.table("tab").columns("id, name").values("1, 'test1'") == "INSERT INTO tab (id, name) VALUES(1, 'test1')"); 56 }