sqlike

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2021 License: MIT Imports: 31 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoValueUpdate = errors.New("sqlike: no value to update")
	// ErrInvalidInput :
	ErrInvalidInput = errors.New("sqlike: invalid input <nil>")
	// ErrUnaddressableEntity :
	ErrUnaddressableEntity = errors.New("sqlike: unaddressable entity")
	// ErrNilEntity :
	ErrNilEntity = errors.New("sqlike: entity is <nil>")
	// ErrNoColumn :
	ErrNoColumn = errors.New("sqlike: no columns to create index")
)

errors : common error of sqlike

View Source
var EOF = io.EOF

EOF : is an alias for end of file

View Source
var ErrEmptyFields = errors.New("empty fields")

ErrEmptyFields :

View Source
var ErrExpectedStruct = errors.New("expected struct as a source")

ErrExpectedStruct :

View Source
var ErrInvalidCursor = errors.New("sqlike: invalid cursor")

ErrInvalidCursor :

View Source
var ErrNoRecordAffected = errors.New("no record affected")

ErrNoRecordAffected :

View Source
var ErrNoRows = sql.ErrNoRows

ErrNoRows : is an alias for no record found

Functions

This section is empty.

Types

type Client

type Client struct {
	*DriverInfo
	*sql.DB
	// contains filtered or unexported fields
}

Client : sqlike client is a client embedded with *sql.DB, so you may use any apis of *sql.DB

func Connect

func Connect(ctx context.Context, driver string, opt *options.ConnectOptions) (client *Client, err error)

Connect : connect and ping the sql server, throw error when unable to ping

func ConnectDB added in v1.1.0

func ConnectDB(ctx context.Context, driver string, conn driver.Connector) (*Client, error)

ConnectDB :

func MustConnect

func MustConnect(ctx context.Context, driver string, opt *options.ConnectOptions) *Client

MustConnect will panic if cannot connect to sql server

func MustConnectDB added in v1.1.0

func MustConnectDB(ctx context.Context, driver string, conn driver.Connector) *Client

MustConnectDB :

func MustOpen

func MustOpen(ctx context.Context, driver string, opt *options.ConnectOptions) *Client

MustOpen : must open will panic if it cannot establish a connection to sql server

func Open

func Open(ctx context.Context, driver string, opt *options.ConnectOptions) (client *Client, err error)

Open : open connection to sql server with connection string

func (*Client) CreateDatabase

func (c *Client) CreateDatabase(ctx context.Context, name string) error

CreateDatabase : create database with name

func (*Client) Database

func (c *Client) Database(name string) *Database

Database : this api will execute `USE database`, which will point your current connection to selected database

func (*Client) DropDatabase

func (c *Client) DropDatabase(ctx context.Context, name string) error

DropDatabase : drop the selected database

func (*Client) ListDatabases

func (c *Client) ListDatabases(ctx context.Context) ([]string, error)

ListDatabases : list all the database on current connection

func (*Client) SetCodec added in v1.0.2

func (c *Client) SetCodec(cdc codec.Codecer) *Client

SetCodec : Codec is a component which handling the : 1. encoding between input data and driver.Valuer 2. decoding between output data and sql.Scanner

func (*Client) SetLogger

func (c *Client) SetLogger(logger logs.Logger) *Client

SetLogger : this is to set the logger for debugging, it will panic if the logger input is nil

func (*Client) SetPrimaryKey

func (c *Client) SetPrimaryKey(pk string) *Client

SetPrimaryKey : this will set a default primary key for subsequent operation such as Insert, InsertOne, ModifyOne

func (*Client) SetStructMapper added in v1.1.1

func (c *Client) SetStructMapper(mapper reflext.StructMapper) *Client

SetStructMapper : StructMapper is a mapper to reflect a struct on runtime and provide struct info

type Column

type Column struct {
	// column name
	Name string

	// column position in sql database
	Position int

	// column data type with precision or size, eg. VARCHAR(20)
	Type string

	// column data type without precision and size, eg. VARCHAR
	DataType string

	// whether column is nullable or not
	IsNullable types.Boolean

	// default value of the column
	DefaultValue *string

	// text character set encoding
	Charset *string

	// text collation for sorting
	Collation *string

	// column comment
	Comment string

	// extra information
	Extra string
}

Column : contains sql column information

type ColumnView

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

ColumnView :

func (*ColumnView) DropOne

func (cv *ColumnView) DropOne(ctx context.Context, name string) error

DropOne : drop column with name

func (*ColumnView) List

func (cv *ColumnView) List(ctx context.Context) ([]Column, error)

List : list all the column from current table

func (*ColumnView) Rename

func (cv *ColumnView) Rename(ctx context.Context, oldColName, newColName string) error

Rename : rename your column name

type Database

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

Database :

