From d3dc58f92271d04961b9bf8e97a20e87af2ffc7f Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Tue, 29 Jan 2019 01:11:52 +0800 Subject: [PATCH] Added type definitions for node-sql-parser --- types/node-sql-parser/index.d.ts | 97 +++++++++++++++++++ .../node-sql-parser/node-sql-parser-tests.ts | 19 ++++ types/node-sql-parser/tsconfig.json | 25 +++++ types/node-sql-parser/tslint.json | 1 + 4 files changed, 142 insertions(+) create mode 100644 types/node-sql-parser/index.d.ts create mode 100644 types/node-sql-parser/node-sql-parser-tests.ts create mode 100644 types/node-sql-parser/tsconfig.json create mode 100644 types/node-sql-parser/tslint.json diff --git a/types/node-sql-parser/index.d.ts b/types/node-sql-parser/index.d.ts new file mode 100644 index 0000000000..c711212c63 --- /dev/null +++ b/types/node-sql-parser/index.d.ts @@ -0,0 +1,97 @@ +// Type definitions for node-sql-parser 1.0 +// Project: https://github.com/taozhi8833998/node-sql-parser#readme +// Definitions by: taozhi8833998 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +export interface With { + name: string; + stmt: any[]; + columns?: any[]; +} +export type WhilteListCheckMode = 'table' | 'column'; +export interface TableColumnAst { + tableList: string[]; + columnsList: string[]; + ast: AST[] | AST; +} +export interface From { + db: string | null; + table: string; + as: string | null; +} +export interface Dual { + type: 'dual'; +} +export interface Limit { + type: string; + value: number; +} +export interface OrderBy { + type: 'ASC' | 'DESC'; + expr: any; +} +export interface ColumnRef { + type: 'column_ref'; + table: string | null; + column: string; +} +export interface SetList { + column: string; + value: any; + table: string | null; +} +export interface InsertReplaceValue { + type: 'expr_list'; + value: any[]; +} +export interface Select { + with: With | null; + type: 'select'; + options: any[] | null; + distinct: 'DISTINCT' | null; + columns: any[] | '*'; + from: Array | null; + where: any; + groupby: ColumnRef[] | null; + having: any[] | null; + orderby: OrderBy[] | null; + limit: Limit[] | null; +} +export interface Insert_Replace { + type: 'replace' | 'insert'; + db: string | null; + table: string; + columns: string[] | null; + values: InsertReplaceValue[]; +} +export interface Update { + type: 'udpate'; + db: string | null; + table: string; + set: SetList[]; + where: any; +} +export interface Delete { + type: 'delete'; + tables: any; + from: Array; + where: any; +} +export type AST = Select | Insert_Replace | Update | Delete; + +export class Parser { + constructor(); + + parse(sql: string): TableColumnAst; + + astify(sql: string): AST[] | AST; + + sqlify(ast: AST[] | AST): string; + + whiteListCheck(sql: string, whiteList: string[], type?: WhilteListCheckMode): Error | undefined; + + tableList(sql: string): string[]; + + columnList(sql: string): string[]; +} diff --git a/types/node-sql-parser/node-sql-parser-tests.ts b/types/node-sql-parser/node-sql-parser-tests.ts new file mode 100644 index 0000000000..050c69a855 --- /dev/null +++ b/types/node-sql-parser/node-sql-parser-tests.ts @@ -0,0 +1,19 @@ +import { Parser } from 'node-sql-parser'; + +const parser = new Parser(); +const sql = 'select id from tableA'; + +// $ExpectType Select | Insert_Replace | Update | Delete | AST[] +const selectAst = parser.astify(sql); + +// $ExpectType string +parser.sqlify(selectAst); + +// $ExpectType string[] +parser.tableList(sql); + +// $ExpectType string[] +parser.columnList(sql); + +// $ExpectType Error | undefined +parser.whiteListCheck(sql, ['select::null::tableA'], 'table'); diff --git a/types/node-sql-parser/tsconfig.json b/types/node-sql-parser/tsconfig.json new file mode 100644 index 0000000000..c7f24afb95 --- /dev/null +++ b/types/node-sql-parser/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-sql-parser-tests.ts" + ] +} diff --git a/types/node-sql-parser/tslint.json b/types/node-sql-parser/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/node-sql-parser/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }