@Test public void testReturnColWithInvalidQuery() { DBListClass dblc = new DBListClass(); assertEquals("-", dblc.returnCol("do something", true)); assertEquals("-", dblc.returnCol("do something", false)); }
public String returnCol(String hqlQuery, boolean first) { String firstCol = "-", secondCol = "-"; if (StringUtils.isEmpty(hqlQuery)) { return firstCol; } int fromIndx = hqlQuery.toLowerCase().indexOf("from"); if (fromIndx > 0) { String beforeFrom = hqlQuery.substring(0, fromIndx).replaceAll("\\s+", " "); int commaIndex = beforeFrom.indexOf(","); if (commaIndex > 0) { StringTokenizer st = new StringTokenizer(beforeFrom, " ,()", true); ArrayList<String> words = new ArrayList<>(); while (st.hasMoreTokens()) { words.add(st.nextToken()); } int comma = words.indexOf(",") - 1; while (words.get(comma).compareTo(" ") == 0) { comma--; } firstCol = words.get(comma).trim(); comma = words.indexOf(",") + 1; while (words.get(comma).compareTo(" ") == 0) { comma++; } if (words.get(comma).compareTo("(") == 0) { int i = comma + 1; while (words.get(i).compareTo(")") != 0) { secondCol += words.get(i); i++; } secondCol += ")"; } else { secondCol = words.get(comma).trim(); } } else { firstCol = StringUtils.substringAfterLast(beforeFrom.trim(), " "); } } if (first == true) { return firstCol; } else { return secondCol; } }
DBListClass extends ListClass { public String returnCol(String hqlQuery, boolean first) { String firstCol = "-", secondCol = "-"; if (StringUtils.isEmpty(hqlQuery)) { return firstCol; } int fromIndx = hqlQuery.toLowerCase().indexOf("from"); if (fromIndx > 0) { String beforeFrom = hqlQuery.substring(0, fromIndx).replaceAll("\\s+", " "); int commaIndex = beforeFrom.indexOf(","); if (commaIndex > 0) { StringTokenizer st = new StringTokenizer(beforeFrom, " ,()", true); ArrayList<String> words = new ArrayList<>(); while (st.hasMoreTokens()) { words.add(st.nextToken()); } int comma = words.indexOf(",") - 1; while (words.get(comma).compareTo(" ") == 0) { comma--; } firstCol = words.get(comma).trim(); comma = words.indexOf(",") + 1; while (words.get(comma).compareTo(" ") == 0) { comma++; } if (words.get(comma).compareTo("(") == 0) { int i = comma + 1; while (words.get(i).compareTo(")") != 0) { secondCol += words.get(i); i++; } secondCol += ")"; } else { secondCol = words.get(comma).trim(); } } else { firstCol = StringUtils.substringAfterLast(beforeFrom.trim(), " "); } } if (first == true) { return firstCol; } else { return secondCol; } } }
DBListClass extends ListClass { public String returnCol(String hqlQuery, boolean first) { String firstCol = "-", secondCol = "-"; if (StringUtils.isEmpty(hqlQuery)) { return firstCol; } int fromIndx = hqlQuery.toLowerCase().indexOf("from"); if (fromIndx > 0) { String beforeFrom = hqlQuery.substring(0, fromIndx).replaceAll("\\s+", " "); int commaIndex = beforeFrom.indexOf(","); if (commaIndex > 0) { StringTokenizer st = new StringTokenizer(beforeFrom, " ,()", true); ArrayList<String> words = new ArrayList<>(); while (st.hasMoreTokens()) { words.add(st.nextToken()); } int comma = words.indexOf(",") - 1; while (words.get(comma).compareTo(" ") == 0) { comma--; } firstCol = words.get(comma).trim(); comma = words.indexOf(",") + 1; while (words.get(comma).compareTo(" ") == 0) { comma++; } if (words.get(comma).compareTo("(") == 0) { int i = comma + 1; while (words.get(i).compareTo(")") != 0) { secondCol += words.get(i); i++; } secondCol += ")"; } else { secondCol = words.get(comma).trim(); } } else { firstCol = StringUtils.substringAfterLast(beforeFrom.trim(), " "); } } if (first == true) { return firstCol; } else { return secondCol; } } DBListClass(String name, String prettyname, PropertyMetaClass wclass);  DBListClass(PropertyMetaClass wclass);  DBListClass(); }
DBListClass extends ListClass { public String returnCol(String hqlQuery, boolean first) { String firstCol = "-", secondCol = "-"; if (StringUtils.isEmpty(hqlQuery)) { return firstCol; } int fromIndx = hqlQuery.toLowerCase().indexOf("from"); if (fromIndx > 0) { String beforeFrom = hqlQuery.substring(0, fromIndx).replaceAll("\\s+", " "); int commaIndex = beforeFrom.indexOf(","); if (commaIndex > 0) { StringTokenizer st = new StringTokenizer(beforeFrom, " ,()", true); ArrayList<String> words = new ArrayList<>(); while (st.hasMoreTokens()) { words.add(st.nextToken()); } int comma = words.indexOf(",") - 1; while (words.get(comma).compareTo(" ") == 0) { comma--; } firstCol = words.get(comma).trim(); comma = words.indexOf(",") + 1; while (words.get(comma).compareTo(" ") == 0) { comma++; } if (words.get(comma).compareTo("(") == 0) { int i = comma + 1; while (words.get(i).compareTo(")") != 0) { secondCol += words.get(i); i++; } secondCol += ")"; } else { secondCol = words.get(comma).trim(); } } else { firstCol = StringUtils.substringAfterLast(beforeFrom.trim(), " "); } } if (first == true) { return firstCol; } else { return secondCol; } } DBListClass(String name, String prettyname, PropertyMetaClass wclass);  DBListClass(PropertyMetaClass wclass);  DBListClass(); List<ListItem> makeList(List<Object> list); List<ListItem> getDBList(XWikiContext context); @Override List<String> getList(XWikiContext context); @Override Map<String, ListItem> getMap(XWikiContext context); @Deprecated String getQuery(XWikiContext context); String getSql(); void setSql(String sql); String getClassname(); void setClassname(String classname); String getIdField(); void setIdField(String idField); String getValueField(); void setValueField(String valueField); List<ListItem> getCachedDBList(XWikiContext context); void setCachedDBList(List<ListItem> cachedDBList, XWikiContext context); @Override void flushCache(); String returnCol(String hqlQuery, boolean first); String getValue(String val, String sql, XWikiContext context); @Override void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); @Override void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); }
DBListClass extends ListClass { public String returnCol(String hqlQuery, boolean first) { String firstCol = "-", secondCol = "-"; if (StringUtils.isEmpty(hqlQuery)) { return firstCol; } int fromIndx = hqlQuery.toLowerCase().indexOf("from"); if (fromIndx > 0) { String beforeFrom = hqlQuery.substring(0, fromIndx).replaceAll("\\s+", " "); int commaIndex = beforeFrom.indexOf(","); if (commaIndex > 0) { StringTokenizer st = new StringTokenizer(beforeFrom, " ,()", true); ArrayList<String> words = new ArrayList<>(); while (st.hasMoreTokens()) { words.add(st.nextToken()); } int comma = words.indexOf(",") - 1; while (words.get(comma).compareTo(" ") == 0) { comma--; } firstCol = words.get(comma).trim(); comma = words.indexOf(",") + 1; while (words.get(comma).compareTo(" ") == 0) { comma++; } if (words.get(comma).compareTo("(") == 0) { int i = comma + 1; while (words.get(i).compareTo(")") != 0) { secondCol += words.get(i); i++; } secondCol += ")"; } else { secondCol = words.get(comma).trim(); } } else { firstCol = StringUtils.substringAfterLast(beforeFrom.trim(), " "); } } if (first == true) { return firstCol; } else { return secondCol; } } DBListClass(String name, String prettyname, PropertyMetaClass wclass);  DBListClass(PropertyMetaClass wclass);  DBListClass(); List<ListItem> makeList(List<Object> list); List<ListItem> getDBList(XWikiContext context); @Override List<String> getList(XWikiContext context); @Override Map<String, ListItem> getMap(XWikiContext context); @Deprecated String getQuery(XWikiContext context); String getSql(); void setSql(String sql); String getClassname(); void setClassname(String classname); String getIdField(); void setIdField(String idField); String getValueField(); void setValueField(String valueField); List<ListItem> getCachedDBList(XWikiContext context); void setCachedDBList(List<ListItem> cachedDBList, XWikiContext context); @Override void flushCache(); String returnCol(String hqlQuery, boolean first); String getValue(String val, String sql, XWikiContext context); @Override void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context); @Override void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context);  }