What problem does it solve?
sqlex simplifies Go database operations, providing advanced features like structured JSON types, Hook aspects for logging and metrics, and automatic rebind for easier SQL injection prevention.
Core Features & Use Cases
- Advanced JSON Handling: Generic JSONValue[T] for auto-serialize/deserialize JSON columns.
- Hook System: Pluggable SQL interceptors for logging, tracing, and metrics.
- Automatic Rebind: Writes ? uniformly across all databases for easier SQL injection prevention.
- Use Case: With sqlex, you can write code like
db.Get(&user, "SELECT * FROM users WHERE id = ?", 1) which will work across PostgreSQL, MySQL, SQLite, and SQL Server without needing to change the query syntax.
Quick Start
Use sqlex to connect to your database and execute queries:
db, err := sqlex.Connect("sqlite3", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Insert a user
_, err = db.Exec("INSERT INTO users (name, email) VALUES (?, ?)", "Alice", "[email protected]")
if err != nil {
log.Fatal(err)
}
// Query a user
var user User
err = db.Get(&user, "SELECT * FROM users WHERE id = ?", 1)
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %+v\n", user)