sqlla

package module
v2.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 24 Imported by: 1

README

go-sqlla

Type safe, reflect free, generative SQL Builder

INSTALL

$ go install github.com/mackee/go-sqlla/v2/cmd/sqlla@latest

SYNOPSIS

person.go:

package table

//go:generate sqlla

//+table: person
type Person struct {
	ID uint64 `db:"id"`
	FirstName string `db:"first_name"`
	LastName  string `db:"last_name"`
}

Run generate:

$ ls
person.go
$ go generate
$ ls
person.go person_auto.go

Same package as the person.go:


import (
	"database/sql"
	"log"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./foo.db")
	if err != nil {
		log.Fatalf("failed connect database: %s", err)
	}

	q := NewPersonSQL().Select().ID(uint64(1))
	query, args, err := q.ToSql()
	if err != nil {
		log.Fatalf("query build error: %s", err)
	}

	row := db.QueryRow(query, args...)
	var id uint64
	var firstName, lastName string
	err = row.Scan(&id, &firstName, &lastName)
	if err != nil {
		log.Fatalf("query exec error: %s", err)
	}
	log.Printf("id=%d, first_name=%s, last_name=%s", id, firstName, lastName)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ToPackages       = toPackages
	DeclToTable      = declToTable
	ErrNotTargetDecl = errNotTargetDecl
)

Functions

func Run

func Run(from, ext string)

func WriteCode

func WriteCode(w io.Writer, table *Table) error

Types

type Column

type Column struct {
	Field      *ast.Field
	Name       string
	MethodName string

	PkgName string

	TableName string
	IsPk      bool
	// contains filtered or unexported fields
}

func (Column) AltTypeName added in v2.6.0

func (c Column) AltTypeName() string

func (Column) BaseTypeName

func (c Column) BaseTypeName() string

func (Column) ExprMultiTypeName added in v2.14.0

func (c Column) ExprMultiTypeName() string

func (Column) ExprTypeName added in v2.14.0

func (c Column) ExprTypeName() string

func (Column) ExprValueIdentifier added in v2.14.0

func (c Column) ExprValueIdentifier() string

func (Column) FieldName

func (c Column) FieldName() string

func (Column) HasUnderlyingType added in v2.14.0

func (c Column) HasUnderlyingType() bool

func (Column) IsNullT added in v2.14.0

func (c Column) IsNullT() bool

func (Column) String

func (c Column) String() string

func (Column) TypeName

func (c Column) TypeName() string

func (Column) TypeParameter added in v2.14.0

func (c Column) TypeParameter() string

type Columns

type Columns []Column

type DB

