Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom functions #24

Merged
merged 35 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
17d40fd
fix https://github.com/Araq/ormin/issues/6
huaxk Dec 30, 2019
57341d2
add support to custom sql function
huaxk Dec 31, 2019
9c8fb05
recover file to origin
huaxk Jan 3, 2020
c778fa2
Merge pull request #1 from Araq/master
huaxk Jan 6, 2020
9a764c1
Create test.yml
huaxk Jan 7, 2020
ad96945
Update test.yml
huaxk Jan 7, 2020
6e64e65
Update test.yml
huaxk Jan 7, 2020
e0fc709
Update test.yml
huaxk Jan 7, 2020
1b712c4
Update test.yml
huaxk Jan 7, 2020
9880ddb
Update test.yml
huaxk Jan 7, 2020
fcc775b
Update test.yml
huaxk Jan 7, 2020
d16da7a
Update test.yml
huaxk Jan 7, 2020
7d55bd4
Update test.yml
huaxk Jan 7, 2020
e7fcd8c
Update test.yml
huaxk Jan 7, 2020
40c9d99
Update test.yml
huaxk Jan 8, 2020
5b036e0
add tests
huaxk Jan 8, 2020
bb5717b
Merge pull request #2 from huaxk/test
huaxk Jan 8, 2020
5764d83
run test
huaxk Jan 8, 2020
facd325
test no install sqlite
huaxk Jan 8, 2020
444ee5e
add test postgres
huaxk Jan 8, 2020
61002c7
try error password
huaxk Jan 8, 2020
a979c1c
test postgresql create table
huaxk Jan 8, 2020
8fcaca3
test postgresql insert and query
huaxk Jan 8, 2020
0c2bbd6
change pg password
huaxk Jan 8, 2020
d271b04
add db global
huaxk Jan 8, 2020
f554974
add .gitignore
huaxk Jan 8, 2020
9be19c6
add test from ormin
huaxk Jan 8, 2020
d639397
from .gitignore remove model_postgre.nim
huaxk Jan 8, 2020
357fb94
add before test task
huaxk Jan 8, 2020
cdb6a2a
add verbose
huaxk Jan 8, 2020
7384359
remove auto increase sequence from table
huaxk Jan 9, 2020
f545b5f
import ormin from local
huaxk Jan 9, 2020
846382d
test pg crud but delete has problem
huaxk Jan 9, 2020
bd2d02c
test sql function
huaxk Jan 9, 2020
dff93ae
test sql function row
huaxk Jan 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: Cache choosenim
id: cache-choosenim
uses: actions/cache@v1
with:
path: ~/.choosenim
key: ${{ runner.os }}-choosenim-stable

- name: Cache nimble
id: cache-nimble
uses: actions/cache@v1
with:
path: ~/.nimble
key: ${{ runner.os }}-nimble-stable

- uses: jiro4989/setup-nim-action@v1.0.1
# with:
# nim-version: 'stable'

- uses: harmon758/postgresql-action@v1
with:
postgresql version: '11'
postgresql user: 'test'
postgresql password: 'test'
postgresql db: 'test'

- name: Run test
run: nimble test --verbose
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tools/ormin_importer
tests/create_postgres
tests/tpostgres
tests/tsqlite
6 changes: 6 additions & 0 deletions ormin.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ bin = @["tools/ormin_importer"]

skipDirs = @["examples"]
installExt = @["nim"]

task test, "Run nimble test":
exec "nim c -r tests/tpostgres"

before test:
exec "nim c -r tests/create_postgres"
6 changes: 3 additions & 3 deletions ormin/queries.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const
nequals = "<>"

type
Function = object
Function* = object
name: string
arity: int # -1 for 'varargs'
typ: DbTypeKind # if dbUnknown, use type of the last argument

const
functions = [
var
functions {.compileTime.} = @[
Function(name: "count", arity: 1, typ: dbInt),
Function(name: "coalesce", arity: -1, typ: dbUnknown),
Function(name: "min", arity: 1, typ: dbUnknown),
Expand Down
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
switch("path", "$projectDir/../ormin")
22 changes: 22 additions & 0 deletions tests/create_postgres.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import strutils, sequtils, db_postgres
include model_postgres

let db = open("localhost", "test", "test", "test")

# find all tables in public schema
let exsitstables = db.getAllRows(sql"select tablename from pg_tables where schemaname='public'").mapIt(it[0])

# drop table in sql file
for name in tableNames:
if name in exsitstables:
echo "drop exsits table: " & name
db.exec(sql("drop table if exists " & name & " cascade"))

# create table in sql file
echo "create postgres table from sql: " & tableNames.join(", ")
let model = readFile("tests/model_postgres.sql")
for m in model.split(';'):
if m.strip != "":
db.exec(sql(m), [])

db.close()
26 changes: 26 additions & 0 deletions tests/model_postgres.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by ormin_importer. DO NOT EDIT.

type
Attr = object
name: string
tabIndex: int
typ: DbTypekind
key: int # 0 nothing special,
# +1 -- primary key
# -N -- references attribute N
const tableNames = [
"users",
"messages"
]

const attributes = [
Attr(name: "id", tabIndex: 0, typ: dbInt, key: 1),
Attr(name: "name", tabIndex: 0, typ: dbVarchar, key: 0),
Attr(name: "password", tabIndex: 0, typ: dbVarchar, key: 0),
Attr(name: "creation", tabIndex: 0, typ: dbTimestamp, key: 0),
Attr(name: "lastonline", tabIndex: 0, typ: dbTimestamp, key: 0),
Attr(name: "id", tabIndex: 1, typ: dbInt, key: 1),
Attr(name: "author", tabIndex: 1, typ: dbInt, key: -1),
Attr(name: "content", tabIndex: 1, typ: dbVarchar, key: 0),
Attr(name: "creation", tabIndex: 1, typ: dbTimestamp, key: 0)
]
16 changes: 16 additions & 0 deletions tests/model_postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
create table if not exists users(
id integer primary key ,
name varchar(20) not null,
password varchar(32) not null,
creation timestamp not null default CURRENT_TIMESTAMP,
lastOnline timestamp not null default CURRENT_TIMESTAMP
);

create table if not exists messages(
id integer primary key,
author integer not null,
content varchar(1000) not null,
creation timestamp not null default CURRENT_TIMESTAMP,

foreign key (author) references users(id)
);
99 changes: 99 additions & 0 deletions tests/tpostgres.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import unittest, sequtils, strformat
import ../ormin

importModel(DbBackend.postgre, "model_postgres")

static:
functions.add([
Function(name: "upper", arity: 1, typ: dbVarchar),
Function(name: "lower", arity: 1, typ: dbVarchar),
Function(name: "row", arity: -1, typ: dbUnknown),
])

suite "test postgres":
let db {.global.} = open("localhost", "test", "test", "test")

test "insert data":
for i in 1..3:
let name = fmt"john{i}"
let password = fmt"pass{i}"
query:
insert users(id = ?i, name = ?name, password = ?password,
lastOnline = !!"CURRENT_TIMESTAMP")

test "query data":
let users = query:
select users(id, name, password)
assert users == [1, 2, 3].mapIt((id: it, name: fmt"john{it}",
password: fmt"pass{it}"))

test "query count":
let count = query:
select users(count(_))
limit 1
assert count == 3

test "update data":
let updatedstr = "updated"
let id = 3
query:
update users(name = name || ?updatedstr,
lastOnline = !!"CURRENT_TIMESTAMP")
where id == ?id
let updatedname = query:
select users(name)
where id == ?id
limit 1
assert updatedname == fmt"john{id}{updatedstr}"

# test "delete data":
# let id = 3
# query:
# delete users
# where id == ?id
# let user = query:
# select users(_)
# where id == ?id
# assert user == []

test "more sql function upper":
let id = 1
let name = query:
select users(upper(name))
where id == ?id
limit 1
assert name == "JOHN1"

test "more sql function lower":
let id = 1
let name = query:
select users(lower(name))
where id == ?id
limit 1
assert name == "john1"

test "more sql function row with a parameter":
let id = 1
let name = query:
select users(row(name))
where id == ?id
limit 1
assert name == "(john1)"

# test "more sql function row with two parameters":
# let id = 1
# let row = query:
# select users(row(id, name))
# where id == ?id
# limit 1
# assert row == "(1, john1)"

test "ormin sql function coalesce with a parameter":
let id = 1
let name = query:
select users(coalesce(name))
where id == ?id
limit 1
assert name == "john1"

db.close()
15 changes: 15 additions & 0 deletions tests/tsqlite.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest
import db_sqlite

suite "connect to sqlite":
setup:
var db {.global.} = open("test.db", "", "", "")

test "create table":
db.exec(sql"""CREATE TABLE my_table (
id INTEGER,
name VARCHAR(50) NOT NULL
)""")

teardown:
db.close()
1 change: 1 addition & 0 deletions tools/ormin_importer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ proc getType(n: SqlNode): DbType =
of "inet", "ip", "tcpip": k = dbInet
of "mac", "macaddress": k = dbMacAddress
of "geometry": k = dbGeometry
# of "geometry": k = dbVarchar
of "point": k = dbPoint
of "line": k = dbLine
of "lseg": k = dbLseg
Expand Down