activerecord

package
v0.0.0-...-d0e8626 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActiveRecord

type ActiveRecord struct {
	DB     *sql.DB
	Tokens []string
	Args   []interface{}
}

ActiveRecord represents a database connection and a sql string.

func (*ActiveRecord) AddArgs

func (ar *ActiveRecord) AddArgs(args ...interface{}) MockableActiveRecord

AddArgs append args to ar.Args

func (*ActiveRecord) AddParenthesis

func (ar *ActiveRecord) AddParenthesis() MockableActiveRecord

AddParenthesis (all the tokens)

func (*ActiveRecord) AddSQL

func (ar *ActiveRecord) AddSQL(sql string) MockableActiveRecord

AddSQL add the given sql into token

func (*ActiveRecord) AddToken

func (ar *ActiveRecord) AddToken(token ...string) MockableActiveRecord

AddToken append token after the existing Tokens.

func (*ActiveRecord) And

func (ar *ActiveRecord) And(cond string, args ...interface{}) MockableActiveRecord

And add "AND" and condition into token, and args to Args

func (*ActiveRecord) Append

Append append another ActiveRecord's Token after the existing Tokens.

func (*ActiveRecord) ArgsString

func (ar *ActiveRecord) ArgsString() string

ArgsString print all the arguments

func (*ActiveRecord) Asc

Asc add "ASC" in token, should be used with OrderBy

func (*ActiveRecord) Begin

func (ar *ActiveRecord) Begin() (MockableTx, error)

Begin begin a real transaction

func (*ActiveRecord) CleanTokens

func (ar *ActiveRecord) CleanTokens() MockableActiveRecord

CleanTokens clean all tokens

func (*ActiveRecord) Close

func (ar *ActiveRecord) Close()

Close close the connection.

func (*ActiveRecord) Connect

func (ar *ActiveRecord) Connect(dbtype string, url string) (err error)

Connect connect to dbtype database by provided url.

func (*ActiveRecord) Delete

func (ar *ActiveRecord) Delete(tables ...string) MockableActiveRecord

Delete add "DELETE" and the tables into tokens

func (*ActiveRecord) Desc

Desc add "DESC" in token, should be used with OrderBy

func (*ActiveRecord) Exec

func (ar *ActiveRecord) Exec() (sql.Result, error)

Exec will execute the sql string represented by ActiveRecord.

func (*ActiveRecord) ExecSQL

func (ar *ActiveRecord) ExecSQL(sql string, args ...interface{}) (sql.Result, error)

ExecSQL execute the sql string in argument.

func (*ActiveRecord) ExecString

func (ar *ActiveRecord) ExecString() string

ExecString return the sql string represented by Tokens.

func (*ActiveRecord) From

func (ar *ActiveRecord) From(tables ...string) MockableActiveRecord

From add "FROM" and the given tables as token

func (*ActiveRecord) GetArgs

func (ar *ActiveRecord) GetArgs() []interface{}

GetArgs return all ar's args

func (*ActiveRecord) GetCount

func (ar *ActiveRecord) GetCount(args ...interface{}) (count int, err error)

GetCount get the row count of the result of ActiveRecord sql.

func (*ActiveRecord) GetRow

func (ar *ActiveRecord) GetRow() (map[string]interface{}, error)

GetRow return the first row of execution result of ActiveRecord sql, all of the values are in interface{} type.

func (*ActiveRecord) GetRows

func (ar *ActiveRecord) GetRows() (result []map[string]interface{}, err error)

GetRows return the full execution result of ActiveRecord sql, all of the values are in interface{} type.

func (*ActiveRecord) GetRowsI

func (ar *ActiveRecord) GetRowsI(query string, args ...interface{}) (result []map[string]interface{}, err error)

GetRowsI get rows as map[string]interface{}

func (*ActiveRecord) GetTokens

func (ar *ActiveRecord) GetTokens() []string

GetTokens return all ar's Tokens

