diff options
Diffstat (limited to 'lib/impure/db_mysql.nim')
-rwxr-xr-x | lib/impure/db_mysql.nim | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index 6284b7995..118ed39bb 100755 --- a/lib/impure/db_mysql.nim +++ b/lib/impure/db_mysql.nim @@ -18,7 +18,11 @@ type EDb* = object of EIO ## exception that is raised if a database error occurs TSqlQuery* = distinct string ## an SQL query string - + + FDb* = object of FIO ## effect that denotes a database operation + FReadDb* = object of FReadIO ## effect that denotes a read operation + FWriteDb* = object of FWriteIO ## effect that denotes a write operation + proc dbError(db: TDbConn) {.noreturn.} = ## raises an EDb exception. var e: ref EDb @@ -60,12 +64,18 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string = else: add(result, c) -proc TryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]): bool = +proc TryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]): bool {. + tags: [FReadDB, FWriteDb].} = ## tries to execute the query and returns true if successful, false otherwise. var q = dbFormat(query, args) return mysql.RealQuery(db, q, q.len) == 0'i32 -proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) = +proc rawExec(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) = + var q = dbFormat(query, args) + if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db) + +proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) {. + tags: [FReadDB, FWriteDb].} = ## executes the query and raises EDB if not successful. var q = dbFormat(query, args) if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db) @@ -80,11 +90,11 @@ proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) = mysql.FreeResult(sqlres) iterator FastRows*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): TRow = + args: varargs[string, `$`]): TRow {.tags: [FReadDB].} = ## executes the query and iterates over the result dataset. This is very ## fast, but potenially dangerous: If the for-loop-body executes another ## query, the results can be undefined. For MySQL this is the case!. - Exec(db, query, args) + rawExec(db, query, args) var sqlres = mysql.UseResult(db) if sqlres != nil: var L = int(mysql.NumFields(sqlres)) @@ -100,9 +110,9 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery, properFreeResult(sqlres, row) proc getRow*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): TRow = + args: varargs[string, `$`]): TRow {.tags: [FReadDB].} = ## retrieves a single row. - Exec(db, query, args) + rawExec(db, query, args) var sqlres = mysql.UseResult(db) if sqlres != nil: var L = int(mysql.NumFields(sqlres)) @@ -115,10 +125,10 @@ proc getRow*(db: TDbConn, query: TSqlQuery, properFreeResult(sqlres, row) proc GetAllRows*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): seq[TRow] = + args: varargs[string, `$`]): seq[TRow] {.tags: [FReadDB].} = ## executes the query and returns the whole result dataset. result = @[] - Exec(db, query, args) + rawExec(db, query, args) var sqlres = mysql.UseResult(db) if sqlres != nil: var L = int(mysql.NumFields(sqlres)) @@ -134,12 +144,12 @@ proc GetAllRows*(db: TDbConn, query: TSqlQuery, mysql.FreeResult(sqlres) iterator Rows*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): TRow = + args: varargs[string, `$`]): TRow {.tags: [FReadDB].} = ## same as `FastRows`, but slower and safe. for r in items(GetAllRows(db, query, args)): yield r proc GetValue*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): string = + args: varargs[string, `$`]): string {.tags: [FReadDB].} = ## executes the query and returns the result dataset's the first column ## of the first row. Returns "" if the dataset contains no rows. This uses ## `FastRows`, so it inherits its fragile behaviour. @@ -149,7 +159,7 @@ proc GetValue*(db: TDbConn, query: TSqlQuery, break proc TryInsertID*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): int64 = + args: varargs[string, `$`]): int64 {.tags: [FWriteDb].} = ## executes the query (typically "INSERT") and returns the ## generated ID for the row or -1 in case of an error. var q = dbFormat(query, args) @@ -159,24 +169,26 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery, result = mysql.InsertId(db) proc InsertID*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): int64 = + args: varargs[string, `$`]): int64 {.tags: [FWriteDb].} = ## executes the query (typically "INSERT") and returns the ## generated ID for the row. result = TryInsertID(db, query, args) if result < 0: dbError(db) proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, - args: varargs[string, `$`]): int64 = + args: varargs[string, `$`]): int64 {. + tags: [FReadDB, FWriteDb].} = ## runs the query (typically "UPDATE") and returns the ## number of affected rows - Exec(db, query, args) + rawExec(db, query, args) result = mysql.AffectedRows(db) -proc Close*(db: TDbConn) = +proc Close*(db: TDbConn) {.tags: [FDb].} = ## closes the database connection. if db != nil: mysql.Close(db) -proc Open*(connection, user, password, database: string): TDbConn = +proc Open*(connection, user, password, database: string): TDbConn {. + tags: [FDb].} = ## opens a database connection. Raises `EDb` if the connection could not ## be established. result = mysql.Init(nil) |