@@ -13,47 +13,50 @@ import {
1313 normalizeResponse ,
1414 getNormalizedStatistics
1515} from "./normalizeResponse" ;
16+ import { CompositeError } from "../common/errors" ;
1617
1718export class Statement {
1819 private context : Context ;
1920 private query : string ;
2021 private executeQueryOptions : ExecuteQueryOptions ;
2122
22- private request : { ready : ( ) => Promise < any > ; abort : ( ) => void } ;
23+ private text ;
2324 private rowStream : RowStream ;
2425
2526 constructor (
2627 context : Context ,
2728 {
2829 query,
29- request ,
30+ text ,
3031 executeQueryOptions
3132 } : {
3233 query : string ;
33- request : { ready : ( ) => Promise < any > ; abort : ( ) => void } ;
34+ text : string ;
3435 executeQueryOptions : ExecuteQueryOptions ;
3536 }
3637 ) {
3738 this . context = context ;
38- this . request = request ;
39+ this . text = text ;
3940 this . query = query ;
4041 this . executeQueryOptions = executeQueryOptions ;
4142 this . rowStream = new RowStream ( ) ;
4243 }
4344
4445 private parseResponse ( response : string ) {
4546 const parsed = JSONbig . parse ( response ) ;
46- const { data, meta, statistics } = parsed ;
47+ const { data, meta, statistics, errors = undefined } = parsed ;
4748 return {
4849 data,
4950 meta,
50- statistics
51+ statistics,
52+ errors
5153 } ;
5254 }
5355
5456 private handleParseResponse ( response : string ) {
57+ let errors , json ;
5558 try {
56- return this . parseResponse ( response ) ;
59+ ( { errors , ... json } = this . parseResponse ( response ) ) ;
5760 } catch ( error ) {
5861 const isData = isDataQuery ( this . query ) ;
5962 if ( isData || ( response . length && ! isData ) ) {
@@ -65,111 +68,37 @@ export class Statement {
6568 statistics : null
6669 } ;
6770 }
71+ if ( errors !== undefined ) throw new CompositeError ( errors ) ;
72+ return json ;
6873 }
6974
7075 async streamResult ( options ?: StreamOptions ) {
71- const response = await this . request . ready ( ) ;
72- const jsonStream = new JSONStream ( {
73- emitter : this . rowStream ,
74- options,
75- executeQueryOptions : this . executeQueryOptions
76- } ) ;
77-
78- let resolveMetadata : ( metadata : Meta [ ] ) => void ;
79- let rejectMetadata : ( reason ?: any ) => void ;
80-
81- let resolveStatistics : ( statistics : Statistics ) => void ;
82- let rejectStatistics : ( reason ?: any ) => void ;
83-
84- const metadataPromise = new Promise < Meta [ ] > ( ( resolve , reject ) => {
85- resolveMetadata = resolve ;
86- rejectMetadata = reject ;
87- } ) ;
88-
89- const statisticsPromise = new Promise < Statistics > ( ( resolve , reject ) => {
90- resolveStatistics = resolve ;
91- rejectStatistics = reject ;
92- } ) ;
93-
94- let str = Buffer . alloc ( 0 ) ;
95-
96- this . rowStream . on ( "metadata" , ( metadata : Meta [ ] ) => {
97- resolveMetadata ( metadata ) ;
98- } ) ;
99-
100- this . rowStream . on ( "statistics" , ( statistics : Statistics ) => {
101- resolveStatistics ( statistics ) ;
102- } ) ;
103-
104- const errorHandler = ( error : any ) => {
105- this . rowStream . emit ( "error" , error ) ;
106- this . rowStream . push ( null ) ;
107- rejectStatistics ( error ) ;
108- rejectMetadata ( error ) ;
109- } ;
110-
111- response . body . on ( "error" , errorHandler ) ;
112-
113- response . body . on ( "data" , ( chunk : Buffer ) => {
114- // content type should be application/json?
115- // maybe in the future it will change
116- const contentType = response . headers . get ( "content-type" ) ;
117-
118- if ( chunk . lastIndexOf ( "\n" ) !== - 1 && str ) {
119- // store in buffer anything after
120- const newLinePosition = chunk . lastIndexOf ( "\n" ) ;
121- const rest = chunk . slice ( newLinePosition + 1 ) ;
122-
123- const lines = Buffer . concat ( [ str , chunk . slice ( 0 , newLinePosition ) ] )
124- . toString ( "utf8" )
125- . split ( "\n" ) ;
126- try {
127- for ( const line of lines ) {
128- jsonStream . processLine ( line ) ;
129- }
130- } catch ( error ) {
131- errorHandler ( error ) ;
132- return ;
133- }
134-
135- const result = jsonStream . getResult ( 0 ) ;
136- // for now only supports single statement sql
137- for ( const row of result . rows ) {
138- this . rowStream . push ( row ) ;
139- }
76+ // Streaming is not supported right now in Firebolt
77+ // This is a placeholder for future implementation
78+ const parsed = this . handleParseResponse ( this . text ) ;
79+ const normalized = normalizeResponse ( parsed , this . executeQueryOptions ) ;
14080
141- result . rows = [ ] ;
142- str = rest ;
143- }
144- } ) ;
81+ const { data, meta, statistics } = normalized ;
14582
146- response . body . on ( "end" , ( ) => {
147- try {
148- const result = jsonStream . getResult ( 0 ) ;
149- const statistics = getNormalizedStatistics ( result . statistics ) ;
150- this . rowStream . emit ( "statistics" , statistics ) ;
151- this . rowStream . push ( null ) ;
152- } catch ( error ) {
153- errorHandler ( error ) ;
154- }
155- } ) ;
83+ for ( const row of data ) {
84+ this . rowStream . push ( row ) ;
85+ }
86+ this . rowStream . push ( null ) ;
87+ this . rowStream . end ( ) ;
15688
15789 return {
15890 data : this . rowStream ,
159- meta : metadataPromise ,
160- statistics : statisticsPromise
91+ meta : Promise . resolve ( meta ) ,
92+ statistics : Promise . resolve ( statistics )
16193 } ;
16294 }
16395
164- async fetchResult ( ) {
165- const response = await this . request . ready ( ) ;
166- const text = await response . text ( ) ;
167-
168- if ( this . executeQueryOptions ?. settings ?. async_execution ) {
169- return JSONbig . parse ( text ) ;
170- }
171-
172- const parsed = this . handleParseResponse ( text ) ;
96+ async fetchResult ( ) : Promise < {
97+ data : any ;
98+ meta : Meta [ ] ;
99+ statistics : Statistics | null ;
100+ } > {
101+ const parsed = this . handleParseResponse ( this . text ) ;
173102 const normalized = normalizeResponse ( parsed , this . executeQueryOptions ) ;
174103
175104 const { data, meta, statistics } = normalized ;
0 commit comments