GoQuery is a Golang query builder library inspired by Laravel's Query Builder, aims to support multiple drivers such as MySQL, PostgreSQL, and SQLite. It aims to simplify building SQL queries in Go without compromising flexibility and performance.
- Chainable API similar to Laravel's Query Builder.
- Multi-driver support: MySQL, PostgreSQL, and SQLite.
- Easily switch between SQL databases.
- Query transactions, joins, aggregates, and more.
- Raw queries for complex SQL statements.
- Easy-to-read, maintainable query construction.
- Built-in error handling.
To install the package, use go get
to download the package and add it to your project:
go get github.com/mohamed-samir907/goquery
You can find the full API docs for the packahge here
You can start by using GoQuery to create simple queries such as SELECT
, INSERT
, UPDATE
, and DELETE
.
package main
import (
"fmt"
"log"
"github.com/mohamed-samir907/goquery"
"github.com/mohamed-samir907/goquery/drivers"
)
func main() {
db := goquery.New(drivers.NewMySQL(myDBConn))
result, err := db.Table("users").Select("id", "name").Where("age", ">", 18).Get()
if err != nil {
log.Fatal(err)
}
for _, row := range result {
fmt.Println(row["name"])
}
}
GoQuery supports more advanced queries like INNER JOIN
, GROUP BY
, and subqueries.
db := goquery.New(drivers.NewMySQL(myDBConn))
result, err := db.Table("users").
Select("users.name", "orders.total").
Join("orders", "users.id", "=", "orders.user_id").
Where("users.age", ">", 18).
Get()
if err != nil {
log.Fatal(err)
}
GoQuery supports SQL transactions to help ensure data consistency and handle complex database operations.
db := goquery.New(drivers.NewMySQL(myDBConn))
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
// Perform operations within the transaction
result, err := tx.Prepared("INSERT INTO users (name) VALUES (?)", []any{"Mohamed Samir"})
if err != nil {
tx.Rollback()
log.Fatal(err)
}
result, err = tx.Table("users").Where("id", query.Eq, 1).First()
if err != nil {
tx.Rollback()
log.Fatal(err)
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
To use aggregate functions such as COUNT
, SUM
, AVG
, etc.:
db := goquery.New(drivers.NewMySQL(myDBConn))
count, err := db.Table("orders").Count()
if err != nil {
log.Fatal(err)
}
fmt.Println("Total Orders: ", count)
For more complex queries that cannot be constructed using the chainable API, raw queries are also supported:
db := goquery.New(drivers.NewMySQL(myDBConn))
// you can use db.Unprepared("SELECT * FROM users WHERE id = 1")
result, err := db.Prepared("SELECT * FROM users WHERE age > ?", []any{18})
if err != nil {
log.Fatal(err)
}
Currently, GoQuery supports the following drivers:
- MySQL
- PostgreSQL
- SQLite
Support for additional drivers can be added as needed.
We welcome contributions to GoQuery! If you have any ideas or improvements, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a new feature branch.
- Commit your changes.
- Open a pull request.
GoQuery is licensed under the MIT License. See the LICENSE file for more details.
- Add testing
- Add joins
- Add insert
- Add update
- Add Delete
- Add Upsert
- Add Raw queries execute
- Add aggregates
- Add transactions support