push addional code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-nosqldb-lib / openecomp-nosqldb-core / src / main / java / org / openecomp / core / nosqldb / impl / cassandra / CassandraNoSqlDbImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.nosqldb.impl.cassandra;
22
23 import com.datastax.driver.core.BoundStatement;
24 import com.datastax.driver.core.PreparedStatement;
25 import com.datastax.driver.core.ResultSet;
26 import com.datastax.driver.core.Session;
27 import com.datastax.driver.mapping.MappingManager;
28 import org.openecomp.core.nosqldb.api.NoSqlDb;
29 import org.openecomp.core.nosqldb.util.CassandraUtils;
30 import org.openecomp.core.utilities.CommonMethods;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.common.errors.ErrorCategory;
33 import org.openecomp.sdc.common.errors.ErrorCode;
34
35 class CassandraNoSqlDbImpl implements NoSqlDb {
36
37   private final Session session;
38   private final String keySpace;
39   private final MappingManager mappingManager;
40
41
42   public CassandraNoSqlDbImpl(Session session) {
43     this.session = session;
44     this.keySpace = this.session.getLoggedKeyspace();
45     this.mappingManager = new MappingManager(this.session);
46
47   }
48
49   @Override
50   public void insert(String tableName, String[] colNames, Object[] values) {
51     if (colNames.length != values.length) {
52       throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
53           "number of colmuns[" + colNames.length + "] is not equal to the number of values["
54               + values.length + "].").withId("E0005").withCategory(ErrorCategory.APPLICATION)
55           .build());
56     }
57
58     StringBuilder sb = new StringBuilder();
59     sb.append("insert into ")
60         .append(tableName)
61         .append(" (")
62         .append(CommonMethods.arrayToCommaSeparatedString(colNames))
63         .append(") values (")
64         .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
65         .append(")");
66     System.out.println(sb.toString());
67     PreparedStatement prepared = session.prepare(sb.toString());
68
69     BoundStatement bound;
70     bound = prepared.bind(values);
71     session.execute(bound);
72
73   }
74
75   @Override
76   public ResultSet execute(String statement) {
77     return session.execute(statement);
78   }
79
80   @Override
81   public ResultSet execute(String statementName, Object... values) {
82
83     String statement = CassandraUtils.getStatement(statementName);
84     if (statement == null) {
85       statement = statementName;
86     }
87     if (values != null) {
88       PreparedStatement prepared = session.prepare(statement);
89
90       BoundStatement bound;
91       bound = prepared.bind(values);
92       return session.execute(bound);
93     } else {
94       return session.execute(statement);
95     }
96
97   }
98
99   @Override
100   public MappingManager getMappingManager() {
101     return mappingManager;
102   }
103
104 }