@@ -23,6 +23,7 @@ import (
2323 "reflect"
2424 "sort"
2525 "strings"
26+ "sync"
2627 "time"
2728
2829 "github.com/linuxsuren/api-testing/pkg/server"
@@ -44,32 +45,48 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result *
4445 Meta : & server.DataMeta {},
4546 }
4647
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 {}
5149
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 )
5556 }
56- }
57+ }()
5758
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+ }
6167
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 ()
6274 // query data
6375 if query .Sql == "" {
6476 return
6577 }
6678
6779 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+ }()
7390
7491 var dataResult * server.DataQueryResult
7592 now := time .Now ()
@@ -254,10 +271,7 @@ func (q *commonDataQuery) GetCurrentDatabase() (current string, err error) {
254271
255272func (q * commonDataQuery ) GetLabels (ctx context.Context , sql string ) (metadata []* server.Pair ) {
256273 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 {
261275 for _ , data := range databaseResult .Items [0 ].Data {
262276 switch data .Key {
263277 case "type" :
@@ -268,6 +282,18 @@ func (q *commonDataQuery) GetLabels(ctx context.Context, sql string) (metadata [
268282 }
269283 }
270284 }
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+ }
271297 return
272298}
273299
0 commit comments