-
-
Save Aracturat/a85625e91b82f261a87b05a5eaa09b57 to your computer and use it in GitHub Desktop.
Function to SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { debug } from 'util'; | |
import * as ts from 'typescript'; | |
function getArrowFunction(func: Function): ts.ArrowFunction { | |
let src = ts.createSourceFile("test.ts", func.toString(), ts.ScriptTarget.ES2016, false); | |
var expr = <ts.ExpressionStatement> src.statements[0]; | |
return <ts.ArrowFunction> expr.expression; | |
} | |
function toSql(expr: ts.Node): string { | |
switch(expr.kind) { | |
case ts.SyntaxKind.PropertyAccessExpression: | |
var paExpr = <ts.PropertyAccessExpression> expr; | |
var idObject = <ts.Identifier> paExpr.expression; | |
return idObject.text + "." + paExpr.name.text; | |
case ts.SyntaxKind.ParenthesizedExpression: | |
var parExpr = <ts.ParenthesizedExpression> expr; | |
return "(" + toSql(parExpr.expression) + ")"; | |
case ts.SyntaxKind.BinaryExpression: | |
var bExpr = <ts.BinaryExpression> expr; | |
var op = ""; | |
switch (bExpr.operatorToken.kind) { | |
case ts.SyntaxKind.EqualsEqualsToken: | |
op = "="; | |
break; | |
case ts.SyntaxKind.GreaterThanToken: | |
op = ">"; | |
break; | |
case ts.SyntaxKind.GreaterThanEqualsToken: | |
op = ">="; | |
break; | |
case ts.SyntaxKind.LessThanToken: | |
op = "<"; | |
break; | |
case ts.SyntaxKind.LessThanEqualsToken: | |
op = "<="; | |
break; | |
case ts.SyntaxKind.AmpersandAmpersandToken: | |
op = "and"; | |
break; | |
case ts.SyntaxKind.BarBarToken: | |
op = "or"; | |
break; | |
default: | |
op = "[UndefinedOperator]"; | |
} | |
return toSql(bExpr.left) + " " + op + " " + toSql(bExpr.right); | |
case ts.SyntaxKind.NumericLiteral: | |
var nlExpr = <ts.NumericLiteral> expr; | |
return nlExpr.text; | |
case ts.SyntaxKind.StringLiteral: | |
var slExpr = <ts.StringLiteral> expr; | |
return "'" + slExpr.text + "'"; | |
case ts.SyntaxKind.CallExpression: | |
var cExpr = <ts.CallExpression> expr; | |
if (cExpr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) { | |
var cpaExpr = <ts.PropertyAccessExpression> cExpr.expression; | |
var funcName = ""; | |
var args = ""; | |
switch (cpaExpr.name.text) { | |
case "toUpperCase": | |
funcName = "UPPER"; | |
break; | |
case "toLowerCase": | |
funcName = "LOWER"; | |
break; | |
case "substring": | |
funcName = "SUBSTRING" | |
args = ", " + cExpr.arguments.map(e => toSql(e)).join(", "); | |
break; | |
default: | |
funcName = "[UndefinedFunction]"; | |
break; | |
} | |
return funcName + "(" + toSql(cpaExpr.expression) + args + ")"; | |
} | |
default: | |
return "[undefined]"; | |
} | |
} | |
var arrFunc = getArrowFunction(e => (e.Param.toUpperCase().toLowerCase() == "3".toLowerCase().substring(0, 10) || e.Param2 >= 4) && e.Param3 <= 2); | |
console.log(toSql(arrFunc.body)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment