You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
401 lines
9.6 KiB
Go
401 lines
9.6 KiB
Go
package stardb
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func (star *StarRows) Orm(ins interface{}) error {
|
|
//check if is slice
|
|
if !star.parsed {
|
|
if err := star.parserows(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
t := reflect.TypeOf(ins)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() != reflect.Ptr {
|
|
return errors.New("interface not writable")
|
|
}
|
|
//now convert to slice
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
//get type of slice
|
|
sigType := t.Elem()
|
|
var result reflect.Value
|
|
result = reflect.New(t).Elem()
|
|
if star.Length == 0 {
|
|
v.Set(result)
|
|
return nil
|
|
}
|
|
for i := 0; i < star.Length; i++ {
|
|
val := reflect.New(sigType)
|
|
star.setAllRefValue(val.Interface(), "db", i)
|
|
result = reflect.Append(result, val.Elem())
|
|
}
|
|
v.Set(result)
|
|
return nil
|
|
}
|
|
if star.Length == 0 {
|
|
return nil
|
|
}
|
|
return star.setAllRefValue(ins, "db", 0)
|
|
}
|
|
|
|
func (star *StarDB) queryX(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range args {
|
|
if k == 0 {
|
|
continue
|
|
}
|
|
switch v.(type) {
|
|
case string:
|
|
str := v.(string)
|
|
if strings.Index(str, ":") == 0 {
|
|
if _, ok := kvMap[str[1:]]; ok {
|
|
args[k] = kvMap[str[1:]]
|
|
} else {
|
|
args[k] = ""
|
|
}
|
|
continue
|
|
}
|
|
if strings.Index(str, `\:`) == 0 {
|
|
args[k] = kvMap[str[1:]]
|
|
}
|
|
}
|
|
}
|
|
return star.query(ctx, args...)
|
|
}
|
|
func (star *StarDB) QueryX(ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
return star.queryX(nil, ins, args)
|
|
}
|
|
func (star *StarDB) QueryXS(ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
var starRes []*StarRows
|
|
t := reflect.TypeOf(ins)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.queryX(nil, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.queryX(nil, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
func (star *StarDB) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
var starRes []sql.Result
|
|
t := reflect.TypeOf(ins)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.execX(nil, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.execX(nil, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
func (star *StarDB) ExecX(ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
return star.execX(nil, ins, args...)
|
|
}
|
|
func (star *StarDB) execX(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range args {
|
|
if k == 0 {
|
|
continue
|
|
}
|
|
switch v.(type) {
|
|
case string:
|
|
str := v.(string)
|
|
if strings.Index(str, ":") == 0 {
|
|
if _, ok := kvMap[str[1:]]; ok {
|
|
args[k] = kvMap[str[1:]]
|
|
} else {
|
|
args[k] = ""
|
|
}
|
|
continue
|
|
}
|
|
if strings.Index(str, `\:`) == 0 {
|
|
args[k] = kvMap[str[1:]]
|
|
}
|
|
}
|
|
}
|
|
return star.exec(ctx, args...)
|
|
}
|
|
|
|
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) {
|
|
var starRes []*StarRows
|
|
t := reflect.TypeOf(ins)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.queryX(ctx, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.queryX(ctx, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
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)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.execX(ctx, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.execX(ctx, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
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 {
|
|
return nil, err
|
|
}
|
|
for k, v := range args {
|
|
if k == 0 {
|
|
continue
|
|
}
|
|
switch v.(type) {
|
|
case string:
|
|
str := v.(string)
|
|
if strings.Index(str, ":") == 0 {
|
|
if _, ok := kvMap[str[1:]]; ok {
|
|
args[k] = kvMap[str[1:]]
|
|
} else {
|
|
args[k] = ""
|
|
}
|
|
continue
|
|
}
|
|
if strings.Index(str, `\:`) == 0 {
|
|
args[k] = kvMap[str[1:]]
|
|
}
|
|
}
|
|
}
|
|
return star.query(ctx, args...)
|
|
}
|
|
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)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.queryX(nil, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.queryX(nil, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
func (star *StarTx) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
var starRes []sql.Result
|
|
t := reflect.TypeOf(ins)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.execX(nil, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.execX(nil, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
func (star *StarTx) ExecX(ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
return star.execX(nil, ins, args...)
|
|
}
|
|
func (star *StarTx) execX(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range args {
|
|
if k == 0 {
|
|
continue
|
|
}
|
|
switch v.(type) {
|
|
case string:
|
|
str := v.(string)
|
|
if strings.Index(str, ":") == 0 {
|
|
if _, ok := kvMap[str[1:]]; ok {
|
|
args[k] = kvMap[str[1:]]
|
|
} else {
|
|
args[k] = ""
|
|
}
|
|
continue
|
|
}
|
|
if strings.Index(str, `\:`) == 0 {
|
|
args[k] = kvMap[str[1:]]
|
|
}
|
|
}
|
|
}
|
|
return star.exec(ctx, args...)
|
|
}
|
|
|
|
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) {
|
|
var starRes []*StarRows
|
|
t := reflect.TypeOf(ins)
|
|
v := reflect.ValueOf(ins)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.queryX(ctx, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.queryX(ctx, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
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)
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
v = v.Elem()
|
|
}
|
|
//now convert to slice
|
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
|
for i := 0; i < v.Len(); i++ {
|
|
result, err := star.execX(ctx, v.Index(i).Interface(), args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
} else {
|
|
result, err := star.execX(ctx, ins, args...)
|
|
if err != nil {
|
|
return starRes, err
|
|
}
|
|
starRes = append(starRes, result)
|
|
}
|
|
return starRes, nil
|
|
}
|
|
|
|
func (star *StarTx) ExecXContext(ctx context.Context,ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
return star.execX(ctx, ins, args...)
|
|
} |