Skip to content

Commit

Permalink
fix sqlite query #235
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h committed Aug 5, 2023
1 parent 1ce6a88 commit bc0641f
Show file tree
Hide file tree
Showing 55 changed files with 2,495 additions and 155 deletions.
108 changes: 0 additions & 108 deletions example/models.nim

This file was deleted.

48 changes: 48 additions & 0 deletions example/rearchtect.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import std/asyncdispatch
import std/json
import std/httpclient
import std/streams
import std/options
import ../src/allographer/connection
import ../src/allographer/query_builder


proc main() {.async.} =
let client = newAsyncHttpClient()
let response = client.getContent("https://nim-lang.org/assets/img/twitter_banner.png").await
let imageStream = newStringStream(response)
let binaryImage = imageStream.readAll()


let rdb = dbOpen(SQLite3, "/root/project/db.sqlite3", shouldDisplayLog=true)
echo rdb.type

rdb.raw("DROP TABLE IF EXISTS \"test\"").exec().await

rdb.raw("""
CREATE TABLE IF NOT EXISTS "test" (
'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
'blob' BLOB,
'int' INTEGER,
'float' NUMERIC,
'str' VARCHAR
)"""
).exec().await

rdb.table("test").insert(%*{"blob":binaryImage, "int": 1, "float": 1.1, "str": "alice"}).await

let res = rdb.table("test").select("id", "int", "float", "str").get().await
for row in res:
echo row

var row = rdb.table("test").select("id", "int", "float", "str").first().await
if row.isSome:
echo row.get

rdb.table("test").where("id", "=", 1).update(%*{"str": "bob"}).await

row = rdb.table("test").select("id", "int", "float", "str").find(1).await
if row.isSome:
echo row.get

main().waitFor
20 changes: 18 additions & 2 deletions example/sqlite_binary.nim → example/sqlite/sqlite_binary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import std/asyncdispatch
import std/httpclient
import std/json
import std/streams
import ../src/allographer/connection
import ../src/allographer/query_builder/rdb/rdb_types
import ../src/allographer/query_builder/rdb/query/grammar
import ../src/allographer/query_builder/rdb/rdb_interface
import ../src/allographer/query_builder/rdb/databases/database_types
import ../src/allographer/query_builder/rdb/databases/sqlite/sqlite_rdb
import ../src/allographer/query_builder/rdb/databases/sqlite/sqlite_lib
# import ../src/allographer/query_builder/rdb/databases/sqlite/sqlite_lib
import ../src/allographer/query_builder/rdb/databases/sqlite/sqlite_impl


Expand Down Expand Up @@ -77,4 +81,16 @@ proc main(){.async.} =
if not res:
dbError(db)

main().waitFor

proc useAllographer() {.async.} =
let client = newAsyncHttpClient()
let response = client.getContent("https://nim-lang.org/assets/img/twitter_banner.png").await
let imageStream = newStringStream(response)
let binaryImage = imageStream.readAll()

let rdb = dbOpen(SQLite3, "/root/project/db.sqlite3")
rdb.table("test").insert(%*{"blob": binaryImage, "int": 100, "float": 1.1, "str": "alice"}).await


# main().waitFor
useAllographer().waitFor
69 changes: 69 additions & 0 deletions example/sqlite/sqlite_test1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import std/asyncdispatch
import std/httpclient
import std/streams
import std/json
import ../../src/allographer/connection
import ../../src/allographer/query_builder


proc main() {.async.} =
let client = newAsyncHttpClient()
let response = client.getContent("https://nim-lang.org/assets/img/twitter_banner.png").await
let imageStream = newStringStream(response)
let binaryImage = imageStream.readAll()

let rdb = dbOpen(SQLite3, "/root/project/db.sqlite3", shouldDisplayLog=true)
await rdb.raw("DROP TABLE IF EXISTS \"test\"").exec()
await rdb.raw("""
CREATE TABLE IF NOT EXISTS "test" (
'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
'bool' TINYINT,
'int' INTEGER,
'float' NUMERIC,
'str' VARCHAR,
'json' VARCHAR,
'null' VARCHAR,
'blob' BLOB
)
""").exec()