func (*Database) BeginTransaction

func (db *Database) BeginTransaction(ctx context.Context, opts ...*sql.TxOptions) (*Transaction, error)

BeginTransaction :

func (*Database) BuildIndexes

func (db *Database) BuildIndexes(ctx context.Context, paths ...string) error

BuildIndexes :

func (*Database) QueryStmt

func (db *Database) QueryStmt(ctx context.Context, query interface{}) (*Result, error)

QueryStmt :

func (*Database) RunInTransaction

func (db *Database) RunInTransaction(ctx context.Context, cb txCallback, opts ...*options.TransactionOptions) error

RunInTransaction :

func (*Database) Table

func (db *Database) Table(name string) *Table

Table :

type DriverInfo

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

DriverInfo :

func (*DriverInfo) Charset

func (d *DriverInfo) Charset() charset.Code

Charset :

func (*DriverInfo) Collate

func (d *DriverInfo) Collate() string

Collate :

func (*DriverInfo) DriverName

func (d *DriverInfo) DriverName() string

DriverName :

func (*DriverInfo) Version

func (d *DriverInfo) Version() *semver.Version

Version :

type Index

type Index struct {
	Name     string
	Type     string
	IsUnique bool
}

Index :

type IndexView

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

IndexView :

func (*IndexView) Create

func (idv *IndexView) Create(ctx context.Context, idxs []indexes.Index) error

Create :

func (*IndexView) CreateIfNotExists

func (idv *IndexView) CreateIfNotExists(ctx context.Context, idxs []indexes.Index) error

CreateIfNotExists :

func (*IndexView) CreateOne

func (idv *IndexView) CreateOne(ctx context.Context, idx indexes.Index) error

CreateOne :

func (*IndexView) CreateOneIfNotExists

func (idv *IndexView) CreateOneIfNotExists(ctx context.Context, idx indexes.Index) error

CreateOneIfNotExists :

func (*IndexView) DropAll added in v1.0.2

func (idv *IndexView) DropAll(ctx context.Context) error

DropAll :

func (IndexView) DropOne

func (idv IndexView) DropOne(ctx context.Context, name string) error

DropOne :

func (*IndexView) List

func (idv *IndexView) List(ctx context.Context) ([]Index, error)

List :

type Paginator

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

Paginator :

func (*Paginator) All

func (pg *Paginator) All(results interface{}) error

All :

func (*Paginator) NextCursor

func (pg *Paginator) NextCursor(ctx context.Context, cursor interface{}) (err error)

NextCursor :

type Result

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

Result :

func (*Result) All

func (r *Result) All(results interface{}) error

All : this will map all the records from sql to a slice of struct.

func (*Result) Close

func (r *Result) Close() error

Close :

func (*Result) ColumnTypes

func (r *Result) ColumnTypes() ([]*sql.ColumnType, error)

ColumnTypes :

func (*Result) Columns

func (r *Result) Columns() []string

Columns :

func (*Result) Decode

func (r *Result) Decode(dst interface{}) error

Decode will decode the current document into val, this will only accepting pointer of struct as an input.

func (*Result) Error

func (r *Result) Error() error

Error :

func (*Result) Next

func (r *Result) Next() bool

Next :

func (*Result) NextResultSet added in v1.1.0

func (r *Result) NextResultSet() bool

NextResultSet :

func (*Result) Scan

func (r *Result) Scan(dests ...interface{}) error

Scan : will behave as similar as sql.Scan.

func (*Result) ScanSlice

func (r *Result) ScanSlice(results interface{}) error

ScanSlice :

type Resulter added in v1.1.0

type Resulter interface {
	Scan(dests ...interface{}) error
	Columns() []string
	Next() bool
	NextResultSet() bool
	Close() error
}

Resulter :

type SessionContext

type SessionContext interface {
	context.Context
	Table(name string) *Table
	QueryStmt(ctx context.Context, query interface{}) (*Result, error)
}

SessionContext :

type SingleResult

type SingleResult interface {
	Scan(dest ...interface{}) error
	Decode(dest interface{}) error
	Columns() []string
	ColumnTypes() ([]*sql.ColumnType, error)
	Error() error
}

SingleResult : single result is an interface implementing apis as similar as driver.Result

type Table

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

Table :

func (*Table) Columns

func (tb *Table) Columns() *ColumnView

Columns :

func (*Table) Delete

func (tb *Table) Delete(ctx context.Context, act actions.DeleteStatement, opts ...*options.DeleteOptions) (int64, error)

Delete : delete multiple record on the table using where clause. If you didn't provided any where clause, it will throw error. For multiple record deletion without where clause, you should use `Truncate` instead.

func (*Table) DeleteOne

func (tb *Table) DeleteOne(ctx context.Context, act actions.DeleteOneStatement, opts ...*options.DeleteOneOptions) (int64, error)

