Skip to content

Commit fccafea

Browse files
committed
Refactor the gateway
- Add Query builder - Refactor Routine and Query statements - Use embed package instead of parcello
1 parent 2c7541a commit fccafea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+821
-1185
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
### Description
2+
23
Please explain the changes you made here.
34

45
### Checklist
6+
57
- [ ] Code compiles correctly
6-
- [ ] Created tests which fail without the change (if possible)
8+
- [ ] Created tests that fail without the change (if possible)
79
- [ ] All tests passing
810
- [ ] Extended the README.md / documentation, if necessary
9-
- [ ] Added myself / the copyright holder to the CONTRIBUTORS file
11+
- [ ] Added me / the copyright holder to the CONTRIBUTORS file

README.md

+31-162
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,25 @@
66
[![Coverage][codecov-img]][codecov-url]
77
[![Go Report Card][report-img]][report-url]
88

9-
*Golang Query Executor and Database Connector*
9+
The package facilitates execution of SQL scripts generated by
10+
[prana][prana-url]. Also it provides a query builder and object relation mapper.
11+
Note that it is in BETA. We may introduce breaking changes until we reach
12+
version 1.0.
1013

1114
[![ORM][orm-img]][orm-url]
1215

13-
## Overview
14-
15-
ORM is a package that facilitates execution of [loukoum][loukoum-url] queries
16-
as well as migrations and scripts generate by [prana][prana-url].
17-
1816
## Installation
1917

2018
```console
2119
$ go get -u github.com/phogolabs/orm
2220
```
2321

24-
## Introduction
25-
26-
Note that ORM is in BETA. We may introduce breaking changes until we reach
27-
v1.0.
28-
29-
Gateway API facilitates object relation mapping and query building by using
30-
[loukoum](loukoum-url) and [sqlx][sqlx-url].
22+
## Getting Started
3123

3224
Let's first import all required packages:
3325

