View Javadoc
1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
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   * JdbcPreparedStatementFactory is the object that encapsulates the creation of the actual PreparedStatement for the
39   * JDBC driver.
40   *
41   * @author david berry
42   */
43  public class JdbcPreparedStatementFactory extends CpoStatementFactory implements CpoReleasible {
44  
45    /**
46     * Version Id for this class.
47     */
48    private static final long serialVersionUID = 1L;
49    /**
50     * DOCUMENT ME!
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     * Used to build the PreparedStatement that is used by CPO to create the actual JDBC PreparedStatement.
61     *
62     * The constructor is called by the internal CPO framework. This is not to be used by users of CPO. Programmers that
63     * build Transforms may need to use this object to get access to the actual connection.
64     *
65     * @param conn The actual jdbc connection that will be used to create the callable statement.
66     * @param jca The JdbcCpoAdapter that is controlling this transaction
67     * @param criteria The object that will be used to look up the cpo meta data
68     * @param function The CpoFunction that is being executed
69     * @param obj The pojo that is being acted upon
70     * @param wheres DOCUMENT ME!
71     * @param orderBy DOCUMENT ME!
72     * @param nativeQueries Additional sql to be embedded into the CpoFunction sql that is used to create the actual JDBC
73     * PreparedStatement
74     *
75     * @throws CpoException if a CPO error occurs
76     * @throws SQLException if a JDBC error occurs
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      // get the list of bindValues from the function parameters
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();  //To change body of implemented methods use File | Settings | File Templates.
106   }
107 
108   @Override
109   protected CpoData getCpoData(CpoAttribute cpoAttribute, int index) {
110     return new JdbcPreparedStatementCpoData(this, cpoAttribute, index);  //To change body of implemented methods use File | Settings | File Templates.
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     * Returns the jdbc prepared statment associated with this object
125     */
126    public PreparedStatement getPreparedStatement() {
127      return ps_;
128    }
129 
130    protected void setPreparedStatement(PreparedStatement ps) {
131      ps_ = ps;
132    }
133 
134 }