Fix MsSQL syntax issues.

feature/appm-store/pbac
ausam riyaaz 5 years ago committed by Dharmakeerthi Lasantha
parent af0024016d
commit 2e8a706724

@ -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);
}
}
}

@ -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);
}
}
}

@ -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<Integer> getAllAppRatingValues(List<String> 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<Integer> 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);
}
}
}

@ -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<Integer> getAllAppRatingValues(List<String> 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<Integer> 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);
}
}
}

@ -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)) {

@ -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 --
Loading…
Cancel
Save