|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SqlWhereVisitor.java | - | 0% | 0% | 0% |
|
1 |
package groovy.sql;
|
|
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.List;
|
|
5 |
|
|
6 |
import org.codehaus.groovy.ast.CodeVisitorSupport;
|
|
7 |
import org.codehaus.groovy.ast.expr.BinaryExpression;
|
|
8 |
import org.codehaus.groovy.ast.expr.BooleanExpression;
|
|
9 |
import org.codehaus.groovy.ast.expr.ConstantExpression;
|
|
10 |
import org.codehaus.groovy.ast.expr.Expression;
|
|
11 |
import org.codehaus.groovy.ast.expr.PropertyExpression;
|
|
12 |
import org.codehaus.groovy.ast.stmt.ReturnStatement;
|
|
13 |
import org.codehaus.groovy.syntax.Token;
|
|
14 |
import org.codehaus.groovy.syntax.Types;
|
|
15 |
|
|
16 |
/**
|
|
17 |
* @author James Strachan
|
|
18 |
* @version $Revision: 1.2 $
|
|
19 |
*/
|
|
20 |
public class SqlWhereVisitor extends CodeVisitorSupport { |
|
21 |
|
|
22 |
private StringBuffer buffer = new StringBuffer(); |
|
23 |
private List parameters = new ArrayList(); |
|
24 |
|
|
25 | 0 |
public String getWhere() {
|
26 | 0 |
return buffer.toString();
|
27 |
} |
|
28 |
|
|
29 | 0 |
public void visitReturnStatement(ReturnStatement statement) { |
30 | 0 |
statement.getExpression().visit(this);
|
31 |
} |
|
32 |
|
|
33 | 0 |
public void visitBinaryExpression(BinaryExpression expression) { |
34 | 0 |
Expression left = expression.getLeftExpression(); |
35 | 0 |
Expression right = expression.getRightExpression(); |
36 |
|
|
37 | 0 |
left.visit(this);
|
38 | 0 |
buffer.append(" ");
|
39 |
|
|
40 | 0 |
Token token = expression.getOperation(); |
41 | 0 |
buffer.append(tokenAsSql(token)); |
42 |
|
|
43 | 0 |
buffer.append(" ");
|
44 | 0 |
right.visit(this);
|
45 |
} |
|
46 |
|
|
47 | 0 |
public void visitBooleanExpression(BooleanExpression expression) { |
48 | 0 |
expression.getExpression().visit(this);
|
49 |
} |
|
50 |
|
|
51 | 0 |
public void visitConstantExpression(ConstantExpression expression) { |
52 | 0 |
getParameters().add(expression.getValue()); |
53 | 0 |
buffer.append("?");
|
54 |
} |
|
55 |
|
|
56 | 0 |
public void visitPropertyExpression(PropertyExpression expression) { |
57 | 0 |
buffer.append(expression.getProperty()); |
58 |
} |
|
59 |
|
|
60 | 0 |
public List getParameters() {
|
61 | 0 |
return parameters;
|
62 |
} |
|
63 |
|
|
64 | 0 |
protected String tokenAsSql(Token token) {
|
65 | 0 |
switch (token.getType()) {
|
66 |
case Types.COMPARE_EQUAL :
|
|
67 | 0 |
return "="; |
68 |
case Types.LOGICAL_AND :
|
|
69 | 0 |
return "and"; |
70 |
case Types.LOGICAL_OR :
|
|
71 | 0 |
return "or"; |
72 |
default :
|
|
73 | 0 |
return token.getText();
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
|
|