Simple database: Difference between revisions

D entry: shorter lines, modules themselves are namespaces, small formatting changes
m (→‎{{header|D}}: small change)
(D entry: shorter lines, modules themselves are namespaces, small formatting changes)
Line 637:
 
=={{header|D}}==
<lang d>import std.stdio, std.stringalgorithm, std.csvstring, std.fileconv, std.datetime, std.convarray,
std.file, std.arraycsv, std.algorithmdatetime;
 
static immutable filename = "simdb.csv";
void main(in string[] args) {
private struct SimDBItem {
if (args.length < 2 || args.length > 4)
static struct Item { string name, date, category; }
return SimDB.printUsage();
}
 
void mainaddItem(in string[] argsitem) {
switch (args[1].toLower()) {
if (item.length < 3)
case "add" : SimDB.addItem(args); break;
casereturn "latest" : SimDB.printLatestprintUsage(args); break;
staticauto Item[]db = load() {;
case "all" : SimDB.printAll(); break;
autoconst date = text(cast(DateTime)Clock.currTime()).text();
default : SimDB.printUsage(); break;
autoconst cat = (aitem.length == 4) ? aitem[3] : "none";
}
db ~= Item(item[2], date, cat);
store(db);
}
 
static void printLatest(in string[] a) {
private struct SimDB {
auto db = load();
static immutable filename = "simdb.csv";
if (db.empty)
static struct Item { string name, date, category; }
return writeln("No entries in database.");
db.sort!("q{ a.date > b.date")(db) };
if (a.length == 3) {
foreach (item; db)
if (item.category == a[2])
writefln("%s, %s, %s", item.tupleof);
} else {
writefln("%s, %s, %s", db[0].tupleof);
}
}
 
static void addItemprintAll(in string[] a) {
auto db = if load(a.length < 3);
if (db.empty)
return printUsage();
autoreturn dbwriteln("No =entries load(in database.");
db.sort!("q{ a.date < b.date")(db) };
auto date = (cast(DateTime)Clock.currTime()).text();
foreach (item; db)
auto cat = (a.length == 4) ? a[3] : "none";
db ~= Itemwritefln(a[2]"%s, %s, date%s", catitem.tupleof);
}
store(db);
}
 
Item[] load() {
static void printLatest(in string[] a) {
autoItem[] db = load();
if (dbfilename.emptyexists && filename.isFile) {
try {
return writeln("No entries in database.");
if (const string text = filename.readText(filename));
sort!("a.date > b.date")(db);
if (a!text.length == 3empty) {
foreach (item; db = csvReader!Item(text).array;
} ifcatch (item.categoryCSVException ==e) a[2]){
writeflnwriteln("%s, %s, %s", iteme.tupleofmsg);
} else {
writefln("%s, %s, %s", db[0].tupleof);
}
}
return db;
}
 
void store(in Item[] db;) {
static void printAll() {
auto dbf = loadFile(filename, "w+");
foreach (item; if (db.empty)
f.writefln("%s,%s,%s", item.tupleof);
return writeln("No entries in database.");
}
sort!("a.date < b.date")(db);
foreach (item; db)
writefln("%s, %s, %s", item.tupleof);
}
 
static void printUsage() {
writeln("Usage:\n simdb cmd [categoryname]\n");
`Usage:
writeln(" add\t add item, followed by optional category");
simdb cmd [categoryName]
writeln(" latest\t print last added item(s), followed by optional " ~
add add item, followed by optional "category");
latest print last added writelnitem(" add\t add items), followed by optional category");
writeln(" all\t print all");
all print all
writeln("\n for instance: add \"some item name\" " ~
"\"some category name\"");
}
 
writeln("\n for For instance: add \"some item name\" "some ~category name"`);
static Item[] load() {
}
Item[] db;
if (exists(filename) && isFile(filename)) {
try {
if (const string text = readText(filename))
db = csvReader!Item(text).array();
} catch (CSVException e) {
writeln(e.msg);
}
}
return db;
}
 
static void storemain(in Itemstring[] dbargs) {
if (args.length < 2 || args.length > 4)
auto f = File(filename, "w+");
foreachreturn printUsage(item); db)
 
f.writefln("%s,%s,%s", item.tupleof);
switch (args[1].toLower()) {
case "add": : SimDB.addItem(args); break;
case "latest": printLatest(args); break;
case "all": : SimDB.printAll(); break;
returndefault: SimDB.printUsage(); break;
}
}</lang>
Line 757 ⟶ 759:
all print all
 
forFor instance: add "some item name" "some category name"</pre>
File:
<pre>item1,2013-Mar-03 15:01:00,cat1