1 2 3 4 module uim.core.additionals.date; 5 @safe: 6 import uim.core; 7 8 /* 9 10 * If the database is using a different timezone than the system, you will 11 12 * need to supply its timezone, otherwise you can use the defaults. 13 14 */ 15 16 Date toDate(string sqlDate) 17 18 19 20 Date t; 21 22 return t.fromISOExtString(sqlDate); 23 24 } 25 26 string toSqlDate(Date date) 27 28 { 29 30 return date.toISOExtString(); 31 32 } 33 34 DateTime toDateTime(string sqlTime) 35 36 { 37 38 DateTime t; 39 40 return t.fromISOExtString(sqlTime.replaceFirst(" ", "T")); 41 42 } 43 44 SysTime toSysTime(string sqlTime, immutable TimeZone tz = null) 45 46 { 47 48 return SysTime(toDateTime(sqlTime),tz); 49 50 } 51 52 string toSqlTime(DateTime time) 53 54 { 55 56 return time.toISOExtString().replaceFirst("T", " "); 57 58 } 59 60 string toSqlTime(SysTime time, immutable TimeZone tz = null) 61 62 { 63 64 if (tz is null) 65 66 return time.toLocalTime().toISOExtString().replaceFirst("T", " "); 67 68 else 69 70 return time.toOtherTZ(tz).toISOExtString().replaceFirst("T", " "); 71 72 } 73 class DBException : Exception 74 75 { 76 77 this () { super("Unknown Error."); } 78 79 this (string msg, uint _code, string _sql) 80 81 { 82 83 super(msg~" ("~to!string(_code)~"), SQL: \""~_sql~"\""); 84 85 code = _code; 86 87 sql = _sql; 88 89 } 90 91 uint code; 92 93 string sql; 94 95 } 96 97 struct Variables(Database) 98 99 { 100 101 Database database; 102 103 string table; 104 105 private strstr values; 106 107 bool isSet(string name) 108 109 { 110 111 if (name in values) 112 113 return true; 114 115 else 116 117 return (database.queryValue("select count(*) from "~table~" where name='"~name~"'") != "0"); 118 119 } 120 121 string get(T:string)(string name) 122 123 { 124 125 if (!(name in values)) 126 127 { 128 129 auto s = database.queryValue("select value from "~table~" where name='"~name~"'"); 130 131 if (s is null) 132 133 return null; 134 135 else 136 137 { 138 139 values[name] = s; 140 141 } 142 143 } 144 145 return values[name]; 146 147 } 148 149 T get(T)(string name) 150 151 { 152 153 auto s = get!string(name); 154 155 return (s is null)?T.init:unserialize!T(s); 156 157 } 158 159 void set(T:string)(string name, string value) 160 161 { 162 163 values[name] = value; 164 165 database.query("replace "~table~" set name='"~name~"', value="~database.quote(value)); 166 167 } 168 169 void set(T)(string name, T value) 170 171 { 172 173 set!string(name,serialize!(T)(value)); 174 175 } 176 177 void unset(string name) 178 179 { 180 181 database.query("delete from "~table~" where name='"~name~"'"); 182 183 values.remove(name); 184 185 } 186 187 alias set!ulong setInt; 188 189 alias set!string setStr; 190 191 alias get!ulong getInt; 192 193 alias get!string getStr; 194 195 }