await rdb.table("test").insert(%*{
"blob": binaryImage,
"bool": true,
"int": 1,
"float": 1.1,
"str": "alice",
"json": {"name":"alice", "email":"[email protected]"},
"null": nil
})
echo rdb.table("test").get().await
echo rdb.select("id", "str").table("test").get().await

await rdb.table("test").where("id", "=", 1).update(%*{
"blob": binaryImage,
"bool": false,
"int": 2,
"float": 2.1,
"json": {"name":"bob", "email":"[email protected]"},
"str": "bob",
})
echo rdb.table("test").get().await

await rdb.raw(
"""
UPDATE "test"
SET
`bool` = ?,
`int` = ?,
`float` = ?,
`json` = ?,
`str` = ?,
`null` = ?
WHERE `id` = ?
""",
%*[true, 3, 3.3, {"name":"charlie", "email":"[email protected]"}, "charlie", nil, 1]
).exec()
echo rdb.raw("SELECT * FROM \"test\"").get().await


main().waitFor()
30 changes: 8 additions & 22 deletions src/allographer/connection.nim
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
import std/asyncdispatch
import std/json
import ./query_builder/log
import ./query_builder/rdb/rdb_types
import ./query_builder/rdb/query/exec
import ./query_builder/surreal/surreal_types
import ./query_builder/surreal/databases/surreal_impl
import ./query_builder/models/sqlite/sqlite_types
import ./query_builder/models/sqlite/sqlite_open
import ./query_builder/models/sqlite/sqlite_connections

export
Driver,
SurrealDb
SQLite3

proc dbOpen*(driver:Driver, database="", user="", password="",
host="", port=0, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""):Rdb =
result = new Rdb
result.driver = driver
result.conn = open(driver, database, user, password, host, port, maxConnections, timeout)
result.log = LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
result.query = newJObject()
result.isInTransaction = false

proc dbOpen*(_:type SurrealDb, namespace="", database="", user="", password="",
host="", port=0, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""):Future[SurrealDb] {.async.} =
result = new SurrealDb
result.conn = await SurrealImpl.open(namespace, database, user, password, host, port.int32, maxConnections, timeout)
proc dbOpen*(driver:type SQLite3, database="", user="", password="",
host="", port:int32=0, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""):SqliteConnections =
result = sqliteOpen(database, user, password, host, port, maxConnections, timeout)
result.log = LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
result.query = newJObject()
result.isInTransaction = false
30 changes: 30 additions & 0 deletions src/allographer/connection.nim.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import std/asyncdispatch
import std/json
import ./query_builder/log
import ./query_builder/rdb/rdb_types
import ./query_builder/rdb/query/exec
import ./query_builder/surreal/surreal_types
import ./query_builder/surreal/databases/surreal_impl

export
Driver,
SurrealDb

proc dbOpen*(driver:Driver, database="", user="", password="",
host="", port=0, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""):Rdb =
result = new Rdb
result.driver = driver
result.conn = open(driver, database, user, password, host, port, maxConnections, timeout)
result.log = LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
result.query = newJObject()
result.isInTransaction = false

proc dbOpen*(_:type SurrealDb, namespace="", database="", user="", password="",
host="", port=0, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""):Future[SurrealDb] {.async.} =
result = new SurrealDb
result.conn = await SurrealImpl.open(namespace, database, user, password, host, port.int32, maxConnections, timeout)
result.log = LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
result.query = newJObject()
result.isInTransaction = false
8 changes: 8 additions & 0 deletions src/allographer/query_builder.bk/enums.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Order* = enum
Asc = "ASC"
Desc = "DESC"

type Collation* = enum
None = ""
Collate = "COLLATE"
Numeric = "NUMERIC"
9 changes: 9 additions & 0 deletions src/allographer/query_builder.bk/error.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type
DbError* = object of IOError ## exception that is raised if a database error occurs

proc dbError*(msg: string) {.noreturn, noinline.} =
## raises an DbError exception with message `msg`.
var e: ref DbError
new(e)
e.msg = msg
raise e
Loading

0 comments on commit bc0641f

Please sign in to comment.