Skip to content

Commit e69978e

Browse files
committed
add readme
1 parent 687ed06 commit e69978e

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

README.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Go SQL Utility Documentation
2+
3+
![GitHub Tag](https://img.shields.io/github/v/tag/go-universal/sql?sort=semver&label=version)
4+
[![Go Reference](https://pkg.go.dev/badge/github.com/go-universal/sql.svg)](https://pkg.go.dev/github.com/go-universal/sql)
5+
[![License](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/go-universal/sql/blob/main/LICENSE)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/go-universal/sql)](https://goreportcard.com/report/github.com/go-universal/sql)
7+
![Contributors](https://img.shields.io/github/contributors/go-universal/sql)
8+
![Issues](https://img.shields.io/github/issues/go-universal/sql)
9+
10+
`sql` is a Go library designed to simplify database interactions, migrations, and query management.
11+
12+
## Packages
13+
14+
### Query Builder
15+
16+
The ConditionBuilder provide functions for dynamically constructing SQL conditions.
17+
18+
```go
19+
import "github.com/go-universal/sql/query"
20+
21+
func main() {
22+
cond := query.NewCondition(query.NumbericResolver)
23+
cond.And("name = ?", "John").
24+
AndClosure("age > ? AND age < ?", 9, 31).
25+
OrIf(false, "age IS NULL").
26+
OrClosureIf(true, "membership @in", "admin", "manager", "accountant")
27+
28+
// Result: "name = $1 AND (age > $2 AND age < $3) OR (membership IN ($4, $5, $6))"
29+
}
30+
```
31+
32+
### Query Manager
33+
34+
The `query` package provides tools for managing and generating SQL queries.
35+
36+
#### Example
37+
38+
```go
39+
import (
40+
"github.com/go-universal/sql/query"
41+
"github.com/go-universal/fs"
42+
)
43+
func main() {
44+
queriesFS := fs.NewDir("database/queries")
45+
queryManager, _err_ := query.NewQueryManager(fs, query.WithRoot("database"))
46+
47+
usersList := queryManager.Get("queries/users/users_list")
48+
usersTrash := queryManager.Get("queries/users/deleted users")
49+
customers, exists := queryManager.Find("queries/customers/list")
50+
customers, exists := queryManager.Find("queries/customers/deleted") // "", false
51+
}
52+
```
53+
54+
Query files style:
55+
56+
```sql
57+
-- users.sql
58+
59+
-- { query: users_list }
60+
SELECT * FROM users WHERE `deleted_at` IS NULL AND `name` LIKE ?;
61+
62+
-- { query: deleted users }
63+
SELECT * FROM users WHERE deleted_at IS NOT NULL;
64+
65+
66+
-- customers.sql
67+
-- { query: list }
68+
SELECT * from customers;
69+
```
70+
71+
### Postgres Package
72+
73+
The `postgres` package provides tools for constructing and executing SQL commands specifically for PostgreSQL databases. Query placeholders must `?`.
74+
75+
```go
76+
package main
77+
78+
import (
79+
"context"
80+
81+
"github.com/go-universal/sql/postgres"
82+
"github.com/jackc/pgx/v5/pgconn"
83+
)
84+
85+
func main() {
86+
ctx := context.Background()
87+
config := postgres.NewConfig().
88+
Host("localhost").
89+
Port(5432).
90+
User("postgres").
91+
Password("password").
92+
Database("test").
93+
SSLMode("disable").
94+
MinConns(2)
95+
conn, err := postgres.New(
96+
ctx, config.Build(),
97+
func(c *pgxpool.Config) { c.MaxConns = 7 },
98+
)
99+
defer conn.Close(ctx)
100+
101+
cmd := postgres.NewCmd(conn)
102+
result, err := cmd.Command("INSERT INTO users (name) VALUES (?)").Exec(ctx, "John Doe")
103+
}
104+
```
105+
106+
### MySQL Package
107+
108+
The `mysql` package provides tools for constructing and executing SQL commands specifically for MySQL databases.
109+
110+
```go
111+
package main
112+
113+
import (
114+
"context"
115+
116+
"github.com/go-universal/sql/mysql"
117+
)
118+
119+
func main() {
120+
conn, err := mysql.New(
121+
context.Background(),
122+
mysql.NewConfig().Database("test").Password("root").Build(),
123+
)
124+
if err != nil {
125+
log.Fatal(err)
126+
}
127+
128+
cmd := mysql.NewCmd(conn)
129+
result, err := cmd.Command("INSERT INTO users (name) VALUES (?)").Exec(context.Background(), "John Doe")
130+
}
131+
```
132+
133+
### Migration Package
134+
135+
The `migration` package provides tools for managing database migrations by stage.
136+
137+
```go
138+
package main
139+
140+
import (
141+
"log"
142+
143+
"github.com/go-universal/fs"
144+
"github.com/go-universal/sql/migration"
145+
"github.com/go-universal/sql/mysql"
146+
)
147+
148+
func main() {
149+
conn := CreateConnection()
150+
fs := CreateFS()
151+
mig, err := migration.NewMigration(
152+
migration.NewMySQLSource(conn),
153+
fs,
154+
migration.WithRoot("migrations"),
155+
)
156+
157+
err := mig.Up([]string{"table", "index", "seed"})
158+
if err != nil {
159+
log.Fatal(err)
160+
}
161+
}
162+
```
163+
164+
Migration files style:
165+
166+
```sql
167+
-- 1741791024-create-users-table.sql
168+
-- { up: table } table is sectin name
169+
CREATE TABLE IF NOT EXISTS ...
170+
171+
-- { down: table }
172+
173+
-- { up: index }
174+
...
175+
176+
-- { down: index }
177+
...
178+
179+
-- { up: seed }
180+
...
181+
182+
-- { down: seed }
183+
...
184+
```
185+
186+
## License
187+
188+
This library is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)