1 package org.synchronoss.cpo.jdbc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.sql.CallableStatement;
26 import java.sql.Connection;
27 import java.sql.SQLException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.synchronoss.cpo.core.CpoData;
33 import org.synchronoss.cpo.core.CpoException;
34 import org.synchronoss.cpo.core.CpoReleasable;
35 import org.synchronoss.cpo.core.meta.domain.CpoArgument;
36 import org.synchronoss.cpo.core.meta.domain.CpoClass;
37 import org.synchronoss.cpo.core.meta.domain.CpoFunction;
38
39
40
41
42
43
44
45 public final class JdbcCallableStatementFactory implements CpoReleasable {
46
47 private static final Logger logger = LoggerFactory.getLogger(JdbcCallableStatementFactory.class);
48 private CallableStatement cs_ = null;
49
50 private JdbcCallableStatementFactory() {}
51
52 private List<CpoReleasable> releasables = new ArrayList<>();
53 private List<CpoArgument> outArguments = new ArrayList<>();
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public JdbcCallableStatementFactory(
69 Connection conn,
70 JdbcCpoAdapter jca,
71 CpoFunction function,
72 Object criteria,
73 CpoClass resultClass)
74 throws CpoException {
75 CallableStatement cstmt;
76 JdbcCpoAttribute attribute;
77 Logger localLogger = criteria == null ? logger : LoggerFactory.getLogger(criteria.getClass());
78
79 try {
80 outArguments = function.getArguments();
81
82 localLogger.debug("SQL = <" + function.getExpression() + ">");
83
84
85 cstmt = conn.prepareCall(function.getExpression());
86 setCallableStatement(cstmt);
87
88 int j = 1;
89 for (CpoArgument argument : outArguments) {
90 JdbcCpoArgument jdbcArgument = (JdbcCpoArgument) argument;
91 attribute = (JdbcCpoAttribute) argument.getAttribute();
92
93 if (jdbcArgument.isInParameter()) {
94 CpoData cpoData = new CallableStatementCpoData(this, attribute, j);
95 cpoData.invokeSetter(criteria);
96 }
97
98 if (jdbcArgument.isOutParameter()) {
99
100
101 if (attribute == null) {
102 attribute = (JdbcCpoAttribute) resultClass.getAttributeJava(argument.getName());
103 if (attribute == null) {
104 throw new CpoException(
105 "Attribute <"
106 + argument.getName()
107 + "> does not exist on class <"
108 + resultClass.getName()
109 + ">");
110 }
111 }
112 localLogger.debug(
113 "Setting OUT parameter " + j + " as Type " + attribute.getDataTypeInt());
114 if (jdbcArgument.getTypeInfo() != null)
115 cstmt.registerOutParameter(j, attribute.getDataTypeInt(), jdbcArgument.getTypeInfo());
116 else cstmt.registerOutParameter(j, attribute.getDataTypeInt());
117 }
118 j++;
119 }
120
121 } catch (SQLException e) {
122 throw new CpoException("Error Instantiating JdbcCallableStatementFactory", e);
123 }
124 }
125
126
127
128
129
130
131 public CallableStatement getCallableStatement() {
132 return cs_;
133 }
134
135
136
137
138
139
140 protected void setCallableStatement(CallableStatement cs) {
141 cs_ = cs;
142 }
143
144
145
146
147
148
149 public List<CpoArgument> getOutArguments() {
150 return outArguments;
151 }
152
153
154
155
156
157
158
159
160 public void AddReleasable(CpoReleasable releasable) {
161 if (releasable != null) {
162 releasables.add(releasable);
163 }
164 }
165
166
167
168
169
170 @Override
171 public void release() throws CpoException {
172 for (CpoReleasable releasable : releasables) {
173 try {
174 releasable.release();
175 } catch (CpoException ce) {
176 logger.error("Error Releasing Callable Statement Transform Object", ce);
177 throw ce;
178 }
179 }
180 }
181 }