Compare commits

..

No commits in common. "master" and "v1.1.0" have entirely different histories.

5 changed files with 187 additions and 452 deletions

3
go.mod
View File

@ -1,3 +0,0 @@
module b612.me/stardb
go 1.16

132
orm_v1.go
View File

@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
)
@ -134,49 +133,6 @@ func (star *StarDB) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result,
func (star *StarDB) ExecX(ins interface{}, args ...interface{}) (sql.Result, error) {
return star.execX(nil, ins, args...)
}
func getUpdateSentence(ins interface{}, sheetName string, primaryKey ...string) (string, []string, error) {
Keys, err := getAllRefKey(ins, "db")
if err != nil {
return "", []string{}, err
}
var mystr string
for k, v := range Keys {
mystr += fmt.Sprintf("%s=? ", v)
Keys[k] = ":" + v
}
mystr = fmt.Sprintf("update %s set %s where ", sheetName, mystr)
var whereSlice []string
for _, v := range primaryKey {
whereSlice = append(whereSlice, v+"=?")
Keys = append(Keys, ":"+v)
}
mystr += strings.Join(whereSlice, " and ")
return mystr, Keys, nil
}
func getInsertSentence(ins interface{}, sheetName string, autoIncrease ...string) (string, []string, error) {
Keys, err := getAllRefKey(ins, "db")
if err != nil {
return "", []string{}, err
}
var mystr, rps string
var rtnKeys []string
cns:
for _, v := range Keys {
for _, vs := range autoIncrease {
if v == vs {
rps += "null,"
continue cns
}
}
rtnKeys = append(rtnKeys, ":"+v)
rps += "?,"
}
mystr = fmt.Sprintf("insert into %s (%s) values (%s) ", sheetName, strings.Join(Keys, ","), rps[:len(rps)-1])
return mystr, rtnKeys, nil
}
func (star *StarDB) execX(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
kvMap, err := getAllRefValue(ins, "db")
if err != nil {
@ -205,44 +161,10 @@ func (star *StarDB) execX(ctx context.Context, ins interface{}, args ...interfac
return star.exec(ctx, args...)
}
func (star *StarDB) Update(ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
return star.updateinsert(nil, true, ins, sheetName, primaryKey...)
}
func (star *StarDB) UpdateContext(ctx context.Context, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
return star.updateinsert(ctx, true, ins, sheetName, primaryKey...)
}
func (star *StarDB) Insert(ins interface{}, sheetName string, autoCreaseKey ...string) (sql.Result, error) {
return star.updateinsert(nil, false, ins, sheetName, autoCreaseKey...)
}
func (star *StarDB) InsertContext(ctx context.Context, ins interface{}, sheetName string, autoCreaseKey ...string) (sql.Result, error) {
return star.updateinsert(ctx, false, ins, sheetName, autoCreaseKey...)
}
func (star *StarDB) updateinsert(ctx context.Context, isUpdate bool, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
var sqlStr string
var para []string
var err error
if isUpdate {
sqlStr, para, err = getUpdateSentence(ins, sheetName, primaryKey...)
} else {
sqlStr, para, err = getInsertSentence(ins, sheetName, primaryKey...)
}
if err != nil {
return nil, err
}
tmpStr := append([]interface{}{}, sqlStr)
for _, v := range para {
tmpStr = append(tmpStr, v)
}
return star.execX(ctx, ins, tmpStr...)
}
func (star *StarDB) QueryXContext(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
func (star *StarDB) QueryXContext(ctx context.Context,ins interface{}, args ...interface{}) (*StarRows, error) {
return star.queryX(ctx, ins, args)
}
func (star *StarDB) QueryXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]*StarRows, error) {
func (star *StarDB) QueryXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]*StarRows, error) {
var starRes []*StarRows
t := reflect.TypeOf(ins)
v := reflect.ValueOf(ins)
@ -269,7 +191,7 @@ func (star *StarDB) QueryXSContext(ctx context.Context, ins interface{}, args ..
return starRes, nil
}
func (star *StarDB) ExecXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]sql.Result, error) {
func (star *StarDB) ExecXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]sql.Result, error) {
var starRes []sql.Result
t := reflect.TypeOf(ins)
v := reflect.ValueOf(ins)
@ -296,10 +218,12 @@ func (star *StarDB) ExecXSContext(ctx context.Context, ins interface{}, args ...
return starRes, nil
}
func (star *StarDB) ExecXContext(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
func (star *StarDB) ExecXContext(ctx context.Context,ins interface{}, args ...interface{}) (sql.Result, error) {
return star.execX(ctx, ins, args...)
}
func (star *StarTx) queryX(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
kvMap, err := getAllRefValue(ins, "db")
if err != nil {
@ -330,7 +254,6 @@ func (star *StarTx) queryX(ctx context.Context, ins interface{}, args ...interfa
func (star *StarTx) QueryX(ins interface{}, args ...interface{}) (*StarRows, error) {
return star.queryX(nil, ins, args)
}
func (star *StarTx) QueryXS(ins interface{}, args ...interface{}) ([]*StarRows, error) {
var starRes []*StarRows
t := reflect.TypeOf(ins)
@ -357,40 +280,7 @@ func (star *StarTx) QueryXS(ins interface{}, args ...interface{}) ([]*StarRows,
}
return starRes, nil
}
func (star *StarTx) Update(ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
return star.updateinsert(nil, true, ins, sheetName, primaryKey...)
}
func (star *StarTx) UpdateContext(ctx context.Context, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
return star.updateinsert(ctx, true, ins, sheetName, primaryKey...)
}
func (star *StarTx) Insert(ins interface{}, sheetName string, autoCreaseKey ...string) (sql.Result, error) {
return star.updateinsert(nil, false, ins, sheetName, autoCreaseKey...)
}
func (star *StarTx) InsertContext(ctx context.Context, ins interface{}, sheetName string, autoCreaseKey ...string) (sql.Result, error) {
return star.updateinsert(ctx, false, ins, sheetName, autoCreaseKey...)
}
func (star *StarTx) updateinsert(ctx context.Context, isUpdate bool, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
var sqlStr string
var para []string
var err error
if isUpdate {
sqlStr, para, err = getUpdateSentence(ins, sheetName, primaryKey...)
} else {
sqlStr, para, err = getInsertSentence(ins, sheetName, primaryKey...)
}
if err != nil {
return nil, err
}
tmpStr := append([]interface{}{}, sqlStr)
for _, v := range para {
tmpStr = append(tmpStr, v)
}
return star.execX(ctx, ins, tmpStr...)
}
func (star *StarTx) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result, error) {
var starRes []sql.Result
t := reflect.TypeOf(ins)
@ -449,10 +339,10 @@ func (star *StarTx) execX(ctx context.Context, ins interface{}, args ...interfac
return star.exec(ctx, args...)
}
func (star *StarTx) QueryXContext(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
func (star *StarTx) QueryXContext(ctx context.Context,ins interface{}, args ...interface{}) (*StarRows, error) {
return star.queryX(ctx, ins, args)
}
func (star *StarTx) QueryXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]*StarRows, error) {
func (star *StarTx) QueryXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]*StarRows, error) {
var starRes []*StarRows
t := reflect.TypeOf(ins)
v := reflect.ValueOf(ins)
@ -479,7 +369,7 @@ func (star *StarTx) QueryXSContext(ctx context.Context, ins interface{}, args ..
return starRes, nil
}
func (star *StarTx) ExecXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]sql.Result, error) {
func (star *StarTx) ExecXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]sql.Result, error) {
var starRes []sql.Result
t := reflect.TypeOf(ins)
v := reflect.ValueOf(ins)
@ -506,6 +396,6 @@ func (star *StarTx) ExecXSContext(ctx context.Context, ins interface{}, args ...
return starRes, nil
}
func (star *StarTx) ExecXContext(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
func (star *StarTx) ExecXContext(ctx context.Context,ins interface{}, args ...interface{}) (sql.Result, error) {
return star.execX(ctx, ins, args...)
}
}

View File

@ -3,7 +3,6 @@ package stardb
import (
"errors"
"reflect"
"time"
)
func (star *StarRows) setAllRefValue(stc interface{}, skey string, rows int) error {
@ -25,31 +24,22 @@ func (star *StarRows) setAllRefValue(stc interface{}, skey string, rows int) err
for i := 0; i < t.NumField(); i++ {
tp := t.Field(i)
srFrd := v.Field(i)
seg := tp.Tag.Get(skey)
if seg == "" && !tp.IsExported() {
if srFrd.Kind() == reflect.Ptr && reflect.TypeOf(srFrd.Interface()).Elem().Kind() == reflect.Struct {
sp := reflect.New(reflect.TypeOf(srFrd.Interface()).Elem()).Interface()
star.setAllRefValue(sp, skey, rows)
v.Field(i).Set(reflect.ValueOf(sp))
continue
}
if srFrd.Kind() == reflect.Ptr && reflect.TypeOf(srFrd.Interface()).Elem().Kind() == reflect.Struct {
if seg == "" {
continue
}
if seg == "---" {
sp := reflect.New(reflect.TypeOf(srFrd.Interface()).Elem()).Interface()
star.setAllRefValue(sp, skey, rows)
v.Field(i).Set(reflect.ValueOf(sp))
continue
}
}
if srFrd.Kind() == reflect.Struct {
if seg == "" {
continue
}
if seg == "---" {
sp := reflect.New(reflect.TypeOf(v.Field(i).Interface())).Interface()
star.setAllRefValue(sp, skey, rows)
v.Field(i).Set(reflect.ValueOf(sp).Elem())
continue
}
sp := reflect.New(reflect.TypeOf(v.Field(i).Interface())).Interface()
star.setAllRefValue(sp, skey, rows)
v.Field(i).Set(reflect.ValueOf(sp).Elem())
continue
}
seg := tp.Tag.Get(skey)
if seg == "" {
continue
}
if _, ok := star.Row(rows).columnref[seg]; !ok {
continue
@ -81,16 +71,6 @@ func (star *StarRows) setAllRefValue(stc interface{}, skey string, rows int) err
v.Field(i).SetFloat(star.Row(rows).MustFloat64(seg))
case reflect.Float32:
v.Field(i).SetFloat(float64(star.Row(rows).MustFloat32(seg)))
case reflect.Slice, reflect.Array:
if t.Field(i).Type.Elem().Kind() == reflect.Uint8 {
v.Field(i).SetBytes(star.Row(rows).MustBytes(seg))
}
case reflect.Interface, reflect.Struct, reflect.Ptr:
inf := star.Row(rows).Result[star.columnref[seg]]
switch vtype := inf.(type) {
case time.Time:
v.Field(i).Set(reflect.ValueOf(vtype))
}
default:
}
@ -123,50 +103,37 @@ func getAllRefValue(stc interface{}, skey string) (map[string]interface{}, error
result := make(map[string]interface{})
t := reflect.TypeOf(stc)
v := reflect.ValueOf(stc)
if t.Kind() == reflect.Ptr {
if v.IsNil() {
return nil, errors.New("ptr interface{} is nil")
}
t = t.Elem()
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return nil, errors.New("interface{} is not a struct")
}
if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
for i := 0; i < t.NumField(); i++ {
tp := t.Field(i)
srFrd := v.Field(i)
seg := tp.Tag.Get(skey)
if seg == "" && !tp.IsExported() {
continue
}
if srFrd.Kind() == reflect.Ptr && reflect.TypeOf(srFrd.Interface()).Elem().Kind() == reflect.Struct {
if srFrd.IsNil() {
continue
res, err := getAllRefValue(reflect.ValueOf(srFrd.Elem().Interface()).Interface(), skey)
if err != nil {
return result, err
}
if seg == "---" {
res, err := getAllRefValue(reflect.ValueOf(srFrd.Elem().Interface()).Interface(), skey)
if err != nil {
return result, err
}
for k, v := range res {
result[k] = v
}
continue
for k, v := range res {
result[k] = v
}
continue
}
if v.Field(i).Kind() == reflect.Struct {
res, err := getAllRefValue(v.Field(i).Interface(), skey)
if seg == "---" {
if err != nil {
return result, err
}
for k, v := range res {
result[k] = v
}
continue
if err != nil {
return result, err
}
for k, v := range res {
result[k] = v
}
continue
}
seg := tp.Tag.Get(skey)
if seg == "" {
continue
}
@ -188,35 +155,22 @@ func getAllRefKey(stc interface{}, skey string) ([]string, error) {
t := reflect.TypeOf(stc)
v := reflect.ValueOf(stc)
if t.Kind() == reflect.Ptr {
if v.IsNil() {
return []string{}, errors.New("ptr interface{} is nil")
}
t = t.Elem()
v = v.Elem()
}
for i := 0; i < t.NumField(); i++ {
srFrd := v.Field(i)
profile := t.Field(i)
seg := profile.Tag.Get(skey)
if seg == "" && !profile.IsExported() {
if srFrd.Kind() == reflect.Ptr && reflect.TypeOf(srFrd.Interface()).Elem().Kind() == reflect.Struct {
res, err := getAllRefKey(reflect.ValueOf(srFrd.Elem().Interface()).Interface(), skey)
if err != nil {
return result, err
}
for _, v := range res {
result = append(result, v)
}
continue
}
if srFrd.Kind() == reflect.Ptr && reflect.TypeOf(srFrd.Interface()).Elem().Kind() == reflect.Struct {
if srFrd.IsNil() {
continue
}
if seg == "---" {
res, err := getAllRefKey(reflect.ValueOf(srFrd.Elem().Interface()).Interface(), skey)
if err != nil {
return result, err
}
for _, v := range res {
result = append(result, v)
}
continue
}
}
if v.Field(i).Kind() == reflect.Struct && seg == "---" {
if v.Field(i).Kind() == reflect.Struct {
res, err := getAllRefKey(v.Field(i).Interface(), skey)
if err != nil {
return result, err
@ -225,6 +179,8 @@ func getAllRefKey(stc interface{}, skey string) ([]string, error) {
result = append(result, v)
}
}
profile := t.Field(i)
seg := profile.Tag.Get(skey)
if seg != "" {
result = append(result, seg)
}

View File

@ -6,42 +6,24 @@ import (
)
type Useless struct {
Leader string `db:"leader"`
Usable bool `db:"use"`
O *Whoami `db:"---"`
Data []byte `db:"data"`
Leader string `db:"leader"`
Usable bool `db:"use"`
O *Whoami
}
type Whoami struct {
Hehe string `db:"hehe"`
}
func TestUpInOrm(t *testing.T) {
var hehe = Useless{
Leader: "no",
Usable: false,
}
sqlstr, param, err := getUpdateSentence(hehe, "ryz", "leader")
fmt.Println(sqlstr, param, err)
sqlstr, param, err = getInsertSentence(hehe, "ryz", "use")
fmt.Println(sqlstr, param, err)
}
func Test_SetRefVal(t *testing.T) {
var hehe = Useless{
Leader: "no",
Data: []byte{1, 2, 3},
}
fmt.Printf("%+v\n", hehe)
fmt.Println(setRefValue(&hehe, "db", "leader", "sb"))
fmt.Printf("%+v\n", hehe)
fmt.Println(getAllRefKey(hehe, "db"))
fmt.Println(getAllRefValue(hehe, "db"))
fmt.Println(setRefValue(&hehe, "db", "data", []byte{4, 5, 6, 7, 8}))
fmt.Printf("%+v\n", hehe)
fmt.Println(getAllRefKey(hehe, "db"))
fmt.Println(getAllRefValue(hehe, "db"))
}
func Test_Ref(t *testing.T) {
@ -49,7 +31,5 @@ func Test_Ref(t *testing.T) {
Leader: "Heheeee",
}
oooooo.O = &Whoami{"fuck"}
fmt.Println(getAllRefKey(oooooo, "db"))
fmt.Println(getAllRefValue(oooooo, "db"))
fmt.Println(getAllRefValue(&oooooo, "db"))
fmt.Println(getAllRefKey(oooooo,"db"))
}

View File

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