c499fefb8f94af5fbcd1798ebb16ff65694614ae
[ccsdk/features.git] / sdnr / wt / data-provider / dblib / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / database / sqldb / query / CountQuery.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
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;
33
34 public class CountQuery implements SqlQuery {
35
36     private final Entity entity;
37     private final List<Filter> filters;
38     private final String countField;
39     public CountQuery(Entity e) {
40         this(e, "*", null);
41     }
42     public CountQuery(Entity e, String controllerId) {
43         this(e, "*", controllerId);
44     }
45     public CountQuery(Entity e, String countField, String controllerId) {
46         this.entity = e;
47         this.countField = countField;
48         this.filters = new ArrayList<>();
49         if (controllerId != null) {
50             this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId);
51         }
52     }
53
54     public CountQuery(Entity e, EntityInput input) {
55         this(e, input, null);
56     }
57
58     public CountQuery(Entity e, EntityInput input, String controllerId) {
59         this(e);
60         Map<FilterKey, Filter> filter = input != null ? input.getFilter() : null;
61         if (filter != null && filter.size() > 0) {
62            this.filters.addAll(filters);
63         }
64         if (controllerId != null) {
65             this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId);
66         }
67     }
68
69     public void addFilter(String property, String filtervalue) {
70         this.filters.add(new FilterBuilder().setProperty(property).setFiltervalue(filtervalue).build());
71
72     }
73
74     @Override
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));
79         return sb.toString();
80     }
81
82 }