Simple database: Difference between revisions

m (→‎{{header|Raku}}: Fix code: Perl 6 --> Raku)
Line 2,670:
item5, 2018-03-23 16:49:46, cat3
</pre>
 
=={{header|Nim}}==
{{trans|D}}
{{trans|Kotlin}}
There are some important differences with D version and Kotlin versions. Firstly, the way to manage the command arguments is somewhat different. And secondly, the database is stored in JSON format rather than in CSV format. Indeed, if Nim provides a parser of CSV files, it provides better support for JSON with procedures for serialization of Nim objects to JSON and deserialization from JSON to Nim objects. This can be seen in the following program.
 
<lang Nim>import algorithm, json, os, strformat, strutils, times
 
const FileName = "simdb.json"
 
type
 
Item = object
name: string
date: string
category: string
 
Database = seq[Item]
 
DbError = object of CatchableError
 
 
proc load(): Database =
if fileExists(FileName):
let node = try: FileName.parseFile()
except JsonParsingError:
raise newException(DbError, getCurrentExceptionMsg())
result = node.to(DataBase)
 
 
proc store(db: Database) =
try:
FileName.writeFile $(%* db)
except IOError:
quit "Unable to save database.", QuitFailure
 
 
proc addItem(args: seq[string]) =
var db = try: load()
except DbError: quit getCurrentExceptionMsg(), QuitFailure
 
let date = now().format("yyyy-MM-dd HH:mm:ss")
let cat = if args.len == 2: args[1] else: "none"
db.add Item(name: args[0], date: date, category: cat)
db.store()
 
 
proc printLatest(args: seq[string]) =
let db = try: load()
except DbError: quit getCurrentExceptionMsg(), QuitFailure
if db.len == 0:
echo "No entries in database."
return
 
# No need to sort db as items are added chronologically.
if args.len == 1:
var found = false
for item in reversed(db):
if item.category == args[0]:
echo item
found = true
break
if not found:
echo &"There are no items for category '{args[0]}'"
else:
echo db[^1]
 
 
proc printAll() =
let db = try: load()
except DbError: quit getCurrentExceptionMsg(), QuitFailure
if db.len == 0:
echo "No entries in database."
return
for item in db:
echo item
 
 
proc printUsage() =
echo &"""
Usage:
{getAppFilename().splitPath().tail} cmd [categoryName]
 
add add item, followed by optional category
latest print last added item(s), followed by optional category
all print all
 
For instance: add "some item name" "some category name"
"""
quit QuitFailure
 
 
if paramCount() notin 1..3: printUsage()
 
var params = commandLineParams()
let command = params[0].toLowerAscii
params.delete(0)
case command
of "add":
if params.len == 0: printUsage()
addItem(params)
of "latest":
if params.len > 1: printUsage()
printLatest(params)
of "all":
if params.len != 0: printUsage()
printAll()</lang>
 
{{out}}
Sample session (same as that of Kotlin).
<pre>$ ./simple_database add item1
$ ./simple_database add item2
$ ./simple_database add item3 cat3
$ ./simple_database add item4 cat3
$ ./simple_database add item5 cat3
$ ./simple_database latest
(name: "item5", date: "2021-04-09 00:31:34", category: "cat3")
$ ./simple_database latest none
(name: "item2", date: "2021-04-09 00:31:08", category: "none")
$ ./simple_database latest cat4
There are no items for category 'cat4'
$ ./simple_database all
(name: "item1", date: "2021-04-09 00:31:03", category: "none")
(name: "item2", date: "2021-04-09 00:31:08", category: "none")
(name: "item3", date: "2021-04-09 00:31:19", category: "cat3")
(name: "item4", date: "2021-04-09 00:31:25", category: "cat3")
(name: "item5", date: "2021-04-09 00:31:34", category: "cat3")</pre>
 
=={{header|Perl}}==
Anonymous user