Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,24 @@ private static String concatString(String... strs) {
return sb.toString();
}

/**
* This method is mainly for unlocking the indexed files.
*
* @return true if post-processing is successful, false otherwise.
*/
protected boolean postProcess() {
return true;
}

private void runPostProcess(AbstractCobolField fnstatus) {
postProcess();
// TODO: Implement error handling
// boolean postProcessSucceeded = postProcess();
// if(!postProcessSucceeded) {
// this.saveStatus(COB_STATUS_30_PERMANENT_ERROR, fnstatus);
// }
}

/**
* TODO: 準備中
*
Expand Down Expand Up @@ -1408,21 +1426,25 @@ public void read(AbstractCobolField key, AbstractCobolField fnstatus, int readOp
if (this.flag_nonexistent) {
if (this.flag_first_read == 0) {
saveStatus(COB_STATUS_23_KEY_NOT_EXISTS, fnstatus);
runPostProcess(fnstatus);
return;
}
this.flag_first_read = 0;
saveStatus(COB_STATUS_10_END_OF_FILE, fnstatus);
runPostProcess(fnstatus);
return;
}

/* sequential read at the end of file is an error */
if (key == null) {
if (this.flag_end_of_file && (readOpts & COB_READ_PREVIOUS) == 0) {
saveStatus(COB_STATUS_46_READ_ERROR, fnstatus);
runPostProcess(fnstatus);
return;
}
if (this.flag_begin_of_file && (readOpts & COB_READ_PREVIOUS) != 0) {
saveStatus(COB_STATUS_46_READ_ERROR, fnstatus);
runPostProcess(fnstatus);
return;
}
}
Expand All @@ -1431,6 +1453,7 @@ public void read(AbstractCobolField key, AbstractCobolField fnstatus, int readOp
|| this.open_mode == COB_OPEN_OUTPUT
|| this.open_mode == COB_OPEN_EXTEND) {
saveStatus(COB_STATUS_47_INPUT_DENIED, fnstatus);
runPostProcess(fnstatus);
return;
}

Expand Down Expand Up @@ -1558,13 +1581,15 @@ public void write(AbstractCobolField rec, int opt, AbstractCobolField fnstatus)
|| this.open_mode == COB_OPEN_INPUT
|| this.open_mode == COB_OPEN_I_O) {
saveStatus(COB_STATUS_48_OUTPUT_DENIED, fnstatus);
runPostProcess(fnstatus);
return;
}
} else {
if (this.open_mode == COB_OPEN_CLOSED
|| this.open_mode == COB_OPEN_INPUT
|| this.open_mode == COB_OPEN_EXTEND) {
saveStatus(COB_STATUS_48_OUTPUT_DENIED, fnstatus);
runPostProcess(fnstatus);
return;
}
}
Expand All @@ -1578,6 +1603,7 @@ public void write(AbstractCobolField rec, int opt, AbstractCobolField fnstatus)

if (this.record.getSize() < this.record_min || this.record_max < this.record.getSize()) {
saveStatus(COB_STATUS_44_RECORD_OVERFLOW, fnstatus);
runPostProcess(fnstatus);
return;
}

