|
@@ -215,6 +215,34 @@ func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string,
|
|
|
return result, nil
|
|
return result, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func row2sliceStr(rows *core.Rows, fields []string) (results []string, err error) {
|
|
|
|
|
+ result := make([]string, 0, len(fields))
|
|
|
|
|
+ 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 i := 0; i < len(fields); i++ {
|
|
|
|
|
+ rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[i]))
|
|
|
|
|
+ // if row is null then as empty string
|
|
|
|
|
+ if rawValue.Interface() == nil {
|
|
|
|
|
+ result = append(result, "")
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if data, err := value2String(&rawValue); err == nil {
|
|
|
|
|
+ result = append(result, data)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return result, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
|
func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
|
|
fields, err := rows.Columns()
|
|
fields, err := rows.Columns()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -231,6 +259,21 @@ func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error)
|
|
|
return resultsSlice, nil
|
|
return resultsSlice, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func rows2SliceString(rows *core.Rows) (resultsSlice [][]string, err error) {
|
|
|
|
|
+ fields, err := rows.Columns()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ for rows.Next() {
|
|
|
|
|
+ record, err := row2sliceStr(rows, fields)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ resultsSlice = append(resultsSlice, record)
|
|
|
|
|
+ }
|
|
|
|
|
+ return resultsSlice, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func (session *Session) QueryRows(sqlorArgs ...interface{}) (*core.Rows, error) {
|
|
func (session *Session) QueryRows(sqlorArgs ...interface{}) (*core.Rows, error) {
|
|
|
if session.isAutoClose {
|
|
if session.isAutoClose {
|
|
|
defer session.Close()
|
|
defer session.Close()
|
|
@@ -269,6 +312,26 @@ func (session *Session) QueryString(sqlorArgs ...interface{}) ([]map[string]stri
|
|
|
return rows2Strings(rows)
|
|
return rows2Strings(rows)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// QuerySliceString runs a raw sql and return records as [][]string
|
|
|
|
|
+func (session *Session) QuerySliceString(sqlorArgs ...interface{}) ([][]string, error) {
|
|
|
|
|
+ if session.isAutoClose {
|
|
|
|
|
+ defer session.Close()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sqlStr, args, err := session.genQuerySQL(sqlorArgs...)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ rows, err := session.queryRows(sqlStr, args...)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ defer rows.Close()
|
|
|
|
|
+
|
|
|
|
|
+ return rows2SliceString(rows)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func row2mapInterface(rows *core.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
|
|
func row2mapInterface(rows *core.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
|
|
|
resultsMap = make(map[string]interface{}, len(fields))
|
|
resultsMap = make(map[string]interface{}, len(fields))
|
|
|
scanResultContainers := make([]interface{}, len(fields))
|
|
scanResultContainers := make([]interface{}, len(fields))
|