@@ -3,10 +3,11 @@ import _ from 'lodash';
33import { reaction } from 'mobx' ;
44import { addDisposer , flow , Instance , toGenerator , types } from 'mobx-state-tree' ;
55import { QueryFailureError } from '~/api-caller' ;
6- import { APIClient } from '~/api-caller/request' ;
6+ import { APIClient , TQueryStructureRequest } from '~/api-caller/request' ;
77import { DataSourceMetaModel } from '~/model/meta-model/datasources' ;
8- import { ColumnsModel } from './columns' ;
9- import { IndexesModel } from './indexes' ;
8+ import { AnyObject } from '~/types' ;
9+ import { ColumnInfoType , ColumnsModel } from './columns' ;
10+ import { IndexesModel , IndexInfoType } from './indexes' ;
1011import { TableDataModel } from './table-data' ;
1112import { TableInfoType , TablesModel } from './tables' ;
1213
@@ -23,6 +24,15 @@ export const DataSourceModel = types
2324 table_name : types . optional ( types . string , '' ) ,
2425 } ) ,
2526 )
27+ . views ( ( self ) => ( {
28+ get sqlDataSourceType ( ) {
29+ return self . type as TQueryStructureRequest [ 'type' ] ;
30+ } ,
31+ get reloadConditionString ( ) {
32+ const { type, table_name, table_schema } = self ;
33+ return `${ type } ;${ table_name } ;${ table_schema } ` ;
34+ } ,
35+ } ) )
2636 . volatile ( ( ) => ( {
2737 controllers : {
2838 tables : new AbortController ( ) ,
@@ -53,10 +63,16 @@ export const DataSourceModel = types
5363 self . controllers . tables = new AbortController ( ) ;
5464 self . tables . state = 'loading' ;
5565 try {
56- const tables : TableInfoType [ ] = yield * toGenerator (
57- APIClient . query ( self . controllers . tables . signal ) (
58- { type : self . type , key : self . key , query : self . tables . sql } ,
59- { } ,
66+ const tables = yield * toGenerator (
67+ APIClient . structure < TableInfoType [ ] > ( self . controllers . tables . signal ) (
68+ {
69+ query_type : 'TABLES' ,
70+ type : self . sqlDataSourceType ,
71+ key : self . key ,
72+ table_schema : '' ,
73+ table_name : '' ,
74+ } ,
75+ { params : { query_type : 'TABLES' } } ,
6076 ) ,
6177 ) ;
6278 self . tables . data = _ . groupBy ( tables , 'table_schema' ) ;
@@ -87,9 +103,15 @@ export const DataSourceModel = types
87103 self . columns . state = 'loading' ;
88104 try {
89105 self . columns . data = yield * toGenerator (
90- APIClient . query ( self . controllers . columns . signal ) (
91- { type : self . type , key : self . key , query : self . columns . sql } ,
92- { } ,
106+ APIClient . structure < ColumnInfoType [ ] > ( self . controllers . columns . signal ) (
107+ {
108+ query_type : 'COLUMNS' ,
109+ type : self . sqlDataSourceType ,
110+ key : self . key ,
111+ table_schema : self . table_schema ,
112+ table_name : self . table_name ,
113+ } ,
114+ { params : { query_type : 'COLUMNS' } } ,
93115 ) ,
94116 ) ;
95117 self . columns . state = 'idle' ;
@@ -112,9 +134,15 @@ export const DataSourceModel = types
112134 self . indexes . state = 'loading' ;
113135 try {
114136 self . indexes . data = yield * toGenerator (
115- APIClient . query ( self . controllers . indexes . signal ) (
116- { type : self . type , key : self . key , query : self . indexes . sql } ,
117- { } ,
137+ APIClient . structure < IndexInfoType [ ] > ( self . controllers . indexes . signal ) (
138+ {
139+ query_type : 'INDEXES' ,
140+ type : self . sqlDataSourceType ,
141+ key : self . key ,
142+ table_schema : self . table_schema ,
143+ table_name : self . table_name ,
144+ } ,
145+ { params : { query_type : 'INDEXES' } } ,
118146 ) ,
119147 ) ;
120148 self . indexes . state = 'idle' ;
@@ -138,12 +166,29 @@ export const DataSourceModel = types
138166 m . state = 'loading' ;
139167 try {
140168 m . data = yield * toGenerator (
141- APIClient . query ( self . controllers . tableData . signal ) ( { type : self . type , key : self . key , query : m . sql } , { } ) ,
169+ APIClient . structure < AnyObject [ ] > ( self . controllers . tableData . signal ) (
170+ {
171+ query_type : 'DATA' ,
172+ type : self . sqlDataSourceType ,
173+ key : self . key ,
174+ table_schema : self . table_schema ,
175+ table_name : self . table_name ,
176+ limit : m . limit ,
177+ offset : m . offset ,
178+ } ,
179+ { params : { query_type : 'DATA' } } ,
180+ ) ,
142181 ) ;
143182 const [ { total } ] = yield * toGenerator (
144- APIClient . query ( self . controllers . tableData . signal ) (
145- { type : self . type , key : self . key , query : m . countSql } ,
146- { } ,
183+ APIClient . structure ( self . controllers . tableData . signal ) (
184+ {
185+ query_type : 'COUNT' ,
186+ type : self . sqlDataSourceType ,
187+ key : self . key ,
188+ table_schema : self . table_schema ,
189+ table_name : self . table_name ,
190+ } ,
191+ { params : { query_type : 'COUNT' } } ,
147192 ) ,
148193 ) ;
149194 m . total = Number ( total ) ;
@@ -167,7 +212,7 @@ export const DataSourceModel = types
167212 afterCreate ( ) {
168213 addDisposer (
169214 self ,
170- reaction ( ( ) => self . columns . sql , self . loadColumns , {
215+ reaction ( ( ) => self . reloadConditionString , self . loadColumns , {
171216 fireImmediately : false ,
172217 delay : 500 ,
173218 } ) ,
@@ -178,17 +223,21 @@ export const DataSourceModel = types
178223 afterCreate ( ) {
179224 addDisposer (
180225 self ,
181- reaction ( ( ) => self . indexes . sql , self . loadIndexes , {
226+ reaction ( ( ) => self . reloadConditionString , self . loadIndexes , {
182227 fireImmediately : false ,
183228 delay : 500 ,
184229 } ) ,
185230 ) ;
186231 addDisposer (
187232 self ,
188- reaction ( ( ) => self . tableData . sql , self . loadTableData , {
189- fireImmediately : false ,
190- delay : 0 ,
191- } ) ,
233+ reaction (
234+ ( ) => `${ self . reloadConditionString } ;limit:${ self . tableData . limit } ;offset:${ self . tableData . offset } ` ,
235+ self . loadTableData ,
236+ {
237+ fireImmediately : false ,
238+ delay : 0 ,
239+ } ,
240+ ) ,
192241 ) ;
193242 } ,
194243 } ) ) ;
0 commit comments