1
1
import * as vscode from "vscode" ;
2
2
import { JobManager } from "../../config" ;
3
- import { JobInfo , SQLJobManager } from "../../connection/manager" ;
3
+ import { JobInfo } from "../../connection/manager" ;
4
4
import Statement from "../../database/statement" ;
5
- import { table } from "console" ;
6
- import { selfCodesResultsView } from "../../views/jobManager/selfCodes/selfCodesResultsView" ;
7
5
import { SelfCodeNode } from "../../views/jobManager/selfCodes/nodes" ;
8
- import { SQLJob } from "../../connection/sqlJob" ;
6
+ import { findPossibleTables } from "../../chat/context" ;
7
+ import { ContextItem , ContextProviderDescription , ContextProviderExtras , ContextSubmenuItem , IContextProvider , LoadSubmenuItemsArgs } from "../../.." ;
9
8
10
9
const db2ContextProviderDesc : ContextProviderDescription = {
11
10
title : "db2i" ,
@@ -14,8 +13,6 @@ const db2ContextProviderDesc: ContextProviderDescription = {
14
13
type : "normal" ,
15
14
} ;
16
15
17
- type TableRefs = { [ key : string ] : TableColumn [ ] }
18
-
19
16
/**
20
17
* - Get Existing Db2 connection from vscode
21
18
* - Use connection to build query for database to add additional context
@@ -36,114 +33,6 @@ export class db2ContextProvider implements IContextProvider {
36
33
return currentJob ?. job . options . libraries [ 0 ] || "QGPL" ;
37
34
} ;
38
35
39
- async findPossibleTables ( schema : string , words : string [ ] ) {
40
- // let refSchema: string = "";
41
- // let table: string = "";
42
- // words.forEach(item => {
43
- // if (item.includes(`.`)) {
44
- // refSchema = item.split(`.`)[0];
45
- // table = item.split(`.`)[1];
46
- // }
47
- // })
48
-
49
- words = words . map ( ( word ) =>
50
- word . replace ( / [ . , \/ # ! ? $ % \^ & \* ; : { } = \- _ ` ~ ( ) ] / g, "" )
51
- ) ;
52
-
53
- // Add extra words for words with S at the end, to ignore possible plurals
54
- words . forEach ( ( item ) => {
55
- if ( item . endsWith ( `s` ) ) {
56
- words . push ( item . slice ( 0 , - 1 ) ) ;
57
- }
58
- } ) ;
59
-
60
- const validWords = words
61
- . filter ( ( item ) => item . length > 2 && ! item . includes ( `'` ) )
62
- . map ( ( item ) => `'${ Statement . delimName ( item , true ) } '` ) ;
63
-
64
- const objectFindStatement = [
65
- `SELECT ` ,
66
- ` column.TABLE_NAME,` ,
67
- ` column.COLUMN_NAME,` ,
68
- ` key.CONSTRAINT_NAME,` ,
69
- ` column.DATA_TYPE, ` ,
70
- ` column.CHARACTER_MAXIMUM_LENGTH,` ,
71
- ` column.NUMERIC_SCALE, ` ,
72
- ` column.NUMERIC_PRECISION,` ,
73
- ` column.IS_NULLABLE, ` ,
74
- // ` column.HAS_DEFAULT, `,
75
- // ` column.COLUMN_DEFAULT, `,
76
- ` column.COLUMN_TEXT, ` ,
77
- ` column.IS_IDENTITY` ,
78
- `FROM QSYS2.SYSCOLUMNS2 as column` ,
79
- `LEFT JOIN QSYS2.syskeycst as key` ,
80
- ` on ` ,
81
- ` column.table_schema = key.table_schema and` ,
82
- ` column.table_name = key.table_name and` ,
83
- ` column.column_name = key.column_name` ,
84
- `WHERE column.TABLE_SCHEMA = '${ schema } '` ,
85
- ...[
86
- words . length > 0
87
- ? `AND column.TABLE_NAME in (${ validWords . join ( `, ` ) } )`
88
- : `` ,
89
- ] ,
90
- `ORDER BY column.ORDINAL_POSITION` ,
91
- ] . join ( ` ` ) ;
92
-
93
- // TODO
94
- const result : TableColumn [ ] = await JobManager . runSQL ( objectFindStatement ) ;
95
-
96
- const tables : TableRefs = { } ;
97
-
98
- for ( const row of result ) {
99
- if ( ! tables [ row . TABLE_NAME ] ) {
100
- tables [ row . TABLE_NAME ] = [ ] ;
101
- }
102
-
103
- tables [ row . TABLE_NAME ] . push ( row ) ;
104
- }
105
-
106
- return tables ;
107
- }
108
-
109
- async findAllTables ( schema : string ) {
110
- const objectFindStatement = [
111
- `SELECT ` ,
112
- ` column.TABLE_NAME,` ,
113
- ` column.COLUMN_NAME,` ,
114
- ` key.CONSTRAINT_NAME,` ,
115
- ` column.DATA_TYPE, ` ,
116
- ` column.CHARACTER_MAXIMUM_LENGTH,` ,
117
- ` column.NUMERIC_SCALE, ` ,
118
- ` column.NUMERIC_PRECISION,` ,
119
- ` column.IS_NULLABLE, ` ,
120
- // ` column.HAS_DEFAULT, `,
121
- // ` column.COLUMN_DEFAULT, `,
122
- ` column.COLUMN_TEXT, ` ,
123
- ` column.IS_IDENTITY` ,
124
- `FROM QSYS2.SYSCOLUMNS2 as column` ,
125
- `LEFT JOIN QSYS2.syskeycst as key` ,
126
- ` on ` ,
127
- ` column.table_schema = key.table_schema and` ,
128
- ` column.table_name = key.table_name and` ,
129
- ` column.column_name = key.column_name` ,
130
- `WHERE column.TABLE_SCHEMA = '${ schema } '` ,
131
- ] . join ( ` ` ) ;
132
-
133
- const result : TableColumn [ ] = await JobManager . runSQL ( objectFindStatement ) ;
134
-
135
- const tables : TableRefs = { } ;
136
-
137
- for ( const row of result ) {
138
- if ( ! tables [ row . TABLE_NAME ] ) {
139
- tables [ row . TABLE_NAME ] = [ ] ;
140
- }
141
-
142
- tables [ row . TABLE_NAME ] . push ( row ) ;
143
- }
144
-
145
- return tables ;
146
- }
147
36
async getSelfCodes ( selected : JobInfo ) : Promise < SelfCodeNode [ ] | undefined > {
148
37
const current_job = selected . job . id ;
149
38
const content = `SELECT
@@ -154,7 +43,7 @@ export class db2ContextProvider implements IContextProvider {
154
43
order by logged_time desc` ;
155
44
156
45
try {
157
- const result = await selected . job . query < SelfCodeNode > ( content ) . run ( 10000 ) ;
46
+ const result = await selected . job . query < SelfCodeNode > ( content ) . execute ( 10000 ) ;
158
47
if ( result . success ) {
159
48
const data : SelfCodeNode [ ] = result . data . map ( ( row ) => ( {
160
49
...row ,
@@ -202,19 +91,21 @@ export class db2ContextProvider implements IContextProvider {
202
91
return contextItems ;
203
92
default :
204
93
// const contextItems: ContextItem[] = [];
205
- const tableRefs = await this . findPossibleTables (
94
+ const tableRefs = await findPossibleTables (
95
+ null ,
206
96
schema ,
207
97
fullInput . split ( ` ` )
208
98
) ;
209
99
for ( const table of Object . keys ( tableRefs ) ) {
210
100
const columnData : TableColumn [ ] = tableRefs [ table ] ;
101
+ const tableSchema = columnData . length > 0 ? columnData [ 0 ] . TABLE_SCHEMA : null ;
211
102
212
103
// create context item
213
- let prompt = `Db2 for i schema ${ schema } table ${ table } \n` ;
104
+ let prompt = `Db2 for i schema ${ tableSchema } table ${ table } \n` ;
214
105
prompt += `Column Info: ${ JSON . stringify ( columnData ) } \n\n` ;
215
106
216
107
contextItems . push ( {
217
- name : `${ job . name } -${ schema } -${ table } ` ,
108
+ name : `${ job . name } -${ tableSchema } -${ table } ` ,
218
109
description : `Schema and table information or ${ table } ` ,
219
110
content : prompt ,
220
111
} ) ;
@@ -255,41 +146,10 @@ export class db2ContextProvider implements IContextProvider {
255
146
}
256
147
}
257
148
258
- class MyCustomProvider implements IContextProvider {
259
-
260
- get description ( ) : ContextProviderDescription {
261
- return {
262
- title : "custom" ,
263
- displayTitle : "Custom" ,
264
- description : "Custom description" ,
265
- type : "normal" ,
266
- } ;
267
- }
268
-
269
- async getContextItems (
270
- query : string ,
271
- extras : ContextProviderExtras
272
- ) : Promise < ContextItem [ ] > {
273
- return [
274
- {
275
- name : "Custom" ,
276
- description : "Custom description" ,
277
- content : "Custom content" ,
278
- } ,
279
- ] ;
280
- }
281
-
282
- async loadSubmenuItems (
283
- args : LoadSubmenuItemsArgs
284
- ) : Promise < ContextSubmenuItem [ ] > {
285
- return [ ] ;
286
- }
287
- }
288
-
289
149
export async function registerContinueProvider ( ) {
290
150
const provider = new db2ContextProvider ( ) ;
291
151
const continueID = `Continue.continue` ;
292
152
const continueEx = vscode . extensions . getExtension ( continueID ) ;
293
153
const continueAPI = continueEx ?. exports ;
294
154
continueAPI ?. registerCustomContextProvider ( provider ) ;
295
- }
155
+ }
0 commit comments