func (*ActiveRecord) GroupBy

func (ar *ActiveRecord) GroupBy(fields ...string) MockableActiveRecord

GroupBy add "GROUP BY" and the fileds into tokens

func (*ActiveRecord) Having

func (ar *ActiveRecord) Having(cond string, args ...interface{}) MockableActiveRecord

Having add the token "HAVING" and the conditions into tokens, args into Args

func (*ActiveRecord) In

func (ar *ActiveRecord) In(vals []string, args ...interface{}) MockableActiveRecord

In add "IN" and values into token and args into Args

func (*ActiveRecord) InAddQuotes

func (ar *ActiveRecord) InAddQuotes(vals []string, args ...interface{}) MockableActiveRecord

InAddQuotes is the same as In but the values are quoted with ”

func (*ActiveRecord) InnerJoin

func (ar *ActiveRecord) InnerJoin(table string) MockableActiveRecord

InnerJoin add "INNER JOIN" and the table name

func (*ActiveRecord) InsertInto

func (ar *ActiveRecord) InsertInto(table string, fields ...string) MockableActiveRecord

InsertInto add "INSERT INTO" and the table into tokens

func (*ActiveRecord) Join

func (ar *ActiveRecord) Join(table string) MockableActiveRecord

Join add "JOIN" and the given table name

func (*ActiveRecord) LeftJoin

func (ar *ActiveRecord) LeftJoin(table string) MockableActiveRecord

LeftJoin add "LEFT JOIN" and the table name

func (*ActiveRecord) LeftOuterJoin

func (ar *ActiveRecord) LeftOuterJoin(table string) MockableActiveRecord

LeftOuterJoin add "LEFT OUTER JOIN" and the table name

func (*ActiveRecord) Limit

func (ar *ActiveRecord) Limit(limit int) MockableActiveRecord

Limit add "LIMIT" and the number in tokens

func (*ActiveRecord) Offset

func (ar *ActiveRecord) Offset(offset int) MockableActiveRecord

Offset add "OFFSET" and the number in tokens

func (*ActiveRecord) On

func (ar *ActiveRecord) On(cond string, args ...interface{}) MockableActiveRecord

On add "ON" and the condition string into tokens, and args into Args

func (*ActiveRecord) Or

func (ar *ActiveRecord) Or(cond string, args ...interface{}) MockableActiveRecord

Or add "OR" and condition into tokens, and args into Args

func (*ActiveRecord) OrderBy

func (ar *ActiveRecord) OrderBy(fields ...string) MockableActiveRecord

OrderBy add "ORDER BY" and the fields joined with comma

func (*ActiveRecord) PrintableString

func (ar *ActiveRecord) PrintableString() string

PrintableString print the sql and arguments

func (*ActiveRecord) RightJoin

func (ar *ActiveRecord) RightJoin(table string) MockableActiveRecord

RightJoin add "RIGHT JOIN" and the table name

func (*ActiveRecord) RightOuterJoin

func (ar *ActiveRecord) RightOuterJoin(table string) MockableActiveRecord

RightOuterJoin add "RIGHT OUTER JOIN" and the table name

func (*ActiveRecord) Select

func (ar *ActiveRecord) Select(fields ...string) MockableActiveRecord

Select add "select" and the given fields

func (*ActiveRecord) SelectDistinct

func (ar *ActiveRecord) SelectDistinct(fields ...string) MockableActiveRecord

SelectDistinct add "SELECT DISTINCT" and the given fields

func (*ActiveRecord) Set

func (ar *ActiveRecord) Set(kv ...string) MockableActiveRecord

Set add the token "SET" and the key values

func (*ActiveRecord) Show

Show add "SHOW" and key value sting

func (*ActiveRecord) String

func (ar *ActiveRecord) String() string

String print all the tokens

func (*ActiveRecord) SubAR

func (ar *ActiveRecord) SubAR(sub MockableActiveRecord, alias string) string

SubAR add add another ar as sub ar

