Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
68
Task/Simple-database/Wren/simple-database-2.wren
Normal file
68
Task/Simple-database/Wren/simple-database-2.wren
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import "os" for Process
|
||||
import "./table" for Table, FieldInfo, File
|
||||
import "./str" for Str
|
||||
|
||||
var printUsage = Fn.new {
|
||||
System.print("""
|
||||
Usage:
|
||||
wren simdb.wren cmd [categoryName]
|
||||
add add item name and date, followed by optional category
|
||||
latest print item with latest date, followed by optional category
|
||||
all print all
|
||||
For instance: add "some item name", "some item date", "some category name"
|
||||
Dates should be in format: yyyy-mm-dd
|
||||
""")
|
||||
}
|
||||
|
||||
var printLatest = Fn.new { |table, a|
|
||||
if (table.isEmpty) {
|
||||
System.print("No entries in table.")
|
||||
return
|
||||
}
|
||||
var records = table.records
|
||||
records.sort { |s, t| Str.lt(s[1], t[1]) } // sort by ascending date
|
||||
if (a.count == 1) {
|
||||
var found = false
|
||||
for (record in records[-1..0]) {
|
||||
if (record[2] == a[0]) {
|
||||
System.print(record)
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!found) System.print("There are no records for category '%(a[0])'.")
|
||||
} else System.print(records[-1])
|
||||
}
|
||||
|
||||
var args = Process.arguments
|
||||
if (!(1..4).contains(args.count)) {
|
||||
printUsage.call()
|
||||
return
|
||||
}
|
||||
|
||||
// create a new Table object
|
||||
var tableName = "simdb"
|
||||
var table
|
||||
if (Table.fileExists(tableName)) {
|
||||
table = Table.load(tableName)
|
||||
} else {
|
||||
var fis = [
|
||||
FieldInfo.new("name", String),
|
||||
FieldInfo.new("date", String),
|
||||
FieldInfo.new("category", String)
|
||||
]
|
||||
table = Table.new(tableName, fis)
|
||||
}
|
||||
|
||||
var cmd = Str.lower(args[0])
|
||||
if (cmd == "add") {
|
||||
if (args.count < 4) args.add("none")
|
||||
table.add(args[1..-1])
|
||||
table.save()
|
||||
} else if (cmd == "latest") {
|
||||
printLatest.call(table, args[1..-1])
|
||||
} else if (cmd == "all") {
|
||||
table.list()
|
||||
} else {
|
||||
printUsage.call()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue