sql

package
v0.0.0-...-3d46182 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateRow = errors.New("1062: Duplicate row")

ErrDuplicateRow is for when MySQL returns a 1062 error code.

Functions

func BuildInsertQuery

func BuildInsertQuery(table string, rows ...[]ColumnValue) (query string, args []any)

BuildInsertQuery prepares an insert sql statement on table. It returns the query string and an array of args for, for example, sql.DB's Exec.

func BuildSelectQuery

func BuildSelectQuery(table string, columns, joins []string, whereClause string) string

BuildSelectQuery prepares a select sql statement. columns and whereClause could be empty strings.

func InClauseQuestionMarks

func InClauseQuestionMarks(n int) string

In question mark returns a string of the format "(?, ?, ?)" where there are n question marks. It panics if n <= 0.

func IsErrDuplicateErr

func IsErrDuplicateErr(err error) bool

IsErrDuplicateErr checks MySQL/MariaDB error string to see a '1062' can be found.

func NilIfEmptyString

func NilIfEmptyString(s string) interface{}

NilIfEmptyString returns nil if s is empty. Otherwise it returns s.

func Transact

func Transact(ctx context.Context, db *sql.DB, f func(tx *sql.Tx) error) error

Transact begins a transaction and calls f. If f returns an error, the transaction is rolled back and the error from f is returned (it is wrapped if there's an error on rollback). Otherwise, the transaction is committed.

Types

type ColumnValue

type ColumnValue struct {
	Name  string // name of column
	Value any
}

ColumnValue represents value in a table's row and the column it belongs to.

type NullBool

type NullBool struct {
	Bool  bool
	Valid bool
}

NullBool represents a bool that may be null. NullBool implements the Scanner interface so it can be used as a scan destination, similar to NullString.

Unlike sql.NullBool, NullBool marshals into JSON properly (with the JSON value being null if Valid is false).

func NewNullBool

func NewNullBool(t interface{}) NullBool

NewNullBool returns a NullBool with Valid set to true is t is of type bool.

func (NullBool) MarshalJSON

func (i NullBool) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshalar interface.

func (*NullBool) Scan

func (i *NullBool) Scan(src interface{}) error

Scan implements sql.Scanner interface

func (*NullBool) UnmarshalJSON

func (i *NullBool) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshalar interface.

type NullFloat64

type NullFloat64 struct {
	sql.NullFloat64
}

NullFloat64 represents a float64 that may be null. NullFloat64 implements the Scanner interface so it can be used as a scan destination, similar to NullString.

Unlike sql.NullFloat64, NullFloat64 marshals into JSON properly (with the JSON value being null if Valid is false).

func NewNullFloat64

func NewNullFloat64(fl interface{}) NullFloat64

NewNullFloat64 returns a NullFloat64 with Valid set to true if fl is a float64.

func (NullFloat64) MarshalJSON

func (i NullFloat64) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshalar interface.

func (*NullFloat64) UnmarshalJSON

func (i *NullFloat64) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshalar interface.

type NullInt32

type NullInt32 struct {
	sql.NullInt32
}

NullInt32 represents an int32 that may be null. NullInt32 implements the Scanner interface so it can be used as a scan destination, similar to NullString.

Unlike sql.NullInt32, NullInt32 marshals into JSON properly (with the JSON value being null if Valid is false).

func NewNullInt32

func NewNullInt32(t interface{}) NullInt32

NewNullInt32 returns a NullInt32 with Valid set to true if t is an int.

func (NullInt32) MarshalJSON

func (i NullInt32) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshalar interface.

func (*NullInt32) UnmarshalJSON

func (i *NullInt32) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshalar interface.

type NullString

type NullString struct {
	sql.NullString
}

NullString represents a string that may be null. NullString implements the Scanner interface so it can be used as a scan destination:

Unlike sql.NullString, NullString marshals into JSON properly (with the JSON value being null if Valid is false).

func NewNullString

func NewNullString(str interface{}) NullString

NewNullString returns a NullString with Valid set to true if str is a string.

func (NullString) MarshalJSON

func (s NullString) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshalar interface

func (*NullString) UnmarshalJSON

func (s *NullString) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshalar interface

type NullTime

type NullTime struct {
	sql.NullTime
}

NullTime represents a time.Time that may be null. NullTime implements the Scanner interface so it can be used as a scan destination, similar to NullString.

Unlike sql.NullTime, NullTime marshals into JSON properly (with the JSON value being null if Valid is false).

func NewNullTime

func NewNullTime(t interface{}) NullTime

NewNullTime returns a NullTime with Valid set to true if t is a time.Time.

func (NullTime) MarshalJSON

func (s NullTime) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshalar interface.

func (*NullTime) UnmarshalJSON

func (s *NullTime) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshalar interface.

Jump to

Keyboard shortcuts

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