1
- import { IBaseQueries , ContextValue } from '@sqltools/types' ;
1
+ import { ContextValue , NSDatabase , QueryBuilder } from '@sqltools/types' ;
2
2
import queryFactory from '@sqltools/base-driver/dist/lib/factory' ;
3
+ import { IQueries } from './irisdb' ;
3
4
4
5
/** write your queries here go fetch desired data. This queries are just examples copied from SQLite driver */
5
6
6
- const describeTable : IBaseQueries [ 'describeTable' ] = queryFactory `
7
+ const describeTable : IQueries [ 'describeTable' ] = queryFactory `
7
8
SELECT C.*
8
9
FROM pragma_table_info('${ p => p . label } ') AS C
9
10
ORDER BY C.cid ASC
10
11
` ;
11
12
12
- const fetchColumns : IBaseQueries [ 'fetchColumns' ] = queryFactory `
13
+ const fetchColumns : IQueries [ 'fetchColumns' ] = queryFactory `
13
14
SELECT
14
15
C.COLUMN_NAME AS label,
15
16
'${ ContextValue . COLUMN } ' as type,
@@ -34,41 +35,40 @@ ORDER BY
34
35
C.ORDINAL_POSITION
35
36
` ;
36
37
37
- const fetchRecords : IBaseQueries [ 'fetchRecords' ] = queryFactory `
38
+ const fetchRecords : IQueries [ 'fetchRecords' ] = queryFactory `
38
39
SELECT TOP ${ p => p . limit || 50 } *
39
40
FROM ${ p => p . table . schema } .${ p => ( p . table . label || p . table ) }
40
41
` ;
41
42
42
- const countRecords : IBaseQueries [ 'countRecords' ] = queryFactory `
43
+ const countRecords : IQueries [ 'countRecords' ] = queryFactory `
43
44
SELECT count(1) AS total
44
45
FROM ${ p => p . table . schema } .${ p => ( p . table . label || p . table ) }
45
46
` ;
46
47
47
- const fetchTablesAndViews = ( type : ContextValue , tableType = 'BASE TABLE' ) : IBaseQueries [ 'fetchTables' ] => queryFactory `
48
- SELECT
49
- T.TABLE_NAME AS label,
50
- '${ type } ' as type,
51
- T.TABLE_SCHEMA AS "schema",
52
- '${ type === ContextValue . VIEW ? 'TRUE' : 'FALSE' } ' AS isView
53
- FROM INFORMATION_SCHEMA.${ type === ContextValue . VIEW ? 'VIEWS' : 'TABLES' } AS T
54
- WHERE
55
- T.TABLE_SCHEMA = '${ p => p . schema } '
56
- AND T.TABLE_TYPE = '${ tableType } '
48
+ const fetchAnyItems = < T > ( type : ContextValue , isView : boolean , name : string , func : string ) : QueryBuilder < NSDatabase . ISchema , T > => queryFactory `
49
+ SELECT
50
+ ${ name } AS label,
51
+ SCHEMA_NAME AS "schema",
52
+ '${ type } ' as "type",
53
+ '${ isView ? 'TRUE' : 'FALSE' } ' as isView
54
+ FROM %SQL_MANAGER.${ func } ()
55
+ WHERE SCHEMA_NAME = '${ p => p . schema } '
57
56
ORDER BY
58
- T.TABLE_NAME
57
+ ${ name }
59
58
` ;
60
59
61
- const fetchTables : IBaseQueries [ 'fetchTables' ] = fetchTablesAndViews ( ContextValue . TABLE ) ;
62
- const fetchViews : IBaseQueries [ 'fetchTables' ] = fetchTablesAndViews ( ContextValue . VIEW , 'view' ) ;
60
+ const fetchTables = fetchAnyItems < NSDatabase . ITable > ( ContextValue . TABLE , false , 'TABLE_NAME' , 'TablesTree' ) ;
61
+ const fetchViews = fetchAnyItems < NSDatabase . ITable > ( ContextValue . TABLE , true , 'VIEW_NAME' , 'ViewsTree' ) ;
62
+ const fetchFunctions = fetchAnyItems < NSDatabase . IProcedure > ( ContextValue . FUNCTION , false , 'PROCEDURE_NAME' , 'ProceduresTree' ) ;
63
63
64
- const searchTables : IBaseQueries [ 'searchTables' ] = queryFactory `
64
+ const searchTables : IQueries [ 'searchTables' ] = queryFactory `
65
65
SELECT name AS label,
66
66
type
67
67
FROM sqlite_master
68
68
${ p => p . search ? `WHERE LOWER(name) LIKE '%${ p . search . toLowerCase ( ) } %'` : '' }
69
69
ORDER BY name
70
70
` ;
71
- const searchColumns : IBaseQueries [ 'searchColumns' ] = queryFactory `
71
+ const searchColumns : IQueries [ 'searchColumns' ] = queryFactory `
72
72
SELECT C.name AS label,
73
73
T.name AS "table",
74
74
C.type AS dataType,
@@ -79,39 +79,47 @@ FROM sqlite_master AS T
79
79
LEFT OUTER JOIN pragma_table_info((T.name)) AS C ON 1 = 1
80
80
WHERE 1 = 1
81
81
${ p => p . tables . filter ( t => ! ! t . label ) . length
82
- ? `AND LOWER(T.name) IN (${ p . tables . filter ( t => ! ! t . label ) . map ( t => `'${ t . label } '` . toLowerCase ( ) ) . join ( ', ' ) } )`
83
- : ''
84
- }
82
+ ? `AND LOWER(T.name) IN (${ p . tables . filter ( t => ! ! t . label ) . map ( t => `'${ t . label } '` . toLowerCase ( ) ) . join ( ', ' ) } )`
83
+ : ''
84
+ }
85
85
${ p => p . search
86
- ? `AND (
86
+ ? `AND (
87
87
LOWER(T.name || '.' || C.name) LIKE '%${ p . search . toLowerCase ( ) } %'
88
88
OR LOWER(C.name) LIKE '%${ p . search . toLowerCase ( ) } %'
89
89
)`
90
- : ''
91
- }
90
+ : ''
91
+ }
92
92
ORDER BY C.name ASC,
93
93
C.cid ASC
94
94
LIMIT ${ p => p . limit || 100 }
95
95
` ;
96
96
97
- const fetchSchemas : IBaseQueries [ 'fetchSchemas' ] = queryFactory `
98
- SELECT
99
- schema_name AS label,
100
- schema_name AS "schema",
97
+ const fetchTypedSchemas = ( type : ContextValue , func : string ) : IQueries [ 'fetchSchemas' ] => queryFactory `
98
+ SELECT
99
+ DISTINCT BY (SCHEMA_NAME)
100
+ %EXACT(SCHEMA_NAME) AS label,
101
+ %EXACT(SCHEMA_NAME) AS "schema",
101
102
'${ ContextValue . SCHEMA } ' as "type",
103
+ '${ type } ' as "childType",
102
104
'folder' as iconId
103
- FROM information_schema.schemata
104
- WHERE schema_name <> 'information_schema'
105
+ FROM %SQL_MANAGER.${ func } ()
105
106
` ;
106
107
108
+ const fetchTableSchemas = fetchTypedSchemas ( ContextValue . TABLE , 'TablesTree' ) ;
109
+ const fetchViewSchemas = fetchTypedSchemas ( ContextValue . VIEW , 'ViewsTree' ) ;
110
+ const fetchFunctionSchemas = fetchTypedSchemas ( ContextValue . FUNCTION , 'ProceduresTree' ) ;
111
+
107
112
export default {
108
113
describeTable,
109
114
countRecords,
110
115
fetchColumns,
111
116
fetchRecords,
112
117
fetchTables,
118
+ fetchFunctions,
113
119
fetchViews,
114
120
searchTables,
115
121
searchColumns,
116
- fetchSchemas,
122
+ fetchTableSchemas,
123
+ fetchViewSchemas,
124
+ fetchFunctionSchemas,
117
125
}
0 commit comments