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.
94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package stardb
|
|
|
|
import (
|
|
"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 star.Length == 0 {
|
|
return nil
|
|
}
|
|
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()
|
|
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
|
|
}
|
|
|
|
return star.setAllRefValue(ins, "db", 0)
|
|
}
|
|
|
|
func (star *StarDB) QueryX(sql string, ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range args {
|
|
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(sql, args)
|
|
}
|
|
|
|
func (star *StarDB) ExecX(sql string, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range args {
|
|
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(sql, args)
|
|
}
|