1+ import { Utils } from "@react-awesome-query-builder/ui" ;
2+ import { expect } from "chai" ;
3+ import {
4+ Parser as NodeSqlParser , Option as SqlParseOption , AST ,
5+ } from "node-sql-parser" ;
6+
7+
8+ describe ( "ExportUtils.SqlString" , ( ) => {
9+ const sqlParser = new NodeSqlParser ( ) ;
10+ const parseString = ( escapedString ) => {
11+ return sqlParser . parse ( "select * from a where b like " + escapedString ) . ast . where . right . value ;
12+ } ;
13+ const extractFromLike = ( escapedString ) => {
14+ const likeSrting = parseString ( escapedString ) ;
15+ if ( likeSrting . startsWith ( "%" ) && likeSrting . endsWith ( "%" ) ) {
16+ return likeSrting . substring ( 1 , likeSrting . length - 1 ) ;
17+ } else if ( likeSrting . startsWith ( "%" ) ) {
18+ return likeSrting . substring ( 1 ) ;
19+ } else if ( likeSrting . endsWith ( "%" ) ) {
20+ return likeSrting . substring ( 0 , likeSrting . length - 1 ) ;
21+ }
22+ } ;
23+
24+ describe ( "escapeLike + unescapeLike" , ( ) => {
25+ const testString = " _ % \\ ' \" " ; // _ % \\ ' \"
26+ it ( "no sqlDialect" , ( ) => {
27+ const a = " ' \" " ;
28+ console . log ( 1 , a )
29+ console . log ( 2 , Utils . ExportUtils . SqlString . escape ( a ) )
30+ console . log ( 3 , parseString ( Utils . ExportUtils . SqlString . escape ( a ) ) )
31+ const escapedString = "'% \\\\_ \\\\% \\\\\\\\ \\' \\\" %'" ; // '% \\_ \\% \\\\ \' \" %'
32+ expect ( Utils . ExportUtils . SqlString . escapeLike ( testString ) , "escapeLike" ) . to . eql ( escapedString ) ;
33+ expect ( Utils . ExportUtils . SqlString . unescapeLike ( extractFromLike ( escapedString ) ) , "unescapeLike" ) . to . eql ( testString ) ;
34+ } ) ;
35+ // it("sqlDialect == MySQL", () => {
36+ // const escapedString = "'% \\_ \\% \\\\\\\\ \\' \\\" %'"; // '% \_ \% \\\\ \' \" %'
37+ // expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "MySQL"), "escapeLike").to.eql(escapedString);
38+ // expect(Utils.ExportUtils.SqlString.unescapeLike(eval(escapedString), "MySQL"), "unescapeLike").to.eql(testString);
39+ // });
40+ // it("sqlDialect == BigQuery", () => {
41+ // const escapedString = "'% \\\\_ \\\\% \\\\\\ \\' \\\" %'"; // '% \\_ \\% \\\ \' \" %'
42+ // expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "BigQuery"), "escapeLike").to.eql(escapedString);
43+ // expect(Utils.ExportUtils.SqlString.unescapeLike(escapedString, "BigQuery"), "unescapeLike").to.eql(testString);
44+ // });
45+ // it("sqlDialect == PostgreSQL", () => {
46+ // const escapedString = "?"; // todo
47+ // expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "PostgreSQL"), "escapeLike").to.eql(escapedString);
48+ // expect(Utils.ExportUtils.SqlString.unescapeLike(escapedString, "PostgreSQL"), "unescapeLike").to.eql(testString);
49+ // });
50+ // it("sqlDialect == XX", () => {
51+ // const escapedString = "'% \\\\_ \\\\% \\\\\\\\ \\' \\\" %'"; // '% \\_ \\% \\\\ \' \" %'
52+ // expect(Utils.ExportUtils.SqlString.escapeLike(testString, true, true, "XX"), "escapeLike").to.eql(escapedString);
53+ // expect(Utils.ExportUtils.SqlString.unescapeLike(escapedString, "XX"), "unescapeLike").to.eql(testString);
54+ // });
55+ } ) ;
56+ } ) ;
0 commit comments