Skip to content

Commit

Permalink
add examples to Readme, adds binds and preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
brugos committed Jan 18, 2021
1 parent 252a31c commit d096b24
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please check https://github.com/brugos/snowflake-multisql/releases
39 changes: 30 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# snowflake-multisql
# snowflake-multisql [![node](https://img.shields.io/node/v/snowflake-multisql.svg)](https://www.npmjs.com/package/snowflake-multisql)

A Multi SQL, Promise-based and Typescript version to your [Snowflake](https://www.snowflake.net/) data warehouse.

Expand All @@ -10,6 +10,21 @@ Unfortunately [Snowflake SDK](https://www.npmjs.com/package/snowflake-sdk) doesn

- `yarn add snowflake-multisql` or `npm i snowflake-multisql`

## Connecting

The constructor extends SnowflakePromise class addin the new method **_executeAll_**.
The unique method's param is deconstructed into the variables below:

```json
{
"sqlText": "your SQL Text string",
"binds": "from the original library, replace tags by sequence",
"replaceTags": "new, replace tags by name, whatever order them appers",
"includeResults": true, // returns field 'data' with results for each chunk.
"preview": true // logs the final order of statements and parsed variables without executing them.
}
```

## Basic usage

```typescript
Expand All @@ -29,17 +44,23 @@ async function main() {

await snowflake.connect();

const rows = await snowflake.executeAll(
"SELECT COUNT(*) FROM CUSTOMER WHERE C_MKTSEGMENT=:1",
["AUTOMOBILE"]
);
const sqlText = `
CREATE OR REPLACE TABLE temp_table_customer as
SELECT COUNT(*) FROM customer WHERE C_MKTSEGMENT=:1;
USE SCHEMA demo_schema;
SELECT COUNT(*) FROM customer WHERE c_mktsegment=:2;
`;
const binds = ["AUTOMOBILE", "BIKE"];

const rows = await snowflake.executeAll({
sqlText,
binds,
includeResults: true,
});

console.log(rows);
}

main();
```

## Connecting

The constructor extends SnowflakePromise class or take your Snowflake instance and argument.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snowflake-multisql",
"version": "0.1.0",
"version": "0.2.0",
"description": "Multi-SQL, Promise-based, TypeScript wrapper for Snowflake SDK",
"repository": "github:brugos/snowflake-multisql",
"bugs": "https://github.com/brugos/snowflake-multisql/issues",
Expand Down
49 changes: 31 additions & 18 deletions src/snowflake-multisql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ export interface ITag {
tag: string;
value: string;
}

export interface IMultiSqlResult {
export interface IPreview {
chunkText: string;
duration: number;
chunkOrder: number;
chunksTotal: number;
totalDuration: number;
}
export interface IMultiSqlResult extends IPreview {
duration?: number;
totalDuration?: number;
data?: any[];
}
export class SnowflakeMultiSql extends Snowflake {
Expand All @@ -20,10 +21,14 @@ export class SnowflakeMultiSql extends Snowflake {
public async executeAll({
sqlText,
replace,
binds,
preview,
includeResults = false,
}: {
sqlText: string;
replace?: ITag[];
binds?: any[];
preview?: boolean;
includeResults?: boolean;
}): Promise<IMultiSqlResult[]> {
const replaced = this.replaceTags(sqlText, replace);
Expand All @@ -33,22 +38,30 @@ export class SnowflakeMultiSql extends Snowflake {
let totalDuration: number = 0;
for (let _i = 0; _i < chunks.length; _i++) {
if (chunks[_i].trim().length > 0) {
const startTime: number = new Date().valueOf();
const data = await this.execute(chunks[_i]);
const duration = new Date().valueOf() - startTime;
totalDuration += duration;
if (preview) {
results.push({
chunkText: chunks[_i],
chunkOrder: _i + 1,
chunksTotal,
});
} else {
const startTime: number = new Date().valueOf();
const data = await this.execute(chunks[_i], binds);
const duration = new Date().valueOf() - startTime;
totalDuration += duration;

const topush: IMultiSqlResult = {
chunkText: chunks[_i],
chunkOrder: _i + 1,
chunksTotal,
duration,
totalDuration,
};
if (includeResults) {
topush.data = data;
const topush: IMultiSqlResult = {
chunkText: chunks[_i],
chunkOrder: _i + 1,
chunksTotal,
duration,
totalDuration,
};
if (includeResults) {
topush.data = data;
}
results.push(topush);
}
results.push(topush);
}
}
return results;
Expand Down

0 comments on commit d096b24

Please sign in to comment.