migrate sdnr features to phosphorus
[ccsdk/features.git] / sdnr / wt / data-provider / dblib / src / main / java / org / onap / ccsdk / features / sdnr / wt / dataprovider / database / sqldb / database / SqlDBReaderWriter.java
index b5322d6..56e965c 100644 (file)
@@ -57,34 +57,47 @@ public class SqlDBReaderWriter<T extends DataObject> {
     protected final Entity entity;
     private final Class<T> clazz;
     protected final SqlDBClient dbService;
-    private final String dbName;
     protected final String controllerId;
     protected final String tableName;
+    private final boolean ignoreControllerId;
 
-    public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz, String dbName,
+    public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
             String controllerId) {
+        this(dbService, e, dbSuffix, clazz, controllerId, false);
+    }
+
+    public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
+            String controllerId, boolean ignoreControllerId) {
         this.dbService = dbService;
         this.entity = e;
         this.clazz = clazz;
-        this.dbName = dbName;
         this.tableName = this.entity.getName() + dbSuffix;
         this.controllerId = controllerId;
+        this.ignoreControllerId = ignoreControllerId;
     }
 
     public long count(List<Filter> filter) throws SQLException {
         String query;
         if (filter == null || filter.isEmpty()) {
-            query = String.format("SELECT table_rows FROM `information_schema`.`tables` "
-                    + "WHERE `table_schema` = '%s' AND `table_name` = '%s'", this.dbName, this.tableName);
+            //            query = String.format("SELECT table_rows FROM `information_schema`.`tables` "
+            //                    + "WHERE `table_schema` = '%s' AND `table_name` = '%s'", this.dbName, this.tableName);
+            query = String.format("SELECT COUNT(`id`) FROM `%s`", this.tableName);
         } else {
             query = String.format("SELECT COUNT(`id`) FROM `%s` %s", this.tableName,
                     SqlQuery.getWhereExpression(filter));
         }
         ResultSet data = this.dbService.read(query);
+        if(data==null) {
+            return 0;
+        }
         long cnt = 0;
         if (data.next()) {
             cnt = data.getLong(1);
         }
+        try {
+            data.close();
+        } catch (SQLException ignore) {
+        }
         return cnt;
     }
 
@@ -106,11 +119,19 @@ public class SqlDBReaderWriter<T extends DataObject> {
 
     public QueryResult<T> getData(EntityInput input) {
         SelectQuery query = new SelectQuery(this.tableName, input, this.controllerId);
-        LOG.trace("query={}", query.toSql());
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         try {
             ResultSet data = this.dbService.read(query.toSql());
             List<T> mappedData = SqlDBMapper.read(data, clazz);
             final Map<FilterKey, Filter> filter = input.getFilter();
+            try {
+                if(data!=null) {
+                    data.close();
+                }
+            } catch (SQLException ignore) {
+            }
             long total = this.count(filter != null ? new ArrayList<>(filter.values()) : null, this.controllerId);
             return new QueryResult<T>(mappedData, query.getPage(), query.getPageSize(), total);
         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
@@ -126,8 +147,11 @@ public class SqlDBReaderWriter<T extends DataObject> {
         if (id == null) {
             return this.writeWithoutId(object);
         }
-        InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId);
+        InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId);
         query.setId(id);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         boolean success = false;
         try {
             success = this.dbService.write(query.toSql());
@@ -140,7 +164,11 @@ public class SqlDBReaderWriter<T extends DataObject> {
 
     private <S extends DataObject> String writeWithoutId(S object) {
 
-        InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId);
+        InsertQuery<S> query =
+                new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         try {
             return this.dbService.writeAndReturnId(query.toSql());
         } catch (SQLException e) {
@@ -150,8 +178,11 @@ public class SqlDBReaderWriter<T extends DataObject> {
     }
 
     public <S extends DataObject> String update(S object, String id) {
-        UpdateQuery<S> query = new UpdateQuery<S>(this.entity, object, this.controllerId);
+        UpdateQuery<S> query = new UpdateQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true);
         query.setId(id);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         String insertedId = null;
         PreparedStatement stmt = null;
         try {
@@ -164,6 +195,9 @@ public class SqlDBReaderWriter<T extends DataObject> {
             if (affectedRows > 0) {
                 insertedId = id;
             }
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("insertedid={}", insertedId);
+            }
         } catch (SQLException e) {
             LOG.warn("problem writing data into db: ", e);
         } finally {
@@ -180,10 +214,12 @@ public class SqlDBReaderWriter<T extends DataObject> {
     }
 
     public <S extends DataObject> String updateOrInsert(S object, String id) {
-        UpsertQuery<S> query = new UpsertQuery<S>(this.entity, object, this.controllerId);
+        UpsertQuery<S> query = new UpsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true);
         query.setId(id);
         String insertedId = null;
-        LOG.trace("query={}", query.toSql());
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         PreparedStatement stmt = null;
         try {
             Connection connection = this.dbService.getConnection();
@@ -221,6 +257,9 @@ public class SqlDBReaderWriter<T extends DataObject> {
 
     public int remove(List<Filter> filters) {
         DeleteQuery query = new DeleteQuery(this.entity, filters);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         int affectedRows = 0;
         PreparedStatement stmt = null;
         try {
@@ -249,9 +288,16 @@ public class SqlDBReaderWriter<T extends DataObject> {
 
     public <S extends DataObject> List<S> readAll(Class<S> clazz) {
         SelectQuery query = new SelectQuery(this.tableName);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         try {
             ResultSet data = this.dbService.read(query.toSql());
             List<S> mappedData = SqlDBMapper.read(data, clazz);
+            try {
+                data.close();
+            } catch (SQLException ignore) {
+            }
             return mappedData;
         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
@@ -262,9 +308,16 @@ public class SqlDBReaderWriter<T extends DataObject> {
 
     public List<String> readAll(String key) {
         SelectQuery query = new SelectQuery(this.tableName, key, this.controllerId).groupBy(key);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
         try {
             ResultSet data = this.dbService.read(query.toSql());
             List<String> mappedData = SqlDBMapper.read(data, String.class, key);
+            try {
+                data.close();
+            } catch (SQLException ignore) {
+            }
             return mappedData;
         } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
                 | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
@@ -272,4 +325,22 @@ public class SqlDBReaderWriter<T extends DataObject> {
         }
         return null;
     }
+
+    public T read(String id) {
+        SelectQuery query =
+                new SelectQuery(this.tableName, this.controllerId).addFilter(SqlDBMapper.ID_DBCOL, id);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("query={}", query.toSql());
+        }
+        T item = null;
+        try {
+            ResultSet data = this.dbService.read(query.toSql());
+            List<T> mappedData = SqlDBMapper.read(data, clazz);
+            item = mappedData.size()>0? mappedData.get(0): null;
+        } catch (SQLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
+                | InstantiationException | SecurityException | NoSuchMethodException | JsonProcessingException e) {
+            LOG.warn("problem reading data {}: ", this.entity, e);
+        }
+        return item;
+    }
 }