如何按序提取出func (session *Session) QueryInterface(sqlOrArgs ...interface{})返回的字段名列表? #2009

Closed
opened 2021-07-24 15:36:57 +00:00 by ankisme · 1 comment
Contributor

我目前的需求是把任意的mysql查询结果导出为csv文件(带字段名),期望达到的是,当用户在浏览器输入框输入任意sql语句时,将sql结果导出为csv供用户下载,例子如下

// 例子1:当输入select * from user时,导出的csv文件内容
Id,Name,Salt,Age,Passwd,Created,Updated
1,1,1,1,1,1,1
1,1,1,1,1,1,1
1,1,1,1,1,1,1
1,1,1,1,1,1,1
// 例子2:当输入select count(1),count(2),count(3),count(*) from user时,导出的csv文件内容
count(1),count(2),count(3),count(*)
4,4,4,4

但现在用xorm好像不太好处理,因为engine.QueryInterface返回的结果格式是[]map[string]interface{},而map是乱序的,这也导致了我无法按序提取出字段名

我看了一下xorm库提供的接口,目前尚未找到较方便的做法来实现按序提取出字段名,请问可以提供支持吗?或者我怎么才能做到这点?

简要的来说,下方的[1].写法无法满足需求,期望的是[2].写法,怎么才能实现?

[1]. 现有的查询方式如下:

// 现有的QueryInterface返回的是[]map[string]interface{},而map是乱序的
func (session *Session) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error) {
    ......
}

results, err := engine.QueryInterface("select * from user")

[2]. 期望的查询方式如下:

// 返回结果
type Result struct {
    Fields []Field
    Rows   []Row
}

// 数据行
type Row struct {
    Cells []interface{}
}

// 字段
type Field struct {
    FieldName string
    FieldType mysql.fieldType	// 这里还期望能返回字段类型
}

// 期望的查询方式
func (session *Session) QueryInterfaceReturnSlice(sqlOrArgs ...interface{}) (*Result, error) {
	......
}

results, err := engine.QueryInterfaceReturnSlice("select * from user")
我目前的需求是**把任意的mysql查询结果导出为csv文件(带字段名)**,期望达到的是,当用户在浏览器输入框输入任意sql语句时,将sql结果导出为csv供用户下载,例子如下 ```csv // 例子1:当输入select * from user时,导出的csv文件内容 Id,Name,Salt,Age,Passwd,Created,Updated 1,1,1,1,1,1,1 1,1,1,1,1,1,1 1,1,1,1,1,1,1 1,1,1,1,1,1,1 ``` ```csv // 例子2:当输入select count(1),count(2),count(3),count(*) from user时,导出的csv文件内容 count(1),count(2),count(3),count(*) 4,4,4,4 ``` 但现在用xorm好像不太好处理,因为engine.QueryInterface返回的结果格式是`[]map[string]interface{}`,而map是乱序的,这也导致了我无法按序提取出字段名 我看了一下xorm库提供的接口,目前尚未找到较方便的做法来实现按序提取出字段名,请问可以提供支持吗?或者我怎么才能做到这点? 简要的来说,下方的[1].写法无法满足需求,期望的是[2].写法,怎么才能实现? [1]. 现有的查询方式如下: ```go // 现有的QueryInterface返回的是[]map[string]interface{},而map是乱序的 func (session *Session) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error) { ...... } results, err := engine.QueryInterface("select * from user") ``` [2]. 期望的查询方式如下: ```go // 返回结果 type Result struct { Fields []Field Rows []Row } // 数据行 type Row struct { Cells []interface{} } // 字段 type Field struct { FieldName string FieldType mysql.fieldType // 这里还期望能返回字段类型 } // 期望的查询方式 func (session *Session) QueryInterfaceReturnSlice(sqlOrArgs ...interface{}) (*Result, error) { ...... } results, err := engine.QueryInterfaceReturnSlice("select * from user") ```
lunny added the
kind
proposal
label 2021-07-24 17:07:08 +00:00
lunny added this to the 2.0.0 milestone 2021-08-04 08:54:58 +00:00
Contributor
row := Engine.DB().QueryRow(newsql)
cols, err = row.Columns()

这种模式可以直接返回列
row := Engine.DB().QueryRow(newsql) cols, err = row.Columns() 这种模式可以直接返回列
lunny added the
proposal:accepted
label 2021-09-17 10:52:58 +00:00
lunny removed this from the 2.0.0 milestone 2022-05-30 10:28:54 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2009
No description provided.