func (*ActiveRecord) Subquery

func (ar *ActiveRecord) Subquery(sub string, alias string) string

Subquery combine a sub query and its alias into (sql) as alias format

func (*ActiveRecord) Update

func (ar *ActiveRecord) Update(tables ...string) MockableActiveRecord

Update add the token "UPDATE" and the tables name into tokens

func (*ActiveRecord) Values

func (ar *ActiveRecord) Values(vals []string, args ...interface{}) MockableActiveRecord

Values add "VALUES" and the options values into token

func (*ActiveRecord) Where

func (ar *ActiveRecord) Where(cond string, args ...interface{}) MockableActiveRecord

Where add "WHERE" and the condition to tokens, and args into Args

func (*ActiveRecord) WhereAnd

func (ar *ActiveRecord) WhereAnd(conds []string, args ...interface{}) MockableActiveRecord

WhereAnd add conditions into token and args into Args

type FakeActiveRecord

type FakeActiveRecord struct {
	*ActiveRecord
	MockGetRows func() ([]map[string]interface{}, error)
	MockGetRow  func(tokens []string) (map[string]interface{}, error)
	MockExecSQL func(sql string, args ...interface{}) (sql.Result, error)
	MockBegin   func() (MockableTx, error)
	MockClose   func()
}

FakeActiveRecord is only for test. You can change MockGetRows to return your own data

func NewFakeActiveRecord

func NewFakeActiveRecord() *FakeActiveRecord

NewFakeActiveRecord return a *FakeActiveRecord

func (*FakeActiveRecord) Begin

func (m *FakeActiveRecord) Begin() (MockableTx, error)

Begin will call Begin if it is set

func (*FakeActiveRecord) Close

func (m *FakeActiveRecord) Close()

Begin will call Begin if it is set

func (*FakeActiveRecord) ExecSQL

func (m *FakeActiveRecord) ExecSQL(sql string, args ...interface{}) (sql.Result, error)

ExecSQL will call MockExecSQL if it is set

func (*FakeActiveRecord) GetRow

func (m *FakeActiveRecord) GetRow() (map[string]interface{}, error)

GetRow will call MockGetRow in MockActiveRecord if it is set

func (*FakeActiveRecord) GetRows

func (m *FakeActiveRecord) GetRows() ([]map[string]interface{}, error)

GetRows will call MockGetRows in MockActiveRecord if it is set

type FakeStmt

type FakeStmt struct {
	MockExec  func(args ...interface{}) (sql.Result, error)
	MockClose func() error
}

FakeStmt is only for test.

func (*FakeStmt) Close

func (m *FakeStmt) Close() error

Close will call MockClose in FakeStmt if it is set

func (*FakeStmt) Exec

func (m *FakeStmt) Exec(args ...interface{}) (sql.Result, error)

Exec will call MockExec in FakeStmt if it is set

type FakeTx

type FakeTx struct {
	*sql.Tx
	MockExec     func(query string, args ...interface{}) (sql.Result, error)
	MockRollback func() error
	MockCommit   func() error
	MockGetRow   func(query string) (result map[string]interface{}, err error)
	MockGetRows  func(query string) (result []map[string]interface{}, err error)
	MockPrepare  func(query string) (MockableStmt, error)
}

FakeActiveRecord is only for test. You can change MockGetRows to return your own data

func NewFakeTx

func NewFakeTx() *FakeTx

NewFakeTx return a *FakeTx

func (*FakeTx) Commit

func (m *FakeTx) Commit() error

Commit will call MockCommit in FakeTx if it is set

func (*FakeTx) Exec

