sqlpdb

package
v0.1.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TagName is the name of the tag to use on struct fields
	TagName           = "sql"
	AutoGenTagName    = "sql-auto"
	IgnoreTagName     = "sql-ign"
	IgnoreEditTagName = "sql-ign-edit"

	QueryReplace = "SELECT *"
)

Variables

View Source
var (
	// NameMapper is the function used to convert struct fields which do not have sql tags
	// into database column names.
	//
	// The default mapper converts field names to lower case. If instead you would prefer
	// field names converted to snake case, simply assign sqlp.ToSnakeCase to the variable:
	//
	//	sqlp.NameMapper = sqlp.ToSnakeCase
	//
	// Alternatively for a custom mapping, any func(string) string can be used instead.
	NameMapper = strings.ToLower

	ErrNotSet = errors.New("sqlp: database not set")
)

Functions

func DeleteDb

func DeleteDb[T any](db *sql.DB, pk any, table string) error

func DeleteRdb

func DeleteRdb[T Repo](db *sql.DB, obj T) error

func InDb

func InDb(db *sql.DB, query string, args ...any) (err error)

func InsertDb

func InsertDb[T any](db *sql.DB, obj T, table string) (int, error)

func InsertRdb

func InsertRdb[T Repo](db *sql.DB, obj T) (int, error)

func QueryBasicDb

func QueryBasicDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (results []T, err error)

QueryBasicDb is Query, but for basic data types.

func QueryBasicRowDb

func QueryBasicRowDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (result T, err error)

QueryBasicRowDb is QueryRow, but for basic data types.

func QueryDb

func QueryDb[T any](db *sql.DB, query string, args ...any) (results []T, err error)

QueryDb executes the given query using the global database handle and returns the resulting objects in a slice. SetDatabase must be called before using this function. The query should use the QueryReplace (* by default) string to indicate where the columns from the struct type T should be inserted.

For example for the following struct:

type User struct {
	ID   int
	Name string
}

and the following query

SELECT * FROM users WHERE id = ?

the query sent to the database will be

SELECT id, name FROM users WHERE id = ?

and a list of User objects will be returned.

In addition, "IN"-queries are supported. If the query contains the InQueryReplace string, the function will automatically replace it with the correct amount of "?". For example, if you give the following query

SELECT * FROM users WHERE id IN (*)

and the following arguments

Query("SELECT * FROM users WHERE id IN (*) AND name LIKE '%?'", []int{1, 2, 3}, "a")

func QueryRowDb

func QueryRowDb[T any](db *sql.DB, query string, args ...any) (result T, err error)

QueryRowDb works similar to Query except it returns only the first row from the result set. SetDatabase must be called before using this function. Check the Query function for more information.

func UpdateDb

func UpdateDb[T any](db *sql.DB, obj T, table string) error

func UpdateRdb

func UpdateRdb[T Repo](db *sql.DB, obj T) error

Types

type Repo

type Repo interface {
	TableName() string
}

type Rows

type Rows interface {
	Scan(...any) error
	Columns() ([]string, error)
}

Rows defines the interface of types that are scannable with the Scan function. It is implemented by the sql.Rows type from the standard library

type Scanner

type Scanner interface {
	Scan(src any) error
}

Scanner is an interface used by Scan.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL