@@ -23,6 +23,7 @@ import (
23
23
"reflect"
24
24
"sort"
25
25
"strings"
26
+ "sync"
26
27
"time"
27
28
28
29
"github.com/linuxsuren/api-testing/pkg/server"
@@ -44,32 +45,48 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result *
44
45
Meta : & server.DataMeta {},
45
46
}
46
47
47
- // query database and tables
48
- if result .Meta .Databases , err = dbQuery .GetDatabases (ctx ); err != nil {
49
- log .Printf ("failed to query databases: %v\n " , err )
50
- }
48
+ wg := sync.WaitGroup {}
51
49
52
- if result .Meta .CurrentDatabase = query .Key ; query .Key == "" {
53
- if result .Meta .CurrentDatabase , err = dbQuery .GetCurrentDatabase (); err != nil {
54
- log .Printf ("failed to query current database: %v\n " , err )
50
+ wg .Add (1 )
51
+ go func () {
52
+ defer wg .Done ()
53
+ // query database and tables
54
+ if result .Meta .Databases , err = dbQuery .GetDatabases (ctx ); err != nil {
55
+ log .Printf ("failed to query databases: %v\n " , err )
55
56
}
56
- }
57
+ }()
57
58
58
- if result .Meta .Tables , err = dbQuery .GetTables (ctx , result .Meta .CurrentDatabase ); err != nil {
59
- log .Printf ("failed to query tables: %v\n " , err )
60
- }
59
+ wg .Add (1 )
60
+ go func () {
61
+ defer wg .Done ()
62
+ if result .Meta .CurrentDatabase = query .Key ; query .Key == "" {
63
+ if result .Meta .CurrentDatabase , err = dbQuery .GetCurrentDatabase (); err != nil {
64
+ log .Printf ("failed to query current database: %v\n " , err )
65
+ }
66
+ }
61
67
68
+ if result .Meta .Tables , err = dbQuery .GetTables (ctx , result .Meta .CurrentDatabase ); err != nil {
69
+ log .Printf ("failed to query tables: %v\n " , err )
70
+ }
71
+ }()
72
+
73
+ defer wg .Wait ()
62
74
// query data
63
75
if query .Sql == "" {
64
76
return
65
77
}
66
78
67
79
query .Sql = dbQuery .GetInnerSQL ().ToNativeSQL (query .Sql )
68
- result .Meta .Labels = dbQuery .GetLabels (ctx , query .Sql )
69
- result .Meta .Labels = append (result .Meta .Labels , & server.Pair {
70
- Key : "_native_sql" ,
71
- Value : query .Sql ,
72
- })
80
+
81
+ wg .Add (1 )
82
+ go func () {
83
+ wg .Done ()
84
+ result .Meta .Labels = dbQuery .GetLabels (ctx , query .Sql )
85
+ result .Meta .Labels = append (result .Meta .Labels , & server.Pair {
86
+ Key : "_native_sql" ,
87
+ Value : query .Sql ,
88
+ })
89
+ }()
73
90
74
91
var dataResult * server.DataQueryResult
75
92
now := time .Now ()
@@ -254,10 +271,7 @@ func (q *commonDataQuery) GetCurrentDatabase() (current string, err error) {
254
271
255
272
func (q * commonDataQuery ) GetLabels (ctx context.Context , sql string ) (metadata []* server.Pair ) {
256
273
metadata = make ([]* server.Pair , 0 )
257
- if databaseResult , err := sqlQuery (ctx , fmt .Sprintf ("explain %s" , sql ), q .db ); err == nil {
258
- if len (databaseResult .Items ) != 1 {
259
- return
260
- }
274
+ if databaseResult , err := sqlQuery (ctx , fmt .Sprintf ("explain %s" , sql ), q .db ); err == nil && len (databaseResult .Items ) != 1 {
261
275
for _ , data := range databaseResult .Items [0 ].Data {
262
276
switch data .Key {
263
277
case "type" :
@@ -268,6 +282,18 @@ func (q *commonDataQuery) GetLabels(ctx context.Context, sql string) (metadata [
268
282
}
269
283
}
270
284
}
285
+
286
+ if databaseResult , err := sqlQuery (ctx , `show variables like 'version'` , q .db ); err == nil && len (databaseResult .Items ) >= 1 {
287
+ for _ , data := range databaseResult .Items [0 ].Data {
288
+ switch data .Key {
289
+ case "version" :
290
+ metadata = append (metadata , & server.Pair {
291
+ Key : "version" ,
292
+ Value : data .Value ,
293
+ })
294
+ }
295
+ }
296
+ }
271
297
return
272
298
}
273
299
0 commit comments