33 lines
879 B
Nim
33 lines
879 B
Nim
|
import tables
|
||
|
import std/[os, db_sqlite, strutils, streams]
|
||
|
|
||
|
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))
|
||
|
echo "Ran command: " & command.strip
|
||
|
db.close()
|
||
|
return true
|
||
|
|
||
|
export createDb
|