6
6
[ ![ Coverage] [ codecov-img ]] [ codecov-url ]
7
7
[ ![ Go Report Card] [ report-img ]] [ report-url ]
8
8
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.
10
13
11
14
[ ![ ORM] [ orm-img ]] [ orm-url ]
12
15
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
-
18
16
## Installation
19
17
20
18
``` console
21
19
$ go get -u github.com/phogolabs/orm
22
20
```
23
21
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
31
23
32
24
Let's first import all required packages:
33
25
34
26
``` golang
35
27
import (
36
- lk " github.com/ulule/loukoum"
37
28
" github.com/phogolabs/orm"
38
29
)
39
30
```
@@ -47,149 +38,30 @@ if err != nil {
47
38
}
48
39
```
49
40
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
154
42
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.
171
45
172
46
``` golang
173
- if err := gateway.Migrate (parcello. Dir ( " ./database/migration " ) ); err != nil {
47
+ if err := gateway.Migrate (resource ); err != nil {
174
48
return err
175
49
}
176
50
```
177
51
178
- ### SQL Scripts and Routines with Prana
52
+ ## SQL Queries
179
53
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 ) .
184
55
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 ` :
186
57
187
58
```
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);
190
62
191
- -- named: show-table
192
- SELECT * FROM {{table}} ;
63
+ -- named: select-all-users
64
+ SELECT * FROM users ;
193
65
```
194
66
195
67
``` golang
@@ -201,29 +73,26 @@ if err = gateway.ReadFrom(file); err != nil {
201
73
Then you can execute the desired script by just passing its name:
202
74
203
75
``` 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)
205
79
```
206
80
207
- Also you can Raw SQL Scripts from your code, you should follow this
208
- example:
209
-
210
81
``` 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)
212
85
```
213
86
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:
217
88
218
89
``` 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)
224
93
```
225
94
226
- ### Example
95
+ ## Example
227
96
228
97
You can check our [ Getting Started Example] ( /example ) .
229
98
@@ -242,7 +111,7 @@ We are open for any contributions. Just fork the
242
111
[ logo-author-url ] : https://www.freepik.com/free-photos-vectors/tree
243
112
[ logo-license ] : http://creativecommons.org/licenses/by/3.0/
244
113
[ orm-url ] : https://github.com/phogolabs/orm
245
- [ orm-img ] : doc /img/logo.png
114
+ [ orm-img ] : media /img/logo.png
246
115
[ codecov-url ] : https://codecov.io/gh/phogolabs/orm
247
116
[ codecov-img ] : https://codecov.io/gh/phogolabs/orm/branch/master/graph/badge.svg
248
117
[ action-img ] : https://github.com/phogolabs/orm/workflows/main/badge.svg
@@ -252,7 +121,7 @@ We are open for any contributions. Just fork the
252
121
[ godoc-img ] : https://godoc.org/github.com/phogolabs/orm?status.svg
253
122
[ license-img ] : https://img.shields.io/badge/license-MIT-blue.svg
254
123
[ 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
257
126
[ prana-url ] : https://github.com/phogolabs/prana
258
127
[ sqlx-url ] : https://github.com/jmoiron/sqlx
0 commit comments