Expand Down Expand Up @@ -1657,20 +1683,24 @@ public void rewrite(AbstractCobolField rec, int opt, AbstractCobolField fnstatus

if (this.open_mode == COB_OPEN_CLOSED || this.open_mode != COB_OPEN_I_O) {
saveStatus(COB_STATUS_49_I_O_DENIED, fnstatus);
runPostProcess(fnstatus);
return;
}
if (this.access_mode == COB_ACCESS_SEQUENTIAL && !readDone) {
saveStatus(COB_STATUS_43_READ_NOT_DONE, fnstatus);
runPostProcess(fnstatus);
return;
}
if (this.organization == COB_ORG_SEQUENTIAL) {
if (this.record.getSize() != rec.getSize()) {
saveStatus(COB_STATUS_44_RECORD_OVERFLOW, fnstatus);
runPostProcess(fnstatus);
return;
}
if (this.record_size != null) {
if (this.record.getSize() != this.record_size.getInt()) {
saveStatus(COB_STATUS_44_RECORD_OVERFLOW, fnstatus);
runPostProcess(fnstatus);
return;
}
}
Expand Down Expand Up @@ -1721,11 +1751,13 @@ public void delete(AbstractCobolField fnstatus) {

if (this.open_mode == COB_OPEN_CLOSED || this.open_mode != COB_OPEN_I_O) {
saveStatus(COB_STATUS_49_I_O_DENIED, fnstatus);
runPostProcess(fnstatus);
return;
}

if (this.access_mode == COB_ACCESS_SEQUENTIAL && !readDone) {
saveStatus(COB_STATUS_43_READ_NOT_DONE, fnstatus);
runPostProcess(fnstatus);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ public int open_(String filename, int mode, int sharing) {
return getConnectionStatus;
}

if(fileExists) {
if (fileExists) {
int code = this.checkVersionOld();
if(code != COB_STATUS_00_SUCCESS) {
if (code != COB_STATUS_00_SUCCESS) {
return code;
}
}
Expand Down Expand Up @@ -303,11 +303,12 @@ private int checkVersionOld() {
IndexedFile p = this.filei;
try (Statement st = p.connection.createStatement()) {
String fileLockTableExistsSql =
"select exists(select 1 from sqlite_master where type = 'table' and name = 'file_lock')";
"select exists(select 1 from sqlite_master where type = 'table' and name ="
+ " 'file_lock')";
ResultSet fileLockTableExistsResultSet = st.executeQuery(fileLockTableExistsSql);
if (fileLockTableExistsResultSet.next()) {
boolean fileLockTableExists = fileLockTableExistsResultSet.getInt(1) == 1;
if(!fileLockTableExists) {
if (!fileLockTableExists) {
return COB_STATUS_92_VERSION_INCOMPATIBLE;
}
} else {
Expand Down Expand Up @@ -700,15 +701,26 @@ private void unlockPreviousRecord() throws SQLException {
String updateSql =
String.format(
"update %s set locked_by = null, process_id = null, locked_at = null where"
+ " key = ?",
+ " key = ? and locked_by = ?",
getTableName(0));
try (PreparedStatement updateStatement = p.connection.prepareStatement(updateSql)) {
updateStatement.setBytes(1, previousLockedRecordKey);
updateStatement.setString(2, this.getProcessUuid());
updateStatement.executeUpdate();
}
previousLockedRecordKey = null;
}

@Override
protected boolean postProcess() {
try {
unlockPreviousRecord();
} catch (SQLException e) {
return false;
}
return true;
}

private void unlockPreviousRecord(byte[] key) throws SQLException {
if (previousLockedRecordKey == null) {
previousLockedRecordKey = key;
Expand All @@ -718,10 +730,11 @@ private void unlockPreviousRecord(byte[] key) throws SQLException {
String updateSql =
String.format(
"update %s set locked_by = null, process_id = null, locked_at = null where"
+ " key = ?",
+ " key = ? and locked_by = ?",
getTableName(0));
try (PreparedStatement updateStatement = p.connection.prepareStatement(updateSql)) {
updateStatement.setBytes(1, previousLockedRecordKey);
updateStatement.setString(2, this.getProcessUuid());
updateStatement.executeUpdate();
}
previousLockedRecordKey = key;
Expand Down Expand Up @@ -756,10 +769,14 @@ public int read_(AbstractCobolField key, int readOpts) {
try {
if (checkOtherProcessLockedRecord(primaryKey)) {
p.connection.rollback();
unlockPreviousRecord();
p.connection.commit();
return COB_STATUS_51_RECORD_LOCKED;
}
if (!lockRecord(primaryKey)) {
p.connection.rollback();
unlockPreviousRecord();
p.connection.commit();
return COB_STATUS_30_PERMANENT_ERROR;
}
unlockPreviousRecord(primaryKey);
Expand Down Expand Up @@ -894,10 +911,14 @@ public int readNext(int readOpts) {
try {
if (checkOtherProcessLockedRecord(primaryKey)) {
p.connection.rollback();
unlockPreviousRecord();
p.connection.commit();
return COB_STATUS_51_RECORD_LOCKED;
}
if (!lockRecord(primaryKey)) {
p.connection.rollback();
unlockPreviousRecord();
p.connection.commit();
return COB_STATUS_30_PERMANENT_ERROR;
}
unlockPreviousRecord(primaryKey);
Expand Down Expand Up @@ -1069,6 +1090,12 @@ public int write_(int opt) {
} else if (this.access_mode == COB_ACCESS_SEQUENTIAL) {
byte[] keyBytes = p.key;
if (p.last_key.memcmp(keyBytes, keyBytes.length) > 0) {
try {
unlockPreviousRecord();
p.connection.commit();
} catch (SQLException e) {
return COB_STATUS_30_PERMANENT_ERROR;
}
return COB_STATUS_21_KEY_INVALID;
}
}
Expand Down Expand Up @@ -1145,6 +1172,12 @@ public int rewrite_(int opt) {

if (this.access_mode == COB_ACCESS_SEQUENTIAL
&& !IndexedCursor.matchKeyHead(p.key, DBT_SET(this.keys[0].getField()))) {
try {
unlockPreviousRecord();
p.connection.commit();
} catch (SQLException e) {
return COB_STATUS_30_PERMANENT_ERROR;
}
return COB_STATUS_21_KEY_INVALID;
}

Expand Down
Loading
Loading