@@ -41,6 +41,7 @@ public class CobolIndexedFile extends CobolFile {
4141 private boolean indexedFirstRead = true ;
4242 private boolean callStart = false ;
4343 private boolean commitOnModification = true ;
44+ private int fetchKeyIndex = -1 ;
4445
4546 /** TODO: 準備中 */
4647 public static final int COB_EQ = 1 ;
@@ -295,6 +296,7 @@ public int open_(String filename, int mode, int sharing) {
295296 this .indexedFirstRead = true ;
296297 this .callStart = false ;
297298
299+ this .fetchKeyIndex = -1 ;
298300 return 0 ;
299301 }
300302
@@ -345,6 +347,7 @@ public int close_(int opt) {
345347 } catch (SQLException e ) {
346348 return COB_STATUS_30_PERMANENT_ERROR ;
347349 }
350+ this .fetchKeyIndex = -1 ;
348351 return COB_STATUS_00_SUCCESS ;
349352 }
350353
@@ -366,6 +369,7 @@ public int indexed_start_internal(
366369 break ;
367370 }
368371 }
372+ this .fetchKeyIndex = p .key_index ;
369373
370374 p .key = DBT_SET (key );
371375
@@ -545,8 +549,12 @@ private int returnWith(IndexedFile p, boolean closeCursor, int index, int return
545549 return returnCode ;
546550 }
547551
548- /** Equivalent to indexed_write_internal in libcob/fileio.c */
549552 private int indexed_write_internal (boolean rewrite , int opt ) {
553+ return this .indexed_write_internal (rewrite , null , opt );
554+ }
555+
556+ /** Equivalent to indexed_write_internal in libcob/fileio.c */
557+ private int indexed_write_internal (boolean rewrite , int [] dupNumbers , int opt ) {
550558 IndexedFile p = this .filei ;
551559
552560 boolean closeCursor ;
@@ -592,7 +600,12 @@ private int indexed_write_internal(boolean rewrite, int opt) {
592600
593601 PreparedStatement insertStatement ;
594602 if (isDuplicateColumn (i )) {
595- int dupNo = getNextKeyDupNo (p .connection , i , p .key );
603+ int dupNo ;
604+ if (dupNumbers == null || dupNumbers [i ] < 0 || i != this .fetchKeyIndex ) {
605+ dupNo = getNextKeyDupNo (p .connection , i , p .key );
606+ } else {
607+ dupNo = dupNumbers [i ];
608+ }
596609 insertStatement =
597610 p .connection .prepareStatement (
598611 String .format (
@@ -693,19 +706,25 @@ public int rewrite_(int opt) {
693706 }
694707
695708 p .key = DBT_SET (this .keys [0 ].getField ());
709+ int [] dupNumbers = new int [this .nkeys ];
710+ java .util .Arrays .fill (dupNumbers , -1 );
696711
697- int ret = this .indexed_delete_internal (true );
712+ int ret = this .indexed_delete_internal (true , dupNumbers );
698713
699714 if (ret != COB_STATUS_00_SUCCESS ) {
700715 p .write_cursor_open = false ;
701716 return ret ;
702717 }
703718
704- return this .indexed_write_internal (true , opt );
719+ return this .indexed_write_internal (true , dupNumbers , opt );
705720 }
706721
707- /** Equivalent to indexed_delete_internal in libcob/fileio.c */
708722 private int indexed_delete_internal (boolean rewrite ) {
723+ return this .indexed_delete_internal (rewrite , null );
724+ }
725+
726+ /** Equivalent to indexed_delete_internal in libcob/fileio.c */
727+ private int indexed_delete_internal (boolean rewrite , int [] dupNumbers ) {
709728 IndexedFile p = this .filei ;
710729 boolean closeCursor ;
711730
@@ -731,6 +750,25 @@ private int indexed_delete_internal(boolean rewrite) {
731750
732751 // delete data from sub tables
733752 for (int i = 1 ; i < this .nkeys ; ++i ) {
753+ // save the duplicate number of the record to be deleted
754+ if (isDuplicateColumn (i )) {
755+ try (PreparedStatement statement =
756+ p .connection .prepareStatement (
757+ String .format (
758+ "select dupNo from %s where value = ?" , getTableName (i )))) {
759+ statement .setBytes (1 , p .key );
760+ ResultSet rs = statement .executeQuery ();
761+ if (rs .next ()) {
762+ int dupNo = rs .getInt (1 );
763+ if (dupNumbers != null ) {
764+ dupNumbers [i ] = dupNo ;
765+ }
766+ }
767+ } catch (SQLException e ) {
768+ return returnWith (p , closeCursor , 0 , COB_STATUS_30_PERMANENT_ERROR );
769+ }
770+ }
771+ // delete the record
734772 try (PreparedStatement statement =
735773 p .connection .prepareStatement (
736774 String .format ("delete from %s where value = ?" , getTableName (i )))) {
0 commit comments