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 UpdateQuery<T extends DataObject> implements SqlQuery {
38 private final Logger LOG = LoggerFactory.getLogger(UpdateQuery.class);
40 private final Entity entity;
41 private final String controllerId;
42 private final T object;
43 private final boolean ignoreNull;
46 public UpdateQuery(Entity e, T object) {
47 this(e, object, null);
50 public UpdateQuery(Entity e, T object, String controllerId) {
52 this.controllerId = controllerId;
54 this.ignoreNull = true;
59 public String toSql() {
61 return this.toSqlWithError();
62 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
63 | JsonProcessingException e) {
64 LOG.warn("unable to create insert statement for table {} from object {}: ", this.entity, this.object, e);
69 private String toSqlWithError() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
70 JsonProcessingException {
71 Class<?> cls = this.object.getClass();
74 StringBuilder sb = new StringBuilder();
75 List<String> args = new ArrayList<>();
76 sb.append("UPDATE `" + entity.getName() + "` SET ");
77 List<Method> methods = SqlDBMapper.getFilteredMethods(cls, true);
79 for (int i = 0; i < methods.size(); i++) {
81 m.setAccessible(true);
82 value = m.invoke(this.object);
83 col = SqlDBMapper.getColumnName(m);
84 if (col.equals("id")) {
85 if (this.id == null) {
86 this.id = String.valueOf(value);
90 if (ignoreNull && value == null) {
93 DBKeyValuePair<String> kvp = SqlDBMapper.getEscapedKeyValue(m, col, value);
94 args.add(String.format("%s=%s", kvp.getKey(), kvp.getValue()));
96 sb.append(String.join(",", args));
97 sb.append(String.format(" WHERE `id`='%s'", this.id));
98 if (this.controllerId != null) {
99 sb.append(String.format(" AND `%s`='%s'", SqlDBMapper.ODLID_DBCOL, this.controllerId));
102 return sb.toString();
105 public void setId(String id) {