2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class InsertQuery<T extends DataObject> implements SqlQuery {
38 private final Logger LOG = LoggerFactory.getLogger(InsertQuery.class);
40 protected final Entity entity;
41 private final String controllerId;
42 private final T object;
43 private final boolean ignoreNull;
46 public InsertQuery(Entity e, T object, String controllerId) {
48 this.controllerId = controllerId;
50 this.ignoreNull = true;
56 public String toSql() {
58 return this.toSqlWithError();
59 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
60 | JsonProcessingException e) {
61 LOG.warn("unable to create insert statement for table {} from object {}: ", this.entity, this.object, e);
66 protected String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
67 JsonProcessingException {
68 Class<?> cls = this.object.getClass();
69 List<DBKeyValuePair<String>> kvps = new ArrayList<>();
70 List<String> cols = new ArrayList<>();
71 List<String> args = new ArrayList<>();
74 StringBuilder sb = new StringBuilder();
75 sb.append("INSERT INTO `" + entity.getName() + "` (");
76 for (Method m : SqlDBMapper.getFilteredMethods(cls, true)) {
78 m.setAccessible(true);
79 value = m.invoke(this.object);
80 col = SqlDBMapper.getColumnName(m);
81 if (col.equals("id") && this.id != null) {
84 if (ignoreNull && value == null) {
87 DBKeyValuePair<String> kvp = SqlDBMapper.getEscapedKeyValue(m,col, value);
88 cols.add(kvp.getKey());
89 args.add(kvp.getValue());
92 if (this.id != null && !cols.contains("`id`")) {
94 args.add("'"+this.id+"'");
96 args.add("'"+this.controllerId+"'");
97 sb.append( String.join(",", cols));
98 sb.append(",`" + SqlDBMapper.ODLID_DBCOL + "`) VALUES (");
99 sb.append( String.join(",", args)+" )");
100 this.appendAdditionalToQuery(sb,kvps);
101 return sb.toString();
104 protected void appendAdditionalToQuery(StringBuilder sb, List<DBKeyValuePair<String>> keyValues) {
108 public void setId(String id) {