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.JdbcMethodMapEntry;
38 import org.synchronoss.cpo.jdbc.meta.JdbcMethodMapper;
39
40 /**
41 * JdbcPreparedStatementFactory is the object that encapsulates the creation of the actual
42 * PreparedStatement for the JDBC driver.
43 *
44 * @author david berry
45 */
46 public final class JdbcPreparedStatementFactory extends CpoStatementFactory
47 implements CpoReleasable {
48
49 /** Version Id for this class. */
50 private static final long serialVersionUID = 1L;
51
52 /** Fallback logger used when no bean is available to derive a class-named logger from. */
53 private static final Logger logger = LoggerFactory.getLogger(JdbcPreparedStatementFactory.class);
54
55 private PreparedStatement ps_ = null;
56
57 /**
58 * Used to build the PreparedStatement that is used by CPO to create the actual JDBC
59 * PreparedStatement.
60 *
61 * <p>The constructor is called by the internal CPO framework. This is not to be used by users of
62 * CPO. Programmers that build Transforms may need to use this object to get access to the actual
63 * connection.
64 *
65 * @param <T> The cpo object being prepared to create a named logger
66 * @param conn The actual jdbc connection that will be used to create the callable statement.
67 * @param jca The JdbcCpoAdapter that is controlling this transaction
68 * @param criteria The object that will be used to look up the cpo metadata
69 * @param function The CpoFunction that is being executed
70 * @param bean The bean that is being acted upon
71 * @param wheres A collection of where clauses to be embedded into the CpoFunction sql
72 * @param orderBy A collection of order-by clauses to be embedded into the CpoFunction sql
73 * @param nativeQueries Additional sql to be embedded into the CpoFunction sql that is used to
74 * create the actual JDBC PreparedStatement
75 * @throws CpoException if a CPO error occurs
76 */
77 public <T> JdbcPreparedStatementFactory(
78 Connection conn,
79 JdbcCpoAdapter jca,
80 CpoClass criteria,
81 CpoFunction function,
82 T bean,
83 Collection<CpoWhere> wheres,
84 Collection<CpoOrderBy> orderBy,
85 Collection<CpoNativeFunction> nativeQueries)
86 throws CpoException {
87 super(bean == null ? logger : LoggerFactory.getLogger(bean.getClass()));
88 // get the list of bindValues from the function parameters
89 List<BindAttribute> bindValues = getBindValues(function, bean);
90
91 String sql =
92 buildSql(criteria, function.getExpression(), wheres, orderBy, nativeQueries, bindValues);
93
94 getLocalLogger().debug("CpoFunction SQL = <{}>", sql);
95
96 try {
97 ps_ = conn.prepareStatement(sql);
98 ps_.setFetchSize(jca.getFetchSize());
99 setBindValues(bindValues);
100 } catch (SQLException se) {
101 throw new CpoException(
102 "Error Instantiating JdbcPreparedStatementFactory SQL=<" + sql + ">", se);
103 }
104 }
105
106 @Override
107 protected MethodMapper<JdbcMethodMapEntry<?, ?>> getMethodMapper() {
108 return JdbcMethodMapper.getMethodMapper();
109 }
110
111 @Override
112 protected CpoData getCpoData(CpoAttribute cpoAttribute, int index) {
113 return new JdbcPreparedStatementCpoData(this, cpoAttribute, index);
114 }
115
116 @Override
117 protected Object getBindableStatement() {
118 return getPreparedStatement();
119 }
120
121 @Override
122 protected int getStartingIndex() {
123 return 1;
124 }
125
126 /**
127 * Returns the jdbc prepared statement associated with this object
128 *
129 * @return A PreparedStatement
130 */
131 public PreparedStatement getPreparedStatement() {
132 return ps_;
133 }
134 }