1- import { IBaseQueries , ContextValue } from '@sqltools/types' ;
1+ import { ContextValue , NSDatabase , QueryBuilder } from '@sqltools/types' ;
22import queryFactory from '@sqltools/base-driver/dist/lib/factory' ;
3+ import { IQueries } from './irisdb' ;
34
45/** write your queries here go fetch desired data. This queries are just examples copied from SQLite driver */
56
6- const describeTable : IBaseQueries [ 'describeTable' ] = queryFactory `
7+ const describeTable : IQueries [ 'describeTable' ] = queryFactory `
78 SELECT C.*
89 FROM pragma_table_info('${ p => p . label } ') AS C
910 ORDER BY C.cid ASC
1011` ;
1112
12- const fetchColumns : IBaseQueries [ 'fetchColumns' ] = queryFactory `
13+ const fetchColumns : IQueries [ 'fetchColumns' ] = queryFactory `
1314SELECT
1415 C.COLUMN_NAME AS label,
1516 '${ ContextValue . COLUMN } ' as type,
@@ -34,41 +35,40 @@ ORDER BY
3435 C.ORDINAL_POSITION
3536` ;
3637
37- const fetchRecords : IBaseQueries [ 'fetchRecords' ] = queryFactory `
38+ const fetchRecords : IQueries [ 'fetchRecords' ] = queryFactory `
3839SELECT TOP ${ p => p . limit || 50 } *
3940FROM ${ p => p . table . schema } .${ p => ( p . table . label || p . table ) }
4041` ;
4142
42- const countRecords : IBaseQueries [ 'countRecords' ] = queryFactory `
43+ const countRecords : IQueries [ 'countRecords' ] = queryFactory `
4344SELECT count(1) AS total
4445FROM ${ p => p . table . schema } .${ p => ( p . table . label || p . table ) }
4546` ;
4647
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 } '
5756ORDER BY
58- T.TABLE_NAME
57+ ${ name }
5958` ;
6059
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' ) ;
6363
64- const searchTables : IBaseQueries [ 'searchTables' ] = queryFactory `
64+ const searchTables : IQueries [ 'searchTables' ] = queryFactory `
6565SELECT name AS label,
6666 type
6767FROM sqlite_master
6868${ p => p . search ? `WHERE LOWER(name) LIKE '%${ p . search . toLowerCase ( ) } %'` : '' }
6969ORDER BY name
7070` ;
71- const searchColumns : IBaseQueries [ 'searchColumns' ] = queryFactory `
71+ const searchColumns : IQueries [ 'searchColumns' ] = queryFactory `
7272SELECT C.name AS label,
7373 T.name AS "table",
7474 C.type AS dataType,
@@ -79,39 +79,47 @@ FROM sqlite_master AS T
7979LEFT OUTER JOIN pragma_table_info((T.name)) AS C ON 1 = 1
8080WHERE 1 = 1
8181${ 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+ }
8585${ p => p . search
86- ? `AND (
86+ ? `AND (
8787 LOWER(T.name || '.' || C.name) LIKE '%${ p . search . toLowerCase ( ) } %'
8888 OR LOWER(C.name) LIKE '%${ p . search . toLowerCase ( ) } %'
8989 )`
90- : ''
91- }
90+ : ''
91+ }
9292ORDER BY C.name ASC,
9393 C.cid ASC
9494LIMIT ${ p => p . limit || 100 }
9595` ;
9696
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",
101102 '${ ContextValue . SCHEMA } ' as "type",
103+ '${ type } ' as "childType",
102104 'folder' as iconId
103- FROM information_schema.schemata
104- WHERE schema_name <> 'information_schema'
105+ FROM %SQL_MANAGER.${ func } ()
105106` ;
106107
108+ const fetchTableSchemas = fetchTypedSchemas ( ContextValue . TABLE , 'TablesTree' ) ;
109+ const fetchViewSchemas = fetchTypedSchemas ( ContextValue . VIEW , 'ViewsTree' ) ;
110+ const fetchFunctionSchemas = fetchTypedSchemas ( ContextValue . FUNCTION , 'ProceduresTree' ) ;
111+
107112export default {
108113 describeTable,
109114 countRecords,
110115 fetchColumns,
111116 fetchRecords,
112117 fetchTables,
118+ fetchFunctions,
113119 fetchViews,
114120 searchTables,
115121 searchColumns,
116- fetchSchemas,
122+ fetchTableSchemas,
123+ fetchViewSchemas,
124+ fetchFunctionSchemas,
117125}
0 commit comments