7 changed files with 251 additions and 11 deletions
@ -0,0 +1,38 @@
|
||||
package org.leolo.nrdatad.db; |
||||
|
||||
import org.leolo.nrdatad.model.ScheduleAssociation; |
||||
|
||||
import java.sql.SQLException; |
||||
|
||||
public abstract class TrainAssociationDao extends BaseDao{ |
||||
|
||||
|
||||
|
||||
public TrainAssociationDao(DatabaseManager manager) { |
||||
super(manager); |
||||
} |
||||
|
||||
public abstract void insert(ScheduleAssociation scheduleAssociation) throws SQLException; |
||||
|
||||
public abstract boolean hasTrainAssociation(String auid) throws SQLException; |
||||
|
||||
public final boolean hasTrainAssociation(ScheduleAssociation scheduleAssociation) throws SQLException { |
||||
return hasTrainAssociation(scheduleAssociation.getAuid()); |
||||
} |
||||
|
||||
public abstract void update(ScheduleAssociation scheduleAssociation) throws SQLException; |
||||
|
||||
public abstract void updateLastCheck(String auid) throws SQLException; |
||||
|
||||
public final void updateLastCheck(ScheduleAssociation scheduleAssociation) throws SQLException{ |
||||
updateLastCheck(scheduleAssociation.getAuid()); |
||||
} |
||||
|
||||
public abstract ReplaceResult replace(ScheduleAssociation scheduleAssociation) throws SQLException; |
||||
|
||||
public abstract int getHashCode(String auid) throws SQLException; |
||||
|
||||
public final int getHashCode(ScheduleAssociation scheduleAssociation) throws SQLException{ |
||||
return getHashCode(scheduleAssociation.getAuid()); |
||||
} |
||||
} |
||||
@ -0,0 +1,137 @@
|
||||
package org.leolo.nrdatad.db.mariadb; |
||||
|
||||
import org.leolo.nrdatad.db.DatabaseManager; |
||||
import org.leolo.nrdatad.db.TrainAssociationDao; |
||||
import org.leolo.nrdatad.exception.NoSuchRecordException; |
||||
import org.leolo.nrdatad.model.ScheduleAssociation; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.PreparedStatement; |
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
|
||||
public class TrainAssociationDaoImpl extends TrainAssociationDao { |
||||
|
||||
public TrainAssociationDaoImpl(DatabaseManager manager) { |
||||
super(manager); |
||||
} |
||||
|
||||
@Override |
||||
public void insert(ScheduleAssociation scheduleAssociation) throws SQLException { |
||||
try ( |
||||
Connection conn = getConnection(); |
||||
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO train_association (" + |
||||
"auid, main_uid, asso_uid, start_date, end_date, " + //1-5
|
||||
"asso_days, asso_type, date_ind, asso_loc, base_suffix, " + //6-10
|
||||
"asso_suffix, stp_ind, hash_code, last_chk) " + //11-14
|
||||
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())") |
||||
){ |
||||
setString(pstmt, 1, scheduleAssociation.getAuid()); |
||||
setString(pstmt, 2, scheduleAssociation.getMainUid()); |
||||
setString(pstmt, 3, scheduleAssociation.getAssoUid()); |
||||
setDate (pstmt, 4, scheduleAssociation.getStartDate()); |
||||
setDate (pstmt, 5, scheduleAssociation.getEndDate()); |
||||
setString(pstmt, 6, scheduleAssociation.getAssoDays()); |
||||
setString(pstmt, 7, scheduleAssociation.getAssociationCategory().getCode()); |
||||
setInt (pstmt, 8, scheduleAssociation.getAssociationDate()); |
||||
setString(pstmt, 9, scheduleAssociation.getAssociationLocation()); |
||||
setString(pstmt, 10, scheduleAssociation.getBaseLocationSuffix()); |
||||
setString(pstmt, 11, scheduleAssociation.getAssocLocationSuffix()); |
||||
setString(pstmt, 12, scheduleAssociation.getStpIndicator().getCode()); |
||||
setInt (pstmt, 13, scheduleAssociation.hashCode()); |
||||
pstmt.executeUpdate(); |
||||
conn.commit(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean hasTrainAssociation(String auid) throws SQLException { |
||||
try( |
||||
Connection conn = getConnection(); |
||||
PreparedStatement pstmt = conn.prepareStatement("SELECT 1 FROM train_association WHERE auid = ?") |
||||
){ |
||||
setString(pstmt, 1, auid); |
||||
try(ResultSet rs = pstmt.executeQuery()){ |
||||
return rs.next(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void update(ScheduleAssociation scheduleAssociation) throws SQLException { |
||||
try( |
||||
Connection conn = getConnection(); |
||||
PreparedStatement pstmt = conn.prepareStatement("UPDATE train_association SET " + |
||||
"main_uid=?, asso_uid=?, start_date=?, end_date=?, asso_days=?," + |
||||
"asso_type=?,date_ind=?,asso_loc=?,base_suffix=?, asso_suffix=?," + |
||||
"stp_ind=?, hash_code=?,last_chk=NOW() WHERE auid=?") |
||||
){ |
||||
setString(pstmt, 1, scheduleAssociation.getMainUid()); |
||||
setString(pstmt, 2, scheduleAssociation.getAssoUid()); |
||||
setDate (pstmt, 3, scheduleAssociation.getStartDate()); |
||||
setDate (pstmt, 4, scheduleAssociation.getEndDate()); |
||||
setString(pstmt, 5, scheduleAssociation.getAssoDays()); |
||||
setString(pstmt, 6, scheduleAssociation.getAssociationCategory().getCode()); |
||||
setInt (pstmt, 7, scheduleAssociation.getAssociationDate()); |
||||
setString(pstmt, 8, scheduleAssociation.getAssociationLocation()); |
||||
setString(pstmt, 9, scheduleAssociation.getBaseLocationSuffix()); |
||||
setString(pstmt, 10, scheduleAssociation.getAssocLocationSuffix()); |
||||
setString(pstmt, 11, scheduleAssociation.getStpIndicator().getCode()); |
||||
setInt (pstmt, 12, scheduleAssociation.hashCode()); |
||||
setString(pstmt, 13, scheduleAssociation.getAuid()); |
||||
pstmt.executeUpdate(); |
||||
conn.commit(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void updateLastCheck(String auid) throws SQLException { |
||||
try( |
||||
Connection conn = getConnection(); |
||||
PreparedStatement pstmt = conn.prepareStatement("UPDATE train_association SET last_chk = NOW() WHERE auid = ?") |
||||
){ |
||||
setString(pstmt,1, auid); |
||||
pstmt.executeUpdate(); |
||||
conn.commit(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public ReplaceResult replace(ScheduleAssociation scheduleAssociation) throws SQLException { |
||||
Object checkResult = _getHashCode(scheduleAssociation.getAuid()); |
||||
if(checkResult==null){ |
||||
insert(scheduleAssociation); |
||||
return ReplaceResult.INSERTED; |
||||
} else if (((int)checkResult) == scheduleAssociation.hashCode()){ |
||||
updateLastCheck(scheduleAssociation); |
||||
return ReplaceResult.NO_ACTION; |
||||
} else { |
||||
update(scheduleAssociation); |
||||
return ReplaceResult.UPDATED; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public int getHashCode(String auid) throws SQLException { |
||||
Object result = _getHashCode(auid); |
||||
if(result instanceof Integer){ |
||||
return (Integer) result; |
||||
} |
||||
throw new NoSuchRecordException(); |
||||
} |
||||
|
||||
private Object _getHashCode(String auid) throws SQLException { |
||||
try( |
||||
Connection conn = getConnection(); |
||||
PreparedStatement pstmt = conn.prepareStatement("SELECT hash_code FROM train_association WHERE auid = ?") |
||||
){ |
||||
setString(pstmt, 1, auid); |
||||
try (ResultSet rs = pstmt.executeQuery()){ |
||||
if(rs.next()){ |
||||
return rs.getInt(1); |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@
|
||||
package org.leolo.nrdatad.exception; |
||||
|
||||
import java.sql.SQLException; |
||||
|
||||
public class NoSuchRecordException extends SQLException { |
||||
|
||||
public NoSuchRecordException(String reason, String SQLState, int vendorCode) { |
||||
super(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public NoSuchRecordException(String reason, String SQLState) { |
||||
super(reason, SQLState); |
||||
} |
||||
|
||||
public NoSuchRecordException(String reason) { |
||||
super(reason); |
||||
} |
||||
|
||||
public NoSuchRecordException() { |
||||
} |
||||
|
||||
public NoSuchRecordException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
|
||||
public NoSuchRecordException(String reason, Throwable cause) { |
||||
super(reason, cause); |
||||
} |
||||
|
||||
public NoSuchRecordException(String reason, String sqlState, Throwable cause) { |
||||
super(reason, sqlState, cause); |
||||
} |
||||
|
||||
public NoSuchRecordException(String reason, String sqlState, int vendorCode, Throwable cause) { |
||||
super(reason, sqlState, vendorCode, cause); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue