preload db queries

4.x.x
ayyoob 8 years ago
parent 44ac6b13b5
commit 0f66c90660

@ -41,11 +41,17 @@ public class DeviceTypePluginDAO {
private static final Log log = LogFactory.getLog(DeviceTypePluginDAO.class); private static final Log log = LogFactory.getLog(DeviceTypePluginDAO.class);
private DeviceTypeDAOHandler deviceTypeDAOHandler; private DeviceTypeDAOHandler deviceTypeDAOHandler;
private DeviceDAODefinition deviceDAODefinition; private DeviceDAODefinition deviceDAODefinition;
private String selectDBQueryForGetDevice;
private String createDBqueryForAddDevice;
private String updateDBQueryForUpdateDevice;
private String deleteDBQueryToRemoveDevicd;
private String selectDBQueryToGetAllDevice;
public DeviceTypePluginDAO(DeviceDAODefinition deviceDAODefinition, public DeviceTypePluginDAO(DeviceDAODefinition deviceDAODefinition,
DeviceTypeDAOHandler deviceTypeDAOHandler) { DeviceTypeDAOHandler deviceTypeDAOHandler) {
this.deviceTypeDAOHandler = deviceTypeDAOHandler; this.deviceTypeDAOHandler = deviceTypeDAOHandler;
this.deviceDAODefinition = deviceDAODefinition; this.deviceDAODefinition = deviceDAODefinition;
initializeDbQueries();
} }
public Device getDevice(String deviceId) throws DeviceTypeMgtPluginException { public Device getDevice(String deviceId) throws DeviceTypeMgtPluginException {
@ -55,9 +61,7 @@ public class DeviceTypePluginDAO {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
conn = deviceTypeDAOHandler.getConnection(); conn = deviceTypeDAOHandler.getConnection();
String selectDBQuery = "SELECT " + getDeviceTableColumnNames() + " FROM " + stmt = conn.prepareStatement(selectDBQueryForGetDevice);
deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?";
stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, deviceId); stmt.setString(1, deviceId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -96,10 +100,7 @@ public class DeviceTypePluginDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = deviceTypeDAOHandler.getConnection(); conn = deviceTypeDAOHandler.getConnection();
String createDBQuery = "INSERT INTO " + deviceDAODefinition.getDeviceTableName() + "(" stmt = conn.prepareStatement(createDBqueryForAddDevice);
+ deviceDAODefinition.getPrimaryKey() + " , " + getDeviceTableColumnNames() + ") VALUES ("
+ getPreparedInputString(deviceDAODefinition.getColumnNames().size() + 1) + ")";
stmt = conn.prepareStatement(createDBQuery);
stmt.setString(1, device.getDeviceIdentifier()); stmt.setString(1, device.getDeviceIdentifier());
int columnIndex = 2; int columnIndex = 2;
for (String columnName : deviceDAODefinition.getColumnNames()) { for (String columnName : deviceDAODefinition.getColumnNames()) {
@ -131,11 +132,7 @@ public class DeviceTypePluginDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = deviceTypeDAOHandler.getConnection(); conn = deviceTypeDAOHandler.getConnection();
String updateDBQuery = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET " stmt = conn.prepareStatement(updateDBQueryForUpdateDevice);
+ getDeviceTableColumnNamesForUpdateQuery()+ " WHERE " + deviceDAODefinition.getPrimaryKey()
+ " = ?";
stmt = conn.prepareStatement(updateDBQuery);
int columnIndex = 1; int columnIndex = 1;
for (String columnName : deviceDAODefinition.getColumnNames()) { for (String columnName : deviceDAODefinition.getColumnNames()) {
stmt.setString(columnIndex, getPropertString(device.getProperties(), columnName)); stmt.setString(columnIndex, getPropertString(device.getProperties(), columnName));
@ -166,9 +163,7 @@ public class DeviceTypePluginDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = deviceTypeDAOHandler.getConnection(); conn = deviceTypeDAOHandler.getConnection();
String deleteDBQuery = "DELETE FROM " + deviceDAODefinition.getDeviceTableName() stmt = conn.prepareStatement(deleteDBQueryToRemoveDevicd);
+ " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?";
stmt = conn.prepareStatement(deleteDBQuery);
stmt.setString(1, deviceId); stmt.setString(1, deviceId);
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
if (rows > 0) { if (rows > 0) {
@ -197,9 +192,7 @@ public class DeviceTypePluginDAO {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
try { try {
conn = deviceTypeDAOHandler.getConnection(); conn = deviceTypeDAOHandler.getConnection();
String selectDBQuery = "SELECT " + getDeviceTableColumnNames() + " FROM " stmt = conn.prepareStatement(selectDBQueryToGetAllDevice);
+ deviceDAODefinition.getDeviceTableName();
stmt = conn.prepareStatement(selectDBQuery);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
device = new Device(); device = new Device();
@ -254,4 +247,23 @@ public class DeviceTypePluginDAO {
} }
return null; return null;
} }
private void initializeDbQueries() {
selectDBQueryForGetDevice = "SELECT " + getDeviceTableColumnNames() + " FROM " +
deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?";
createDBqueryForAddDevice = "INSERT INTO " + deviceDAODefinition.getDeviceTableName() + "("
+ deviceDAODefinition.getPrimaryKey() + " , " + getDeviceTableColumnNames() + ") VALUES ("
+ getPreparedInputString(deviceDAODefinition.getColumnNames().size() + 1) + ")";
updateDBQueryForUpdateDevice = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET "
+ getDeviceTableColumnNamesForUpdateQuery()+ " WHERE " + deviceDAODefinition.getPrimaryKey()
+ " = ?";
deleteDBQueryToRemoveDevicd = "DELETE FROM " + deviceDAODefinition.getDeviceTableName()
+ " WHERE " + deviceDAODefinition.getPrimaryKey() + " = ?";
selectDBQueryToGetAllDevice = "SELECT " + getDeviceTableColumnNames() + " FROM "
+ deviceDAODefinition.getDeviceTableName();
}
} }
Loading…
Cancel
Save