-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ce6a88
commit bc0641f
Showing
55 changed files
with
2,495 additions
and
155 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.