Skip to content

Commit 2efcbde

Browse files
committed
Fixed bug assembling simple "IN" statement with string fields in the getWhereStatement() method in ServiceRequest
1 parent ea67015 commit 2efcbde

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/javaxt/express/ServiceRequest.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ public ServiceRequest(String service, javaxt.http.servlet.HttpServletRequest req
101101
try{
102102
LinkedHashMap<String, List<String>> params =
103103
javaxt.utils.URL.parseQueryString(new String(b, "UTF-8"));
104-
Iterator<String> it = params.keySet().iterator();
105-
while (it.hasNext()){
106-
String key = it.next();
104+
for (String key : params.keySet()) {
107105
List<String> values = params.get(key);
108106
List<String> currValues = this.parameters.get(key);
109107
if (currValues==null){
@@ -935,9 +933,7 @@ public Filter getFilter(){
935933
}
936934
else{
937935
LinkedHashMap<String, List<String>> map = javaxt.utils.URL.parseQueryString(str);
938-
Iterator<String> it = map.keySet().iterator();
939-
while (it.hasNext()){
940-
String key = it.next();
936+
for (String key : map.keySet()){
941937
List<String> vals = map.get(key);
942938
if (!vals.isEmpty()) params.put(key, new javaxt.utils.Value(vals.get(0)));
943939
}
@@ -953,9 +949,8 @@ public Filter getFilter(){
953949
//Create filter
954950
filter = new Filter();
955951
HashSet<String> reservedKeywords = getKeywords();
956-
Iterator<String> it = params.keySet().iterator();
957-
while (it.hasNext()){
958-
String key = it.next().trim();
952+
for (String key : params.keySet()){
953+
key = key.trim();
959954
if (key.isEmpty()) continue;
960955
if (key.equals("_")) continue;
961956
if (reservedKeywords.contains(key.toLowerCase())) continue;
@@ -1509,9 +1504,7 @@ public boolean isEmpty(){
15091504
*/
15101505
public Item[] getItems(){
15111506
ArrayList<Item> arr = new ArrayList<>();
1512-
Iterator<String> it = items.keySet().iterator();
1513-
while (it.hasNext()){
1514-
String key = it.next();
1507+
for (String key : items.keySet()){
15151508
for (Item item : items.get(key)){
15161509
arr.add(item);
15171510
}
@@ -1704,9 +1697,7 @@ private String[] getSelectStatements(Field[] fields, HashMap<String, Object> tab
17041697
String col = field.getColumn();
17051698

17061699
HashMap<String, String> fieldMap = (HashMap<String, String>) tablesAndFields.get("fieldMap");
1707-
Iterator<String> it = fieldMap.keySet().iterator();
1708-
while (it.hasNext()){
1709-
String fieldName = it.next();
1700+
for (String fieldName : fieldMap.keySet()){
17101701
String columnName = fieldMap.get(fieldName);
17111702
if (col.equalsIgnoreCase(fieldName) || col.equalsIgnoreCase(columnName)){
17121703
tableName = (String) tablesAndFields.get("tableName");
@@ -1788,10 +1779,8 @@ private String getWhereStatement(ArrayList<HashMap<String, Object>> tablesAndFie
17881779

17891780

17901781
ArrayList<String> a2 = new ArrayList<>();
1791-
Iterator<String> it = filter.items.keySet().iterator();
1792-
while (it.hasNext()){
1782+
for (String key : filter.items.keySet()){
17931783
ArrayList<String> arr = new ArrayList<>();
1794-
String key = it.next();
17951784
for (Filter.Item item : filter.items.get(key)){
17961785
String name = item.getField();
17971786
String op = item.getOperation();
@@ -1842,18 +1831,31 @@ private String getWhereStatement(ArrayList<HashMap<String, Object>> tablesAndFie
18421831
HashSet<String> arrayFields = (HashSet<String>) map.get("arrayFields");
18431832

18441833

1845-
Iterator<String> i2 = fieldMap.keySet().iterator();
1846-
while (i2.hasNext()){
1847-
String fieldName = i2.next();
1834+
for (String fieldName : fieldMap.keySet()){
18481835
String columnName = fieldMap.get(fieldName);
18491836
if (name.equalsIgnoreCase(fieldName) || name.equalsIgnoreCase(columnName)){
18501837
foundField = true;
18511838

1852-
//Wrap value is single quote as needed
1839+
//Wrap value(s) in single quote as needed
18531840
if (v!=null && stringFields.contains(fieldName)){
18541841
if (!(v.startsWith("'") && v.endsWith("'"))){
18551842
if (op.equals("IN")){
1856-
//TODO: split by commas and add quotes
1843+
1844+
if (v.startsWith("(") && v.endsWith(")")){
1845+
v = v.substring(1, v.length()-1);
1846+
}
1847+
StringBuilder str = new StringBuilder("(");
1848+
String[] a = v.split(","); //very weak!
1849+
for (int i=0; i<a.length; i++){
1850+
String s = a[i];
1851+
if (i>0) str.append(",");
1852+
if (!(s.startsWith("'") && s.endsWith("'"))){
1853+
s = "'" + s.replace("'","''") + "'";
1854+
}
1855+
str.append(s);
1856+
}
1857+
str.append(")");
1858+
v = str.toString();
18571859
}
18581860
else{
18591861
v = "'" + v.replace("'","''") + "'";
@@ -1910,10 +1912,13 @@ else if (op.equals("IN")){
19101912

19111913
break;
19121914
}
1915+
1916+
if (foundField) break;
19131917
}
1914-
if (foundField) break;
1918+
1919+
//console.log(foundField, name, tableName);
19151920
}
1916-
//console.log(foundField, name, tableName);
1921+
19171922
}
19181923
}
19191924

@@ -1964,7 +1969,7 @@ public String getOrderByStatement(){
19641969
if (!sort.isEmpty()){
19651970
StringBuilder sql = new StringBuilder();
19661971
sql.append(" order by ");
1967-
java.util.Iterator<String> it = sort.getKeySet().iterator();
1972+
Iterator<String> it = sort.getKeySet().iterator();
19681973
while (it.hasNext()){
19691974
String colName = it.next();
19701975
String direction = sort.get(colName);

0 commit comments

Comments
 (0)