11# Copyright Thomas T. Jarløv (TTJ) - ttj@ttj.dk
22
3+ when NimMajor >= 2 :
4+ import db_connector/ db_common
5+ else :
6+ import std/ db_common
7+
8+
9+ import
10+ std/ strutils
11+
12+
13+ proc querycompare * (a, b: SqlQuery ): bool =
14+ var
15+ a1: seq [string ]
16+ b1: seq [string ]
17+ for c in splitWhitespace (string (a)):
18+ a1.add ($ c)
19+ for c in splitWhitespace (string (b)):
20+ b1.add ($ c)
21+
22+ if a1 != b1:
23+ echo " "
24+ echo " a1: " , string (a)
25+ echo " b1: " , string (b).replace (" \n " , " " ).splitWhitespace ().join (" " )
26+ echo " "
27+
28+ return a1 == b1
329
430
531proc dbQuotePrivate * (s: string ): string =
@@ -10,4 +36,106 @@ proc dbQuotePrivate*(s: string): string =
1036 of '\' ' : add (result , " ''" )
1137 of '\0 ' : add (result , " \\ 0" )
1238 else : add (result , c)
13- add (result , '\' ' )
39+ add (result , '\' ' )
40+
41+
42+
43+ proc formatWhereParams * (v: string ): string =
44+ # # Format the WHERE part of the query.
45+ let
46+ field = v.strip ()
47+
48+ var fieldSplit: seq [string ]
49+ if field.contains (" " ):
50+ if field.contains (" =" ):
51+ fieldSplit = field.split (" =" )
52+ elif field.contains (" IS NOT" ):
53+ fieldSplit = field.split (" IS NOT" )
54+ elif field.contains (" IS" ):
55+ fieldSplit = field.split (" IS" )
56+ elif field.contains (" NOT IN" ):
57+ fieldSplit = field.split (" NOT IN" )
58+ elif field.contains (" IN" ):
59+ fieldSplit = field.split (" IN" )
60+ elif field.contains (" !=" ):
61+ fieldSplit = field.split (" !=" )
62+ elif field.contains (" <=" ):
63+ fieldSplit = field.split (" <=" )
64+ elif field.contains (" >=" ):
65+ fieldSplit = field.split (" >=" )
66+ elif field.contains (" <" ):
67+ fieldSplit = field.split (" <" )
68+ elif field.contains (" >" ):
69+ fieldSplit = field.split (" >" )
70+ else :
71+ fieldSplit = field.split (" =" )
72+
73+ #
74+ # Does the data have a `=` sign?
75+ #
76+ if fieldSplit.len () == 2 :
77+ #
78+ # If the data is only having equal but no value, insert a `?` sign
79+ #
80+ if fieldSplit[1 ] == " " :
81+ return (field & " ?" )
82+ #
83+ # Otherwise just add the data as is, eg. `field = value`
84+ #
85+ else :
86+ return (field)
87+
88+ #
89+ # Otherwise revert to default
90+ #
91+ else :
92+ return (field & " = ?" )
93+
94+
95+
96+ proc hasIllegalFormats * (query: string ): string =
97+ const illegalFormats = [
98+ " WHERE AND" ,
99+ " WHERE OR" ,
100+ " AND AND" ,
101+ " OR OR" ,
102+ " AND OR" ,
103+ " OR AND" ,
104+ " WHERE IN" ,
105+ " WHERE =" ,
106+ " WHERE >" ,
107+ " WHERE <" ,
108+ " WHERE !" ,
109+ " WHERE LIKE" ,
110+ " WHERE NOT" ,
111+ " WHERE IS" ,
112+ " WHERE NULL" ,
113+ " WHERE ANY"
114+ ]
115+
116+ for illegalFormat in illegalFormats:
117+ if illegalFormat in query:
118+ return illegalFormat
119+
120+
121+ #
122+ # Parentheses check
123+ #
124+ let
125+ parentheseOpen = count (query, " (" )
126+ parentheseClose = count (query, " )" )
127+
128+ if parentheseOpen > parentheseClose:
129+ return " parentheses does not match. Missing closing parentheses. (" & $ parentheseOpen & " open, " & $ parentheseClose & " close)"
130+ elif parentheseOpen < parentheseClose:
131+ return " parentheses does not match. Missing opening parentheses. (" & $ parentheseOpen & " open, " & $ parentheseClose & " close)"
132+
133+
134+ #
135+ # Check for double insert
136+ #
137+ let noSpaces = query.strip ().replace (" " , " " )
138+
139+ if " ??" in noSpaces:
140+ return " double insert detected. (??)"
141+
0 commit comments