Skip to content

Commit 457a6c0

Browse files
committed
feat: support to get sql explain data info
1 parent d42704a commit 457a6c0

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.4
55
toolchain go1.22.6
66

77
require (
8-
github.com/linuxsuren/api-testing v0.0.19-0.20250312132236-e3e7357d6687
8+
github.com/linuxsuren/api-testing v0.0.20-0.20250319020913-f5f9383e2948
99
github.com/spf13/cobra v1.8.1
1010
github.com/stretchr/testify v1.9.0
1111
github.com/taosdata/driver-go/v3 v3.6.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ github.com/linuxsuren/api-testing v0.0.19-0.20250310093028-dd08c128ca0e h1:3dVXB
138138
github.com/linuxsuren/api-testing v0.0.19-0.20250310093028-dd08c128ca0e/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
139139
github.com/linuxsuren/api-testing v0.0.19-0.20250312132236-e3e7357d6687 h1:AFF1vWQbxOCqEENxK5F2va3d4Xmu5wK9tl2zZfUgHdY=
140140
github.com/linuxsuren/api-testing v0.0.19-0.20250312132236-e3e7357d6687/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
141+
github.com/linuxsuren/api-testing v0.0.20-0.20250319020913-f5f9383e2948 h1:YOyqT+9GQsAhRJ0nfqkFMtFJufAS3X/nKmxs25iPUss=
142+
github.com/linuxsuren/api-testing v0.0.20-0.20250319020913-f5f9383e2948/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
141143
github.com/linuxsuren/go-fake-runtime v0.0.4 h1:y+tvBuw6MKTCav8Bo5HWwaXhBx1Z//VAvqI3gpOWqvw=
142144
github.com/linuxsuren/go-fake-runtime v0.0.4/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
143145
github.com/linuxsuren/go-service v0.0.0-20231225060426-efabcd3a5161 h1:dSL/ah6zaRGqH3FW0ogtMjP6xCFXX5NsgWJTaNIofI4=

pkg/data_query.go

+19
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result *
6464
return
6565
}
6666

67+
result.Meta.Labels = dbQuery.GetLabels(ctx, query.Sql)
68+
6769
var dataResult *server.DataQueryResult
6870
now := time.Now()
6971
if dataResult, err = sqlQuery(ctx, query.Sql, db); err == nil {
@@ -166,6 +168,7 @@ type DataQuery interface {
166168
GetDatabases(context.Context) (databases []string, err error)
167169
GetTables(ctx context.Context, currentDatabase string) (tables []string, err error)
168170
GetCurrentDatabase() (string, error)
171+
GetLabels(context.Context, string) map[string]string
169172
GetClient() *gorm.DB
170173
}
171174

@@ -236,6 +239,7 @@ func (q *commonDataQuery) GetTables(ctx context.Context, currentDatabase string)
236239
}
237240
return
238241
}
242+
239243
func (q *commonDataQuery) GetCurrentDatabase() (current string, err error) {
240244
var row *sql.Row
241245
if row = q.db.Raw(q.currentDatabase).Row(); row != nil {
@@ -244,6 +248,21 @@ func (q *commonDataQuery) GetCurrentDatabase() (current string, err error) {
244248
return
245249
}
246250

251+
func (q *commonDataQuery) GetLabels(ctx context.Context, sql string) (metadata map[string]string) {
252+
if databaseResult, err := sqlQuery(ctx, fmt.Sprintf("explain %s", sql), q.db); err == nil {
253+
if len(databaseResult.Items) != 1 {
254+
metadata = make(map[string]string)
255+
for _, data := range databaseResult.Items[0].Data {
256+
switch data.Key {
257+
case "type":
258+
metadata["sql_type"] = data.Value
259+
}
260+
}
261+
}
262+
}
263+
return
264+
}
265+
247266
func (q *commonDataQuery) GetClient() *gorm.DB {
248267
return q.db
249268
}

0 commit comments

Comments
 (0)