Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zolern committed Feb 15, 2019
1 parent 9b69b10 commit 3ecbd0f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/aggregations.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import adodb, random, strformat

let
connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb"
db = newADODB(connect)
tbl = "users"
uid = "user_id"


var userId: int = nz(db.dMax(uid, tbl), 0)

db.exec(sql"DELETE * FROM {tbl}")

proc iteration(id: var int) =
id.inc

db.exec(sql"INSERT INTO {tbl} ( {uid}, user_name, user_balance ) VALUES ({id}, 'NEW TEST USER {id}', {rand(1000.0)})")

let
count = db.dCount(uid, tbl)
sum: float = db.dSum("user_balance", tbl)
avg: float = db.dAvg("user_balance", tbl)

if count >= 1000:
# keep count of records to not exceed 1000
let minID = db.dMin(uid, tbl)
db.exec(sql"DELETE * FROM {tbl} WHERE (({uid})={minID})")

stdout.write('\r', fmt"MaxID: {id:>5}, Count: {count:>4}, Total sum: {sum:>9.2f}, Average: {avg:>6.2f}")

while true:
iteration(userId)
28 changes: 28 additions & 0 deletions examples/sqloperations.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import adodb

let
connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb"
ado = newADODB(connect)

prefix = "Test User"
id = 1

tbl = "Users"

ado.exec(sql"DELETE * FROM {tbl}")

ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id}, '{prefix} {id}')")
ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id + 1}, '{prefix} {id + 1}')")
ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id + 2}, '{prefix} {id + 2}')")
ado.exec(sql"INSERT INTO {tbl} ( user_id, user_name ) VALUES ({id + 3}, '{prefix} {id + 3}')")

let updateSQL = sql"UPDATE {tbl} SET"
ado.exec(sql"{updateSQL} user_bday=#31.12.1999# WHERE ((user_id)={id})") # dd.mm.yyyy
ado.exec(sql"{updateSQL} user_bday=#{1985, 8, 22} WHERE ((user_id)={id + 1})") # mm/dd/yyyy
ado.exec(sql"{updateSQL} user_bday=#1987-11-15# WHERE ((user_id)={id + 2})") # yyyy-mm-dd

let rst = ado.query($&"SELECT * FROM {tbl}")

for rowIdx, row in rst:
for idx, name, fld in row.fields:
echo "Row ", rowIdx, ", Fld[", idx," | ", name, "] = ", nz(fld, "!!! Not set !!!")
Binary file added examples/test.mdb
Binary file not shown.

0 comments on commit 3ecbd0f

Please sign in to comment.