11import { createPrivateKey } from "crypto" ;
22import { snowflake } from "@pipedream/snowflake-sdk" ;
33import { promisify } from "util" ;
4- import { sqlProp } from "@pipedream/platform" ;
4+ import {
5+ sqlProxy , sqlProp ,
6+ } from "@pipedream/platform" ;
57
68snowflake . configure ( {
79 logLevel : "WARN" ,
@@ -88,6 +90,7 @@ export default {
8890 } ,
8991 } ,
9092 methods : {
93+ ...sqlProxy . methods ,
9194 ...sqlProp . methods ,
9295 async _getConnection ( ) {
9396 if ( this . connection ) {
@@ -169,7 +172,7 @@ export default {
169172 const binds = [
170173 this . $auth . schema ,
171174 ] ;
172- const rows = await this . collectRows ( {
175+ const rows = await this . executeQuery ( {
173176 sqlText,
174177 binds,
175178 } ) ;
@@ -186,13 +189,31 @@ export default {
186189 return acc ;
187190 } , { } ) ;
188191 } ,
189- async getRows ( statement ) {
192+ proxyAdapter ( preparedStatement = { } ) {
193+ const {
194+ sqlText : query = "" ,
195+ binds : params = [ ] ,
196+ } = preparedStatement ;
197+ return {
198+ query,
199+ params,
200+ } ;
201+ } ,
202+ executeQueryAdapter ( proxyArgs = { } ) {
203+ const {
204+ query : sqlText = "" ,
205+ params : binds = [ ] ,
206+ } = proxyArgs ;
207+ return {
208+ sqlText,
209+ binds,
210+ } ;
211+ } ,
212+ async executeQuery ( statement ) {
190213 const connection = await this . _getConnection ( ) ;
191214 const executedStatement = connection . execute ( statement ) ;
192- return executedStatement . streamRows ( ) ;
193- } ,
194- async collectRows ( statement ) {
195- const rowStream = await this . getRows ( statement ) ;
215+
216+ const rowStream = await executedStatement . streamRows ( ) ;
196217 const rows = [ ] ;
197218 for await ( const row of rowStream ) {
198219 rows . push ( row ) ;
@@ -203,19 +224,19 @@ export default {
203224 database, schema,
204225 } ) {
205226 let sqlText = `SHOW TABLES IN SCHEMA ${ database } .${ schema } ` ;
206- return this . collectRows ( {
227+ return this . executeQuery ( {
207228 sqlText,
208229 } ) ;
209230 } ,
210231 async listDatabases ( ) {
211232 const sqlText = "SHOW DATABASES" ;
212- return this . collectRows ( {
233+ return this . executeQuery ( {
213234 sqlText,
214235 } ) ;
215236 } ,
216237 async listSchemas ( database ) {
217238 const sqlText = "SHOW SCHEMAS IN DATABASE IDENTIFIER(:1)" ;
218- return this . collectRows ( {
239+ return this . executeQuery ( {
219240 sqlText,
220241 binds : [
221242 database ,
@@ -224,26 +245,26 @@ export default {
224245 } ,
225246 async listWarehouses ( ) {
226247 const sqlText = "SHOW WAREHOUSES" ;
227- return this . collectRows ( {
248+ return this . executeQuery ( {
228249 sqlText,
229250 } ) ;
230251 } ,
231252 async listUsers ( ) {
232253 const sqlText = "SHOW USERS" ;
233- return this . collectRows ( {
254+ return this . executeQuery ( {
234255 sqlText,
235256 } ) ;
236257 } ,
237258 async maxQueryHistoryTimestamp ( ) {
238259 const sqlText = "SELECT MAX(START_TIME) AS max_ts FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY" ;
239- const maxTs = await this . collectRows ( {
260+ const maxTs = await this . executeQuery ( {
240261 sqlText,
241262 } ) ;
242263 return + new Date ( maxTs [ 0 ] ?. MAX_TS ) ;
243264 } ,
244265 async maxTaskHistoryTimestamp ( ) {
245266 const sqlText = "SELECT MAX(QUERY_START_TIME) AS max_ts FROM TABLE(INFORMATION_SCHEMA.TASK_HISTORY())" ;
246- const maxTs = await this . collectRows ( {
267+ const maxTs = await this . executeQuery ( {
247268 sqlText,
248269 } ) ;
249270 return + new Date ( maxTs [ 0 ] ?. MAX_TS ) ;
@@ -257,7 +278,7 @@ export default {
257278 sqlText,
258279 binds,
259280 } ;
260- return this . collectRows ( statement ) ;
281+ return this . executeQuery ( statement ) ;
261282 } ,
262283 // Query Snowflake query history.
263284 // start and endTime bound the query. Both expect epoch ms timestamps.
@@ -273,7 +294,7 @@ export default {
273294 sqlText,
274295 } ;
275296 console . log ( `Running query: ${ sqlText } ` ) ;
276- return this . collectRows ( statement ) ;
297+ return this . executeQuery ( statement ) ;
277298 } ,
278299 async getFailedTasksInDatabase ( {
279300 startTime, database, schemas, taskName,
@@ -311,7 +332,7 @@ export default {
311332 sqlText,
312333 binds,
313334 } ;
314- return this . collectRows ( statement ) ;
335+ return this . executeQuery ( statement ) ;
315336 } ,
316337 async getFailedTasksInWarehouse ( {
317338 startTime, endTime, warehouse,
@@ -331,7 +352,7 @@ export default {
331352 warehouse ,
332353 ] ,
333354 } ;
334- return this . collectRows ( statement ) ;
355+ return this . executeQuery ( statement ) ;
335356 } ,
336357 async getChangesForSpecificObject ( startTime , endTime , objectType ) {
337358 const filters = `QUERY_TYPE != 'SELECT' AND (QUERY_TEXT ILIKE '%CREATE ${ objectType } %' OR QUERY_TEXT ILIKE '%ALTER ${ objectType } %' OR QUERY_TEXT ILIKE '%DROP ${ objectType } %')` ;
@@ -345,15 +366,15 @@ export default {
345366 sqlText,
346367 binds,
347368 } ;
348- return this . collectRows ( statement ) ;
369+ return this . executeQuery ( statement ) ;
349370 } ,
350371 async insertRows ( tableName , columns , binds ) {
351372 const sqlText = `INSERT INTO ${ tableName } (${ columns . join ( "," ) } ) VALUES (${ columns . map ( ( ) => "?" ) . join ( ", " ) } );` ;
352373 const statement = {
353374 sqlText,
354375 binds,
355376 } ;
356- return this . collectRows ( statement ) ;
377+ return this . executeQuery ( statement ) ;
357378 } ,
358379 } ,
359380} ;
0 commit comments