Documentation ¶
Index ¶
- Constants
- Variables
- func ChangePoolSize(size int)
- func IntsToInterfaces(ints ...int64) []interface{}
- func Register(driverName, driverSourceName, databaseName string, dialect Dialect)
- func RegisterSqlite3(dbFileName string)
- func RegisterWithDataSourceName(dsn *DataSourceName)
- func RegisterWithDb(driverName string, database *sql.DB, dialect Dialect)
- func SetConnectionLimit(maxCon int, blocking bool)
- func SetLogger(query *log.Logger, err *log.Logger)
- func StringsToInterfaces(strs ...string) []interface{}
- func WithMigration(task func(mg *Migration) error) error
- func WithQbs(task func(*Qbs) error) error
- type Assert
- func (ast *Assert) Equal(expected, actual interface{}, logs ...interface{})
- func (ast *Assert) MustEqual(expected, actual interface{}, logs ...interface{})
- func (ast *Assert) MustNil(value interface{}, logs ...interface{})
- func (ast *Assert) MustNotEqual(expected, actual interface{}, logs ...interface{})
- func (ast *Assert) MustNotNil(value interface{}, logs ...interface{})
- func (ast *Assert) MustTrue(boolValue bool, logs ...interface{})
- func (ast *Assert) Nil(value interface{}, logs ...interface{})
- func (ast *Assert) NotEqual(expected, actual interface{}, logs ...interface{})
- func (ast *Assert) NotNil(value interface{}, logs ...interface{})
- func (ast *Assert) True(boolValue bool, logs ...interface{})
- type Condition
- func (c *Condition) And(expr string, args ...interface{}) *Condition
- func (c *Condition) AndCondition(subCondition *Condition) *Condition
- func (c *Condition) AndEqual(column string, value interface{}) *Condition
- func (c *Condition) Merge() (expr string, args []interface{})
- func (c *Condition) Or(expr string, args ...interface{}) *Condition
- func (c *Condition) OrCondition(subCondition *Condition) *Condition
- func (c *Condition) OrEqual(column string, value interface{}) *Condition
- type DataSourceName
- type Dialect
- type Indexed
- type Indexes
- type Migration
- type Qbs
- func (q *Qbs) Begin() error
- func (q *Qbs) BulkInsert(sliceOfStructPtr interface{}) error
- func (q *Qbs) Close() error
- func (q *Qbs) Commit() error
- func (q *Qbs) Condition(condition *Condition) *Qbs
- func (q *Qbs) ContainsValue(table interface{}, column string, value interface{}) bool
- func (q *Qbs) Count(table interface{}) int64
- func (q *Qbs) Delete(structPtr interface{}) (affected int64, err error)
- func (q *Qbs) Exec(query string, args ...interface{}) (sql.Result, error)
- func (q *Qbs) Find(structPtr interface{}) error
- func (q *Qbs) FindAll(ptrOfSliceOfStructPtr interface{}) error
- func (q *Qbs) InTransaction() bool
- func (q *Qbs) Iterate(structPtr interface{}, do func() error) error
- func (q *Qbs) Limit(limit int) *Qbs
- func (q *Qbs) Offset(offset int) *Qbs
- func (q *Qbs) OmitFields(fieldName ...string) *Qbs
- func (q *Qbs) OmitJoin() *Qbs
- func (q *Qbs) OrderBy(path string) *Qbs
- func (q *Qbs) OrderByDesc(path string) *Qbs
- func (q *Qbs) Query(query string, args ...interface{}) (rows *sql.Rows, err error)
- func (q *Qbs) QueryMap(query string, args ...interface{}) (map[string]interface{}, error)
- func (q *Qbs) QueryMapSlice(query string, args ...interface{}) ([]map[string]interface{}, error)
- func (q *Qbs) QueryRow(query string, args ...interface{}) *sql.Row
- func (q *Qbs) QueryStruct(dest interface{}, query string, args ...interface{}) error
- func (q *Qbs) Reset()
- func (q *Qbs) Rollback() error
- func (q *Qbs) Save(structPtr interface{}) (affected int64, err error)
- func (q *Qbs) Update(structPtr interface{}) (affected int64, err error)
- func (q *Qbs) Where(expr string, args ...interface{}) *Qbs
- func (q *Qbs) WhereEqual(column string, value interface{}) *Qbs
- func (q *Qbs) WhereIn(column string, values []interface{}) *Qbs
- type TableNamer
- type Validator
Constants ¶
const QBS_COLTYPE_BIGINT = "bigint"
const QBS_COLTYPE_BOOL = "boolean"
const QBS_COLTYPE_DOUBLE = "double"
const QBS_COLTYPE_INT = "int"
const QBS_COLTYPE_TEXT = "text"
const QBS_COLTYPE_TIME = "timestamp"
Variables ¶
var ColumnNameToFieldName func(string) string = snakeToUpperCamel
onvert column name to struct field name.
var ConnectionLimitError = errors.New("Connection limit reached")
var FieldNameToColumnName func(string) string = toSnake
convert struct field name to column name.
var StructNameToTableName func(string) string = toSnake
convert struct name to table name.
var TableNameToStructName func(string) string = snakeToUpperCamel
convert table name to struct name.
Functions ¶
func IntsToInterfaces ¶
func IntsToInterfaces(ints ...int64) []interface{}
func RegisterSqlite3 ¶
func RegisterSqlite3(dbFileName string)
func RegisterWithDataSourceName ¶
func RegisterWithDataSourceName(dsn *DataSourceName)
func SetConnectionLimit ¶
Set the connection limit, there is no limit by default. If blocking is true, GetQbs method will be blocked, otherwise returns ConnectionLimitError.
func StringsToInterfaces ¶
func StringsToInterfaces(strs ...string) []interface{}
Used for in condition.
func WithMigration ¶
A safe and easy way to work with Migration instance without the need to open and close it.
Types ¶
type Assert ¶
type Assert struct {
// contains filtered or unexported fields
}
func (*Assert) MustEqual ¶
func (ast *Assert) MustEqual(expected, actual interface{}, logs ...interface{})
func (*Assert) MustNotEqual ¶
func (ast *Assert) MustNotEqual(expected, actual interface{}, logs ...interface{})
func (*Assert) MustNotNil ¶
func (ast *Assert) MustNotNil(value interface{}, logs ...interface{})
type Condition ¶
type Condition struct {
// contains filtered or unexported fields
}
Conditions are structured in a way to define complex where clause easily.
func NewCondition ¶
func NewEqualCondition ¶
Snakecase column name
func NewInCondition ¶
func (*Condition) AndCondition ¶
func (*Condition) OrCondition ¶
type DataSourceName ¶
type DataSourceName struct { DbName string Username string Password string UnixSocket bool Host string Port string Variables []string Dialect Dialect }
func DefaultMysqlDataSourceName ¶
func DefaultMysqlDataSourceName(dbName string) *DataSourceName
func DefaultPostgresDataSourceName ¶
func DefaultPostgresDataSourceName(dbName string) *DataSourceName
func (*DataSourceName) Append ¶
func (dsn *DataSourceName) Append(key, value string) *DataSourceName
func (*DataSourceName) String ¶
func (dsn *DataSourceName) String() string
type Dialect ¶
type Dialect interface {
// contains filtered or unexported methods
}
func NewPostgres ¶
func NewPostgres() Dialect
func NewSqlite3 ¶
func NewSqlite3() Dialect
type Migration ¶
type Migration struct { Log bool // contains filtered or unexported fields }
func GetMigration ¶
Get a Migration instance should get closed like Qbs instance.
func (*Migration) CreateIndexIfNotExists ¶
func (mg *Migration) CreateIndexIfNotExists(table interface{}, name string, unique bool, columns ...string) error
CreateIndex creates the specified index on table. Some databases like mysql do not support this feature directly, So dialect may need to query the database schema table to find out if an index exists. Normally you don't need to do it explicitly, it will be created automatically in CreateTableIfNotExists method.
func (*Migration) CreateTableIfNotExists ¶
CreateTableIfNotExists creates a new table and its indexes based on the table struct type It will panic if table creation failed, and it will return error if the index creation failed.
type Qbs ¶
type Qbs struct { Dialect Dialect Log bool //Set to true to print out sql statement. // contains filtered or unexported fields }
func GetQbs ¶
Get an Qbs instance, should call `defer q.Close()` next, like:
q, err := qbs.GetQbs() if err != nil { fmt.Println(err) return } defer q.Close() ...
func (*Qbs) Begin ¶
Begin create a transaction object internally You can perform queries with the same Qbs object no matter it is in transaction or not. It panics if it's already in a transaction.
func (*Qbs) BulkInsert ¶
func (*Qbs) Close ¶
If the connection pool is not full, the Db will be sent back into the pool, otherwise the Db will get closed.
func (*Qbs) Commit ¶
Commit commits a started transaction and will report the first error that occurred inside the transaction.
func (*Qbs) Condition ¶
Condition defines the SQL "WHERE" clause If other condition can be inferred by the struct argument in Find method, it will be merged with AND
func (*Qbs) ContainsValue ¶
This method can be used to validate unique column before trying to save The table parameter can be either a string or a struct pointer
func (*Qbs) Count ¶
Query the count of rows in a table the talbe parameter can be either a string or struct pointer. If condition is given, the count will be the count of rows meet that condition.
func (*Qbs) Delete ¶
The delete condition can be inferred by the Id value of the struct If neither Id value or condition are provided, it would cause runtime panic
func (*Qbs) Find ¶
Perform select query by parsing the struct's type and then fill the values into the struct All fields of supported types in the struct will be added in select clause. If Id value is provided, it will be added into the where clause If a foreign key field with its referenced struct pointer field are provided, It will perform a join query, the referenced struct pointer field will be filled in the values obtained by the query. If not found, "sql.ErrNoRows" will be returned.
func (*Qbs) FindAll ¶
Similar to Find, except that FindAll accept pointer of slice of struct pointer, rows will be appended to the slice.
func (*Qbs) InTransaction ¶
func (*Qbs) Iterate ¶
Iterate the rows, the first parameter is a struct pointer, the second parameter is a fucntion which will get called on each row, the in `do` function the structPtr's value will be set to the current row's value.. if `do` function returns an error, the iteration will be stopped.
func (*Qbs) OrderByDesc ¶
func (*Qbs) QueryMapSlice ¶
Query raw sql and return a slice of map..
func (*Qbs) QueryRow ¶
Same as sql.Db.QueryRow or sql.Tx.QueryRow depends on if transaction has began
func (*Qbs) QueryStruct ¶
Do a raw sql query and set the result values in dest parameter. The dest parameter can be either a struct pointer or a pointer of struct pointer.slice This method do not support pointer field in the struct.
func (*Qbs) Save ¶
If Id value is not provided, save will insert the record, and the Id value will be filled in the struct after insertion. If Id value is provided, save will do a query count first to see if the row exists, if not then insert it, otherwise update it. If struct implements Validator interface, it will be validated first
func (*Qbs) Update ¶
If the struct type implements Validator interface, values will be validated before update. In order to avoid inadvertently update the struct field to zero value, it is better to define a temporary struct in function, only define the fields that should be updated. But the temporary struct can not implement Validator interface, we have to validate values manually. The update condition can be inferred by the Id value of the struct. If neither Id value or condition are provided, it would cause runtime panic
func (*Qbs) WhereEqual ¶
Snakecase column name
type TableNamer ¶
type TableNamer interface {
TableName() string
}