@@ -397,15 +397,55 @@ export default class schemaBrowser {
397
397
}
398
398
} ) ,
399
399
400
- vscode . commands . registerCommand ( `vscode-db2i.importData ` , async ( ) => {
401
- vscode . window . withProgress ( { location : vscode . ProgressLocation . Window , title : `Generating SQL` } , async ( ) => {
400
+ vscode . commands . registerCommand ( `vscode-db2i.importDataContextMenu ` , async ( ) => {
401
+ vscode . window . withProgress ( { location : vscode . ProgressLocation . Window , title : `Generating SQL` } , async ( arg ?: any ) => {
402
402
try {
403
+ const data = vscode . window . activeTextEditor . document . getText ( ) ;
404
+ const uri = vscode . window . activeTextEditor . document . uri ;
405
+ await this . generateInsert ( uri , data ) ;
406
+ } catch ( e ) {
407
+ vscode . window . showErrorMessage ( e . message ) ;
408
+ }
409
+ } ) ;
410
+ } ) ,
411
+
412
+ vscode . commands . registerCommand ( `vscode-db2i.importData` , async ( ) => {
413
+ vscode . window . withProgress ( { location : vscode . ProgressLocation . Window , title : `Generating SQL` } , async ( arg ?: any ) => {
414
+ try {
403
415
const uri = await this . pickFile ( ) ;
404
416
if ( ! uri ) { return ; }
405
417
const data = await this . readFile ( uri ) ;
406
- const tableName = `SYSIBM.SYSDUMMY1` ;
407
- let ext : string = ( uri . fsPath . split ( '.' ) . pop ( ) || '' ) . toLowerCase ( ) ;
408
418
419
+ await this . generateInsert ( uri , data ) ;
420
+ } catch ( e ) {
421
+ vscode . window . showErrorMessage ( e . message ) ;
422
+ }
423
+ } ) ;
424
+ } )
425
+ )
426
+
427
+ getInstance ( ) . subscribe ( context , `connected` , `db2i-clearCacheAndRefresh` , ( ) => this . clearCacheAndRefresh ( ) ) ;
428
+ }
429
+
430
+ async pickFile ( ) : Promise < vscode . Uri | undefined > {
431
+ const res = await vscode . window . showOpenDialog ( {
432
+ canSelectMany : false ,
433
+ openLabel : 'Import' ,
434
+ filters : {
435
+ 'Data files' : [ 'csv' , 'json' ] ,
436
+ 'All files' : [ '*' ]
437
+ }
438
+ } ) ;
439
+ return res ?. [ 0 ] ;
440
+ }
441
+
442
+ async readFile ( uri : vscode . Uri ) : Promise < string > {
443
+ const ab = await vscode . workspace . fs . readFile ( uri ) ;
444
+ return new TextDecoder ( 'utf-8' ) . decode ( ab ) ;
445
+ }
446
+
447
+ async generateInsert ( uri : vscode . Uri , data : string ) {
448
+ let ext : string = ( uri . fsPath . split ( '.' ) . pop ( ) || '' ) . toLowerCase ( ) ;
409
449
if ( ext != `csv` && ext != `json` ) {
410
450
ext = await vscode . window . showQuickPick ( [ 'csv' , 'json' ] , { placeHolder : 'What format is this file?' } ) ;
411
451
if ( ! ext ) { return ; }
@@ -467,31 +507,6 @@ export default class schemaBrowser {
467
507
// Open the generated SQL in a new file
468
508
const textDoc = await vscode . workspace . openTextDocument ( { language : `sql` , content } ) ;
469
509
await vscode . window . showTextDocument ( textDoc ) ;
470
- } catch ( e ) {
471
- vscode . window . showErrorMessage ( e . message ) ;
472
- }
473
- } ) ;
474
- } )
475
- )
476
-
477
- getInstance ( ) . subscribe ( context , `connected` , `db2i-clearCacheAndRefresh` , ( ) => this . clearCacheAndRefresh ( ) ) ;
478
- }
479
-
480
- async pickFile ( ) : Promise < vscode . Uri | undefined > {
481
- const res = await vscode . window . showOpenDialog ( {
482
- canSelectMany : false ,
483
- openLabel : 'Import' ,
484
- filters : {
485
- 'Data files' : [ 'csv' , 'json' ] ,
486
- 'All files' : [ '*' ]
487
- }
488
- } ) ;
489
- return res ?. [ 0 ] ;
490
- }
491
-
492
- async readFile ( uri : vscode . Uri ) : Promise < string > {
493
- const ab = await vscode . workspace . fs . readFile ( uri ) ;
494
- return new TextDecoder ( 'utf-8' ) . decode ( ab ) ;
495
510
}
496
511
497
512
clearCacheAndRefresh ( ) {
0 commit comments