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 java.util.ArrayList;
 
  25 import java.util.List;
 
  27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper;
 
  28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
 
  29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EntityInput;
 
  30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter;
 
  31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterBuilder;
 
  32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey;
 
  34 public class CountQuery implements SqlQuery {
 
  36     private final Entity entity;
 
  37     private final List<Filter> filters;
 
  38     private final String countField;
 
  39     public CountQuery(Entity e) {
 
  42     public CountQuery(Entity e, String controllerId) {
 
  43         this(e, "*", controllerId);
 
  45     public CountQuery(Entity e, String countField, String controllerId) {
 
  47         this.countField = countField;
 
  48         this.filters = new ArrayList<>();
 
  49         if (controllerId != null) {
 
  50             this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId);
 
  54     public CountQuery(Entity e, EntityInput input) {
 
  58     public CountQuery(Entity e, EntityInput input, String controllerId) {
 
  60         Map<FilterKey, Filter> filter = input != null ? input.getFilter() : null;
 
  61         if (filter != null && filter.size() > 0) {
 
  62            this.filters.addAll(filters);
 
  64         if (controllerId != null) {
 
  65             this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId);
 
  69     public void addFilter(String property, String filtervalue) {
 
  70         this.filters.add(new FilterBuilder().setProperty(property).setFiltervalue(filtervalue).build());
 
  75     public String toSql() {
 
  76         StringBuilder sb = new StringBuilder();
 
  77         sb.append(String.format("SELECT COUNT(`%s`) FROM `%s`", this.countField, this.entity.getName()));
 
  78         sb.append(SqlQuery.getWhereExpression(this.filters));