Skip to content

Commit 6db86b7

Browse files
author
lj
committed
Portable Version of z.Data
0 parents  commit 6db86b7

18 files changed

+2532
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.vs
2+
/bin
3+
/obj

Converters/SqlToJs.cs

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
using System.Threading.Tasks;
7+
8+
namespace z.Data.Converters
9+
{
10+
/// <summary>
11+
/// @LJ20170915
12+
/// Intended for InSys Base Paradigm
13+
/// </summary>
14+
public class SqlToJs
15+
{
16+
private tmps nTmp;
17+
18+
public string Translate(string SQL)
19+
{
20+
if (SQL == null) return "NULL";
21+
22+
nTmp = new tmps();
23+
24+
var g = Match(SQL, @"(\$)?\b(?<!\$\.|@|\:|''|\~)([^\d|,]\w+)\b").Where(x =>
25+
{
26+
var gg = true;
27+
foreach (var sd in new string[] { "OR", "AND", "NOT", "IN", "IS", "NULL" })
28+
if (x.ToUpper().Trim() == sd) gg = false;
29+
return gg;
30+
}).OrderByDescending(x => x);
31+
32+
33+
foreach (string gg in g)
34+
nTmp.Add(gg.Trim(), gg.Replace("$", "").Replace(" ", ""));
35+
36+
37+
foreach (var h in nTmp)
38+
{
39+
h.matchtext = "$." + h.matchtext.Replace("$", "");
40+
}
41+
42+
43+
// include OR and AND
44+
nTmp.Add("OR", "||");
45+
nTmp.Add("AND", "&&");
46+
47+
//replace
48+
foreach (var t in nTmp)
49+
{
50+
if (t.NParam.Substring(0, 1) != "$")
51+
SQL = Replace(SQL, @"\b(?<!\$|\$\.|@|\:|'')" + t.NParam + @"\b", t.matchtext);
52+
else
53+
SQL = Replace(SQL, @"\" + t.NParam, t.matchtext);
54+
}
55+
56+
SQL = SQL.Replace("=", "==").Replace("<>", "!==");
57+
SQL = Replace(SQL, "Is( )+NULL", "== null");
58+
SQL = Replace(SQL, "Is( )+not( )+NULL", "!= null");
59+
60+
//get num collection
61+
nTmp.Clear();
62+
63+
var g2 = Match(SQL, @"([^\&\| ]\w*( )+)+([NOT( )+]?)(IN( )+\(([^()]+)\))");
64+
foreach (string g22 in g2)
65+
{
66+
var hh = g22;
67+
hh = hh.Substring(0, 1).TrimStart() == "." ? "$" + g22.TrimStart() : g22.TrimStart();
68+
nTmp.Add(g22, hh);
69+
}
70+
71+
foreach (var t in nTmp)
72+
{
73+
var a = Match(t.matchtext, @"\([^(]+\)");
74+
var n = Match(t.matchtext, @"(\S\.|\@|\:|\$)\b\w+\b");
75+
76+
foreach (var aa in a)
77+
{
78+
var fr = Replace("[@List].indexOf(@Param) @Compare -1", "@List", aa.Replace("(", "").Replace(")", ""));
79+
foreach (var nn in n)
80+
{
81+
fr = Replace(fr, "@Param", nn);
82+
t.matchtext = Replace(fr, "@Compare", t.NParam.Contains("not") ? "==" : "!=");
83+
}
84+
}
85+
}
86+
87+
// group by 1 or 0
88+
89+
var j = Match(SQL, @"(\$.|:|@|\~)\b\w+\b( )+(==|\!=|\<|\>)( )+\b(0|1)\b");
90+
foreach (var jj in j)
91+
{
92+
var n = Match(jj, @"(\S\.|\@|\:|\$|\~)\b\w+\b");
93+
foreach (var nn in n)
94+
{
95+
var BText = "(" + nn + " @Compare " + (jj.Contains("1") ? "1" : "0") + " || " + nn + " @Compare " + (jj.Contains("1") ? "true" : "false") + ")";
96+
97+
string BComp = "";
98+
foreach (var jjj in new string[] { "==", "!=", "<", ">" })
99+
{
100+
if (jj.Contains(jjj))
101+
BComp = jjj;
102+
}
103+
104+
nTmp.Add(jj, BText.Replace("@Compare", BComp));
105+
}
106+
}
107+
108+
// replace
109+
110+
foreach (var h in nTmp)
111+
{
112+
SQL = SQL.Replace(h.NParam, h.matchtext);
113+
}
114+
115+
SQL = SQL.Replace(@"$[", "[");
116+
117+
return SQL;
118+
}
119+
120+
public static string Convert(string SQL) => new SqlToJs().Translate(SQL);
121+
122+
protected IEnumerable<string> Match(string Command, string Pattern)
123+
{
124+
return Regex.Matches(Command.ToString(), Pattern.ToString()).Cast<Match>().Select(x => x.Value);
125+
}
126+
127+
protected string Replace(string Command, string Pattern, string Value)
128+
{
129+
return Regex.Replace(Command, Pattern, Value, RegexOptions.IgnoreCase);
130+
}
131+
132+
private class tmp
133+
{
134+
public int ID;
135+
public string NParam;
136+
public string matchtext;
137+
}
138+
139+
private class tmps : List<tmp>
140+
{
141+
public void Add(string nParam, string matchText)
142+
{
143+
base.Add(new tmp
144+
{
145+
ID = base.Count + 1,
146+
NParam = nParam,
147+
matchtext = matchText
148+
});
149+
}
150+
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)