|
| 1 | +import transformer from '../components/transformer.js'; |
| 2 | +import emitter from '../components/emitter.js'; |
| 3 | +import csvMaker from '../components/csv.js'; |
| 4 | +import u from 'ak-tools'; |
| 5 | +import mssql from 'mssql'; |
| 6 | +import sql from 'node-sql-parser'; |
| 7 | +import dayjs from "dayjs"; |
| 8 | + |
| 9 | + |
| 10 | +export default async function azure(config, outStream) { |
| 11 | + |
| 12 | + const { query, ...dwhAuth } = config.dwhAuth(); |
| 13 | + const sqlParse = new sql.Parser(); |
| 14 | + let tableList, columnList, ast; |
| 15 | + try { |
| 16 | + ({ tableList, columnList, ast } = sqlParse.parse(query, { database: 'transactsql' })); |
| 17 | + config.store({ sqlAnalysis: { tableList, columnList, ast } }); |
| 18 | + } catch (e) { |
| 19 | + if (config.verbose) u.cLog("\ncould not parse SQL query to AST...\n\tthat's ok though!!!\n"); |
| 20 | + } |
| 21 | + |
| 22 | + // * AUTH |
| 23 | + const auth = dwhAuth.connectionString ? dwhAuth.connectionString : dwhAuth; |
| 24 | + const pool = await mssql.connect(auth); //todo support things other than connection string |
| 25 | + pool.on('error', (e) => { throw e; }); |
| 26 | + config.store({ job: { ...pool.config, password: `******` } }); |
| 27 | + |
| 28 | + // * MODELING |
| 29 | + if (config.type === "event") { |
| 30 | + //events get unix epoch |
| 31 | + config.timeTransform = (time) => { return dayjs(time).valueOf(); }; |
| 32 | + } |
| 33 | + else { |
| 34 | + //all others get ISO |
| 35 | + config.timeTransform = (time) => { return dayjs(time).format('YYYY-MM-DDTHH:mm:ss'); }; |
| 36 | + } |
| 37 | + let mpModel; //not available until "readable" |
| 38 | + |
| 39 | + // * LOOKUP TABLES |
| 40 | + if (config.type === 'table') { |
| 41 | + emitter.emit('dwh query start', config); |
| 42 | + const { recordset, rowsAffected } = await (new mssql.Request(pool)).query(query); |
| 43 | + emitter.emit('dwh query end', config); |
| 44 | + config.store({ rows: rowsAffected[0] }); |
| 45 | + config.store({ schema: recordset.columns }); |
| 46 | + mpModel = transformer(config, []); |
| 47 | + const transformedRows = recordset.map(mpModel); |
| 48 | + const csv = csvMaker(transformedRows); |
| 49 | + return csv; |
| 50 | + } |
| 51 | + |
| 52 | + // * METADATA + ROW COUNTS |
| 53 | + emitter.emit('dwh query start', config); |
| 54 | + try { |
| 55 | + const getRowCount = await (new mssql.Request(pool)).query(`WITH count_query AS (${query}) SELECT COUNT(*) as rows FROM count_query;`); |
| 56 | + config.store({ rows: getRowCount.recordset[0].rows }); |
| 57 | + } |
| 58 | + catch (e) { |
| 59 | + try { |
| 60 | + // trailing semicolons mess up the row count query |
| 61 | + if (query.endsWith(';')) { |
| 62 | + const rowCountQuery = query.substring(0, query.length - 1); |
| 63 | + const getRowCount = await (new mssql.Request(pool)).query(`WITH count_query AS (${rowCountQuery}) SELECT COUNT(*) as rows FROM count_query;`); |
| 64 | + config.store({ rows: getRowCount.recordset[0].rows }); |
| 65 | + } |
| 66 | + |
| 67 | + // try to re-serialize query from the ast |
| 68 | + else if (ast) { |
| 69 | + const rowCountQuery = sqlParse.sqlify(ast); |
| 70 | + const getRowCount = await (new mssql.Request(pool)).query(`WITH count_query AS (${rowCountQuery}) SELECT COUNT(*) as rows FROM count_query;`); |
| 71 | + config.store({ rows: getRowCount.recordset[0].rows }); |
| 72 | + } |
| 73 | + |
| 74 | + // todo add a @@ROWCOUNT attempt to get rows |
| 75 | + // ? https://www.sqlshack.com/working-with-sql-server-rowcount/ |
| 76 | + |
| 77 | + else { |
| 78 | + throw e; |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + catch (e) { |
| 83 | + if (config.verbose) u.cLog('error getting row counts', e.message, 'ERROR'); |
| 84 | + config.store({ rows: 0 }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // * STREAMING QUERY |
| 89 | + const job = new mssql.Request(pool); |
| 90 | + job.stream = true; |
| 91 | + job.query(`${query}`); |
| 92 | + |
| 93 | + return new Promise((resolve, reject) => { |
| 94 | + job.on('recordset', (columns) => { |
| 95 | + emitter.emit('dwh query end', config); |
| 96 | + emitter.emit('dwh stream start', config); |
| 97 | + config.store({ schema: columns }); |
| 98 | + const schemaDateFields = Object.keys(u.objFilter(columns, (col) => col?.type?.declaration === 'datetime')); |
| 99 | + const dateFields = new Set([config.mappings.time_col, ...schemaDateFields]); |
| 100 | + mpModel = transformer(config, dateFields); |
| 101 | + }); |
| 102 | + |
| 103 | + job.on('row', (row) => { |
| 104 | + config.got(); |
| 105 | + outStream.push(mpModel(row)); |
| 106 | + }); |
| 107 | + |
| 108 | + // job.on('rowsaffected', (rowCount) => { |
| 109 | + // // ! seems to fire last |
| 110 | + // config.store({ rows: rowCount }); |
| 111 | + // }); |
| 112 | + |
| 113 | + // job.on('info', (message) => { |
| 114 | + // // ! dunno what this is |
| 115 | + // debugger; |
| 116 | + // }); |
| 117 | + |
| 118 | + job.on('error', (err) => { |
| 119 | + reject(err); |
| 120 | + }); |
| 121 | + |
| 122 | + job.on('done', () => { |
| 123 | + emitter.emit('dwh stream end', config); |
| 124 | + outStream.push(null); |
| 125 | + pool.close(); |
| 126 | + resolve(config); |
| 127 | + }); |
| 128 | + }); |
| 129 | +} |
0 commit comments