Documentation
¶
Overview ¶
Example ¶
package main
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)
}
if !result.Valid {
return
}
user := result.V
// Process result
if user.LastName.Valid {
log.Printf("Found user: %s-%s", user.FirstName, user.LastName.String)
} else {
log.Printf("Found user: %s", user.FirstName)
}
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Executor ¶
type Executor interface {
// ExecContext executes a query without returning any rows.
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
// QueryContext executes a query that returns rows, typically a SELECT.
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}
Executor interface declares required methods to execute query, that are implemented by *sql.DB, *sql.Conn and *sql.Tx.
type QueryFunc0 ¶
QueryFunc executes a retrieving query without parameters, returns the first result.
func MakeQueryFunc0 ¶
MakeQueryFunc0 makes a QueryFunc0 from query lines.
type QuerySeqFunc ¶
QuerySeqFunc executes a retrieving query, returns an iterator for all matched results.
func MakeQuerySeqFunc ¶
MakeQuerySeqFunc makes a QuerySeqFunc from query template lines.
type QuerySeqFunc0 ¶
QuerySeqFunc0 executes a retrieving query without parameters, returns an iterator for all matched results.
func MakeQuerySeqFunc0 ¶
MakeQuerySeqFunc0 makes a QuerySeqFunc0 from query lines.
type Scannable ¶
type Scannable interface {
// Dest returns scanning dest for columns.
Dest(columns []string) (dest []any)
}
Scannable is an interface that can be implemented by query result struct, to avoid the performance overhead of reflection.
When result struct implements Scannable interface, sqlfunc calls Dest to get corresponding dests for columns.
Example:
type UserResult struct{
Foo string
Bar int
}
func (r *UserResult) Dest(columns []string) (dest []any) {
for _, column := range columns {
switch column {
case "foo":
dest = append(dest, &r.Foo)
case "bar":
dest = append(dest, &r.Bar)
default:
dest = append(dest, sqlfunc.Void{})
}
}
return
}
type UpdateFunc ¶
UpdateFunc executes an updating query, returns the number or rows affected by updating query.
func MakeUpdateFunc ¶
MakeUpdateFunc makes an UpdateFunc from query template lines.