https://github.com/deadblue/sqlfunc
A SQL framework for Golang.
https://github.com/deadblue/sqlfunc
database golang
Last synced: 2 months ago
JSON representation
A SQL framework for Golang.
- Host: GitHub
- URL: https://github.com/deadblue/sqlfunc
- Owner: deadblue
- License: mit
- Created: 2025-08-27T03:51:09.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-10-17T07:40:45.000Z (9 months ago)
- Last Synced: 2025-12-30T18:15:24.124Z (6 months ago)
- Topics: database, golang
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SQL-Function
Run your SQL as function.
## Example
```golang
import (
"context"
"database/sql"
"log"
"github.com/deadblue/sqlfunc"
)
type (
QueryParams struct {
UserId string
Status int
}
UserResult struct {
UserId string
FirstName string
// sql.NullXXX types are supported.
LastName sql.NullString
// Mapping "sex" column to [Gender] field.
Gender int `sql:"sex"`
}
)
func main() {
// Make SQL function
queryUser, err := sqlfunc.MakeQueryFunc[QueryParams, UserResult](
"SELECT user_id, first_name, last_name, sex",
"FROM tbl_user",
"WHERE user_id = {{ .UserID }} AND status = {{ .Status }}",
)
if err != nil {
panic(err)
}
// Connect to database
db, err := sql.Open("driver", "DSN")
if err != nil {
panic(err)
}
// Put DB to context
ctx := sqlfunc.NewContext(context.TODO(), db)
// Execute query
result, err := queryUser(ctx, QueryParams{
UserId: "123",
Status: 1,
})
if err != nil {
panic(err)
}
// Process result
if result.Valid {
user := result.V
if user.LastName.Valid {
log.Printf("Found user: %s-%s", user.FirstName, user.LastName.String)
} else {
log.Printf("Found user: %s", user.FirstName)
}
}
}
```
## License
MIT