DeleteOne : delete single record on the table using where clause.

func (*Table) DestroyOne

func (tb *Table) DestroyOne(ctx context.Context, delete interface{}, opts ...*options.DestroyOneOptions) error

DestroyOne : hard delete a record on the table using primary key. You should alway have primary key defined in your struct in order to use this api.

func (Table) Drop

func (tb Table) Drop(ctx context.Context) (err error)

Drop : drop the table, but it might throw error when the table is not exists

func (Table) DropIfExists

func (tb Table) DropIfExists(ctx context.Context) (err error)

DropIfExists : will drop the table only if it exists.

func (*Table) Exists

func (tb *Table) Exists(ctx context.Context) bool

Exists : this will return true when the table exists in the database

func (*Table) Find

func (tb *Table) Find(ctx context.Context, act actions.SelectStatement, opts ...*options.FindOptions) (*Result, error)

Find : find multiple records on the table.

func (*Table) FindOne

FindOne : find single record on the table, you should alway check the return error to ensure it have result return.

func (*Table) HasIndexByName

func (tb *Table) HasIndexByName(ctx context.Context, name string) (bool, error)

HasIndexByName :

func (*Table) Indexes

func (tb *Table) Indexes() *IndexView

Indexes :

func (*Table) Insert

func (tb *Table) Insert(ctx context.Context, src interface{}, opts ...*options.InsertOptions) (sql.Result, error)

Insert : insert multiple records. You should always pass in the address of the slice.

func (*Table) InsertOne

func (tb *Table) InsertOne(ctx context.Context, src interface{}, opts ...*options.InsertOneOptions) (sql.Result, error)

InsertOne : insert single record. You should always pass in the address of input.

func (*Table) ListColumns

func (tb *Table) ListColumns(ctx context.Context) ([]Column, error)

ListColumns : list all the column of the table.

func (*Table) ListIndexes

func (tb *Table) ListIndexes(ctx context.Context) ([]Index, error)

ListIndexes : list all the index of the table.

func (*Table) Migrate

func (tb *Table) Migrate(ctx context.Context, entity interface{}) error

Migrate : migrate will create a new table follows by the definition of struct tag, alter when the table already exists

func (*Table) ModifyOne

func (tb *Table) ModifyOne(ctx context.Context, update interface{}, opts ...*options.ModifyOneOptions) error

ModifyOne :

func (Table) MustMigrate

func (tb Table) MustMigrate(ctx context.Context, entity interface{})

MustMigrate : this will ensure the migrate is complete, otherwise it will panic

func (*Table) MustUnsafeMigrate

func (tb *Table) MustUnsafeMigrate(ctx context.Context, entity interface{})

MustUnsafeMigrate : this will panic if it get error on unsafe migrate

func (*Table) Paginate

func (tb *Table) Paginate(ctx context.Context, act actions.PaginateStatement, opts ...*options.PaginateOptions) (*Paginator, error)

Paginate :

func (*Table) Rename

func (tb *Table) Rename(ctx context.Context, name string) error

Rename : rename the current table name to new table name

func (*Table) Replace

func (tb *Table) Replace(ctx context.Context, fields []string, query *sql.SelectStmt) error

Replace :

func (*Table) ReplaceOne

func (tb *Table) ReplaceOne(ctx context.Context, src interface{}, opts ...*options.InsertOneOptions) (sql.Result, error)

ReplaceOne :

func (*Table) Truncate

func (tb *Table) Truncate(ctx context.Context) (err error)

Truncate : delete all the table data.

func (*Table) UnsafeMigrate

func (tb *Table) UnsafeMigrate(ctx context.Context, entity interface{}) error

UnsafeMigrate : unsafe migration will delete non-exist index and columns, beware when you use this

func (*Table) Update

func (tb *Table) Update(ctx context.Context, act actions.UpdateStatement, opts ...*options.UpdateOptions) (int64, error)

Update :

func (*Table) UpdateOne

func (tb *Table) UpdateOne(ctx context.Context, act actions.UpdateOneStatement, opts ...*options.UpdateOneOptions) (int64, error)

UpdateOne :

type Transaction

type Transaction struct {
	// transaction context
	context.Context
	// contains filtered or unexported fields
}

Transaction :

func (*Transaction) CommitTransaction

func (tx *Transaction) CommitTransaction() error

CommitTransaction :

func (*Transaction) QueryStmt

func (tx *Transaction) QueryStmt(ctx context.Context, query interface{}) (*Result, error)

QueryStmt : support complex and advance query statement

func (*Transaction) RollbackTransaction

func (tx *Transaction) RollbackTransaction() error

RollbackTransaction :

func (*Transaction) Table

func (tx *Transaction) Table(name string) *Table

Table :

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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