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.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
41
42
43
44
45 public class CassandraBoundStatementFactory extends CpoStatementFactory implements CpoReleasible {
46
47
48
49
50 private static final long serialVersionUID = 1L;
51
52
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
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();
102 }
103
104 @Override
105 protected CpoData getCpoData(CpoAttribute cpoAttribute, int index) {
106 return new CassandraBoundStatementCpoData(this, cpoAttribute, index);
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 }