diff options
author | ning.xi <ning.xi@est.tech> | 2019-12-26 21:37:52 +0800 |
---|---|---|
committer | ning.xi <ning.xi@est.tech> | 2020-01-08 11:15:36 +0800 |
commit | 86374588a6103114b1f0f335a1b65da1ad0817a5 (patch) | |
tree | 5256c00f48e6c145bbf670db64e7dbc9e2dbe14d /models-dao | |
parent | f69b6681486e4d1c5859f649a3293488c1859712 (diff) |
update Db provider to support query number
Issue-ID: POLICY-1629
Signed-off-by: ning.xi <ning.xi@est.tech>
Change-Id: I42c9542adc44d52b760a70e710ab4cc6fa8a1b3b
Diffstat (limited to 'models-dao')
-rw-r--r-- | models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java | 8 | ||||
-rw-r--r-- | models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java | 18 |
2 files changed, 20 insertions, 6 deletions
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java index 692aa08bf..062ec4662 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -162,11 +162,13 @@ public interface PfDao { * <= endTime. null for ignore start time. * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <= * endTime. null for ignore end time - * @filterMap Map store extra key/value used to filter from database, can be null. + * @param filterMap Map store extra key/value used to filter from database, can be null. + * @param sortOrder sortOrder to query database + * @param getRecordNum Total query count from database * @return the objects that was retrieved from the database */ <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version, Date startTime, - Date endTime, Map<String, Object> filterMap); + Date endTime, Map<String, Object> filterMap, String sortOrder, int getRecordNum); /** * Get an object from the database, referred to by concept key. diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java index e98266706..b3d72a3fd 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -66,9 +66,11 @@ public class DefaultPfDao implements PfDao { private static final String WHERE = " WHERE "; private static final String AND = " AND "; + private static final String ORDER = " ORDER BY "; private static final String NAME_FILTER = "c.key.name = :name"; private static final String VERSION_FILTER = "c.key.version = :version"; + private static final String TIMESTAMP_FILTER = "c.key.timeStamp = :timeStamp"; private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime"; private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime"; private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; @@ -80,6 +82,9 @@ public class DefaultPfDao implements PfDao { private static final String DELETE_BY_CONCEPT_KEY = DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; + private static final String DELETE_BY_TIMESTAMP_KEY = + DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER + AND + TIMESTAMP_FILTER; + private static final String DELETE_BY_REFERENCE_KEY = DELETE_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER; @@ -221,7 +226,7 @@ public class DefaultPfDao implements PfDao { try { // @formatter:off mg.getTransaction().begin(); - mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass) + mg.createQuery(setQueryTable(DELETE_BY_TIMESTAMP_KEY, someClass), someClass) .setParameter(NAME, key.getName()) .setParameter(VERSION, key.getVersion()) .setParameter(TIMESTAMP, key.getTimeStamp()) @@ -347,7 +352,8 @@ public class DefaultPfDao implements PfDao { @Override public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, final String version, - final Date startTime, final Date endTime, final Map<String, Object> filterMap) { + final Date startTime, final Date endTime, final Map<String, Object> filterMap, final String sortOrder, + final int getRecordNum) { final EntityManager mg = getEntityManager(); String filterQueryString = SELECT_FROM_TABLE + WHERE; @@ -361,6 +367,9 @@ public class DefaultPfDao implements PfDao { filterQueryString = bld.toString(); } filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); + if (getRecordNum > 0) { + filterQueryString += ORDER + " c.key.timeStamp " + sortOrder; + } TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); if (filterMap != null) { @@ -383,6 +392,9 @@ public class DefaultPfDao implements PfDao { query.setParameter("endTime", endTime); } } + if (getRecordNum > 0) { + query.setMaxResults(getRecordNum); + } LOGGER.error("filterQueryString is \"{}\"", filterQueryString); return query.getResultList(); |