SEQUEL module
This module includes easy-to-use SQL statements.
This modules name is just here to trigger a good friend of mine :)
Types
DatabaseError = object of ValueError
- Generic error with database
Definition = tuple[id: int, word, definition, author: string, upvotes, downvotes: int, timestamp, sha3hash: string]
DuplicateHash = object of DatabaseError
- Duplicate entry
InvalidData = object of DatabaseError
- Missing fields (word/definition)
Consts
databaseName {.strdefine.} = "database-testing.db"
- Database name, can be overridden manually
databaseSwitch: string = "database-testing.db"
- Database switch (changes depending on if -d:release was used)
Procs
proc getAllDefinitions(): seq[Definition] {....raises: [DbError, DatabaseError], tags: [DbEffect, ReadDbEffect, WriteDbEffect], forbids: [].}
- Gets all definitions
proc getDefinitionById(id: string | int): Option[Definition]
- Gets definition by its ID
proc getDefinitionsByName(name: string): seq[Definition] {. ...raises: [DbError, DatabaseError], tags: [DbEffect, ReadDbEffect, WriteDbEffect], forbids: [].}
- Gets definitions by their name
proc getDefinitionsBySqlStatement(statement: SqlQuery; args: varargs[string]): seq[ Definition] {....raises: [DbError, DatabaseError, DbError], tags: [DbEffect, ReadDbEffect, WriteDbEffect], forbids: [].}
- Gets definitions based on statement and parameters
proc initDatabaseTables() {....raises: [DbError, DatabaseError, DbError], tags: [DbEffect, ReadDbEffect, WriteDbEffect], forbids: [].}
- Initialises all tables
proc newDefinition(definition: Definition; hash: string) {. ...raises: [InvalidData, DbError, DatabaseError], tags: [DbEffect, ReadDbEffect, WriteDbEffect], forbids: [].}
- New entry, author field is optional
proc newDefinition(word, description: string; author: string = ""; hash: string) {. ...raises: [InvalidData, DbError, DatabaseError, DbError], tags: [DbEffect, ReadDbEffect, WriteDbEffect], forbids: [].}
- New entry, author field is optional
proc toDefinition(row: Row): Definition {....raises: [ValueError], tags: [], forbids: [].}
- Converts Row to Definition
proc toDefinitions(rows: seq[Row]): seq[Definition] {....raises: [ValueError], tags: [], forbids: [].}
- Converts a sequence of Rows to a sequence of Definitions
Templates
template withDatabase(db: untyped; body: untyped)
-
Template to avoid writing repetitive code
Usage:
withDatabase db: db.exec(sql"DROP TABLE definitions;")