func (m *FakeTx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec will call MockExec in FakeTx if it is set

func (*FakeTx) GetRow

func (m *FakeTx) GetRow(query string) (result map[string]interface{}, err error)

GetRow will call MockGetRow in FakeTx if it is set

func (*FakeTx) GetRows

func (m *FakeTx) GetRows(query string) (result []map[string]interface{}, err error)

GetRows will call MockGetRows in FakeTx if it is set

func (*FakeTx) Prepare

func (m *FakeTx) Prepare(query string) (MockableStmt, error)

Prepare will call MockPrepare in FakeTx if it is set

func (*FakeTx) Rollback

func (m *FakeTx) Rollback() error

Rollback will call MockRollback in FakeTx if it is set

type MockableActiveRecord

type MockableActiveRecord interface {
	GetRows() ([]map[string]interface{}, error)
	GetRow() (map[string]interface{}, error)
	GetRowsI(query string, args ...interface{}) (result []map[string]interface{}, err error)
	Exec() (sql.Result, error)
	GetCount(args ...interface{}) (count int, err error)
	Connect(dbtype string, url string) (err error)
	ExecSQL(sql string, args ...interface{}) (sql.Result, error)
	Select(fields ...string) MockableActiveRecord
	SelectDistinct(fields ...string) MockableActiveRecord
	From(tables ...string) MockableActiveRecord
	Join(table string) MockableActiveRecord
	InnerJoin(table string) MockableActiveRecord
	LeftJoin(table string) MockableActiveRecord
	RightJoin(table string) MockableActiveRecord
	LeftOuterJoin(table string) MockableActiveRecord
	RightOuterJoin(table string) MockableActiveRecord
	On(cond string, args ...interface{}) MockableActiveRecord
	Where(cond string, args ...interface{}) MockableActiveRecord
	And(cond string, args ...interface{}) MockableActiveRecord
	WhereAnd(conds []string, args ...interface{}) MockableActiveRecord
	Or(cond string, args ...interface{}) MockableActiveRecord
	In(vals []string, args ...interface{}) MockableActiveRecord
	InAddQuotes(vals []string, args ...interface{}) MockableActiveRecord
	OrderBy(fields ...string) MockableActiveRecord
	Asc() MockableActiveRecord
	Desc() MockableActiveRecord
	Limit(limit int) MockableActiveRecord
	Offset(offset int) MockableActiveRecord
	GroupBy(fields ...string) MockableActiveRecord
	Having(cond string, args ...interface{}) MockableActiveRecord
	Update(tables ...string) MockableActiveRecord
	Set(kv ...string) MockableActiveRecord
	Show(kv string) MockableActiveRecord
	Delete(tables ...string) MockableActiveRecord
	InsertInto(table string, fields ...string) MockableActiveRecord
	Values(vals []string, args ...interface{}) MockableActiveRecord
	AddSQL(sql string) MockableActiveRecord
	AddToken(token ...string) MockableActiveRecord
	AddArgs(args ...interface{}) MockableActiveRecord
	AddParenthesis() MockableActiveRecord
	Append(other MockableActiveRecord) MockableActiveRecord
	CleanTokens() MockableActiveRecord
	GetTokens() []string
	GetArgs() []interface{}
	Close()
	String() string
	Subquery(sub string, alias string) string
	ExecString() string
	SubAR(sub MockableActiveRecord, alias string) string
	ArgsString() string
	PrintableString() string
	Begin() (MockableTx, error)
}

MockableActiveRecord is a mock AR for test usage

func NewActiveRecord

func NewActiveRecord() MockableActiveRecord

NewActiveRecord is the factory method to create an empty ActiveRecord

func NewActiveRecordWithDB

func NewActiveRecordWithDB(db *sql.DB) MockableActiveRecord

NewActiveRecordWithDB return a ActiveRecord with given sql.DB connection

type MockableStmt

type MockableStmt interface {
	Exec(args ...interface{}) (sql.Result, error)
	Close() error
}

type MockableTx

type MockableTx interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Rollback() error
	Commit() error
	GetRow(query string) (result map[string]interface{}, err error)
	GetRows(query string) (result []map[string]interface{}, err error)
	Prepare(query string) (MockableStmt, error)
}

MockableTx is a mock TX for test usage

Jump to

Keyboard shortcuts

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