Welcome to the sqlt repository! This project is a Go library designed to simplify SQL query building and struct mapping. With sqlt, you can create SQL queries in a type-safe manner while maintaining the flexibility of templates. This README will guide you through the features, installation, usage, and more.
- Template-Based: Utilize Go templates to construct SQL queries.
- Type-Safe: Enjoy compile-time checks for SQL queries.
- Multi-Database Support: Works with MySQL, PostgreSQL, SQLite, and more.
- No ORM Required: Directly map structs to SQL without the overhead of an ORM.
- Flexible: Easily modify queries using templates.
To install sqlt, you can use the following command:
go get github.com/ght665/sqlt
Make sure you have Go installed on your machine. If you need help with installation, refer to the Go installation guide.
To get started with sqlt, follow these simple steps:
-
Import the package:
import "github.com/ght665/sqlt"
-
Create a new SQL builder:
builder := sqlt.NewBuilder()
-
Build your SQL query:
query := builder.Select("*").From("users").Where("id = ?", userID).Build()
-
Execute the query:
Use your preferred database driver to execute the query.
Here are a few examples to demonstrate how to use sqlt effectively.
package main
import (
"fmt"
"github.com/ght665/sqlt"
)
func main() {
builder := sqlt.NewBuilder()
query := builder.Select("name, email").From("users").Where("active = true").Build()
fmt.Println(query)
}
package main
import (
"github.com/ght665/sqlt"
)
type User struct {
Name string
Email string
}
func main() {
user := User{Name: "John Doe", Email: "john@example.com"}
builder := sqlt.NewBuilder()
query := builder.InsertInto("users").Values(user).Build()
fmt.Println(query)
}
package main
import (
"github.com/ght665/sqlt"
)
func main() {
template := "SELECT {{.Fields}} FROM {{.Table}} WHERE {{.Condition}}"
data := map[string]interface{}{
"Fields": "name, email",
"Table": "users",
"Condition": "active = true",
}
builder := sqlt.NewBuilder()
query := builder.Template(template, data).Build()
fmt.Println(query)
}
We welcome contributions! If you want to help improve sqlt, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them.
- Push your branch and create a pull request.
Please ensure that your code follows the project's style and includes tests where applicable.
This project is licensed under the MIT License. See the LICENSE file for details.
To download the latest release, visit the Releases section. You can find the files you need to download and execute there.
For the latest updates and changes, keep an eye on the releases page.
sqlt provides a straightforward way to build SQL queries in Go. Its template-based approach allows for flexibility while ensuring type safety. Whether you are working with MySQL, PostgreSQL, or SQLite, sqlt can help streamline your database interactions.
If you have any questions or need further assistance, feel free to open an issue in the repository. We appreciate your interest in sqlt and look forward to your contributions!