Permalink
Browse files

Added operator changing class OperatorResolver. Quote character is es…

…caped now.

Refactored some method names.
  • Loading branch information...
1 parent 70d2e79 commit 26fbe944927e63cd7442115a8707500a6ccbcb00 @sphynx committed Sep 30, 2010
@@ -1,10 +0,0 @@
-package com.os.sp.search;
-
-public class Escaper {
-
- public static String escape(String str) {
- // TODO backslash '
- return str;
- }
-
-}
@@ -1,6 +1,7 @@
package com.os.sp.search;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -47,4 +48,14 @@ public int hashCode() {
joins.addAll(joins2);
return new ArrayList<Join>(joins);
}
+
+ public static String toSql(List<Join> joinList) {
+ String res = "";
+ for (Iterator<Join> i = joinList.iterator(); i.hasNext(); ) {
+ Join j = i.next();
+ res += "join " + j.getSql();
+ res += "\n";
+ }
+ return res;
+ }
}
@@ -0,0 +1,29 @@
+package com.os.sp.search;
+
+public final class OperatorResolver {
+
+ public static String resolve(Operator op, String fieldName, String value) {
+
+ String tpl = op.getTemplate();
+ tpl = tpl.replace("$(F)", fieldName);
+ tpl = tpl.replace("$(V)", prepareValue(op, value));
+ return tpl;
+ }
+
+ private static String prepareValue(Operator op, String value) {
+ String res = value;
+
+ if (op.getType() == Type.STRING) {
+ if (op.getName().equals(Operator.STR_MATCHES)) {
+ // replace asterisk with %
+ res = res.replaceAll("\\*", "%");
+ }
+
+ // escape single quotes
+ res = res.replaceAll("'", "\\\\'");
+ }
+
+ return res;
+ }
+
+}
@@ -35,20 +35,16 @@ public Predicate(String fieldKey, String operatorKey, Object value) {
this.value = value;
}
@Override
- public String prettyPrint(String searchArea) {
+ public String toSql(String searchArea) {
Field f = SearchConfiguration.getField(searchArea, getFieldKey());
String clause = "(";
Operator op = SearchConfiguration.getOperator(getOperatorKey());
for (Iterator<String> i = f.getColumns().iterator(); i.hasNext(); ) {
- String c = i.next();
- String tpl = op.getTemplate();
- tpl = tpl.replace("$(F)", c);
- // TODO: add correct value-to-string transformer
- tpl = tpl.replace("$(V)", getValue().toString());
- clause += tpl;
- if (i.hasNext()) { // merge with `or`
+ String column = i.next();
+ clause += OperatorResolver.resolve(op, column, getValue().toString());
+ if (i.hasNext()) { // merge with `or` by default
clause += " or ";
}
}
@@ -57,7 +53,7 @@ public String prettyPrint(String searchArea) {
return clause;
}
@Override
- public List<Join> joins(String searchArea) {
+ public List<Join> getJoins(String searchArea) {
Field f = SearchConfiguration.getField(searchArea, getFieldKey());
return f.getJoins();
}
@@ -1,37 +1,21 @@
package com.os.sp.search;
-import java.util.Iterator;
-import java.util.List;
public class SQLGenerator {
public static String generateSql(SearchRequest req) {
- String queryTemplate = SearchConfiguration.getQueryTemplate();
String areaKey = req.getSearchArea();
SearchArea area = SearchConfiguration.getSearchArea(areaKey);
+ SearchCondition condition = req.getCondition();
+ String queryTemplate = SearchConfiguration.getQueryTemplate();
queryTemplate = queryTemplate.replace("$(what)", area.getSelectColumns());
queryTemplate = queryTemplate.replace("$(table)", area.getTable());
queryTemplate = queryTemplate.replace("$(alias)", area.getAlias());
-
- SearchCondition cond = req.getCondition();
-
- String joins = "";
- List<Join> joinList = cond.joins(areaKey);
- joinList = Join.merge(joinList, area.getJoins());
-
- for (Iterator<Join> i = joinList.iterator(); i.hasNext(); ) {
- Join j = i.next();
- joins += "join " + j.getSql();
- joins += "\n";
- }
-
- queryTemplate = queryTemplate.replace("$(joins)", joins);
-
- String where = req.getCondition().prettyPrint(areaKey);
-
- queryTemplate = queryTemplate.replace("$(where)", where);
+ queryTemplate = queryTemplate.replace("$(joins)", Join.toSql(
+ Join.merge(condition.getJoins(areaKey), area.getJoins())));
+ queryTemplate = queryTemplate.replace("$(where)", condition.toSql(areaKey));
queryTemplate = queryTemplate.replace("$(area_where)", area.getWhere());
return queryTemplate;
@@ -7,8 +7,8 @@ public static void main(String[] args) {
SearchCondition cond =
new Predicate(Field.CONTACT_PHONE, Operator.STR_CONTAINS, "422")
.and(new Predicate(Field.CONTACT_FIRST_NAME, Operator.STR_STARTS_WITH, "a"))
- .and(new Predicate(Field.CONTACT_LAST_NAME, Operator.STR_STARTS_WITH, "a"))
- .or(new Predicate(Field.CONTACT_COMPANY_NAME, Operator.STR_CONTAINS, "Sony"));
+ .and(new Predicate(Field.CONTACT_LAST_NAME, Operator.STR_EQUALS, "O'Neill"))
+ .or(new Predicate(Field.CONTACT_COMPANY_NAME, Operator.STR_MATCHES, "Sony*"));
SearchCondition cond2 = new Predicate(Field.CONTACT_USERNAME, Operator.STR_EQUALS, "root");
@@ -4,8 +4,8 @@
public abstract class SearchCondition {
- public abstract String prettyPrint(String searchArea);
- public abstract List<Join> joins(String searchArea);
+ public abstract String toSql(String searchArea);
+ public abstract List<Join> getJoins(String searchArea);
public SearchCondition and(SearchCondition that) {
return new SearchConditionAnd(this, that);
@@ -20,19 +20,19 @@ public SearchConditionAnd(SearchCondition left, SearchCondition right) {
}
@Override
- public String prettyPrint(String searchArea) {
+ public String toSql(String searchArea) {
String clause = "(";
- clause += getLeft().prettyPrint(searchArea);
+ clause += getLeft().toSql(searchArea);
clause += " and ";
- clause += getRight().prettyPrint(searchArea);
+ clause += getRight().toSql(searchArea);
clause += ")";
return clause;
}
@Override
- public List<Join> joins(String searchArea) {
- return Join.merge(getLeft().joins(searchArea), getRight().joins(searchArea));
+ public List<Join> getJoins(String searchArea) {
+ return Join.merge(getLeft().getJoins(searchArea), getRight().getJoins(searchArea));
}
}
@@ -15,17 +15,17 @@ public SearchConditionNot(SearchCondition condition) {
}
@Override
- public String prettyPrint(String searchArea) {
+ public String toSql(String searchArea) {
String clause = "not (";
- clause += getCondition().prettyPrint(searchArea);
+ clause += getCondition().toSql(searchArea);
clause += ")";
return clause;
}
@Override
- public List<Join> joins(String searchArea) {
- return condition.joins(searchArea);
+ public List<Join> getJoins(String searchArea) {
+ return condition.getJoins(searchArea);
}
@@ -20,17 +20,17 @@ public SearchConditionOr(SearchCondition left, SearchCondition right) {
}
@Override
- public String prettyPrint(String searchArea) {
+ public String toSql(String searchArea) {
String clause = "(";
- clause += getLeft().prettyPrint(searchArea);
+ clause += getLeft().toSql(searchArea);
clause += " or ";
- clause += getRight().prettyPrint(searchArea);
+ clause += getRight().toSql(searchArea);
clause += ")";
return clause;
}
@Override
- public List<Join> joins(String searchArea) {
- return Join.merge(getLeft().joins(searchArea), getRight().joins(searchArea));
+ public List<Join> getJoins(String searchArea) {
+ return Join.merge(getLeft().getJoins(searchArea), getRight().getJoins(searchArea));
}
}

0 comments on commit 26fbe94

Please sign in to comment.