1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.wotonomy.access;
19
20 import java.lang.reflect.Constructor;
21
22 import net.wotonomy.control.EOFetchSpecification;
23 import net.wotonomy.control.EOQualifier;
24 import net.wotonomy.foundation.NSArray;
25 import net.wotonomy.foundation.NSDictionary;
26
27 /***
28 *
29 * @author ezamudio@nasoft.com
30 * @author $Author: cgruber $
31 * @version $Revision: 894 $
32 */
33 public class EOSQLExpressionFactory {
34
35 protected EOAdaptor _adaptor;
36 protected Class _expressionClass;
37 private Constructor _instantiator;
38
39 public EOSQLExpressionFactory(EOAdaptor adaptor) {
40 super();
41 _adaptor = adaptor;
42 _expressionClass = _adaptor.expressionClass();
43 if (_expressionClass == null)
44 throw new IllegalStateException("EOAdaptor " + _adaptor.name() + " returned null for expressionClass()");
45 }
46
47 public EOAdaptor adaptor() {
48 return _adaptor;
49 }
50
51 /***
52 * Creates an instance of the adaptor's expression class,
53 * with entity assigned to it.
54 * @param entity The entity with which to initialize the expression.
55 * @return An EOSQLExpression.
56 */
57 public EOSQLExpression createExpression(EOEntity entity) {
58 EOSQLExpression expr = null;
59 if (_instantiator == null) {
60 try {
61 _instantiator = _expressionClass.getConstructor(new Class[]{ EOEntity.class });
62 } catch (Exception ex) {
63 throw new IllegalArgumentException("The expression class " + _expressionClass.getName() + " has no constructor with an entity parameter.");
64 }
65 }
66 try {
67 expr = (EOSQLExpression)_instantiator.newInstance(new Object[]{ entity });
68 } catch (Exception ex) {
69 throw new IllegalArgumentException("Cannot create new expression of class " + _expressionClass.getName());
70 }
71 return expr;
72 }
73
74 public EOSQLExpression expressionForEntity(EOEntity entity) {
75 return createExpression(entity);
76 }
77
78 public EOSQLExpression deleteStatementWithQualifier(EOQualifier qualifier, EOEntity entity) {
79 EOSQLExpression expr = createExpression(entity);
80 expr.prepareDeleteExpressionForQualifier(qualifier);
81 return expr;
82 }
83
84 public EOSQLExpression insertStatementForRow(NSDictionary row, EOEntity entity) {
85 EOSQLExpression expr = createExpression(entity);
86 expr.prepareInsertExpressionWithRow(row);
87 return expr;
88 }
89
90 public EOSQLExpression selectStatementForAttributes(NSArray atts, boolean lock, EOFetchSpecification fspec, EOEntity entity) {
91 EOSQLExpression expr = createExpression(entity);
92 expr.prepareSelectExpressionWithAttributes(atts, lock, fspec);
93 return expr;
94 }
95
96 public EOSQLExpression updateStatementForRow(NSDictionary row, EOQualifier qualifier, EOEntity entity) {
97 EOSQLExpression expr = createExpression(entity);
98 expr.prepareUpdateExpressionWithRow(row, qualifier);
99 return expr;
100 }
101
102 public EOSQLExpression expressionForString(String sql) {
103 EOSQLExpression expr = null;
104 try {
105 expr = (EOSQLExpression)_expressionClass.newInstance();
106 } catch (Exception e) {
107 return null;
108 }
109 expr.setStatement(sql);
110 return expr;
111 }
112
113 public Class expressionClass() {
114 return _expressionClass;
115 }
116
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131