1 package org.synchronoss.cpo.jdbc;
2
3 /*-
4 * [[
5 * jdbc
6 * ==
7 * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8 * ==
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU General Lesser Public
20 * License along with this program. If not, see
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22 * ]]
23 */
24
25 import java.sql.Connection;
26 import java.sql.PreparedStatement;
27 import java.sql.SQLException;
28 import java.util.Collection;
29 import java.util.List;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.synchronoss.cpo.core.*;
33 import org.synchronoss.cpo.core.meta.MethodMapper;
34 import org.synchronoss.cpo.core.meta.domain.CpoAttribute;
35 import org.synchronoss.cpo.core.meta.domain.CpoClass;
36 import org.synchronoss.cpo.core.meta.domain.CpoFunction;
37 import org.synchronoss.cpo.jdbc.meta.JdbcMethodMapper;
38
39 /**
40 * JdbcPreparedStatementFactory is the object that encapsulates the creation of the actual
41 * PreparedStatement for the JDBC driver.
42 *
43 * @author david berry
44 */
45 public class JdbcPreparedStatementFactory extends CpoStatementFactory implements CpoReleasable {
46
47 /** Version Id for this class. */
48 private static final long serialVersionUID = 1L;
49
50 /** Fallback logger used when no bean is available to derive a class-named logger from. */
51 private static final Logger logger = LoggerFactory.getLogger(JdbcPreparedStatementFactory.class);
52
53 private PreparedStatement ps_ = null;
54
55 /**
56 * Used to build the PreparedStatement that is used by CPO to create the actual JDBC
57 * PreparedStatement.
58 *
59 * <p>The constructor is called by the internal CPO framework. This is not to be used by users of
60 * CPO. Programmers that build Transforms may need to use this object to get access to the actual
61 * connection.
62 *
63 * @param <T> The cpo object being prepared to create a named logger
64 * @param conn The actual jdbc connection that will be used to create the callable statement.
65 * @param jca The JdbcCpoAdapter that is controlling this transaction
66 * @param criteria The object that will be used to look up the cpo metadata
67 * @param function The CpoFunction that is being executed
68 * @param bean The bean that is being acted upon
69 * @param wheres A collection of where clauses to be embedded into the CpoFunction sql
70 * @param orderBy A collection of order-by clauses to be embedded into the CpoFunction sql
71 * @param nativeQueries Additional sql to be embedded into the CpoFunction sql that is used to
72 * create the actual JDBC PreparedStatement
73 * @throws CpoException if a CPO error occurs
74 */
75 public <T> JdbcPreparedStatementFactory(
76 Connection conn,
77 JdbcCpoAdapter jca,
78 CpoClass criteria,
79 CpoFunction function,
80 T bean,
81 Collection<CpoWhere> wheres,
82 Collection<CpoOrderBy> orderBy,
83 Collection<CpoNativeFunction> nativeQueries)
84 throws CpoException {
85 super(bean == null ? logger : LoggerFactory.getLogger(bean.getClass()));
86 // get the list of bindValues from the function parameters
87 List<BindAttribute> bindValues = getBindValues(function, bean);
88
89 String sql =
90 buildSql(criteria, function.getExpression(), wheres, orderBy, nativeQueries, bindValues);
91
92 getLocalLogger().debug("CpoFunction SQL = <{}>", sql);
93
94 try {
95 ps_ = conn.prepareStatement(sql);
96 ps_.setFetchSize(jca.getFetchSize());
97 setBindValues(bindValues);
98 } catch (SQLException se) {
99 throw new CpoException(
100 "Error Instantiating JdbcPreparedStatementFactory SQL=<" + sql + ">", se);
101 }
102 }
103
104 @Override
105 protected MethodMapper getMethodMapper() {
106 return JdbcMethodMapper.getMethodMapper();
107 }
108
109 @Override
110 protected CpoData getCpoData(CpoAttribute cpoAttribute, int index) {
111 return new JdbcPreparedStatementCpoData(this, cpoAttribute, index);
112 }
113
114 @Override
115 protected Object getBindableStatement() {
116 return getPreparedStatement();
117 }
118
119 @Override
120 protected int getStartingIndex() {
121 return 1;
122 }
123
124 /**
125 * Returns the jdbc prepared statement associated with this object
126 *
127 * @return A PreparedStatement
128 */
129 public PreparedStatement getPreparedStatement() {
130 return ps_;
131 }
132 }