Documentation
¶
Overview ¶
Provides database connections in factory mode to optimize database connections
以工厂的模式构建数据库,以避免数据库被多次打开。 因database/sql本身已实现连接池,因此没有必要创建多个同一的数据库连接实例
Example:
qSql = &database.Page{ CountSql:`SELECT count(1) FROM user_info WHERE create_time >= ? AND create_time <= ?`, DataSql:`SELECT mobile, balance FROM user_info WHERE create_time >= ? AND create_time <= ?` }
count, titles, result, err := qSql.QueryPageArray(db, true, condition, 0, 10) ... Or count, titles, result, err := qSql.QueryPageMap(db, true, condtion, 0, 10) ... if err != nil { ... }
Index ¶
- Constants
- Variables
- func Close(closer io.Closer)
- func CloseCache()
- func Exec(db Execer, querySql string, args ...interface{}) (sql.Result, error)
- func ExecContext(db Execer, ctx context.Context, querySql string, args ...interface{}) (sql.Result, error)
- func ExecMultiTx(tx *sql.Tx, mTx []*MultiTx) error
- func ExecMultiTxContext(tx *sql.Tx, ctx context.Context, mTx []*MultiTx) error
- func InsertStruct(exec Execer, obj interface{}, tbName string, drvNames ...string) (sql.Result, error)
- func InsertStructContext(exec Execer, ctx context.Context, obj interface{}, tbName string, ...) (sql.Result, error)
- func Query(db Queryer, querySql string, args ...interface{}) (*sql.Rows, error)
- func QueryContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) (*sql.Rows, error)
- func QueryElem(db Queryer, result interface{}, querySql string, args ...interface{}) error
- func QueryElemContext(db Queryer, ctx context.Context, result interface{}, querySql string, ...) error
- func QueryElems(db Queryer, result interface{}, querySql string, args ...interface{}) error
- func QueryElemsContext(db Queryer, ctx context.Context, result interface{}, querySql string, ...) error
- func QueryPageArr(db Queryer, querySql string, args ...interface{}) (titles []string, result [][]interface{}, err error)
- func QueryPageArrContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) (titles []string, result [][]interface{}, err error)
- func QueryPageMap(db Queryer, querySql string, args ...interface{}) (titles []string, result []map[string]interface{}, err error)
- func QueryPageMapContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) (titles []string, result []map[string]interface{}, err error)
- func QueryRow(db Queryer, querySql string, args ...interface{}) *sql.Row
- func QueryRowContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) *sql.Row
- func QueryStruct(db Queryer, obj interface{}, querySql string, args ...interface{}) error
- func QueryStructContext(db Queryer, ctx context.Context, obj interface{}, querySql string, ...) error
- func QueryStructs(db Queryer, obj interface{}, querySql string, args ...interface{}) error
- func QueryStructsContext(db Queryer, ctx context.Context, obj interface{}, querySql string, ...) error
- func RegCache(iniFileName, sectionName string, db *DB)
- func Rollback(tx *sql.Tx)
- func ScanStruct(rows Rows, obj interface{}) error
- func ScanStructs(rows Rows, obj interface{}) error
- type Bool
- type DB
- type DBData
- type Execer
- type Float64
- type Int64
- type MultiTx
- type PageArgs
- type PageSql
- func (p PageSql) CountSql() string
- func (p PageSql) DataSql() string
- func (p PageSql) FmtPage(args ...interface{}) PageSql
- func (p *PageSql) QueryCount(db *DB, args ...interface{}) (int64, error)
- func (p *PageSql) QueryPageArr(db *DB, doCount bool, args *PageArgs) (int64, []string, [][]interface{}, error)
- func (p *PageSql) QueryPageMap(db *DB, doCount bool, args *PageArgs) (int64, []string, []map[string]interface{}, error)
- type Queryer
- type Rows
- type String
Constants ¶
const ( DRV_NAME_MYSQL = "mysql" DRV_NAME_ORACLE = "oracle" // or "oci8" DRV_NAME_POSTGRES = "postgres" DRV_NAME_SQLITE3 = "sqlite3" DRV_NAME_SQLSERVER = "sqlserver" // or "mssql" )
Variables ¶
var ( // Whe reflect the QueryStruct, InsertStruct, it need set the Driver first. // For example: // func init(){ // database.REFLECT_DRV_NAME = database.DEV_NAME_SQLITE3 // } // Default is using the mysql driver. REFLECT_DRV_NAME = DRV_NAME_MYSQL )
Functions ¶
func ExecContext ¶
func ExecMultiTxContext ¶
func InsertStruct ¶
func InsertStruct(exec Execer, obj interface{}, tbName string, drvNames ...string) (sql.Result, error)
Reflect one db data to the struct. the struct tag format like `db:"field_title"`, reference to: http://github.com/jmoiron/sqlx When you no set the REFLECT_DRV_NAME, you can point out with the drvName
func InsertStructContext ¶
func QueryContext ¶
func QueryElemContext ¶
func QueryElems ¶
Query one field to a sql.Scanner array.
func QueryElemsContext ¶
func QueryPageArr ¶
func QueryPageArr(db Queryer, querySql string, args ...interface{}) (titles []string, result [][]interface{}, err error)
Reflect the query result to a string array.
func QueryPageArrContext ¶
func QueryPageMap ¶
func QueryPageMap(db Queryer, querySql string, args ...interface{}) (titles []string, result []map[string]interface{}, err error)
Reflect the query result to a string map.
func QueryPageMapContext ¶
func QueryRowContext ¶
func QueryStruct ¶
Reflect the sql.Query result to a struct.
func QueryStructContext ¶
func QueryStructs ¶
Reflect the sql.Query result to a struct array. Return empty array if data not found.
func QueryStructsContext ¶
func ScanStruct ¶
Relect the sql.Rows to a struct.
func ScanStructs ¶
Reflect the sql.Rows to a struct array. Return empty array if data not found. Refere to: github.com/jmoiron/sqlx
Types ¶
type DB ¶
仅继承并重写sql.DB, 不增加新的方法, 以便可直接使用sql.DB的方法,提高访问效率与降低使用复杂性
func GetCache ¶
Get the db instance from the cache. If the db not in the cache, it will create a new instance from the ini file.
func (*DB) DriverName ¶
type PageArgs ¶
type PageArgs struct {
// contains filtered or unexported fields
}
func NewPageArgs ¶
func NewPageArgs(args ...interface{}) *PageArgs
type PageSql ¶
type PageSql struct {
// contains filtered or unexported fields
}
func NewPageSql ¶
func (PageSql) FmtPage ¶
fill the page sql with fmt arg, and return a new page Typically used for table name formatting
func (*PageSql) QueryCount ¶
func (*PageSql) QueryPageArr ¶
type Queryer ¶
type Queryer interface { Query(query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }