2022-04-13 15:14:13 +00:00
|
|
|
import tables
|
|
|
|
import std/[os, db_sqlite, strutils, streams]
|
2022-04-13 20:22:52 +00:00
|
|
|
import formatter
|
2022-04-13 15:14:13 +00:00
|
|
|
|
|
|
|
proc createDb(dbSettings: Table): bool =
|
|
|
|
if not fileExists(dbSettings["path"]):
|
|
|
|
var userConsent: bool = false
|
|
|
|
while userConsent != true:
|
|
|
|
stdout.write "Did not find database. Create one now? (y/n) "
|
|
|
|
stdout.flushFile()
|
|
|
|
var userInput = readChar(stdin)
|
|
|
|
stdin.flushFile()
|
|
|
|
case userInput
|
|
|
|
of 'y', 'Y':
|
|
|
|
userConsent = true
|
|
|
|
of 'n', 'N':
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
let
|
|
|
|
schema = readFile(dbSettings["schema_path"])
|
|
|
|
db = open(dbSettings["path"], "", "", "")
|
|
|
|
for command in schema.split(";"):
|
|
|
|
# Skip "command" if it's just a blank line
|
|
|
|
if command == "\c\n" or command == "\n":
|
|
|
|
continue
|
|
|
|
db.exec(sql(command.strip))
|
2022-04-13 20:22:52 +00:00
|
|
|
logPrint("Ran SQL command from schema", "Info")
|
2022-04-13 15:14:13 +00:00
|
|
|
db.close()
|
|
|
|
return true
|
|
|
|
|
|
|
|
export createDb
|