sqlrun

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2020 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsFold

func ContainsFold(a, b string) bool

ContainsFold tell if a contains b in case-insensitively.

func FirstWord

func FirstWord(s string) string

FirstWord returns the first word of the SQL statement s.

func IfElse

func IfElse(ifCondition bool, ifValue, elseValue string) string

IfElse if else ...

func ImplSQLScanner

func ImplSQLScanner(t reflect.Type) bool

ImplSQLScanner tells t whether it implements sql.Scanner interface.

func ImplType

func ImplType(src, target reflect.Type) bool

ImplType tells src whether it implements target type.

func IsQuerySQL

func IsQuerySQL(sql string) (string, bool)

IsQuerySQL tests a sql is a query or not.

func LookupDriverName

func LookupDriverName(driver driver.Driver) string

LookupDriverName get driverName from the driver instance. The database/sql API doesn't provide a way to get the registry name for a driver from the driver type. from https://github.com/golang/go/issues/12600

Types

type ExecResult

type ExecResult struct {
	Error     error
	CostTime  time.Duration
	Headers   []string
	Rows      interface{} // [][]string or []YourStruct
	RowsCount int

	RowsAffected int64
	LastInsertID int64
	IsQuery      bool
}

ExecResult defines the result structure of sql execution.

func (ExecResult) StringRows

func (r ExecResult) StringRows() [][]string

StringRows return the string rows when using MapPreparer.

type MapMapping

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

MapMapping maps the query rows to maps.

func (*MapMapping) RowsData

func (m *MapMapping) RowsData() interface{}

RowsData returns the mapped rows data.

func (*MapMapping) Scan

func (m *MapMapping) Scan(rowNum int) error

Scan scans the rows one by one.

type MapPreparer

type MapPreparer struct {
	// NullReplace is the replacement of null values.
	NullReplace string
}

MapPreparer prepares to scan query rows.

func NewMapPreparer

func NewMapPreparer(nullReplace string) *MapPreparer

NewMapPreparer creates a new MapPreparer.

func (*MapPreparer) Prepare

func (m *MapPreparer) Prepare(rows *sql.Rows, columns []string) Mapping

Prepare prepares to scan query rows.

type Mapping

type Mapping interface {
	Scan(rowNum int) error
	RowsData() interface{}
}

Mapping defines the interface for SQL query processing.

type MiniDB

type MiniDB interface {
	// Exec executes update.
	Exec(query string, args ...interface{}) (sql.Result, error)
	// Query performs query.
	Query(query string, args ...interface{}) (*sql.Rows, error)
}

MiniDB wraps Exec method.

type NullAny

type NullAny struct {
	Type reflect.Type
	Val  reflect.Value
}

NullAny represents any that may be null. it implements the Scanner interface so it can be used as a scan destination.

func (*NullAny) Scan

func (n *NullAny) Scan(value interface{}) error

Scan assigns a value from a database driver.

The src value will be of one of the following types:

int64
float64
bool
[]byte
string
time.Time
nil - for NULL values

An error should be returned if the value cannot be stored without loss of information.

Reference types such as []byte are only valid until the next call to Scan and should not be retained. Their underlying memory is owned by the driver. If retention is necessary, copy their values before the next call to Scan.

type Preparer

type Preparer interface {
	// Prepare prepares to scan query rows.
	Prepare(rows *sql.Rows, columns []string) Mapping
}

Preparer prepares to scan query rows.

type SQLExec

type SQLExec struct {
	MiniDB
}

SQLExec is used to execute only updates.

func NewSQLExec

func NewSQLExec(db MiniDB) *SQLExec

NewSQLExec creates a new SQLExec for only updates.

func (*SQLExec) DoUpdate

func (s *SQLExec) DoUpdate(query string, vars ...interface{}) (result ExecResult)

DoUpdate does the update.

type SQLRun

type SQLRun struct {
	*SQLExec
	Preparer // required only for query

	MaxRows int
}

SQLRun is used to execute queries and updates.

func NewSQLRun

func NewSQLRun(db MiniDB, preparer Preparer) *SQLRun

NewSQLRun creates a new SQLRun for queries and updates.

func (*SQLRun) DoExec

func (s *SQLRun) DoExec(query string, args ...interface{}) ExecResult

DoExec executes a SQL.

func (*SQLRun) DoQuery

func (s *SQLRun) DoQuery(query string, args ...interface{}) (result ExecResult)

DoQuery does the query.

type StructMapping

type StructMapping struct {
	*StructPreparer
	// contains filtered or unexported fields
}

StructMapping is the structure for mapping row to a structure.

func (*StructMapping) RowsData

func (s *StructMapping) RowsData() interface{}

RowsData returns the mapped rows data.

func (*StructMapping) Scan

func (s *StructMapping) Scan(rowNum int) error

Scan scans the query result to fetch the rows one by one.

type StructPreparer

type StructPreparer struct {
	StructType reflect.Type
}

StructPreparer is the the structure to create struct mapping.

func NewStructPreparer

func NewStructPreparer(v interface{}) *StructPreparer

NewStructPreparer creates a new StructPreparer.

func (*StructPreparer) Prepare

func (m *StructPreparer) Prepare(rows *sql.Rows, columns []string) Mapping

Prepare prepares to scan query rows.

Jump to

Keyboard shortcuts

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