|
|
|
@ -16,9 +16,8 @@ type StarDB struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StarTx struct {
|
|
|
|
|
Db *sql.DB
|
|
|
|
|
Tx *sql.Tx
|
|
|
|
|
ManualScan bool
|
|
|
|
|
Db *StarDB
|
|
|
|
|
Tx *sql.Tx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StarRows 为查询结果集(按行)
|
|
|
|
@ -33,6 +32,11 @@ type StarRows struct {
|
|
|
|
|
parsed bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StarDBStmt struct {
|
|
|
|
|
Stmt *sql.Stmt
|
|
|
|
|
Db *StarDB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StarResult 为查询结果集(总)
|
|
|
|
|
type StarResult struct {
|
|
|
|
|
Result []interface{}
|
|
|
|
@ -848,7 +852,7 @@ func (star *StarDB) Begin() (*StarTx, error) {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stx := new(StarTx)
|
|
|
|
|
stx.Db = star.Db
|
|
|
|
|
stx.Db = star
|
|
|
|
|
stx.Tx = tx
|
|
|
|
|
return stx, err
|
|
|
|
|
}
|
|
|
|
@ -859,7 +863,7 @@ func (star *StarDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*StarTx,
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stx := new(StarTx)
|
|
|
|
|
stx.Db = star.Db
|
|
|
|
|
stx.Db = star
|
|
|
|
|
stx.Tx = tx
|
|
|
|
|
return stx, err
|
|
|
|
|
}
|
|
|
|
@ -871,40 +875,64 @@ func (star *StarTx) Query(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
func (star *StarTx) QueryContext(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
return star.query(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
func (star *StarTx) ExecStmt(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.Prepare(args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.Exec(args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) ExecStmtContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.PrepareContext(ctx, args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.ExecContext(ctx, args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) QueryStmt(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.Prepare(args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.Query(args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) QueryStmtContext(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.PrepareContext(ctx, args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.QueryContext(ctx, args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) query(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var rows *sql.Rows
|
|
|
|
|
var stmt *sql.Stmt
|
|
|
|
|
effect := new(StarRows)
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return effect, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if rows, err = star.Tx.Query(sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if rows, err = star.Tx.QueryContext(ctx, sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
effect.Rows = rows
|
|
|
|
|
err = effect.parserows()
|
|
|
|
|
if err = star.Db.Ping(); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
stmt, err = star.Tx.Prepare(sqlStr.(string))
|
|
|
|
|
} else {
|
|
|
|
|
stmt, err = star.Tx.PrepareContext(ctx, sqlStr.(string))
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return effect, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
var para []interface{}
|
|
|
|
|
for k, v := range args {
|
|
|
|
|
if k != 0 {
|
|
|
|
@ -915,16 +943,16 @@ func (star *StarTx) query(ctx context.Context, args ...interface{}) (*StarRows,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if rows, err = stmt.Query(para...); err != nil {
|
|
|
|
|
if rows, err = star.Tx.Query(args[0].(string), para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if rows, err = stmt.QueryContext(ctx, para...); err != nil {
|
|
|
|
|
if rows, err = star.Tx.QueryContext(ctx, args[0].(string), para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
effect.Rows = rows
|
|
|
|
|
if !star.ManualScan {
|
|
|
|
|
if !star.Db.ManualScan {
|
|
|
|
|
err = effect.parserows()
|
|
|
|
|
}
|
|
|
|
|
return effect, err
|
|
|
|
@ -938,43 +966,162 @@ func (star *StarDB) QueryContext(ctx context.Context, args ...interface{}) (*Sta
|
|
|
|
|
return star.query(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query 进行Query操作
|
|
|
|
|
func (star *StarDB) query(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
func (star *StarDB) QueryStmt(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.Prepare(args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.Query(args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) QueryStmtContext(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.PrepareContext(ctx, args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.QueryContext(ctx, args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) ExecStmt(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.Prepare(args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.Exec(args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) ExecStmtContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
if len(args) <= 1 {
|
|
|
|
|
return nil, errors.New("parameter not enough")
|
|
|
|
|
}
|
|
|
|
|
stmt, err := star.PrepareContext(ctx, args[0].(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
return stmt.ExecContext(ctx, args[1:]...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) Query(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
return star.query(nil, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) QueryContext(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
return star.query(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) Exec(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
return star.exec(nil, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
return star.exec(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) Close() error {
|
|
|
|
|
return star.Stmt.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) query(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var rows *sql.Rows
|
|
|
|
|
var stmt *sql.Stmt
|
|
|
|
|
effect := new(StarRows)
|
|
|
|
|
if err = star.Db.Ping(); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return effect, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if rows, err = star.Db.Query(sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if rows, err = star.Db.Query(sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if rows, err = star.Stmt.Query(args...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if rows, err = star.Stmt.QueryContext(ctx, args...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
effect.Rows = rows
|
|
|
|
|
}
|
|
|
|
|
effect.Rows = rows
|
|
|
|
|
if !star.Db.ManualScan {
|
|
|
|
|
err = effect.parserows()
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDBStmt) exec(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return nil, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
stmt, err = star.Db.Prepare(sqlStr.(string))
|
|
|
|
|
} else {
|
|
|
|
|
stmt, err = star.Db.PrepareContext(ctx, sqlStr.(string))
|
|
|
|
|
return star.Stmt.Exec(args...)
|
|
|
|
|
}
|
|
|
|
|
return star.Stmt.ExecContext(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) Prepare(sqlStr string) (*StarDBStmt, error) {
|
|
|
|
|
stmt := new(StarDBStmt)
|
|
|
|
|
stmtS, err := star.Db.Prepare(sqlStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stmt.Stmt = stmtS
|
|
|
|
|
stmt.Db = star
|
|
|
|
|
return stmt, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) PrepareContext(ctx context.Context, sqlStr string) (*StarDBStmt, error) {
|
|
|
|
|
stmt := new(StarDBStmt)
|
|
|
|
|
stmtS, err := star.Db.PrepareContext(ctx, sqlStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stmt.Stmt = stmtS
|
|
|
|
|
stmt.Db = star
|
|
|
|
|
return stmt, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) Prepare(sqlStr string) (*StarDBStmt, error) {
|
|
|
|
|
stmt := new(StarDBStmt)
|
|
|
|
|
stmtS, err := star.Tx.Prepare(sqlStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stmt.Stmt = stmtS
|
|
|
|
|
stmt.Db = star.Db
|
|
|
|
|
return stmt, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) PrepareContext(ctx context.Context, sqlStr string) (*StarDBStmt, error) {
|
|
|
|
|
stmt := new(StarDBStmt)
|
|
|
|
|
stmtS, err := star.Tx.PrepareContext(ctx, sqlStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stmt.Db = star.Db
|
|
|
|
|
stmt.Stmt = stmtS
|
|
|
|
|
return stmt, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query 进行Query操作
|
|
|
|
|
func (star *StarDB) query(ctx context.Context, args ...interface{}) (*StarRows, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var rows *sql.Rows
|
|
|
|
|
effect := new(StarRows)
|
|
|
|
|
if err = star.Db.Ping(); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return effect, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
var para []interface{}
|
|
|
|
|
for k, v := range args {
|
|
|
|
|
if k != 0 {
|
|
|
|
@ -985,11 +1132,11 @@ func (star *StarDB) query(ctx context.Context, args ...interface{}) (*StarRows,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if rows, err = stmt.Query(para...); err != nil {
|
|
|
|
|
if rows, err = star.Db.Query(args[0].(string), para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if rows, err = stmt.QueryContext(ctx, para...); err != nil {
|
|
|
|
|
if rows, err = star.Db.QueryContext(ctx, args[0].(string), para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -1046,37 +1193,12 @@ func (star *StarDB) ExecContext(ctx context.Context, args ...interface{}) (sql.R
|
|
|
|
|
// Exec 执行Exec操作
|
|
|
|
|
func (star *StarDB) exec(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var effect sql.Result
|
|
|
|
|
if err = star.Db.Ping(); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return effect, errors.New("no args")
|
|
|
|
|
return nil, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if effect, err = star.Db.Exec(sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if effect, err = star.Db.ExecContext(ctx, sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return effect, nil
|
|
|
|
|
}
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
var stmt *sql.Stmt
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
stmt, err = star.Db.Prepare(sqlStr.(string))
|
|
|
|
|
} else {
|
|
|
|
|
stmt, err = star.Db.PrepareContext(ctx, sqlStr.(string))
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
var para []interface{}
|
|
|
|
|
for k, v := range args {
|
|
|
|
|
if k != 0 {
|
|
|
|
@ -1087,15 +1209,9 @@ func (star *StarDB) exec(ctx context.Context, args ...interface{}) (sql.Result,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if effect, err = stmt.Exec(para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if effect, err = stmt.ExecContext(ctx, para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
return star.Db.Exec(args[0].(string), para...)
|
|
|
|
|
}
|
|
|
|
|
return effect, nil
|
|
|
|
|
return star.Db.ExecContext(ctx, args[0].(string), para...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) Exec(args ...interface{}) (sql.Result, error) {
|
|
|
|
@ -1107,34 +1223,12 @@ func (star *StarTx) ExecContext(ctx context.Context, args ...interface{}) (sql.R
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) exec(ctx context.Context, args ...interface{}) (sql.Result, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var effect sql.Result
|
|
|
|
|
var stmt *sql.Stmt
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return effect, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if _, err = star.Tx.Exec(sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if _, err = star.Tx.ExecContext(ctx, sqlStr.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return effect, nil
|
|
|
|
|
}
|
|
|
|
|
sqlStr := args[0]
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
stmt, err = star.Tx.Prepare(sqlStr.(string))
|
|
|
|
|
} else {
|
|
|
|
|
stmt, err = star.Tx.PrepareContext(ctx, sqlStr.(string))
|
|
|
|
|
if err = star.Db.Ping(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return nil, errors.New("no args")
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
var para []interface{}
|
|
|
|
|
for k, v := range args {
|
|
|
|
|
if k != 0 {
|
|
|
|
@ -1145,15 +1239,9 @@ func (star *StarTx) exec(ctx context.Context, args ...interface{}) (sql.Result,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
if effect, err = stmt.Exec(para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if effect, err = stmt.ExecContext(ctx, para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
return star.Tx.Exec(args[0].(string), para...)
|
|
|
|
|
}
|
|
|
|
|
return effect, nil
|
|
|
|
|
return star.Tx.ExecContext(ctx, args[0].(string), para...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) Commit() error {
|
|
|
|
|