3426
```golang
3527
import (
36-
lk "github.com/ulule/loukoum"
3728
"github.com/phogolabs/orm"
3829
)
3930
```
@@ -47,149 +38,30 @@ if err != nil {
4738
}
4839
```
4940

50-
### SQL Queries
51-
52-
All [loukoum][loukoum-url] queries are complaint with `orm.Query` interface:
53-
54-
```golang
55-
// Query returns the underlying query
56-
type Query interface {
57-
// Query prepares the query
58-
Query() (string, []Param)
59-
}
60-
61-
// NamedQuery returns the underlying query
62-
type NamedQuery interface {
63-
// Query prepares the query
64-
NamedQuery() (string, map[string]Param)
65-
}
66-
```
67-
68-
That allows easy execution of all kind of queries.
69-
70-
Because the package is empowered by [sqlx][sqlx-url]. It can perform field
71-
mapping of Golang structs by reading a `db` field tag. Let's assume that we
72-
have the following struct:
73-
74-
```golang
75-
// Package model contains an object model of database schema 'default'
76-
// Auto-generated at Thu Apr 19 21:36:35 CEST 2018
77-
package model
78-
79-
import null "gopkg.in/volatiletech/null.v6"
80-
81-
// User represents a data base table 'users'
82-
type User struct {
83-
// ID represents a database column 'id' of type 'INT PRIMARY KEY NOT NULL'
84-
ID int `db:"id,primary_key,not_null" json:"id" xml:"id" validate:"required"`
85-
86-
// FirstName represents a database column 'first_name' of type 'TEXT NOT NULL'
87-
FirstName string `db:"first_name,not_null" json:"first_name" xml:"first_name" validate:"required"`
88-
89-
// LastName represents a database column 'last_name' of type 'TEXT NULL'
90-
LastName null.String `db:"last_name,null" json:"last_name" xml:"last_name" validate:"-"`
91-
}
92-
```
93-
94-
#### Insert a new record
95-
96-
```golang
97-
98-
query := lk.Insert("users").
99-
Set(
100-
lk.Pair("first_name", "John"),
101-
lk.Pair("last_name", "Doe"),
102-
)
103-
104-
if _, err := gateway.Exec(query); err != nil {
105-
return err
106-
}
107-
```
108-
109-
#### Select all records
110-
111-
```golang
112-
query := lk.Select("id", "first_name", "last_name").From("users")
113-
users := []User{}
114-
115-
if err := gateway.Select(&users, query); err != nil {
116-
return err
117-
}
118-
```
119-
120-
#### Select a record
121-
122-
```golang
123-
query := lk.Select("id", "first_name", "last_name").
124-
From("users").
125-
Where(lk.Condition("first_name").Equal("John"))
126-
127-
user := User{}
128-
129-
if err := gateway.SelectOne(&user, query); err != nil {
130-
return err
131-
}
132-
```
133-
134-
You can read more details about [loukoum][loukoum-url] on their repository.
135-
136-
#### RQL Support
137-
138-
The package support [RQL](https://github.com/a8m/rql) queries. RQL provides a
139-
simple and light-weight API for adding dynamic querying capabilities to
140-
web-applications that use SQL-based database.
141-
142-
Let's have the following query that is received via HTTP:
143-
144-
```json
145-
{
146-
"$or": [
147-
{ "city": "TLV" },
148-
{ "zip": { "$gte": 49800, "$lte": 57080 } }
149-
]
150-
}
151-
```
152-
153-
Then we can process it in the following way:
41+
## SQL Migrations
15442

155-
```golang
156-
param := &orm.RQLQuery{}
157-
158-
err := json.Unmarshal(body, param)
159-
if err != nil {
160-
panic(err)
161-
}
162-
163-
rows, err := gateway.Query(orm.RQL("addresses", param))
164-
```
165-
166-
### SQL Migrations with Prana
167-
168-
You can execute the migration generated by [Prana][prana-url]. First, you
169-
should load the migration directory by using [Parcello][parcello-url]. You can
170-
load it from embedded resource or from the local directory:
43+
You can execute the migration generated by [prana][prana-url]. For that you have
44+
to use either [embed][embed-url] package or [os][os-url] package.
17145

17246
```golang
173-
if err := gateway.Migrate(parcello.Dir("./database/migration")); err != nil {
47+
if err := gateway.Migrate(resource); err != nil {
17448
return err
17549
}
17650
```
17751

178-
### SQL Scripts and Routines with Prana
52+
## SQL Queries
17953

180-
ORM provides a way to work with embeddable SQL scripts. It can understand
181-
[SQL Scripts](https://github.com/phogolabs/prana#sql-scripts-and-commands) from
182-
file and execute them as standard SQL queries. Let's assume that we have SQL
183-
query named `show-sqlite-master`.
54+
The package provides a way to work with embeddable SQL scripts. It understands predefined files with [SQL Scripts](https://github.com/phogolabs/prana#sql-scripts-and-commands).
18455

185-
Let's first load the SQL script from file:
56+
It executes them as standard SQL queries. Let's define a SQL routines named `insert-user` and `select-all-users`:
18657

18758
```
188-
-- name: show-sqlite-master
189-
SELECT * FROM sqlite_master;
59+
-- name: insert-user
60+
INSERT INTO users (id, first_name, last_name)
61+
VALUES (:id, :first_name, :last_name);
19062
191-
-- named: show-table
192-
SELECT * FROM {{table}};
63+
-- named: select-all-users
64+
SELECT * FROM users;
19365
```
19466

19567
```golang
@@ -201,29 +73,26 @@ if err = gateway.ReadFrom(file); err != nil {
20173
Then you can execute the desired script by just passing its name:
20274

20375
```golang
204-
_, err = gateway.Exec(orm.Routine("show-sqlite-master"))
76+
routine := orm.Routine("select-all-users")
77+
// execute the routine
78+
_, err = gateway.All(context.TODO(), routine, &users)
20579
```
20680

207-
Also you can Raw SQL Scripts from your code, you should follow this
208-
example:
209-
21081
```golang
211-
rows, err := gateway.Query(orm.SQL("SELECT * FROM users WHERE id = ?", 5432))
82+
routine := orm.Routine("insert-user", &user)
83+
// execute the routine
84+
_, err = gateway.Exec(context.TODO(), routine)
21285
```
21386

214-
Also you can use [handlerbars](https://handlebarsjs.com) to template your
215-
query. But be aware how you use it in order not to open possible SQL
216-
injections:
87+
Also you can execute raw SQL Scripts from your code:
21788

21889
```golang
219-
param := orm.Map{
220-
"table": "users",
221-
}
222-
223-
_, err = gateway.Exec(orm.Routine("show-table", param))
90+
query := orm.Query("SELECT * FROM users WHERE id = ?", 5432)
91+
// fetch the records as a slice of users
92+
rows, err := gateway.Only(context.TODO(), query, &user)
22493
```
22594

226-
### Example
95+
## Example
22796

22897
You can check our [Getting Started Example](/example).
22998

@@ -242,7 +111,7 @@ We are open for any contributions. Just fork the
242111
[logo-author-url]: https://www.freepik.com/free-photos-vectors/tree
243112
[logo-license]: http://creativecommons.org/licenses/by/3.0/
244113
[orm-url]: https://github.com/phogolabs/orm
245-
[orm-img]: doc/img/logo.png
114+
[orm-img]: media/img/logo.png
246115
[codecov-url]: https://codecov.io/gh/phogolabs/orm
247116
[codecov-img]: https://codecov.io/gh/phogolabs/orm/branch/master/graph/badge.svg
248117
[action-img]: https://github.com/phogolabs/orm/workflows/main/badge.svg
@@ -252,7 +121,7 @@ We are open for any contributions. Just fork the
252121
[godoc-img]: https://godoc.org/github.com/phogolabs/orm?status.svg
253122
[license-img]: https://img.shields.io/badge/license-MIT-blue.svg
254123
[software-license-url]: LICENSE
255-
[loukoum-url]: https://github.com/ulule/loukoum
256-
[parcello-url]: https://github.com/phogolabs/parcello
124+
[embed-url]: https://golang.org/pkg/embed
125+
[os-url]: https://golang.org/pkg/os
257126
[prana-url]: https://github.com/phogolabs/prana
258127
[sqlx-url]: https://github.com/jmoiron/sqlx

_example/database/generate.go

-4
This file was deleted.

_example/database/resource.go

-93
This file was deleted.

0 commit comments

Comments
 (0)