@@ -4,6 +4,7 @@ import { IConnectionDriver, MConnectionExplorer, NSDatabase, ContextValue, Arg0
4
4
import { v4 as generateId } from 'uuid' ;
5
5
import IRISdb , { IRISDirect , IQueries } from './irisdb' ;
6
6
import keywordsCompletion from './keywords' ;
7
+ import zipObject from 'lodash/zipObject' ;
7
8
8
9
const toBool = ( v : any ) => v && ( v . toString ( ) === '1' || v . toString ( ) . toLowerCase ( ) === 'true' || v . toString ( ) . toLowerCase ( ) === 'yes' ) ;
9
10
@@ -55,21 +56,48 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
55
56
return queries . split ( / ; \s * ( \n | $ ) / gm) . filter ( query => query . trim ( ) . length ) ;
56
57
}
57
58
59
+ // Handle duplicate column names by appending counter
60
+ private getColumnNames ( columns : { name : string , type : string } [ ] ) : string [ ] {
61
+ return columns . reduce ( ( names , { name } ) => {
62
+ const count = names . filter ( ( n ) => n === name ) . length ;
63
+ return names . concat ( count > 0 ? `${ name } (${ count } )` : name ) ;
64
+ } , [ ] ) ;
65
+ }
66
+
67
+ // Modify to take account of deduplicated column names
68
+ private mapRows ( rows : any [ ] , columns : string [ ] ) : any [ ] {
69
+ return rows . map ( ( r ) => zipObject ( columns , r ) ) ;
70
+ }
71
+
58
72
public query : ( typeof AbstractDriver ) [ 'prototype' ] [ 'query' ] = async ( queries , opt = { } ) => {
59
73
const irisdb = await this . open ( ) ;
60
74
const listQueries = this . splitQueries ( queries . toString ( ) ) ;
61
75
const queriesResults = await Promise . all ( listQueries . map ( query => irisdb . query ( query , [ ] ) ) ) ;
62
76
const resultsAgg : NSDatabase . IResult [ ] = [ ] ;
63
77
queriesResults . forEach ( queryResult => {
64
- resultsAgg . push ( {
65
- cols : queryResult . length ? Object . keys ( queryResult [ 0 ] ) : [ ] ,
66
- connId : this . getId ( ) ,
67
- messages : [ { date : new Date ( ) , message : `Query ok with ${ queryResult . length } results` } ] ,
68
- results : queryResult ,
69
- query : queries . toString ( ) ,
70
- requestId : opt . requestId ,
71
- resultId : generateId ( ) ,
72
- } ) ;
78
+ if ( irisdb . apiVersion < 6 ) {
79
+ resultsAgg . push ( {
80
+ cols : queryResult . content . length ? Object . keys ( queryResult . content [ 0 ] ) : [ ] ,
81
+ connId : this . getId ( ) ,
82
+ messages : [ { date : new Date ( ) , message : `Query ok with ${ queryResult . content . length } results` } ] ,
83
+ results : queryResult . content ,
84
+ query : queries . toString ( ) ,
85
+ requestId : opt . requestId ,
86
+ resultId : generateId ( ) ,
87
+ } ) ;
88
+ }
89
+ else {
90
+ const cols = this . getColumnNames ( queryResult [ 0 ] . columns || [ ] ) ;
91
+ resultsAgg . push ( {
92
+ cols,
93
+ connId : this . getId ( ) ,
94
+ messages : [ { date : new Date ( ) , message : `Query ok with ${ queryResult [ 0 ] ?. content . length ?? 'no' } results` } ] ,
95
+ results : this . mapRows ( queryResult [ 0 ] ?. content , cols ) ,
96
+ query : queries . toString ( ) ,
97
+ requestId : opt . requestId ,
98
+ resultId : generateId ( ) ,
99
+ } ) ;
100
+ }
73
101
} ) ;
74
102
75
103
return resultsAgg ;
0 commit comments