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