Future(s) for D.
Etienne via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jun 22 07:22:11 PDT 2015
On Monday, 22 June 2015 at 14:03:47 UTC, Laeeth Isharc wrote:
> On Monday, 22 June 2015 at 11:29:50 UTC, Jacob Carlborg wrote:
>> On 22/06/15 01:43, Nick Sabalausky wrote:
>>
>>> Curiosity, what libraries do you feel a lack of?
>>
>> For work, that would be:
>
>> * Some way to interface with Selenium and PhantomJS
>
> It's not pretty, but for what I want to do I have found
> embedding Python calls to phantomjs code within a D program
> works quite well.
>
> The SQLite stuff looks okay, although I haven't used for a big
> project yet.
This is how I use it as a persistent storage for property
get/setters. Takes some imagination at first, but it saves a huge
amount of time/headaches on the long run.
/// Opens the SQLite database from the data folder. The user must
call .close() when finished!
Database openDB(bool read = true) {
// DB is always created in static ctor
return Database(DATA_FOLDER_PATH() ~ "my.db", read ?
SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE |
SQLITE_OPEN_CREATE));
}
private T get(T)(string name, lazy T default_value = T.init) {
Database sqlite = openDB(READ);
scope(exit) sqlite.close();
T ret;
try {
auto results = sqlite.execute("SELECT value FROM config WHERE
name='" ~ name ~ "';");
auto val = results.oneValue!string;
ret = val.to!T;
}
catch { ret = default_value; }
return ret;
}
private void set(T)(string name, T value) {
Database sqlite = openDB(WRITE);
scope(exit) sqlite.close();
bool exists;
try {
ResultRange res = sqlite.prepare("SELECT value FROM config
WHERE name=?;").bind(1, name).execute();
string val = res.oneValue!string; // will throw if the value
is not there
// Update the existing value.
auto prep = sqlite.prepare("UPDATE TABLE config SET value=?
WHERE name=?;").bind(1, name).execute();
prep.bind(1, value.to!string);
res = prep.execute();
}
catch {
exists = false;
}
if (!exists) {
auto res = sqlite.prepare("INSERT INTO config (name, value)
VALUES (?, ?);").bind(1, name).bind(2, value.to!string).execute();
}
}
// usage:
@property Duration created() {
return get!ulong("created", 120).dur!"seconds";
}
@property void created(Duration val) {
set("created", val.total!"seconds");
}
More information about the Digitalmars-d
mailing list