type DB interface {
	QueryRow(string, ...interface{}) *sql.Row
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row

	Query(string, ...interface{}) (*sql.Rows, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)

	Exec(string, ...interface{}) (sql.Result, error)
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

DB is interface like *database/sql.DB

type Expr

type Expr interface {
	ToSql() (string, []interface{}, error)
}

type ExprBool

type ExprBool struct {
	Column string
	Value  bool
	Op     Operator
}

func (ExprBool) ToSql

func (e ExprBool) ToSql() (string, []interface{}, error)

type ExprBytes added in v2.6.0

type ExprBytes struct {
	Column string
	Value  []byte
	Op     Operator
}

func (ExprBytes) ToSql added in v2.6.0

func (e ExprBytes) ToSql() (string, []interface{}, error)

type ExprFloat64

type ExprFloat64 struct {
	Column string
	Value  float64
	Op     Operator
}

func (ExprFloat64) ToSql

func (e ExprFloat64) ToSql() (string, []interface{}, error)

type ExprInt32

type ExprInt32 struct {
	Column string
	Value  int32
	Op     Operator
}

func (ExprInt32) ToSql

func (e ExprInt32) ToSql() (string, []interface{}, error)

type ExprInt64

type ExprInt64 struct {
	Column string
	Value  int64
	Op     Operator
}

func (ExprInt64) ToSql

func (e ExprInt64) ToSql() (string, []interface{}, error)

type ExprInt8

type ExprInt8 struct {
	Column string
	Value  int8
	Op     Operator
}

func (ExprInt8) ToSql

func (e ExprInt8) ToSql() (string, []interface{}, error)

type ExprMultiBool

type ExprMultiBool struct {
	Column string
	Values []bool
	Op     Operator
}

func (ExprMultiBool) ToSql

func (e ExprMultiBool) ToSql() (string, []interface{}, error)

type ExprMultiBytes added in v2.6.0

type ExprMultiBytes struct {
	Column string
	Values [][]byte
	Op     Operator
}

func (ExprMultiBytes) ToSql added in v2.6.0

func (e ExprMultiBytes) ToSql() (string, []interface{}, error)

type ExprMultiFloat64

type ExprMultiFloat64 struct {
	Column string
	Values []float64
	Op     Operator
}

func (ExprMultiFloat64) ToSql

func (e ExprMultiFloat64) ToSql() (string, []interface{}, error)

type ExprMultiInt32

type ExprMultiInt32 struct {
	Column string
	Values []int32
	Op     Operator
}

func (ExprMultiInt32) ToSql

func (e ExprMultiInt32) ToSql() (string, []interface{}, error)

type ExprMultiInt64

type ExprMultiInt64 struct {
	Column string
	Values []int64
	Op     Operator
}

func (ExprMultiInt64) ToSql

func (e ExprMultiInt64) ToSql() (string, []interface{}, error)

type ExprMultiInt8

type ExprMultiInt8 struct {
	Column string
	Values []int8
	Op     Operator
}

func (ExprMultiInt8) ToSql

func (e ExprMultiInt8) ToSql() (string, []interface{}, error)

type ExprMultiMysqlNullTime added in v2.9.0

type ExprMultiMysqlNullTime struct {
	Column string
	Values []mysql.NullTime
	Op     Operator
}

func (ExprMultiMysqlNullTime) ToSql added in v2.9.0

func (e ExprMultiMysqlNullTime) ToSql() (string, []interface{}, error)

type ExprMultiNullBool

type ExprMultiNullBool struct {
	Column string
	Values []sql.NullBool
	Op     Operator
}

func (ExprMultiNullBool) ToSql

func (e ExprMultiNullBool) ToSql() (string, []interface{}, error)

type ExprMultiNullFloat64

type ExprMultiNullFloat64 struct {
	Column string
	Values []sql.NullFloat64
	Op     Operator
}

func (ExprMultiNullFloat64) ToSql

func (e ExprMultiNullFloat64) ToSql() (string, []interface{}, error)

type ExprMultiNullInt64

type ExprMultiNullInt64 struct {
	Column string
	Values []sql.NullInt64
	Op     Operator
}

func (ExprMultiNullInt64) ToSql

func (e ExprMultiNullInt64) ToSql() (string, []interface{}, error)

type ExprMultiNullString

type ExprMultiNullString struct {
	Column string
	Values []sql.NullString
	Op     Operator
}

func (ExprMultiNullString) ToSql

func (e ExprMultiNullString) ToSql() (string, []interface{}, error)

type ExprMultiNullTime

type ExprMultiNullTime struct {
	Column string
	Values []sql.NullTime
	Op     Operator
}

func (ExprMultiNullTime) ToSql

func (e ExprMultiNullTime) ToSql() (string, []interface{}, error)

type ExprMultiString

type ExprMultiString struct {
	Column string
	Values []string
	Op     Operator
}

func (ExprMultiString) ToSql

func (e ExprMultiString) ToSql() (string, []interface{}, error)

type ExprMultiTime

type ExprMultiTime struct {
	Column string
	Values []time.Time
	Op     Operator
}

func (ExprMultiTime) ToSql

func (e ExprMultiTime) ToSql() (string, []interface{}, error)

type ExprMultiUint32

type ExprMultiUint32 struct {
	Column string
	Values []uint32
	Op     Operator
}

func (ExprMultiUint32) ToSql

func (e ExprMultiUint32) ToSql() (string, []interface{}, error)

type ExprMultiUint64

type ExprMultiUint64 struct {
	Column string
	Values []uint64
	Op     Operator
}

func (ExprMultiUint64) ToSql

func (e ExprMultiUint64) ToSql() (string, []interface{}, error)

type ExprMultiUint8

type ExprMultiUint8 struct {
	Column string
	Values []uint8
	Op     Operator
}

func (ExprMultiUint8) ToSql

func (e ExprMultiUint8) ToSql() (string, []interface{}, error)

type ExprMultiValue added in v2.14.0

type ExprMultiValue[T any] struct {
	Column string
	Values []T
	Op     Operator
}

func (ExprMultiValue[T]) ToSql added in v2.14.0

func (e ExprMultiValue[T]) ToSql() (string, []interface{}, error)

type ExprMysqlNullTime added in v2.9.0

type ExprMysqlNullTime struct {
	Column string
	Value  mysql.NullTime
	Op     Operator
}

func (ExprMysqlNullTime) ToSql added in v2.9.0

func (e ExprMysqlNullTime) ToSql() (string, []interface{}, error)

type ExprNull added in v2.14.0

type ExprNull[T any] struct {
	Column string
	Value  sql.Null[T]
	Op     Operator
}

func (ExprNull[T]) ToSql added in v2.14.0

func (e ExprNull[T]) ToSql() (string, []interface{}, error)

type ExprNullBool

type ExprNullBool struct {
	Column string
	Value  sql.NullBool
	Op     Operator
}

func (ExprNullBool) ToSql

func (e ExprNullBool) ToSql() (string, []interface{}, error)

type ExprNullFloat64

type ExprNullFloat64 struct {
	Column string
	Value  sql.NullFloat64
	Op     Operator
}

func (ExprNullFloat64) ToSql

func (e ExprNullFloat64) ToSql() (string, []interface{}, error)

type ExprNullInt64

type ExprNullInt64 struct {
	Column string
	Value  sql.NullInt64
	Op     Operator
}

func (ExprNullInt64) ToSql

func (e ExprNullInt64) ToSql() (string, []interface{}, error)

type ExprNullString

type ExprNullString struct {
	Column string
	Value  sql.NullString
	Op     Operator
}

func (ExprNullString) ToSql

func (e ExprNullString) ToSql() (string, []interface{}, error)

type ExprNullTime

type ExprNullTime struct {
	Column string
	Value  sql.NullTime
	Op     Operator
}

func (ExprNullTime) ToSql

func (e ExprNullTime) ToSql() (string, []interface{}, error)

type ExprOr

type ExprOr []Where

func (ExprOr) ToSql

func (e ExprOr) ToSql() (string, []interface{}, error)

type ExprString

type ExprString struct {
	Column string
	Value  string
	Op     Operator
}

func (ExprString) ToSql

func (e ExprString) ToSql() (string, []interface{}, error)

type ExprTime

type ExprTime struct {
	Column string
	Value  time.Time
	Op     Operator
}

func (ExprTime) ToSql

func (e ExprTime) ToSql() (string, []interface{}, error)

type ExprUint32

type ExprUint32 struct {
	Column string
	Value  uint32
	Op     Operator
}

func (ExprUint32) ToSql

func (e ExprUint32) ToSql() (string, []interface{}, error)

type ExprUint64

type ExprUint64 struct {
	Column string
	Value  uint64
	Op     Operator
}

func (ExprUint64) ToSql

func (e ExprUint64) ToSql() (string, []interface{}, error)

type ExprUint8

type ExprUint8 struct {
	Column string
	Value  uint8
	Op     Operator
}

func (ExprUint8) ToSql

func (e ExprUint8) ToSql() (string, []interface{}, error)

type ExprValue added in v2.14.0

type ExprValue[T any] struct {
	Column string
	Value  T
	Op     Operator
}

func (ExprValue[T]) ToSql added in v2.14.0

func (e ExprValue[T]) ToSql() (string, []interface{}, error)

type Operator

type Operator string
var (
	OpEqual        Operator = "="  // Operator for equal. Column(value, sqlla.OpEqual) same as Column = value
	OpGreater      Operator = ">"  // Operator for greater. Column(value, sqlla.OpGreater) same as Column > value
	OpGreaterEqual Operator = ">=" // Operator for greater equal. Column(value, sqlla.OpGreaterEqual) same as Column >= value
	OpLess         Operator = "<"  // Operator for less. Column(value, sqlla.OpLess) same as Column < value
	OpLessEqual    Operator = "<=" // Operator for less equal. Column(value, sqlla.OpLessEqual) same as Column <= value
	OpNot          Operator = "<>" // Operator for not equal. Column(value, sqlla.OpNot) same as Column <> value
	OpIs           Operator = "IS" // Operator for is. Column(value, sqlla.OpIs) same as Column IS value

	OpLike Operator = "LIKE" // Operator for like. Column(value, sqlla.OpLike) same as Column LIKE value
)

func MakeInOperator

func MakeInOperator(n int) Operator

func (Operator) ToSql

func (op Operator) ToSql() (string, error)

type Order

type Order bool
const (
	Asc  Order = true
	Desc Order = false
)

type RowAffected

type RowAffected int64

RowAffected is result of upsert

const (
	RowAffectedNoupdated RowAffected = iota
	RowAffectedInserted
	RowAffectedUpdated
)

RowAffected results

type Scanner

type Scanner interface {
	Scan(...interface{}) error
}

Scanner is interface like *database/sql.Row

type SetMap

type SetMap map[string]interface{}

func (SetMap) NewIterator added in v2.7.0

func (sm SetMap) NewIterator() *SetMapIterator

func (SetMap) ToInsertColumnsAndValues added in v2.7.0

func (sm SetMap) ToInsertColumnsAndValues() (string, string, []interface{})

func (SetMap) ToInsertSql

func (sm SetMap) ToInsertSql() (string, []interface{}, error)

func (SetMap) ToUpdateSql

func (sm SetMap) ToUpdateSql() (string, []interface{}, error)

type SetMapIterator added in v2.7.0

type SetMapIterator struct {
	// contains filtered or unexported fields
}

func (*SetMapIterator) Iterate added in v2.7.0

func (s *SetMapIterator) Iterate() bool

func (*SetMapIterator) Key added in v2.7.0

func (s *SetMapIterator) Key() string

func (*SetMapIterator) Value added in v2.7.0

func (s *SetMapIterator) Value() interface{}

type SetMapRawValue added in v2.7.0

type SetMapRawValue string

type SetMaps added in v2.7.0

type SetMaps []SetMap

func (SetMaps) ToInsertSql added in v2.7.0

func (s SetMaps) ToInsertSql() (string, []interface{}, error)

type Table

type Table struct {
	Package     *types.Package
	PackageName string
	Name        string
	StructName  string
	TableName   string
	Columns     Columns
	PkColumn    *Column
	// contains filtered or unexported fields
}

func (*Table) AddColumn

func (t *Table) AddColumn(c Column)

func (*Table) AdditionalPackages

func (t *Table) AdditionalPackages() []string

func (*Table) HasPk

func (t *Table) HasPk() bool

func (*Table) NamingIsStructName added in v2.12.0

func (t *Table) NamingIsStructName() bool

func (Table) Render

func (t Table) Render(w io.Writer) error

type Where

type Where []Expr

func (Where) ToSql

func (wh Where) ToSql() (string, []interface{}, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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