@@ -4,6 +4,7 @@ import { IConnectionDriver, MConnectionExplorer, NSDatabase, ContextValue, Arg0
44import { v4 as generateId } from 'uuid' ;
55import IRISdb , { IRISDirect , IQueries } from './irisdb' ;
66import keywordsCompletion from './keywords' ;
7+ import zipObject from 'lodash/zipObject' ;
78
89const toBool = ( v : any ) => v && ( v . toString ( ) === '1' || v . toString ( ) . toLowerCase ( ) === 'true' || v . toString ( ) . toLowerCase ( ) === 'yes' ) ;
910
@@ -55,21 +56,48 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
5556 return queries . split ( / ; \s * ( \n | $ ) / gm) . filter ( query => query . trim ( ) . length ) ;
5657 }
5758
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+
5872 public query : ( typeof AbstractDriver ) [ 'prototype' ] [ 'query' ] = async ( queries , opt = { } ) => {
5973 const irisdb = await this . open ( ) ;
6074 const listQueries = this . splitQueries ( queries . toString ( ) ) ;
6175 const queriesResults = await Promise . all ( listQueries . map ( query => irisdb . query ( query , [ ] ) ) ) ;
6276 const resultsAgg : NSDatabase . IResult [ ] = [ ] ;
6377 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+ }
73101 } ) ;
74102
75103 return resultsAgg ;
0 commit comments