1 2 module hunt.entity.utils.Common; 3 4 import std.traits; 5 import hunt.entity.Entity; 6 import hunt.entity.Constant; 7 8 import std.format; 9 10 class Common { 11 static T sampleCopy(T)(T t) { 12 T copy = new T(); 13 foreach(memberName; __traits(derivedMembers, T)) { 14 static if (__traits(getProtection, __traits(getMember, T, memberName)) == "public") { 15 alias memType = typeof(__traits(getMember, T ,memberName)); 16 static if (!isFunction!(memType)) { 17 mixin("copy."~memberName~" = "~"t."~memberName~";\n"); 18 } 19 } 20 } 21 foreach(key, value; t.getAllLazyData()) { 22 copy.addLazyData(key, new LazyData(value)); 23 } 24 copy.setManager(t.getManager()); 25 return copy; 26 } 27 28 static bool inArray(T)(T[] ts, T t) { 29 foreach(v; ts) { 30 if(v == t) 31 return true; 32 } 33 return false; 34 } 35 36 static string quoteStr(string s) { 37 if (s == "length") 38 return s; 39 return "\""~s~"\""; 40 } 41 42 } 43 44 /// returns table name for class type 45 string getTableName(T : Object)() { 46 string name = T.stringof; 47 foreach (a; __traits(getAttributes, T)) { 48 static if (is(typeof(a) == Table)) { 49 name = a.name; 50 break; 51 } 52 } 53 return name; 54 } 55 56 string getJoinTableName(T, string m)() { 57 string name = null; 58 { 59 foreach(a; __traits(getAttributes, __traits(getMember,T,m))) { 60 static if (is(typeof(a) == JoinTable)) { 61 name = (a.name); 62 break; 63 } 64 } 65 } 66 return name; 67 } 68 69 string getPrimaryKey(T : Object)() { 70 string name = "id"; 71 foreach (m; __traits(allMembers, T)) { 72 static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){ 73 alias memType = typeof(__traits(getMember, T ,m)); 74 static if (!isFunction!(memType) && hasUDA!(__traits(getMember, T ,m), PrimaryKey)) { 75 name = m; 76 } 77 } 78 } 79 80 return name; 81 } 82 83 JoinColumn getJoinColumn(T, string m)() { 84 JoinColumn joinColum ; 85 { 86 foreach(a; __traits(getAttributes, __traits(getMember,T,m))) { 87 static if (is(typeof(a) == JoinColumn)) { 88 joinColum = a; 89 break; 90 } 91 } 92 } 93 return joinColum; 94 } 95 96 InverseJoinColumn getInverseJoinColumn(T, string m)() { 97 InverseJoinColumn joinColum ; 98 { 99 foreach(a; __traits(getAttributes, __traits(getMember,T,m))) { 100 static if (is(typeof(a) == InverseJoinColumn)) { 101 joinColum = a; 102 break; 103 } 104 } 105 } 106 return joinColum; 107 } 108 109 unittest{ 110 import hunt.entity; 111 import hunt.logging; 112 113 @Table("UserInfo") 114 class UserInfo { 115 116 @AutoIncrement @PrimaryKey 117 int id; 118 119 120 @Column("nickname") 121 string nickName; 122 int age; 123 124 125 } 126 127 @Table("AppSInfo") 128 class AppInfo { 129 130 @AutoIncrement @PrimaryKey 131 int id; 132 133 string name; 134 string desc; 135 136 @JoinTable("UserApp") 137 @JoinColumn("appid","id") 138 @InverseJoinColumn("uid","id") 139 @ManyToMany("apps") 140 UserInfo[] uinfos; 141 } 142 143 logDebug("Table Name : %s ".format(getTableName!AppInfo)); 144 logDebug("Join Table Name : %s ".format(getJoinTableName!(AppInfo,"uinfos"))); 145 logDebug("Join Column : %s ".format(getJoinColumn!(AppInfo,"uinfos"))); 146 logDebug("Inverse Join Column : %s ".format(getInverseJoinColumn!(AppInfo,"uinfos"))); 147 logDebug("PrimaryKey : %s ".format(getPrimaryKey!AppInfo)); 148 149 } 150 151 152 private enum string IndentString = " "; // 32 spaces 153 154 string indent(size_t number) { 155 assert(number>0 && IndentString.length, "Out of range"); 156 return IndentString[0..number]; 157 }