2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.datastore;
25 import java.util.ArrayList;
26 import java.util.List;
33 public class PreparedQueryObject {
36 private List<Object> values;
37 private StringBuilder query;
38 private String consistency;
39 private String keyspaceName;
40 private String tableName;
41 private String operation;
42 private String primaryKeyValue;
46 * Create PreparedQueryObject
48 public PreparedQueryObject() {
49 this.values = new ArrayList<>();
50 this.query = new StringBuilder();
54 * Create PreparedQueryObject
55 * @param query query portion of the prepared query
57 public PreparedQueryObject(String query) {
58 this.values = new ArrayList<>();
59 this.query = new StringBuilder(query);
63 * Create PreparedQueryObject
64 * @param query query portion of the prepared query
65 * @param values to be added to the query string as prepared query
67 public PreparedQueryObject(String query, Object...values) {
68 this.query = new StringBuilder(query);
69 this.values = new ArrayList<>();
70 for (Object value: values) {
71 this.values.add(value);
75 public String getKeyspaceName() {
79 public void setKeyspaceName(String keyspaceName) {
80 this.keyspaceName = keyspaceName;
83 public String getTableName() {
87 public void setTableName(String tableName) {
88 this.tableName = tableName;
91 public String getOperation() {
92 if (operation!=null) return operation;
93 if (query.length()==0) return null;
94 String queryStr = query.toString().toLowerCase();
95 String firstOp = null;
96 int firstOpChar = query.length();
97 if (queryStr.indexOf("insert")>-1 && queryStr.indexOf("insert")<firstOpChar) {
99 firstOpChar = queryStr.indexOf("insert");
101 if (queryStr.indexOf("update")>-1 && queryStr.indexOf("update")<firstOpChar) {
103 firstOpChar = queryStr.indexOf("update");
105 if (queryStr.indexOf("delete")>-1 && queryStr.indexOf("delete")<firstOpChar) {
107 firstOpChar = queryStr.indexOf("delete");
109 if (queryStr.indexOf("select")>-1 && queryStr.indexOf("select")<firstOpChar) {
111 firstOpChar = queryStr.indexOf("select");
116 public void setOperation(String operation) {
117 this.operation = operation;
120 public String getPrimaryKeyValue() {
121 return primaryKeyValue;
124 public void setPrimaryKeyValue(String primaryKeyValue) {
125 this.primaryKeyValue = primaryKeyValue;
128 public String getConsistency() {
132 public void setConsistency(String consistency) {
133 this.consistency = consistency;
137 * @return values to be set as part of the prepared query
139 public List<Object> getValues() {
144 * @param o object to be added as a value to the prepared query, in order
146 public void addValue(Object o) {
151 * Add values to the preparedQuery
152 * @param objs ordered list of objects to be added as values to the prepared query
154 public void addValues(Object... objs) {
155 for (Object obj: objs) {
156 this.values.add(obj);
163 public void appendQueryString(String s) {
164 this.query.append(s);
166 public void replaceQueryString(String s) {
167 this.query.replace(0, query.length(), s);
173 public String getQuery() {
174 return this.query.toString();