Deadlock detection by owner
[music.git] / src / main / java / org / onap / music / datastore / PreparedQueryObject.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.datastore;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29  * 
30  * @author srupane
31  *
32  */
33 public class PreparedQueryObject {
34
35
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;
43
44
45     /**
46      * Create PreparedQueryObject
47      */
48     public PreparedQueryObject() {
49         this.values = new ArrayList<>();
50         this.query = new StringBuilder();
51     }
52     
53     /**
54      * Create PreparedQueryObject
55      * @param query query portion of the prepared query
56      */
57     public PreparedQueryObject(String query) {
58         this.values = new ArrayList<>();
59         this.query = new StringBuilder(query);
60     }
61     
62     /**
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
66      */
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);
72         }
73     }
74
75     public String getKeyspaceName() {
76         return keyspaceName;
77     }
78
79     public void setKeyspaceName(String keyspaceName) {
80         this.keyspaceName = keyspaceName;
81     }
82
83     public String getTableName() {
84         return tableName;
85     }
86
87     public void setTableName(String tableName) {
88         this.tableName = tableName;
89     }
90
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) {
98             firstOp = "insert";
99             firstOpChar = queryStr.indexOf("insert");
100         }
101         if (queryStr.indexOf("update")>-1 && queryStr.indexOf("update")<firstOpChar) {
102             firstOp = "update";
103             firstOpChar = queryStr.indexOf("update");
104         }
105         if (queryStr.indexOf("delete")>-1 && queryStr.indexOf("delete")<firstOpChar) {
106             firstOp = "delete";
107             firstOpChar = queryStr.indexOf("delete");
108         }
109         if (queryStr.indexOf("select")>-1 && queryStr.indexOf("select")<firstOpChar) {
110             firstOp = "select";
111             firstOpChar = queryStr.indexOf("select");
112         }
113         return firstOp;
114     }
115
116     public void setOperation(String operation) {
117         this.operation = operation;
118     }
119
120     public String getPrimaryKeyValue() {
121         return primaryKeyValue;
122     }
123
124     public void setPrimaryKeyValue(String primaryKeyValue) {
125         this.primaryKeyValue = primaryKeyValue;
126     }
127
128     public String getConsistency() {
129         return consistency;
130     }
131
132     public void setConsistency(String consistency) {
133         this.consistency = consistency;
134     }
135
136     /**
137      * @return values to be set as part of the prepared query
138      */
139     public List<Object> getValues() {
140         return values;
141     }
142
143     /**
144      * @param o object to be added as a value to the prepared query, in order
145      */
146     public void addValue(Object o) {
147         this.values.add(o);
148     }
149     
150     /**
151      * Add values to the preparedQuery
152      * @param objs ordered list of objects to be added as values to the prepared query
153      */
154     public void addValues(Object... objs) {
155         for (Object obj: objs) {
156             this.values.add(obj);
157         }
158     }
159
160     /**
161      * @param s
162      */
163     public void appendQueryString(String s) {
164         this.query.append(s);
165     }
166     public void replaceQueryString(String s) {
167         this.query.replace(0, query.length(), s);
168     }
169
170     /**
171      * @return the query
172      */
173     public String getQuery() {
174         return this.query.toString();
175     }
176 }