import { Timezone } from "chronoshift" ; import { Expression , Datum , PlywoodValue , Dataset , SQLParse } from "plywood" ; import { createJSONServer , JSONParameters } from './json-server' ; import { executeSQLParse , executePlywood } from "./plyql-executor" ; export function plyqlJSONServer ( port , context , timezone ) { createJSONServer ( port , ( parameters , res ) => { let { sql , expression } = parameters ; let resultPromise ; if ( expression ) { let ex ; try { ex = Expression . fromJSLoose ( expression ) ; } catch ( e ) { res . status ( 400 ) . send ( { error : e . message } ) ; return ; } resultPromise = executePlywood ( ex , context , timezone ) ; } else { let sqlParse ; try { sqlParse = Expression . parseSQL ( sql ) ; } catch ( e ) { res . status ( 400 ) . send ( { error : e . message } ) ; return ; } if ( sqlParse . verb && sqlParse . verb !== 'SELECT' ) { res . status ( 400 ) . send ( { error : `Unsupported SQL verb ${ sqlParse . verb } must be SELECT, DESCRIBE, SHOW, or a raw expression` } ) ; return ; } resultPromise = executeSQLParse ( sqlParse , context , timezone ) ; } resultPromise . then ( ( value ) => { if ( Dataset . isDataset ( value ) ) { res . json ( { result : value . toJS ( ) } ) ; } else { res . json ( { result : value } ) ; } } ) . catch ( ( e ) => { res . status ( 500 ) . send ( { error : e . message } ) ; } ) ; } ) ; }