File: IdentitySequenceTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (843 lines) | stat: -rw-r--r-- 29,814 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.IdentitySequenceTest

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to you under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.tests.lang;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * Test for identity columns backed by sequence generators.
 */
public class IdentitySequenceTest extends GeneratedColumnsHelper
{
    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTANTS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    private static  final   String  BAD_NEXT_VALUE = "42XAR";
    private static  final   String  TABLE_DOESNT_HAVE_IDENTITY = "X0X81";

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // STATE
    //
    ///////////////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // CONSTRUCTOR
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * Create a new instance.
     */

    public IdentitySequenceTest( String name )
    {
        super(name);
    }

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // JUnit BEHAVIOR
    //
    ///////////////////////////////////////////////////////////////////////////////////


    /**
     * Construct top level suite in this JUnit test
     */
    public static Test suite()
    {
        BaseTestSuite suite = new BaseTestSuite();

        Test    cleanTest = new CleanDatabaseTestSetup
            (
             TestConfiguration.embeddedSuite( IdentitySequenceTest.class )
             );

        suite.addTest( cleanTest );
        
        return suite;
    }

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // TESTS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    /**
     * <p>
     * Test catalog changes.
     * </p>
     */
    public  void    test_001_catalog()
        throws Exception
    {
        Connection  conn = getConnection();

        goodStatement
            (
             conn,
             "create table T1_01_IST\n" +
             "(\n" +
             "    a int generated always as identity ( start with 10, increment by 20 ),\n" +
             "    b int\n" +
             ")\n"
             );
        String  sequenceName = getIdentitySequenceName( conn, "T1_01_IST" );

        // sequence should be in SYS, its name should be based on the table id,
        // and its start/stop/max/min/cycle values should be correct.

        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][]
             {
                 { "SYS", sequenceName, "10", "10", "-2147483648", "2147483647", "20", "N" },
             },
             false
             );

        assertResults
            (
             conn,
             "values syscs_util.syscs_peek_at_identity( 'APP', 'T1_01_IST' )",
             new String[][]
             {
                 { "10" },
             },
             false
             );

        // should not be able to issue a NEXT VALUE on the sequence generator which backs the identity column
        expectCompilationError
            ( conn, BAD_NEXT_VALUE, "values ( next value for sys.\"" + sequenceName + "\" )" );

        // alter the identity column and observe that the sequence generator changes
        goodStatement( conn, "alter table T1_01_IST alter column a set increment by 15" );
        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][]
             {
                 { "SYS", sequenceName, "10", "10", "-2147483648", "2147483647", "15", "N" },
             },
             false
             );
        goodStatement( conn, "alter table T1_01_IST alter column a restart with 500" );
        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][]
             {
                 { "SYS", sequenceName, "500", "500", "-2147483648", "2147483647", "15", "N" },
             },
             false
             );
        
        // system sequence should disappear when the table is dropped
        goodStatement( conn, "drop table T1_01_IST" );
        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][] {},
             false
             );

        // can add an identity column to a table (DERBY-3888)
        goodStatement( conn, "create table T2_01_IST( b int )" );
        goodStatement( conn,
              "alter table T2_01_IST add column a int generated always as identity ( start with 10, increment by 20 )" );
        goodStatement( conn, "drop table T2_01_IST" );

        // dropping an identity column should drop the sequence generator too
        goodStatement
            (
             conn,
             "create table T3_03_IST\n" +
             "(\n" +
             "    a int generated always as identity ( start with 10, increment by 20 ),\n" +
             "    b int\n" +
             ")\n"
             );
        sequenceName = getIdentitySequenceName( conn, "T3_03_IST" );
        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][]
             {
                 { "SYS", sequenceName, "10", "10", "-2147483648", "2147483647", "20", "N" },
             },
             false
             );
        assertResults
            (
             conn,
             "values syscs_util.syscs_peek_at_identity( 'APP', 'T3_03_IST' )",
             new String[][]
             {
                 { "10" },
             },
             false
             );
        goodStatement( conn, "alter table T3_03_IST drop column a" );
        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][] {},
             false
             );
        expectExecutionError
            ( conn, TABLE_DOESNT_HAVE_IDENTITY,
             "values syscs_util.syscs_peek_at_identity( 'APP', 'T3_03_IST' )"
              );
    }
    
    /**
     * <p>
     * Test ALTER TABLE behavior.
     * </p>
     */
    public  void    test_002_alterTable()
        throws Exception
    {
        Connection  conn = getConnection();

        //
        // Test that changing the increment value for an identity
        // column does not affect its current value. See DERBY-6579.
        //
        goodStatement( conn, "create table t1_002( a int, b int generated always as identity )" );
        goodStatement( conn, "insert into t1_002( a ) values ( 100 ), ( 200 )" );
        goodStatement( conn, "alter table t1_002 alter b set increment by 10" );
        goodStatement( conn, "insert into t1_002( a ) values ( 300 ), ( 400 )" );
        assertResults
            (
             conn,
             "select * from t1_002 order by a",
             new String[][]
             {
                 { "100", "1" },
                 { "200", "2" },
                 { "300", "3" },
                 { "400", "13" },
             },
             false
             );

        goodStatement( conn, "drop table t1_002" );
        goodStatement( conn, "create table t1_002( a int, b int generated always as identity )" );
        goodStatement( conn, "insert into t1_002( a ) values ( 100 ), ( 200 )" );
        goodStatement( conn, "delete from t1_002 where a = 200" );
        goodStatement( conn, "alter table t1_002 alter b set increment by 10" );
        goodStatement( conn, "insert into t1_002( a ) values ( 300 ), ( 400 )" );
        assertResults
            (
             conn,
             "select * from t1_002 order by a",
             new String[][]
             {
                 { "100", "1" },
                 { "300", "3" },
                 { "400", "13" },
             },
             false
             );

        // now restart the identity column at a later number
        goodStatement( conn, "alter table t1_002 alter b restart with 1000" );
        goodStatement( conn, "insert into t1_002( a ) values ( 500 ), ( 600 )" );
        assertResults
            (
             conn,
             "select * from t1_002 order by a",
             new String[][]
             {
                 { "100", "1" },
                 { "300", "3" },
                 { "400", "13" },
                 { "500", "1000" },
                 { "600", "1010" },
             },
             false
             );
    }

    /**
     * <p>
     * Test that too much contention on an identity column raises a LOCK_TIMEOUT.
     * </p>
     */
    public  void    test_003_identityTimeout()
        throws Exception
    {
        Connection  conn = getConnection();

        goodStatement( conn, "call syscs_util.syscs_set_database_property( 'derby.locks.waitTimeout', '1' )" );
        goodStatement( conn, "create table t_timeout( a int generated always as identity, b int )" );
        conn.setAutoCommit( false );

        try {
            PreparedStatement   ps = chattyPrepare( conn, "select count(*) from sys.syssequences with rs\n" );
            getScalarInteger( ps );
            expectExecutionError( conn, LOCK_TIMEOUT, "insert into t_timeout( b ) values ( 1 )" );
        }
        finally
        {
            conn.setAutoCommit( true );
            goodStatement( conn, "call syscs_util.syscs_set_database_property( 'derby.locks.waitTimeout', '60' )" );
        }
    }

	    /**
     * <p>
     * Tests for newly added cycle option in DERBY-6852.
     * </p>
     */
    public  void    testcycleOption()
        throws Exception
    {
        Connection  conn = getConnection();

        
        // Test the restart value and syntax of identity column without COMMA. 
        goodStatement( conn, "create table t( a int generated always as identity(START WITH 2147483647 INCREMENT BY 2 CYCLE) , b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 ), ( 3 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "2147483647", "1" },
                 { "-2147483648", "2" },
                 { "-2147483646", "3" },
             },
             false
             );
        goodStatement( conn, "drop table t" );
		// Test the restart value and syntax of identity column with COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 2147483647, INCREMENT BY 2, CYCLE) , b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 ), ( 3 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "2147483647", "1" },
                 { "-2147483648", "2" },
                 { "-2147483646", "3" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Only Start with. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 47), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "47", "1" },
                 { "48", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Start with and increment by without COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 47 INCREMENT BY 3), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "47", "1" },
                 { "50", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Start with and increment by with COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 47, INCREMENT BY 3), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "47", "1" },
                 { "50", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Start with and increment by with COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 47, INCREMENT BY 3), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "47", "1" },
                 { "50", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Start with and cycle with COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 2147483647, CYCLE), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 ), ( 3 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "2147483647", "1" },
                 { "-2147483648", "2" },
				 { "-2147483647", "3" },
             },
             false
             );

        goodStatement( conn, "drop table t" );
		//Start with and cycle without COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(START WITH 2147483647 CYCLE), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 ), ( 3 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "2147483647", "1" },
                 { "-2147483648", "2" },
				 { "-2147483647", "3" },
             },
             false
             );

 		goodStatement( conn, "drop table t" );
		//increment by. 
		goodStatement( conn, "create table t( a int generated always as identity(INCREMENT BY 3), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "1", "1" },
                 { "4", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//increment by with COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(INCREMENT BY 3, CYCLE), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "1", "1" },
                 { "4", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//increment by without COMMA. 
		goodStatement( conn, "create table t( a int generated always as identity(INCREMENT BY 3, CYCLE), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "1", "1" },
                 { "4", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Cycle. 
		goodStatement( conn, "create table t( a int generated always as identity(CYCLE), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "1", "1" },
                 { "2", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		//Changing the order of the identity column options. 
		goodStatement( conn, "create table t( a int generated always as identity(increment by 4 start with 4), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "4", "1" },
                 { "8", "2" },
             },
             false
             );
		goodStatement( conn, "drop table t" );
		// Changing the order of identity column options. 
        goodStatement( conn, "create table t( a int generated always as identity(CYCLE START WITH 2147483647 INCREMENT BY 2 ) , b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 ), ( 3 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "2147483647", "1" },
                 { "-2147483648", "2" },
                 { "-2147483646", "3" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		// With comma and without comma. 
        goodStatement( conn, "create table t( a int generated always as identity(start with 2147483647, increment by 7 cycle) , b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 ), ( 3 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "2147483647", "1" },
                 { "-2147483648", "2" },
                 { "-2147483641", "3" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		// Changing the order of identity column options. 
        goodStatement( conn, "create table t( a int generated always as identity(cycle , increment by 4), b int)" );
        goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
        assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "1", "1" },
                 { "5", "2" },
             },
             false
             );

		goodStatement( conn, "drop table t" );
		// () should be an error 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated by default as identity ())" );

		// Missing "with". 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated by default as identity (start 47))" );
	
		// Missing "by". 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated by default as identity (increment 4))" );

		// Redundant cycle
        expectCompilationError( conn,"42XAJ" ,"create table t(a int generated by default as identity (cycle cycle))" );
		
		// Redundant start with  
        expectCompilationError( conn,"42XAJ" ,"create table t( a int generated by default as identity(start with 4 start with 8))" );

		// Syntax error. 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated by default as identity(start with 4 , , , cycle))" );

		// Syntax error. 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated by default as identity(no cycle))" );

		// try to exceed the maximum value without cycle option. 
        goodStatement( conn, "create table t( a int generated always as identity(start with 2147483647, increment by 4), b int)" );
        expectExecutionError( conn,"2200H" ,"insert into t( b ) values ( 1 ), ( 2 )" );

	goodStatement( conn, "drop table t" );
		// Syntax error. 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated always as identity(START WITH, 47))" );
		
		// Syntax error. 
        expectCompilationError( conn,"42X01" ,"create table t( a int generated always as identity(START WITH 47 ,))" );

		// Support cycle option after altering the table.
        goodStatement( conn, "create table t( a int generated always as identity(start with 2147483647 cycle), b int)" );
	goodStatement( conn, "alter table t alter column a set increment by 4" );
	goodStatement( conn ,"insert into t( b ) values ( 1 ), ( 2 )" );
        

	goodStatement( conn, "drop table t" );
		// Alter table works fine without cycling with cycle option in the syntax.
	goodStatement( conn, "create table t( a int generated always as identity(start with 7 increment by 2 cycle), b int)" );
	goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
	goodStatement( conn, "alter table t alter column a set increment by 4" );
        goodStatement( conn, "insert into t( b ) values ( 3 ), ( 4 )" );
	assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "7", "1" },
                 { "9", "2" },
		 { "11", "3" },
                 { "15", "4" },
             },
             false
             );

	goodStatement( conn, "drop table t" );
		// supports cycle option after altering the table.
        goodStatement( conn, "create table t( a int generated always as identity(increment by 2 cycle), b int)" );
	goodStatement( conn, "alter table t alter column a restart with 2147483647" );
    goodStatement( conn ,"insert into t( b ) values ( 1 ), ( 2 )" );

	goodStatement( conn, "drop table t" );
		// Alter table works fine without cycling with cycle option in the syntax.
	goodStatement( conn, "create table t( a int generated always as identity(start with 7 increment by 2 cycle), b int)" );
	goodStatement( conn, "insert into t( b ) values ( 1 ), ( 2 )" );
	goodStatement( conn, "alter table t alter column a restart with 50" );
        goodStatement( conn, "insert into t( b ) values ( 3 ), ( 4 )" );
	assertResults
            (
             conn,
             "select * from t",
             new String[][]
             {
                 { "7", "1" },
                 { "9", "2" },
		 { "50", "3" },
                 { "52", "4" },
             },
             false
             );

	goodStatement( conn, "drop table t" );
	goodStatement( conn, "create table t( A_6852 int generated always as identity(start with 7 increment by 2 cycle), b_6852 int)" );
		//SELECT from sys.syscolumns with cycle option
        // ADDING AUTOINCREMENTCYCLE Derby-6905

	assertResults
            (
             conn,
             "select AUTOINCREMENTVALUE, AUTOINCREMENTSTART, AUTOINCREMENTINC, AUTOINCREMENTCYCLE from sys.syscolumns where COLUMNNAME ='A_6852'",
             new String[][]
             {
                 { "7", "7", "2", "true" },
             },
             false
             );

	assertResults
            (
             conn,
             "select AUTOINCREMENTVALUE, AUTOINCREMENTSTART, AUTOINCREMENTINC, AUTOINCREMENTCYCLE from sys.syscolumns where COLUMNNAME ='B_6852'",
             new String[][]
             {
                 { null, null, null, "false" },
             },
             false
             );

	goodStatement( conn, "drop table t" );
		//SELECT from sys.syscolumns without cycle option
        // ADDING AUTOINCREMENTCYCLE Derby-6905

        goodStatement( conn, "create table T_6852( A_6852 int generated always as identity(start with 7 increment by 2))" );

	assertResults
            (
             conn,
             "select AUTOINCREMENTVALUE, AUTOINCREMENTSTART, AUTOINCREMENTINC,  AUTOINCREMENTCYCLE  from sys.syscolumns where COLUMNNAME ='A_6852'",
             new String[][]
             {
                 { "7", "7", "2", "false" },
             },
             false
             );

		//SELECT from sys.syssequences without cycle option
        String  sequenceName = getIdentitySequenceName( conn, "T_6852" );

        // sequence should be in SYS, its name should be based on the table id,
        // and its start/stop/max/min/cycle values should be correct.

        assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][]
             {
                 { "SYS", sequenceName, "7", "7", "-2147483648", "2147483647", "2", "N" },
             },
             false
             );

	goodStatement( conn, "drop table T_6852" );
		//SELECT from sys.syssequences with cycle option
	goodStatement( conn, "create table T_6852( A_6852 int generated always as identity(start with 7 increment by 2 cycle))" );

        sequenceName = getIdentitySequenceName( conn, "T_6852" );
	assertResults
            (
             conn,
            "select\n" +
            "    c.schemaName, s.sequenceName, s.currentValue, s.startValue,\n" +
            "    s.minimumValue, s.maximumValue, s.increment, s.cycleoption\n" +
            "from sys.syssequences s, sys.sysschemas c\n" +
            "where s.schemaID = c.schemaID\n" +
             "and s.sequenceName = '" + sequenceName + "'",
             new String[][]
             {
                 { "SYS", sequenceName, "7", "7", "-2147483648", "2147483647", "2", "Y" },
             },
             false
             );
	
	goodStatement( conn, "drop table T_6852" );
  
    }

    /** Get a scalar integer result from a query */
    private int getScalarInteger( PreparedStatement ps ) throws Exception
    {
        ResultSet rs = ps.executeQuery();
        rs.next();
        int retval = rs.getInt( 1 );

        rs.close();
        ps.close();

        return retval;
    }

    ///////////////////////////////////////////////////////////////////////////////////
    //
    // MINIONS
    //
    ///////////////////////////////////////////////////////////////////////////////////

    public  static String  getIdentitySequenceName( Connection conn, String tableName )
        throws Exception
    {
        PreparedStatement   ps = conn.prepareStatement( "select tableID from sys.systables where tablename = ?" );
        ps.setString( 1, tableName.toUpperCase() );
        ResultSet   rs = ps.executeQuery();
        rs.next();
        String  uuidString = rs.getString( 1 );
        rs.close();
        ps.close();

        return uuidToSequenceName( uuidString );
    }

    public  static  String  uuidToSequenceName( String uuidString )
    {
        return "U" + uuidString.replace( "-", "X" );
    }

    
}