Skip to content

Commit 773432c

Browse files
committed
feat: add example for mysql
1 parent a892202 commit 773432c

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

example/mysql/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# MySQL Example for tqla
2+
3+
This example demonstrates how to use the `tqla` library with MySQL database.
4+
5+
## Set up the environment
6+
7+
### 1. Pull and run MySQL Docker container
8+
9+
```shell
10+
# Pull the latest MySQL image
11+
docker pull mysql:latest
12+
13+
# Start MySQL container
14+
docker run --name mysql_test -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=mysql-test-db -p 3306:3306 -d mysql:latest
15+
```

example/mysql/go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/VauntDev/tqla/example/mysql
2+
3+
go 1.21
4+
5+
replace github.com/VauntDev/tqla => ../../
6+
7+
require (
8+
github.com/VauntDev/tqla v0.0.0-00010101000000-000000000000
9+
github.com/go-sql-driver/mysql v1.8.1
10+
)
11+
12+
require filippo.io/edwards25519 v1.1.0 // indirect

example/mysql/go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
2+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
6+
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
10+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
11+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
12+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

example/mysql/main.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
8+
"github.com/VauntDev/tqla"
9+
_ "github.com/go-sql-driver/mysql"
10+
)
11+
12+
const (
13+
host = "localhost"
14+
port = 3306
15+
user = "root"
16+
password = "root"
17+
dbname = "mysql-test-db"
18+
)
19+
20+
type todo struct {
21+
Id int
22+
Title string
23+
Description string
24+
Completed bool
25+
}
26+
27+
const todoSchema = `CREATE TABLE IF NOT EXISTS todos (
28+
id INT PRIMARY KEY,
29+
title TEXT NOT NULL,
30+
description TEXT NOT NULL,
31+
completed BOOLEAN DEFAULT FALSE
32+
);`
33+
34+
func main() {
35+
36+
log.Println("connecting to db...")
37+
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", user, password, host, port, dbname)
38+
39+
db, err := sql.Open("mysql", dsn)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
defer db.Close()
44+
45+
log.Println("creating table if it does not exist...")
46+
if _, err := db.Exec(todoSchema); err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
todos := []*todo{
51+
{Id: 1, Title: "todo 1", Description: "first todo", Completed: false},
52+
{Id: 2, Title: "todo 2", Description: "second todo", Completed: false},
53+
{Id: 3, Title: "todo 3", Description: "third todo", Completed: false},
54+
{Id: 4, Title: "todo 4", Description: "fourth todo", Completed: false},
55+
{Id: 5, Title: "todo 5", Description: "fifth todo", Completed: false},
56+
}
57+
58+
t, err := tqla.New()
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
63+
log.Println("adding todos...")
64+
insertStmt, insertArgs, err := t.Compile(`
65+
{{ $len := 4 -}}
66+
INSERT INTO todos (id, title, description, completed)
67+
VALUES {{ range $i, $v := . }}
68+
( {{$v.Id}}, {{$v.Title}}, {{$v.Description}}, {{ $v.Completed }} ){{if lt $i $len}},{{else}};{{end -}}
69+
{{end}}
70+
`, todos)
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
75+
if _, err := db.Exec(insertStmt, insertArgs...); err != nil {
76+
log.Fatal(err)
77+
}
78+
79+
log.Println("looking up todo...")
80+
selectStmt, selectArgs, err := t.Compile(`SELECT * FROM todos WHERE id = {{ . }}`, 5)
81+
if err != nil {
82+
log.Fatal(err)
83+
}
84+
85+
todo := &todo{}
86+
row := db.QueryRow(selectStmt, selectArgs...)
87+
if err := row.Scan(&todo.Id, &todo.Title, &todo.Description, &todo.Completed); err != nil {
88+
log.Fatal(err)
89+
}
90+
91+
log.Println("found: ", todo)
92+
}

go.work

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ use (
44
.
55
./example/sqlite
66
./example/postgres
7+
./example/mysql
78
)

0 commit comments

Comments
 (0)