-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sqlc/sqlc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Different ways of working with SQL Databases in Go | ||
|
||
Read the full article at https://packagemain.tech |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func main() { | ||
conn, err := sql.Open("sqlite3", "db.sqlite") | ||
if err != nil { | ||
log.Fatalf("failed to open db: %v", err) | ||
} | ||
|
||
users, err := getUsersStats(conn, 2) | ||
if err != nil { | ||
log.Fatalf("failed to get users: %v", err) | ||
} | ||
|
||
fmt.Println(users) | ||
} | ||
|
||
type userStats struct { | ||
UserName sql.NullString | ||
PostCount sql.NullInt64 | ||
} | ||
|
||
func getUsersStats(conn *sql.DB, minPosts int) ([]userStats, error) { | ||
query := `SELECT u.name, COUNT(p.id) AS post_count | ||
FROM users AS u | ||
JOIN posts AS p ON u.id = p.user_id | ||
GROUP BY u.id | ||
HAVING post_count >= ?;` | ||
|
||
rows, err := conn.Query(query, minPosts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
users := []userStats{} | ||
for rows.Next() { | ||
var user userStats | ||
|
||
if err := rows.Scan(&user.UserName, &user.PostCount); err != nil { | ||
return nil, err | ||
} | ||
|
||
users = append(users, user) | ||
} | ||
|
||
if err := rows.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return users, nil | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module github.com/plutov/packagemain/sql-gorm-sqlx-sqlc | ||
|
||
go 1.23.0 | ||
|
||
require github.com/mattn/go-sqlite3 v1.14.23 | ||
|
||
require ( | ||
github.com/jmoiron/sqlx v1.4.0 | ||
gorm.io/driver/sqlite v1.5.6 | ||
gorm.io/gorm v1.25.12 | ||
) | ||
|
||
require ( | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= | ||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= | ||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= | ||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= | ||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= | ||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= | ||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= | ||
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= | ||
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= | ||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= | ||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func main() { | ||
conn, err := gorm.Open(sqlite.Open("db.sqlite"), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatalf("failed to open db: %v", err) | ||
} | ||
|
||
users, err := getUsersStats(conn, 2) | ||
if err != nil { | ||
log.Fatalf("failed to get users: %v", err) | ||
} | ||
|
||
fmt.Println(users) | ||
} | ||
|
||
type User struct { | ||
gorm.Model | ||
ID int `gorm:"primaryKey"` | ||
Name string | ||
Posts []Post | ||
} | ||
|
||
type Post struct { | ||
gorm.Model | ||
ID int `gorm:"primaryKey"` | ||
UserID int | ||
} | ||
|
||
type userStats struct { | ||
Name string | ||
Count int `gorm:"column:post_count"` | ||
} | ||
|
||
func getUsersStats(conn *gorm.DB, minPosts int) ([]userStats, error) { | ||
var users []userStats | ||
err := conn.Model(&User{}). | ||
Select("name", "COUNT(p.id) AS post_count"). | ||
Joins("JOIN posts AS p ON users.id = p.user_id"). | ||
Group("users.id"). | ||
Having("post_count >= ?", minPosts). | ||
Find(&users).Error | ||
return users, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
INSERT INTO users (id, name, email) VALUES (1000, "Alice", "[email protected]"); | ||
INSERT INTO users (id, name, email) VALUES (1001, "Bob", "[email protected]"); | ||
INSERT INTO users (id, name, email) VALUES (1002, "Charlie", "[email protected]"); | ||
|
||
INSERT INTO blogs (id, name, url) VALUES (2000, "devsecops", "http://devsecops.com"); | ||
INSERT INTO blogs (id, name, url) VALUES (2001, "devops", "http://devops.com"); | ||
INSERT INTO blogs (id, name, url) VALUES (2002, "oop", "http://oop.com"); | ||
|
||
INSERT INTO posts (title, content, user_id, blog_id) VALUES ("", "", 1000, 2000); | ||
INSERT INTO posts (title, content, user_id, blog_id) VALUES ("", "", 1001, 2001); | ||
INSERT INTO posts (title, content, user_id, blog_id) VALUES ("", "", 1002, 2002); | ||
INSERT INTO posts (title, content, user_id, blog_id) VALUES ("", "", 1000, 2001); | ||
INSERT INTO posts (title, content, user_id, blog_id) VALUES ("", "", 1001, 2002); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func main() { | ||
conn, err := sql.Open("sqlite3", "./../db.sqlite") | ||
if err != nil { | ||
log.Fatalf("failed to open db: %v", err) | ||
} | ||
|
||
users, err := getUsersStats(conn, 2) | ||
if err != nil { | ||
log.Fatalf("failed to get users: %v", err) | ||
} | ||
|
||
fmt.Println(users) | ||
} | ||
|
||
func getUsersStats(conn *sql.DB, minPosts int) ([]GetUsersWithMinPostsRow, error) { | ||
queries := New(conn) | ||
|
||
ctx := context.Background() | ||
return queries.GetUsersWithMinPosts(ctx) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- name: GetUsersWithMinPosts :many | ||
SELECT u.name, COUNT(p.id) AS post_count | ||
FROM users AS u | ||
JOIN posts AS p ON u.id = p.user_id | ||
GROUP BY u.id | ||
HAVING COUNT(p.id) >= 2; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE TABLE users ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL, | ||
email TEXT NOT NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE blogs ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL, | ||
url TEXT NOT NULL UNIQUE | ||
); | ||
|
||
CREATE TABLE posts ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL, | ||
content TEXT NOT NULL, | ||
user_id INTEGER NOT NULL, | ||
blog_id INTEGER NOT NULL, | ||
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, | ||
FOREIGN KEY (blog_id) REFERENCES blogs (id) ON DELETE CASCADE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: "2" | ||
sql: | ||
- engine: "sqlite" | ||
queries: "query.sql" | ||
schema: "schema.sql" | ||
gen: | ||
go: | ||
package: "main" | ||
out: "." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/jmoiron/sqlx" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func main() { | ||
conn, err := sqlx.Connect("sqlite3", "db.sqlite") | ||
if err != nil { | ||
log.Fatalf("failed to open db: %v", err) | ||
} | ||
|
||
users, err := getUsersStats(conn, 2) | ||
if err != nil { | ||
log.Fatalf("failed to get users: %v", err) | ||
} | ||
|
||
fmt.Println(users) | ||
} | ||
|
||
type userStats struct { | ||
UserName string `db:"name"` | ||
PostCount string `db:"post_count"` | ||
} | ||
|
||
func getUsersStats(conn *sqlx.DB, minPosts int) ([]userStats, error) { | ||
users := []userStats{} | ||
query := `SELECT u.name, COUNT(p.id) AS post_count | ||
FROM users AS u | ||
JOIN posts AS p ON u.id = p.user_id | ||
GROUP BY u.id | ||
HAVING post_count >= ?;` | ||
|
||
if err := conn.Select(&users, query, minPosts); err != nil { | ||
return nil, err | ||
} | ||
|
||
return users, nil | ||
} |