Skip to content

Commit

Permalink
Merge pull request #143 from itsumura-h/fix_mysql_insert_bool
Browse files Browse the repository at this point in the history
fix to load .env/fix mysql insert bool
  • Loading branch information
itsumura-h authored May 3, 2021
2 parents 771cfd0 + 6e19941 commit 012de4b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 34 deletions.
2 changes: 1 addition & 1 deletion allographer.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.17.1"
version = "0.17.2"
author = "Hidenobu Itsumura @dumblepytech1 as 'medy'"
description = "A Nim query builder library inspired by Laravel/PHP and Orator/Python"
license = "MIT"
Expand Down
21 changes: 21 additions & 0 deletions src/allographer/baseEnv.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os, strutils
import dotenv

for f in walkDir(getCurrentDir()):
if f.path.contains(".env"):
let env = initDotEnv(getCurrentDir(), f.path.split("/")[^1])
env.load()
echo("used config file '", f.path, "'")

const
DRIVER* = getEnv("DB_DRIVER","sqlite").string

let
CONN* = getEnv("DB_CONNECTION", getCurrentDir() / "db.sqlite3").string
USER* = getEnv("DB_USER", "").string
PASSWORD* = getEnv("DB_PASSWORD", "").string
DATABASE* = getEnv("DB_DATABASE", "").string
MAX_CONNECTION* = getEnv("DB_MAX_CONNECTION", "1").parseInt
IS_DISPLAY* = if existsEnv("LOG_IS_DISPLAY"): getEnv("LOG_IS_DISPLAY").string.parseBool else: false
IS_FILE* = if existsEnv("LOG_IS_FILE"): getEnv("LOG_IS_FILE").string.parseBool else: false
LOG_DIR* = if existsEnv("LOG_DIR"): getEnv("LOG_DIR").string else: ""
17 changes: 1 addition & 16 deletions src/allographer/connection.nim
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import os, strutils
import dotenv

const
DRIVER = getEnv("DB_DRIVER","sqlite").string

for f in walkDirRec(getCurrentDir(), {pcFile}):
if f.contains(".env"):
let env = initDotEnv(getCurrentDir(), f.split("/")[^1])
env.load()

let
CONN = getEnv("DB_CONNECTION", getCurrentDir() / "db.sqlite3").string
USER = getEnv("DB_USER", "").string
PASSWORD = getEnv("DB_PASSWORD", "").string
DATABASE = getEnv("DB_DATABASE", "").string
MAX_CONNECTION* = getEnv("DB_MAX_CONNECTION", "1").parseInt
import baseEnv

when DRIVER == "sqlite":
import db_sqlite
Expand Down
14 changes: 12 additions & 2 deletions src/allographer/query_builder/generators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ proc insertValueSql*(this: RDB, items: JsonNode): RDB =
elif val.kind == JFloat:
this.placeHolder.add($(val.getFloat()))
elif val.kind == JBool:
this.placeHolder.add($(val.getBool()))
let val =
if val.getBool():
1
else:
0
this.placeHolder.add($val)
elif val.kind == JObject:
this.placeHolder.add($val)
else:
Expand Down Expand Up @@ -319,7 +324,12 @@ proc insertValuesSql*(this: RDB, rows: openArray[JsonNode]): RDB =
elif val.kind == JFloat:
this.placeHolder.add($(val.getFloat()))
elif val.kind == JBool:
this.placeHolder.add($(val.getBool()))
let val =
if val.getBool():
1
else:
0
this.placeHolder.add($val)
elif val.kind == JObject:
this.placeHolder.add($val)
else:
Expand Down
15 changes: 2 additions & 13 deletions src/allographer/utils.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import os, parsecfg, terminal, logging, macros, strformat, strutils
# self
import baseEnv
from connection import getDriver

const
IS_DISPLAY = when existsEnv("LOG_IS_DISPLAY"): getEnv("LOG_IS_DISPLAY").string.parseBool else: false
IS_FILE = when existsEnv("LOG_IS_FILE"): getEnv("LOG_IS_FILE").string.parseBool else: false
LOG_DIR = when existsEnv("LOG_DIR"): getEnv("LOG_DIR").string else: ""


proc driverTypeError*() =
let driver = getDriver()
if driver != "sqlite" and driver != "mysql" and driver != "postgres":
Expand Down Expand Up @@ -67,13 +62,7 @@ proc liteWrapUpper(input:var string) =
input = &"\"{input}\""

proc myWrapUpper(input:var string) =
var isUpper = false
for c in input:
if c.isUpperAscii():
isUpper = true
break
if isUpper:
input = &"`{input}`"
input = &"`{input}`"

proc pgWrapUpper(input:var string) =
var isUpper = false
Expand Down
38 changes: 36 additions & 2 deletions tests/test_schema_builder.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest, json
import unittest, json, times
import ../src/allographer/schema_builder
import ../src/allographer/query_builder


suite "Schema builder":
Expand All @@ -18,7 +19,7 @@ suite "Schema builder":

Column().char("char", 100).unique().default("").unsigned().index(),
Column().string("string").unique().default("").unsigned().index(),
Column().text("text").default("").unique().default("").unsigned().index(),
Column().text("text").unique().default("").unsigned().index(),
Column().mediumText("mediumText").unique().default("").unsigned().index(),
Column().longText("longText").unique().default("").unsigned().index(),

Expand Down Expand Up @@ -95,3 +96,36 @@ suite "Schema builder":
# Column().json("json").default(%*{"key": "value"}),
# ], reset=true)
])

test "insert":
try:
rdb().table("sqlite").insert(%*{
"increments": 1,
"integer": 1,
"smallInteger": 1,
"mediumInteger": 1,
"bigInteger": 1,
"decimal": 111.11,
"double": 111.11,
"float": 111.11,
"char": "a",
"string": "a",
"text": "a",
"mediumText": "a",
"longText": "a",
"date": "2020-01-01".parse("yyyy-MM-dd").format("yyyy-MM-dd"),
"datetime": "2020-01-01".parse("yyyy-MM-dd").format("yyyy-MM-dd HH:MM:ss"),
"time": "2020-01-01".parse("yyyy-MM-dd").format("HH:MM:ss"),
"timestamp": "2020-01-01".parse("yyyy-MM-dd").format("yyyy-MM-dd HH:MM:ss"),
"binary": "a",
"boolean": true,
"enumField": "a",
"json": {"key": "value"}
})
assert true
alter(
drop("sqlite")
)
except:
echo getCurrentExceptionMsg()
assert false

0 comments on commit 012de4b

Please sign in to comment.