1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.synchronoss.cpo.jdbc;
22
23 import java.sql.Connection;
24 import java.sql.PreparedStatement;
25 import java.sql.SQLException;
26 import java.util.*;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.synchronoss.cpo.*;
30 import org.synchronoss.cpo.helper.ExceptionHelper;
31 import org.synchronoss.cpo.jdbc.meta.JdbcMethodMapper;
32 import org.synchronoss.cpo.meta.MethodMapper;
33 import org.synchronoss.cpo.meta.domain.CpoAttribute;
34 import org.synchronoss.cpo.meta.domain.CpoClass;
35 import org.synchronoss.cpo.meta.domain.CpoFunction;
36
37
38
39
40
41
42
43 public class JdbcPreparedStatementFactory extends CpoStatementFactory implements CpoReleasible {
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50
51
52 private static final Logger logger = LoggerFactory.getLogger(JdbcPreparedStatementFactory.class);
53 private PreparedStatement ps_ = null;
54
55 private List<CpoReleasible> releasibles = new ArrayList<>();
56 private static final String WHERE_MARKER = "__CPO_WHERE__";
57 private static final String ORDERBY_MARKER = "__CPO_ORDERBY__";
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public <T> JdbcPreparedStatementFactory(Connection conn, JdbcCpoAdapter jca, CpoClass criteria,
79 CpoFunction function, T obj, Collection<CpoWhere> wheres, Collection<CpoOrderBy> orderBy,
80 Collection<CpoNativeFunction> nativeQueries) throws CpoException {
81 super(obj == null ? logger : LoggerFactory.getLogger(obj.getClass()));
82
83 List<BindAttribute> bindValues = getBindValues(function, obj);
84
85 String sql = buildSql(criteria, function.getExpression(), wheres, orderBy, nativeQueries, bindValues);
86
87 getLocalLogger().debug("CpoFunction SQL = <" + sql + ">");
88
89 PreparedStatement pstmt = null;
90
91 try {
92 pstmt = conn.prepareStatement(sql);
93 } catch (SQLException se) {
94 getLocalLogger().error("Error Instantiating JdbcPreparedStatementFactory SQL=<" + sql + ">" + ExceptionHelper.getLocalizedMessage(se));
95 throw new CpoException(se);
96 }
97 setPreparedStatement(pstmt);
98
99 setBindValues(bindValues);
100
101 }
102
103 @Override
104 protected MethodMapper getMethodMapper() {
105 return JdbcMethodMapper.getMethodMapper();
106 }
107
108 @Override
109 protected CpoData getCpoData(CpoAttribute cpoAttribute, int index) {
110 return new JdbcPreparedStatementCpoData(this, cpoAttribute, index);
111 }
112
113 @Override
114 protected Object getBindableStatement() {
115 return getPreparedStatement();
116 }
117
118 @Override
119 protected int getStartingIndex() {
120 return 1;
121 }
122
123
124
125
126 public PreparedStatement getPreparedStatement() {
127 return ps_;
128 }
129
130 protected void setPreparedStatement(PreparedStatement ps) {
131 ps_ = ps;
132 }
133
134 }