34
34
/**
35
35
* Executes a statement on a database and optionally stores the result in a variable. Expressions
36
36
* embedded in the query will be escaped to avoid SQL injection.
37
- *
37
+ * <p>
38
38
* If a single variable, such as `{test}`, is passed, the variable will be set to the number of
39
39
* affected rows.
40
- *
40
+ * <p>
41
41
* If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list
42
42
* variable in the form `{test::<column name>::<row number>}`
43
43
*
@@ -145,16 +145,41 @@ private PreparedStatement createStatement(Event e, Connection conn) throws SQLEx
145
145
StringBuilder sb = new StringBuilder ();
146
146
List <Object > parameters = new ArrayList <>();
147
147
Object [] objects = SkriptUtil .getTemplateString (((VariableString ) query ));
148
- for (Object o : objects ) {
148
+ for (int i = 0 ; i < objects .length ; i ++) {
149
+ Object o = objects [i ];
149
150
if (o instanceof String ) {
150
151
sb .append (o );
151
152
} else {
152
153
Expression <?> expr = SkriptUtil .getExpressionFromInfo (o );
154
+
155
+ String before = getString (objects , i - 1 );
156
+ String after = getString (objects , i + 1 );
157
+ boolean standaloneString = false ;
158
+
159
+ if (before != null && after != null ) {
160
+ if (before .endsWith ("'" ) && after .endsWith ("'" )) {
161
+ standaloneString = true ;
162
+ }
163
+ }
164
+
165
+ Object expressionValue = expr .getSingle (e );
166
+
153
167
if (expr instanceof ExprUnsafe ) {
154
- sb .append (expr .getSingle (e ));
168
+ sb .append (expressionValue );
169
+
170
+ if (standaloneString && expressionValue instanceof String ) {
171
+ String rawExpression = ((ExprUnsafe ) expr ).getRawExpression ();
172
+ Skript .warning (
173
+ String .format ("Unsafe may have been used unnecessarily. Try replacing 'unsafe %1$s' with %1$s" ,
174
+ rawExpression ));
175
+ }
155
176
} else {
156
- parameters .add (expr . getSingle ( e ) );
177
+ parameters .add (expressionValue );
157
178
sb .append ('?' );
179
+
180
+ if (standaloneString ) {
181
+ Skript .warning ("Do not surround expressions with quotes!" );
182
+ }
158
183
}
159
184
}
160
185
}
@@ -168,6 +193,20 @@ private PreparedStatement createStatement(Event e, Connection conn) throws SQLEx
168
193
return stmt ;
169
194
}
170
195
196
+ private String getString (Object [] objects , int index ) {
197
+ if (index < 0 || index >= objects .length ) {
198
+ return null ;
199
+ }
200
+
201
+ Object object = objects [index ];
202
+
203
+ if (object instanceof String ) {
204
+ return (String ) object ;
205
+ }
206
+
207
+ return null ;
208
+ }
209
+
171
210
private void setVariable (Event e , String name , Object obj ) {
172
211
Variables .setVariable (name .toLowerCase (Locale .ENGLISH ), obj , e , isLocal );
173
212
}
0 commit comments