goql
goql copied to clipboard
Super simple SQL query builder
goql
goql is a super fast and easy to use query builder and database table to struct modeling convention. It is like an ORM but it just gets out of your way and let's you keep control of your queries.
For the following examples, let's assume you have the following table user:
id | username | password |
---|---|---|
1 | ricardo | secret123 |
2 | admin | ultrasecret |
Mapping a table to a struct
type User struct {
ID int64 `db:"id" pk:"true"`
Username string `db:"username"`
Password string `db:"password"`
}
Select queries
query := goql.QueryBuilder{}
query.Select("id, username, password").From("user").Where("id = $?", 1).QueryAndScan(db, &user)
fmt.Println(user.username) // -> "ricardo"
Or even better...
myuser := User{}
query := goql.QueryBuilder{}
query.Select(myser).Where("id = $?", 1).QueryAndScan(db, &user)
fmt.Println(user.username) // -> "ricardo"
Insert or update
newUser := User{ID: 3, Name: "John", Password: "123"}
Insert(db, "user", newUser)
newUser.Name = "Bob"
Update(db, "user", newUser)
Note db
in both cases must be either of type *sql.DB
or *sql.Tx