Merge branch 'master' of github.com:go-xorm/xorm
This commit is contained in:
commit
78a84cbefc
156
helpers.go
156
helpers.go
|
@ -163,3 +163,159 @@ func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
|
||||||
|
|
||||||
return resultsSlice, nil
|
return resultsSlice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
|
||||||
|
result := make(map[string][]byte)
|
||||||
|
scanResultContainers := make([]interface{}, len(fields))
|
||||||
|
for i := 0; i < len(fields); i++ {
|
||||||
|
var scanResultContainer interface{}
|
||||||
|
scanResultContainers[i] = &scanResultContainer
|
||||||
|
}
|
||||||
|
if err := rows.Scan(scanResultContainers...); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for ii, key := range fields {
|
||||||
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||||
|
//if row is null then ignore
|
||||||
|
if rawValue.Interface() == nil {
|
||||||
|
//fmt.Println("ignore ...", key, rawValue)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if data, err := value2Bytes(&rawValue); err == nil {
|
||||||
|
result[key] = data
|
||||||
|
} else {
|
||||||
|
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string, err error) {
|
||||||
|
result := make(map[string]string)
|
||||||
|
scanResultContainers := make([]interface{}, len(fields))
|
||||||
|
for i := 0; i < len(fields); i++ {
|
||||||
|
var scanResultContainer interface{}
|
||||||
|
scanResultContainers[i] = &scanResultContainer
|
||||||
|
}
|
||||||
|
if err := rows.Scan(scanResultContainers...); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for ii, key := range fields {
|
||||||
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||||
|
//if row is null then ignore
|
||||||
|
if rawValue.Interface() == nil {
|
||||||
|
//fmt.Println("ignore ...", key, rawValue)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if data, err := value2String(&rawValue); err == nil {
|
||||||
|
result[key] = data
|
||||||
|
} else {
|
||||||
|
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
|
||||||
|
rows, err := tx.Query(sqlStr, params...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return rows2Strings(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func query2(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
|
||||||
|
s, err := db.Prepare(sqlStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer s.Close()
|
||||||
|
rows, err := s.Query(params...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
return rows2Strings(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
|
||||||
|
colNames := make([]string, 0)
|
||||||
|
args := make([]interface{}, 0)
|
||||||
|
|
||||||
|
for _, col := range table.Columns() {
|
||||||
|
lColName := strings.ToLower(col.Name)
|
||||||
|
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
|
||||||
|
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if col.MapType == core.ONLYFROMDB {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
|
if err != nil {
|
||||||
|
session.Engine.LogError(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fieldValue := *fieldValuePtr
|
||||||
|
|
||||||
|
if col.IsAutoIncrement {
|
||||||
|
switch fieldValue.Type().Kind() {
|
||||||
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
|
||||||
|
if fieldValue.Int() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
|
||||||
|
if fieldValue.Uint() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case reflect.String:
|
||||||
|
if len(fieldValue.String()) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if col.IsDeleted {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if session.Statement.ColumnStr != "" {
|
||||||
|
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if session.Statement.OmitStr != "" {
|
||||||
|
if _, ok := session.Statement.columnMap[lColName]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
||||||
|
args = append(args, session.Engine.NowTime(col.SQLType.Name))
|
||||||
|
} else if col.IsVersion && session.Statement.checkVersion {
|
||||||
|
args = append(args, 1)
|
||||||
|
//} else if !col.DefaultIsEmpty {
|
||||||
|
} else {
|
||||||
|
arg, err := session.value2Interface(col, fieldValue)
|
||||||
|
if err != nil {
|
||||||
|
return colNames, args, err
|
||||||
|
}
|
||||||
|
args = append(args, arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if includeQuote {
|
||||||
|
colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
|
||||||
|
} else {
|
||||||
|
colNames = append(colNames, col.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return colNames, args, nil
|
||||||
|
}
|
||||||
|
|
156
session.go
156
session.go
|
@ -1458,62 +1458,6 @@ func (session *Session) dropAll() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string, err error) {
|
|
||||||
result := make(map[string]string)
|
|
||||||
scanResultContainers := make([]interface{}, len(fields))
|
|
||||||
for i := 0; i < len(fields); i++ {
|
|
||||||
var scanResultContainer interface{}
|
|
||||||
scanResultContainers[i] = &scanResultContainer
|
|
||||||
}
|
|
||||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for ii, key := range fields {
|
|
||||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
|
||||||
//if row is null then ignore
|
|
||||||
if rawValue.Interface() == nil {
|
|
||||||
//fmt.Println("ignore ...", key, rawValue)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if data, err := value2String(&rawValue); err == nil {
|
|
||||||
result[key] = data
|
|
||||||
} else {
|
|
||||||
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
|
|
||||||
result := make(map[string][]byte)
|
|
||||||
scanResultContainers := make([]interface{}, len(fields))
|
|
||||||
for i := 0; i < len(fields); i++ {
|
|
||||||
var scanResultContainer interface{}
|
|
||||||
scanResultContainers[i] = &scanResultContainer
|
|
||||||
}
|
|
||||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for ii, key := range fields {
|
|
||||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
|
||||||
//if row is null then ignore
|
|
||||||
if rawValue.Interface() == nil {
|
|
||||||
//fmt.Println("ignore ...", key, rawValue)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if data, err := value2Bytes(&rawValue); err == nil {
|
|
||||||
result[key] = data
|
|
||||||
} else {
|
|
||||||
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) *reflect.Value {
|
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) *reflect.Value {
|
||||||
var col *core.Column
|
var col *core.Column
|
||||||
if col = table.GetColumnIdx(key, idx); col == nil {
|
if col = table.GetColumnIdx(key, idx); col == nil {
|
||||||
|
@ -1946,30 +1890,6 @@ func (session *Session) query2(sqlStr string, paramStr ...interface{}) (resultsS
|
||||||
return txQuery2(session.Tx, sqlStr, paramStr...)
|
return txQuery2(session.Tx, sqlStr, paramStr...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
|
|
||||||
rows, err := tx.Query(sqlStr, params...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
return rows2Strings(rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
func query2(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
|
|
||||||
s, err := db.Prepare(sqlStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer s.Close()
|
|
||||||
rows, err := s.Query(params...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
return rows2Strings(rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
// insert one or more beans
|
// insert one or more beans
|
||||||
func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
||||||
var affected int64 = 0
|
var affected int64 = 0
|
||||||
|
@ -3755,79 +3675,3 @@ func (session *Session) Unscoped() *Session {
|
||||||
session.Statement.Unscoped()
|
session.Statement.Unscoped()
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
|
|
||||||
colNames := make([]string, 0)
|
|
||||||
args := make([]interface{}, 0)
|
|
||||||
|
|
||||||
for _, col := range table.Columns() {
|
|
||||||
lColName := strings.ToLower(col.Name)
|
|
||||||
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
|
|
||||||
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if col.MapType == core.ONLYFROMDB {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldValuePtr, err := col.ValueOf(bean)
|
|
||||||
if err != nil {
|
|
||||||
session.Engine.LogError(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fieldValue := *fieldValuePtr
|
|
||||||
|
|
||||||
if col.IsAutoIncrement {
|
|
||||||
switch fieldValue.Type().Kind() {
|
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
|
|
||||||
if fieldValue.Int() == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
|
|
||||||
if fieldValue.Uint() == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case reflect.String:
|
|
||||||
if len(fieldValue.String()) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if col.IsDeleted {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if session.Statement.ColumnStr != "" {
|
|
||||||
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if session.Statement.OmitStr != "" {
|
|
||||||
if _, ok := session.Statement.columnMap[lColName]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
|
||||||
args = append(args, session.Engine.NowTime(col.SQLType.Name))
|
|
||||||
} else if col.IsVersion && session.Statement.checkVersion {
|
|
||||||
args = append(args, 1)
|
|
||||||
//} else if !col.DefaultIsEmpty {
|
|
||||||
} else {
|
|
||||||
arg, err := session.value2Interface(col, fieldValue)
|
|
||||||
if err != nil {
|
|
||||||
return colNames, args, err
|
|
||||||
}
|
|
||||||
args = append(args, arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if includeQuote {
|
|
||||||
colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
|
|
||||||
} else {
|
|
||||||
colNames = append(colNames, col.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return colNames, args, nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue