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.cassandra;
22  
23  import com.datastax.driver.core.BoundStatement;
24  import com.datastax.driver.core.Session;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.synchronoss.cpo.*;
28  import org.synchronoss.cpo.cassandra.meta.CassandraMethodMapper;
29  import org.synchronoss.cpo.helper.ExceptionHelper;
30  import org.synchronoss.cpo.meta.MethodMapper;
31  import org.synchronoss.cpo.meta.domain.CpoAttribute;
32  import org.synchronoss.cpo.meta.domain.CpoClass;
33  import org.synchronoss.cpo.meta.domain.CpoFunction;
34  
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.List;
38  
39  /**
40   * CassandraBoundStatementFactory is the object that encapsulates the creation of the actual PreparedStatement for the
41   * JDBC driver.
42   *
43   * @author david berry
44   */
45  public class CassandraBoundStatementFactory extends CpoStatementFactory implements CpoReleasible {
46  
47    /**
48     * Version Id for this class.
49     */
50    private static final long serialVersionUID = 1L;
51    /**
52     * DOCUMENT ME!
53     */
54    private static final Logger logger = LoggerFactory.getLogger(CassandraBoundStatementFactory.class);
55    private BoundStatement boundStatement;
56  
57    private List<CpoReleasible> releasibles = new ArrayList<>();
58    private static final String WHERE_MARKER = "__CPO_WHERE__";
59    private static final String ORDERBY_MARKER = "__CPO_ORDERBY__";
60  
61    /**
62     * Used to build the PreparedStatement that is used by CPO to create the actual JDBC PreparedStatement.
63     *
64     * The constructor is called by the internal CPO framework. This is not to be used by users of CPO. Programmers that
65     * build Transforms may need to use this object to get access to the actual connection.
66     *
67     * @param sess The actual jdbc connection that will be used to create the callable statement.
68     * @param cassandraCpoAdapter The JdbcCpoAdapter that is controlling this transaction
69     * @param criteria The object that will be used to look up the cpo meta data
70     * @param function The CpoFunction that is being executed
71     * @param obj The pojo that is being acted upon
72     * @param wheres DOCUMENT ME!
73     * @param orderBy DOCUMENT ME!
74     * @param nativeQueries Additional sql to be embedded into the CpoFunction sql that is used to create the actual JDBC
75     * PreparedStatement
76     *
77     * @throws org.synchronoss.cpo.CpoException if a CPO error occurs
78     * @throws java.sql.SQLException if a JDBC error occurs
79     */
80    public <T> CassandraBoundStatementFactory(Session sess, CassandraCpoAdapter cassandraCpoAdapter, CpoClass criteria,
81                                              CpoFunction function, T obj, Collection<CpoWhere> wheres, Collection<CpoOrderBy> orderBy,
82                                              Collection<CpoNativeFunction> nativeQueries) throws CpoException {
83      super(obj == null ? logger : LoggerFactory.getLogger(obj.getClass()));
84      // get the list of bindValues from the function parameters
85      List<BindAttribute> bindValues = getBindValues(function, obj);
86  
87      String sql = buildSql(criteria, function.getExpression(), wheres, orderBy, nativeQueries, bindValues);
88  
89      getLocalLogger().debug("CpoFunction SQL = <" + sql + ">");
90      try {
91        setBoundStatement(sess.prepare(sql).bind());
92        setBindValues(bindValues);
93      } catch (Throwable t) {
94        getLocalLogger().error("Error Instantiating CassandraBoundStatementFactory SQL=<" + sql + ">" + ExceptionHelper.getLocalizedMessage(t));
95        throw new CpoException(t);
96      }
97    }
98  
99    @Override
100   protected MethodMapper getMethodMapper() {
101     return CassandraMethodMapper.getMethodMapper();  //To change body of implemented methods use File | Settings | File Templates.
102   }
103 
104   @Override
105   protected CpoData getCpoData(CpoAttribute cpoAttribute, int index) {
106     return new CassandraBoundStatementCpoData(this, cpoAttribute, index);  //To change body of implemented methods use File | Settings | File Templates.
107   }
108 
109   @Override
110   protected Object getBindableStatement() {
111     return getBoundStatement();
112   }
113 
114   @Override
115   protected int getStartingIndex() {
116     return 0;
117   }
118 
119   public BoundStatement getBoundStatement() {
120     return boundStatement;
121   }
122 
123   public void setBoundStatement(BoundStatement boundStatement) {
124     this.boundStatement = boundStatement;
125   }
126 
127 }