2017-09-13 10:48:22 +05:30
|
|
|
// Copyright 2017 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
2020-03-22 20:42:55 +05:30
|
|
|
"xorm.io/xorm/core"
|
2017-09-13 10:48:22 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Query runs a raw sql and return records as []map[string][]byte
|
2019-06-23 20:52:43 +05:30
|
|
|
func (session *Session) Query(sqlOrArgs ...interface{}) ([]map[string][]byte, error) {
|
2017-09-13 10:48:22 +05:30
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
2018-01-27 20:50:59 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
return session.queryBytes(sqlStr, args...)
|
|
|
|
}
|
|
|
|
|
2021-07-04 18:40:46 +05:30
|
|
|
func (session *Session) rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
|
|
|
fields, err := rows.Columns()
|
|
|
|
if err != nil {
|
2018-12-12 06:31:41 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-04 18:40:46 +05:30
|
|
|
types, err := rows.ColumnTypes()
|
2017-09-13 10:48:22 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-04 18:40:46 +05:30
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
for rows.Next() {
|
2021-07-04 18:40:46 +05:30
|
|
|
result, err := session.engine.row2mapStr(rows, types, fields)
|
2017-09-13 10:48:22 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultsSlice = append(resultsSlice, result)
|
|
|
|
}
|
2021-08-13 04:41:42 +05:30
|
|
|
if rows.Err() != nil {
|
|
|
|
return nil, rows.Err()
|
|
|
|
}
|
2017-09-13 10:48:22 +05:30
|
|
|
|
|
|
|
return resultsSlice, nil
|
|
|
|
}
|
|
|
|
|
2021-07-04 18:40:46 +05:30
|
|
|
func (session *Session) rows2SliceString(rows *core.Rows) (resultsSlice [][]string, err error) {
|
2018-12-12 06:31:41 +05:30
|
|
|
fields, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-04 18:40:46 +05:30
|
|
|
types, err := rows.ColumnTypes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-12 06:31:41 +05:30
|
|
|
for rows.Next() {
|
2021-07-04 18:40:46 +05:30
|
|
|
record, err := session.engine.row2sliceStr(rows, types, fields)
|
2018-12-12 06:31:41 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultsSlice = append(resultsSlice, record)
|
|
|
|
}
|
2021-08-13 04:41:42 +05:30
|
|
|
if rows.Err() != nil {
|
|
|
|
return nil, rows.Err()
|
|
|
|
}
|
2018-12-12 06:31:41 +05:30
|
|
|
|
|
|
|
return resultsSlice, nil
|
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
// QueryString runs a raw sql and return records as []map[string]string
|
2019-06-23 20:52:43 +05:30
|
|
|
func (session *Session) QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error) {
|
2017-09-13 10:48:22 +05:30
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
2018-01-27 20:50:59 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2021-07-04 18:40:46 +05:30
|
|
|
return session.rows2Strings(rows)
|
2017-09-13 10:48:22 +05:30
|
|
|
}
|
|
|
|
|
2018-12-12 06:31:41 +05:30
|
|
|
// QuerySliceString runs a raw sql and return records as [][]string
|
2019-06-23 20:52:43 +05:30
|
|
|
func (session *Session) QuerySliceString(sqlOrArgs ...interface{}) ([][]string, error) {
|
2018-12-12 06:31:41 +05:30
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
2018-12-12 06:31:41 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2021-07-04 18:40:46 +05:30
|
|
|
return session.rows2SliceString(rows)
|
2018-12-12 06:31:41 +05:30
|
|
|
}
|
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
func (session *Session) rows2Interfaces(rows *core.Rows) (resultsSlice []map[string]interface{}, err error) {
|
|
|
|
fields, err := rows.Columns()
|
|
|
|
if err != nil {
|
2017-09-13 10:48:22 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-13 04:41:42 +05:30
|
|
|
types, err := rows.ColumnTypes()
|
2017-09-13 10:48:22 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
2021-08-13 04:41:42 +05:30
|
|
|
result, err := session.engine.row2mapInterface(rows, types, fields)
|
2017-09-13 10:48:22 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultsSlice = append(resultsSlice, result)
|
|
|
|
}
|
2021-08-13 04:41:42 +05:30
|
|
|
if rows.Err() != nil {
|
|
|
|
return nil, rows.Err()
|
|
|
|
}
|
2017-09-13 10:48:22 +05:30
|
|
|
|
|
|
|
return resultsSlice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryInterface runs a raw sql and return records as []map[string]interface{}
|
2019-06-23 20:52:43 +05:30
|
|
|
func (session *Session) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error) {
|
2017-09-13 10:48:22 +05:30
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
sqlStr, args, err := session.statement.GenQuerySQL(sqlOrArgs...)
|
2018-01-27 20:50:59 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
return session.rows2Interfaces(rows)
|
2017-09-13 10:48:22 +05:30
|
|
|
}
|