From 2e8a70672464fb8b156ed3f258fbaede0f1d5435 Mon Sep 17 00:00:00 2001 From: ausam riyaaz Date: Fri, 18 Oct 2019 11:40:49 +0000 Subject: [PATCH] Fix MsSQL syntax issues. --- .../OracleApplicationReleaseDAOImpl.java | 44 +++++++++++++++++++ .../SQLServerApplicationReleaseDAOImpl.java | 44 +++++++++++++++++++ .../dao/impl/review/OracleReviewDAOImpl.java | 37 ++++++++++++++++ .../impl/review/SQLServerReviewDAOImpl.java | 41 ++++++++++++++++- .../impl/device/SQLServerDeviceDAOImpl.java | 2 +- .../main/resources/dbscripts/cdm/mssql.sql | 9 ++-- 6 files changed, 169 insertions(+), 8 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/OracleApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/OracleApplicationReleaseDAOImpl.java index 07fc421771..d030fa82b7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/OracleApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/OracleApplicationReleaseDAOImpl.java @@ -17,8 +17,52 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + /** * This handles Application Release operations which are specific to MSSQL. */ public class OracleApplicationReleaseDAOImpl extends GenericApplicationReleaseDAOImpl { + private static final Log log = LogFactory.getLog(GenericApplicationReleaseDAOImpl.class); + + public boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId, String inactiveState) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Verifying application release existence for package name:" + packageName); + } + String sql = "SELECT AR.ID AS RELEASE_ID " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.PACKAGE_NAME = ? AND " + + "AR.CURRENT_STATE != ? AND " + + "AR.TENANT_ID = ? ORDER BY AR.ID OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, packageName); + stmt.setString(2, inactiveState); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return rs.next(); + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to verify the existence of package name for " + + "active application release. Package name: " + packageName; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL error occurred while verifying the existence of package name for active application " + + "release. package name: " + packageName; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/SQLServerApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/SQLServerApplicationReleaseDAOImpl.java index a19cab152c..4afcdf0928 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/SQLServerApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/SQLServerApplicationReleaseDAOImpl.java @@ -17,8 +17,52 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + /** * This handles Application Release operations which are specific to MSSQL. */ public class SQLServerApplicationReleaseDAOImpl extends GenericApplicationReleaseDAOImpl { + private static final Log log = LogFactory.getLog(GenericApplicationReleaseDAOImpl.class); + + public boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId, String inactiveState) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Verifying application release existence for package name:" + packageName); + } + String sql = "SELECT AR.ID AS RELEASE_ID " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.PACKAGE_NAME = ? AND " + + "AR.CURRENT_STATE != ? AND " + + "AR.TENANT_ID = ? ORDER BY AR.ID OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, packageName); + stmt.setString(2, inactiveState); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return rs.next(); + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to verify the existence of package name for " + + "active application release. Package name: " + packageName; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL error occurred while verifying the existence of package name for active application " + + "release. package name: " + packageName; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/OracleReviewDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/OracleReviewDAOImpl.java index 7abec2ecc8..2231560930 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/OracleReviewDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/OracleReviewDAOImpl.java @@ -30,6 +30,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; import java.util.StringJoiner; @@ -197,4 +198,40 @@ public class OracleReviewDAOImpl extends GenericReviewDAOImpl { throw new ReviewManagementDAOException(msg, e); } } + public List getAllAppRatingValues(List uuids, int tenantId) throws ReviewManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("DAO request is received to Get all application rating values of an application."); + } + try { + int index = 1; + Connection conn = this.getDBConnection(); + StringJoiner joiner = new StringJoiner(",", + "SELECT AP_APP_REVIEW.RATING AS RATING FROM AP_APP_REVIEW INNER JOIN AP_APP_RELEASE ON " + + "AP_APP_REVIEW.AP_APP_RELEASE_ID = AP_APP_RELEASE.ID WHERE AP_APP_RELEASE.UUID IN (", + ") AND AP_APP_REVIEW.ACTIVE_REVIEW = 'true' AND AP_APP_REVIEW.TENANT_ID = ?"); + uuids.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (String uuid : uuids) { + ps.setObject(index++, uuid); + } + ps.setInt(index, tenantId); + try (ResultSet rs = ps.executeQuery()) { + List reviews = new ArrayList<>(); + while (rs.next()) { + reviews.add(rs.getInt("RATING")); + } + return reviews; + } + } + } catch (DBConnectionException e) { + String msg = "Error occured while getting DB connection to retrieve all rating values for an application."; + log.error(msg, e); + throw new ReviewManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occured while executing SQL to get all rating values for the application."; + log.error(msg, e); + throw new ReviewManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/SQLServerReviewDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/SQLServerReviewDAOImpl.java index 4949e7f23a..d169a6190a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/SQLServerReviewDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/review/SQLServerReviewDAOImpl.java @@ -30,6 +30,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; import java.util.StringJoiner; @@ -112,7 +113,7 @@ public class SQLServerReviewDAOImpl extends GenericReviewDAOImpl { + "AP_APP_REVIEW.AP_APP_RELEASE_ID = AP_APP_RELEASE.ID " + "WHERE AP_APP_REVIEW.AP_APP_RELEASE_ID IN (", ") AND AP_APP_REVIEW.ROOT_PARENT_ID = ? AND " - + "AP_APP_REVIEW.ACTIVE_REVIEW = true AND " + + "AP_APP_REVIEW.ACTIVE_REVIEW = 'true' AND " + "AP_APP_REVIEW.TENANT_ID = ? " + "ORDER BY AP_APP_REVIEW.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); releaseIds.stream().map(ignored -> "?").forEach(joiner::add); @@ -166,7 +167,7 @@ public class SQLServerReviewDAOImpl extends GenericReviewDAOImpl { + "AP_APP_REVIEW.AP_APP_RELEASE_ID = AP_APP_RELEASE.ID " + "WHERE AP_APP_REVIEW.AP_APP_RELEASE_ID IN (", ") AND AP_APP_REVIEW.ROOT_PARENT_ID = ? AND " - + "AP_APP_REVIEW.ACTIVE_REVIEW = true AND " + + "AP_APP_REVIEW.ACTIVE_REVIEW = 'true' AND " + "AP_APP_REVIEW.USERNAME = ? AND " + "AP_APP_REVIEW.TENANT_ID = ? " + "ORDER BY AP_APP_REVIEW.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); @@ -197,4 +198,40 @@ public class SQLServerReviewDAOImpl extends GenericReviewDAOImpl { throw new ReviewManagementDAOException(msg, e); } } + public List getAllAppRatingValues(List uuids, int tenantId) throws ReviewManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("DAO request is received to Get all application rating values of an application."); + } + try { + int index = 1; + Connection conn = this.getDBConnection(); + StringJoiner joiner = new StringJoiner(",", + "SELECT AP_APP_REVIEW.RATING AS RATING FROM AP_APP_REVIEW INNER JOIN AP_APP_RELEASE ON " + + "AP_APP_REVIEW.AP_APP_RELEASE_ID = AP_APP_RELEASE.ID WHERE AP_APP_RELEASE.UUID IN (", + ") AND AP_APP_REVIEW.ACTIVE_REVIEW = 'true' AND AP_APP_REVIEW.TENANT_ID = ?"); + uuids.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (String uuid : uuids) { + ps.setObject(index++, uuid); + } + ps.setInt(index, tenantId); + try (ResultSet rs = ps.executeQuery()) { + List reviews = new ArrayList<>(); + while (rs.next()) { + reviews.add(rs.getInt("RATING")); + } + return reviews; + } + } + } catch (DBConnectionException e) { + String msg = "Error occured while getting DB connection to retrieve all rating values for an application."; + log.error(msg, e); + throw new ReviewManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occured while executing SQL to get all rating values for the application."; + log.error(msg, e); + throw new ReviewManagementDAOException(msg, e); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 2c7dccc4ee..e8517b00cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -619,7 +619,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { sql = sql + " AND e.OWNERSHIP = ?"; } - sql = sql + " LIMIT ?,?"; + sql = sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (Connection conn = this.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index c87b947eca..838528e65c 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -480,7 +480,7 @@ CREATE TABLE DM_DEVICE_INFO ( VALUE_FIELD VARCHAR(1000) NULL, PRIMARY KEY (ID), INDEX DM_DEVICE_INFO_DEVICE_idx (DEVICE_ID ASC), - INDEX DM_DEVICE_INFO_DEVICE_ENROLLMENT_idx (ENROLMENT_ID ASC) + INDEX DM_DEVICE_INFO_DEVICE_ENROLLMENT_idx (ENROLMENT_ID ASC), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) @@ -529,8 +529,7 @@ CREATE TABLE DM_DEVICE_LOCATION ( ); IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_HISTORY_LAST_SEVEN_DAYS]') AND TYPE IN (N'U')) -CREATE TABLE IF NOT EXISTS DM_DEVICE_HISTORY_LAST_SEVEN_DAYS -( +CREATE TABLE DM_DEVICE_HISTORY_LAST_SEVEN_DAYS( ID INTEGER IDENTITY (1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, DEVICE_ID_NAME VARCHAR(255) NOT NULL, @@ -540,7 +539,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_HISTORY_LAST_SEVEN_DAYS LONGITUDE FLOAT NULL, SPEED FLOAT NULL, HEADING FLOAT NULL, - TIMESTAMP BIGINT(15) NOT NULL, + TIMESTAMP BIGINT NOT NULL, GEO_HASH VARCHAR(45) NULL, DEVICE_OWNER VARCHAR(45) NULL, DEVICE_ALTITUDE FLOAT NULL, @@ -663,4 +662,4 @@ DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID ORDER BY TENANT_ID, DEVICE_ID'); --- END OF DASHBOARD RELATED VIEWS -- +-- END OF DASHBOARD RELATED VIEWS -- \ No newline at end of file