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 class JdbcCallableStatementFactory implements CpoReleasable {
46
47 private static final Logger logger = LoggerFactory.getLogger(JdbcCallableStatementFactory.class);
48 private CallableStatement cs_ = null;
49
50 @SuppressWarnings("unused")
51 private JdbcCallableStatementFactory() {}
52
53 private List<CpoReleasable> releasables = new ArrayList<>();
54 private List<CpoArgument> outArguments = new ArrayList<>();
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public JdbcCallableStatementFactory(
70 Connection conn,
71 JdbcCpoAdapter jca,
72 CpoFunction function,
73 Object criteria,
74 CpoClass resultClass)
75 throws CpoException {
76 CallableStatement cstmt;
77 JdbcCpoAttribute attribute;
78 Logger localLogger = criteria == null ? logger : LoggerFactory.getLogger(criteria.getClass());
79
80 try {
81 outArguments = function.getArguments();
82
83 localLogger.debug("SQL = <" + function.getExpression() + ">");
84
85
86 cstmt = conn.prepareCall(function.getExpression());
87 setCallableStatement(cstmt);
88
89 int j = 1;
90 for (CpoArgument argument : outArguments) {
91 JdbcCpoArgument jdbcArgument = (JdbcCpoArgument) argument;
92 attribute = (JdbcCpoAttribute) argument.getAttribute();
93
94 if (jdbcArgument.isInParameter()) {
95 CpoData cpoData = new CallableStatementCpoData(this, attribute, j);
96 cpoData.invokeSetter(criteria);
97 }
98
99 if (jdbcArgument.isOutParameter()) {
100
101
102 if (attribute == null) {
103 attribute = (JdbcCpoAttribute) resultClass.getAttributeJava(argument.getName());
104 if (attribute == null) {
105 throw new CpoException(
106 "Attribute <"
107 + argument.getName()
108 + "> does not exist on class <"
109 + resultClass.getName()
110 + ">");
111 }
112 }
113 localLogger.debug(
114 "Setting OUT parameter " + j + " as Type " + attribute.getDataTypeInt());
115 if (jdbcArgument.getTypeInfo() != null)
116 cstmt.registerOutParameter(j, attribute.getDataTypeInt(), jdbcArgument.getTypeInfo());
117 else cstmt.registerOutParameter(j, attribute.getDataTypeInt());
118 }
119 j++;
120 }
121
122 } catch (SQLException e) {
123 throw new CpoException("Error Instantiating JdbcCallableStatementFactory", e);
124 }
125 }
126
127
128
129
130
131
132 public CallableStatement getCallableStatement() {
133 return cs_;
134 }
135
136
137
138
139
140
141 protected void setCallableStatement(CallableStatement cs) {
142 cs_ = cs;
143 }
144
145
146
147
148
149
150 public List<CpoArgument> getOutArguments() {
151 return outArguments;
152 }
153
154
155
156
157
158
159
160
161 public void AddReleasable(CpoReleasable releasable) {
162 if (releasable != null) {
163 releasables.add(releasable);
164 }
165 }
166
167
168
169
170
171 @Override
172 public void release() throws CpoException {
173 for (CpoReleasable releasable : releasables) {
174 try {
175 releasable.release();
176 } catch (CpoException ce) {
177 logger.error("Error Releasing Callable Statement Transform Object", ce);
178 throw ce;
179 }
180 }
181 }
182 }