| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.builder.sql; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Exchange; |
| 20 |
|
import org.apache.camel.Expression; |
| 21 |
|
import org.apache.camel.Message; |
| 22 |
|
import org.apache.camel.Predicate; |
| 23 |
|
import org.apache.camel.RuntimeExpressionException; |
| 24 |
|
import org.apache.camel.util.ObjectHelper; |
| 25 |
|
import org.josql.Query; |
| 26 |
|
import org.josql.QueryExecutionException; |
| 27 |
|
import org.josql.QueryParseException; |
| 28 |
|
|
| 29 |
|
import java.util.Collections; |
| 30 |
|
import java.util.List; |
| 31 |
|
import java.util.Map; |
| 32 |
|
import java.util.Set; |
| 33 |
|
import java.util.HashMap; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
5 |
public class SqlBuilder<E extends Exchange> implements Expression<E>, Predicate<E> { |
| 41 |
|
|
| 42 |
|
private Query query; |
| 43 |
5 |
private Map<String,Object> variables = new HashMap<String, Object>(); |
| 44 |
|
|
| 45 |
5 |
public SqlBuilder(Query query) { |
| 46 |
5 |
this.query = query; |
| 47 |
5 |
} |
| 48 |
|
|
| 49 |
|
public Object evaluate(E exchange) { |
| 50 |
2 |
return evaluateQuery(exchange); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
public boolean matches(E exchange) { |
| 54 |
3 |
List list = evaluateQuery(exchange); |
| 55 |
3 |
return matches(exchange, list); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
public void assertMatches(String text, E exchange) throws AssertionError { |
| 59 |
2 |
List list = evaluateQuery(exchange); |
| 60 |
2 |
if (!matches(exchange, list)) { |
| 61 |
0 |
throw new AssertionError(this + " failed on " + exchange + " as found " + list); |
| 62 |
|
} |
| 63 |
2 |
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
public static <E extends Exchange> SqlBuilder<E> sql(String sql) throws QueryParseException { |
| 76 |
5 |
Query q = new Query(); |
| 77 |
5 |
q.parse(sql); |
| 78 |
5 |
return new SqlBuilder(q); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
public SqlBuilder<E> variable(String name, Object value) { |
| 85 |
0 |
getVariables().put(name, value); |
| 86 |
0 |
return this; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
public Map<String, Object> getVariables() { |
| 93 |
7 |
return variables; |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
public void setVariables(Map<String, Object> properties) { |
| 97 |
0 |
this.variables = properties; |
| 98 |
0 |
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
protected boolean matches(E exchange, List list) { |
| 104 |
5 |
return ObjectHelper.matches(list); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
protected List evaluateQuery(E exchange) { |
| 108 |
7 |
configureQuery(exchange); |
| 109 |
7 |
Message in = exchange.getIn(); |
| 110 |
7 |
List list = in.getBody(List.class); |
| 111 |
7 |
if (list == null) { |
| 112 |
0 |
list = Collections.singletonList(in.getBody()); |
| 113 |
|
} |
| 114 |
|
try { |
| 115 |
7 |
return query.execute(list).getResults(); |
| 116 |
|
} |
| 117 |
0 |
catch (QueryExecutionException e) { |
| 118 |
0 |
throw new RuntimeExpressionException(e); |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
protected void configureQuery(E exchange) { |
| 123 |
|
|
| 124 |
7 |
addVariables(exchange.getProperties()); |
| 125 |
7 |
addVariables(exchange.getIn().getHeaders()); |
| 126 |
7 |
addVariables(getVariables()); |
| 127 |
|
|
| 128 |
7 |
query.setVariable("exchange", exchange); |
| 129 |
7 |
query.setVariable("in", exchange.getIn()); |
| 130 |
7 |
query.setVariable("out", exchange.getOut()); |
| 131 |
7 |
} |
| 132 |
|
|
| 133 |
|
protected void addVariables(Map <String, Object> map) { |
| 134 |
21 |
Set<Map.Entry<String, Object>> propertyEntries = map.entrySet(); |
| 135 |
21 |
for (Map.Entry<String, Object> entry : propertyEntries) { |
| 136 |
7 |
query.setVariable(entry.getKey(), entry.getValue()); |
| 137 |
7 |
} |
| 138 |
21 |
} |
| 139 |
|
} |