~ubuntu-branches/ubuntu/maverick/hsqldb/maverick

« back to all changes in this revision

Viewing changes to src/org/hsqldb/DatabaseCommandInterpreter.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-26 11:47:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060926114749-3jd0utm7w21x1iqt
Tags: 1.8.0.5-2ubuntu1
* Synchronise with Debian unstable; remaining changes:
  - build using java-gcj-compat.
* libhsqldb-java: Add gij as alternative dependency.
* Build a libhsqldb-java-gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2
 
 * All rights reserved.
3
 
 *
4
 
 * Redistribution and use in source and binary forms, with or without
5
 
 * modification, are permitted provided that the following conditions are met:
6
 
 *
7
 
 * Redistributions of source code must retain the above copyright notice, this
8
 
 * list of conditions and the following disclaimer.
9
 
 *
10
 
 * Redistributions in binary form must reproduce the above copyright notice,
11
 
 * this list of conditions and the following disclaimer in the documentation
12
 
 * and/or other materials provided with the distribution.
13
 
 *
14
 
 * Neither the name of the Hypersonic SQL Group nor the names of its
15
 
 * contributors may be used to endorse or promote products derived from this
16
 
 * software without specific prior written permission.
17
 
 *
18
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22
 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 
 *
30
 
 * This software consists of voluntary contributions made by many individuals 
31
 
 * on behalf of the Hypersonic SQL Group.
32
 
 *
33
 
 *
34
 
 * For work added by the HSQL Development Group:
35
 
 *
36
 
 * Copyright (c) 2001-2005, The HSQL Development Group
37
 
 * All rights reserved.
38
 
 *
39
 
 * Redistribution and use in source and binary forms, with or without
40
 
 * modification, are permitted provided that the following conditions are met:
41
 
 *
42
 
 * Redistributions of source code must retain the above copyright notice, this
43
 
 * list of conditions and the following disclaimer.
44
 
 *
45
 
 * Redistributions in binary form must reproduce the above copyright notice,
46
 
 * this list of conditions and the following disclaimer in the documentation
47
 
 * and/or other materials provided with the distribution.
48
 
 *
49
 
 * Neither the name of the HSQL Development Group nor the names of its
50
 
 * contributors may be used to endorse or promote products derived from this
51
 
 * software without specific prior written permission.
52
 
 *
53
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56
 
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57
 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58
 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59
 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60
 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63
 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64
 
 */
65
 
 
66
 
 
 
1
/* Copyright (c) 1995-2000, The Hypersonic SQL Group.
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * Redistributions of source code must retain the above copyright notice, this
 
8
 * list of conditions and the following disclaimer.
 
9
 *
 
10
 * Redistributions in binary form must reproduce the above copyright notice,
 
11
 * this list of conditions and the following disclaimer in the documentation
 
12
 * and/or other materials provided with the distribution.
 
13
 *
 
14
 * Neither the name of the Hypersonic SQL Group nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from this
 
16
 * software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
 * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
 
22
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 * This software consists of voluntary contributions made by many individuals 
 
31
 * on behalf of the Hypersonic SQL Group.
 
32
 *
 
33
 *
 
34
 * For work added by the HSQL Development Group:
 
35
 *
 
36
 * Copyright (c) 2001-2005, The HSQL Development Group
 
37
 * All rights reserved.
 
38
 *
 
39
 * Redistribution and use in source and binary forms, with or without
 
40
 * modification, are permitted provided that the following conditions are met:
 
41
 *
 
42
 * Redistributions of source code must retain the above copyright notice, this
 
43
 * list of conditions and the following disclaimer.
 
44
 *
 
45
 * Redistributions in binary form must reproduce the above copyright notice,
 
46
 * this list of conditions and the following disclaimer in the documentation
 
47
 * and/or other materials provided with the distribution.
 
48
 *
 
49
 * Neither the name of the HSQL Development Group nor the names of its
 
50
 * contributors may be used to endorse or promote products derived from this
 
51
 * software without specific prior written permission.
 
52
 *
 
53
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
54
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
55
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
56
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 
57
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
58
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
59
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
60
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
61
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
62
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
63
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
64
 */
 
65
 
 
66
 
67
67
package org.hsqldb;
68
68
 
69
69
import java.io.IOException;
73
73
 
74
74
import org.hsqldb.HsqlNameManager.HsqlName;
75
75
import org.hsqldb.lib.ArrayUtil;
76
 
import org.hsqldb.lib.HashSet;
 
76
import org.hsqldb.lib.HashMappedList;
77
77
import org.hsqldb.lib.HsqlArrayList;
78
78
import org.hsqldb.lib.StringUtil;
79
79
import org.hsqldb.lib.java.JavaSystem;
181
181
            }
182
182
        } catch (Throwable t) {
183
183
            try {
184
 
                session.endSchemaDefinition();
 
184
                if (session.isSchemaDefintion()) {
 
185
                    HsqlName schemaName = session.getSchemaHsqlName(null);
 
186
 
 
187
                    database.schemaManager.dropSchema(schemaName.name, true);
 
188
                    database.logger.writeToLog(session,
 
189
                                               Token.T_DROP + ' '
 
190
                                               + Token.T_SCHEMA + ' '
 
191
                                               + schemaName.statementName
 
192
                                               + ' ' + Token.T_CASCADE);
 
193
                    session.endSchemaDefinition();
 
194
                }
185
195
            } catch (HsqlException e) {}
186
196
 
187
197
            result = new Result(t, tokenizer.getLastPart());
204
214
                    break;
205
215
 
206
216
                default :
207
 
                    String schema = session.getSchemaName(null);
208
 
 
209
 
                    session.endSchemaDefinition();
210
 
                    database.schemaManager.dropSchema(schema, true);
211
 
 
212
217
                    throw Trace.error(Trace.INVALID_IDENTIFIER,
213
218
                                      Trace.IN_SCHEMA_DEFINITION,
214
219
                                      new Object[]{ token });
218
223
        switch (cmd) {
219
224
 
220
225
            case Token.OPENBRACKET : {
221
 
                brackets = Parser.parseOpenBrackets(tokenizer) + 1;
 
226
                Parser parser = new Parser(session, database, tokenizer);
222
227
 
223
 
                tokenizer.getThis(Token.T_SELECT);
 
228
                brackets = parser.parseOpenBracketsSelect() + 1;
224
229
            }
225
230
            case Token.SELECT : {
226
231
                Parser parser = new Parser(session, database, tokenizer);
234
239
                            Trace.ASSERT_DIRECT_EXEC_WITH_PARAM));
235
240
                }
236
241
 
237
 
                result = session.sqlExecuteCompiledNoPreChecks(cStatement);
 
242
                result = session.sqlExecuteCompiledNoPreChecks(cStatement,
 
243
                        null);
238
244
 
239
245
                break;
240
246
            }
250
256
                            Trace.ASSERT_DIRECT_EXEC_WITH_PARAM));
251
257
                }
252
258
 
253
 
                result = session.sqlExecuteCompiledNoPreChecks(cStatement);
 
259
                result = session.sqlExecuteCompiledNoPreChecks(cStatement,
 
260
                        null);
254
261
 
255
262
                break;
256
263
            }
266
273
                            Trace.ASSERT_DIRECT_EXEC_WITH_PARAM));
267
274
                }
268
275
 
269
 
                result = session.sqlExecuteCompiledNoPreChecks(cStatement);
 
276
                result = session.sqlExecuteCompiledNoPreChecks(cStatement,
 
277
                        null);
270
278
 
271
279
                break;
272
280
            }
282
290
                            Trace.ASSERT_DIRECT_EXEC_WITH_PARAM));
283
291
                }
284
292
 
285
 
                result = session.sqlExecuteCompiledNoPreChecks(cStatement);
 
293
                result = session.sqlExecuteCompiledNoPreChecks(cStatement,
 
294
                        null);
286
295
 
287
296
                break;
288
297
            }
297
306
                            Trace.ASSERT_DIRECT_EXEC_WITH_PARAM));
298
307
                }
299
308
 
300
 
                result = session.sqlExecuteCompiledNoPreChecks(cStatement);
 
309
                result = session.sqlExecuteCompiledNoPreChecks(cStatement,
 
310
                        null);
301
311
 
302
312
                break;
303
313
            }
317
327
                processSavepoint();
318
328
                break;
319
329
 
 
330
            case Token.RELEASE :
 
331
                processReleaseSavepoint();
 
332
                break;
 
333
 
320
334
            case Token.CREATE :
321
335
                processCreate();
322
 
                database.setMetaDirty(true);
 
336
                database.setMetaDirty(false);
323
337
                break;
324
338
 
325
339
            case Token.ALTER :
369
383
                result = processExplainPlan();
370
384
                break;
371
385
 
372
 
            case Token.RELEASE :
373
 
                processReleaseSavepoint();
374
 
                break;
375
 
 
376
386
            default :
377
387
                throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
378
388
        }
502
512
                break;
503
513
 
504
514
            case Token.SCHEMA :
 
515
                session.setScripting(false);
505
516
                processCreateSchema();
506
517
                break;
507
518
 
514
525
                break;
515
526
 
516
527
            case Token.ROLE :
517
 
                database.getGranteeManager().createRole(getUserIdentifier());
 
528
                database.getGranteeManager().addRole(getUserIdentifier());
518
529
                break;
519
530
 
520
531
            case Token.VIEW :
547
558
     * @return  column index map
548
559
     * @throws  HsqlException if a column is not found or is duplicate
549
560
     */
550
 
    private int[] processColumnList(Table t) throws HsqlException {
551
 
 
552
 
        HsqlArrayList list;
553
 
        HashSet       set;
554
 
        String        token;
555
 
        int[]         col;
556
 
        int           size;
557
 
 
558
 
        list = new HsqlArrayList();
559
 
        set  = new HashSet();
560
 
 
561
 
        tokenizer.getThis(Token.T_OPENBRACKET);
562
 
 
563
 
        while (true) {
564
 
            token = tokenizer.getSimpleName();
565
 
 
566
 
            list.add(token);
567
 
            set.add(token);
568
 
 
569
 
            if (list.size() != set.size()) {
570
 
                throw Trace.error(Trace.COLUMN_ALREADY_EXISTS,
571
 
                                  Trace.ERROR_IN_CONSTRAINT_COLUMN_LIST);
572
 
            }
573
 
 
574
 
            token = tokenizer.getSimpleToken();
575
 
 
576
 
            if (token.equals(Token.T_DESC) || token.equals(Token.T_ASC)) {
577
 
                token = tokenizer.getSimpleToken();    // OJ: eat it up
578
 
            }
579
 
 
580
 
            if (token.equals(Token.T_COMMA)) {
581
 
                continue;
582
 
            }
583
 
 
584
 
            if (token.equals(Token.T_CLOSEBRACKET)) {
585
 
                break;
586
 
            }
587
 
 
588
 
            throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
589
 
        }
590
 
 
591
 
        size = list.size();
592
 
        col  = new int[size];
 
561
    private int[] processColumnList(Table t,
 
562
                                    boolean acceptAscDesc)
 
563
                                    throws HsqlException {
 
564
 
 
565
        HashMappedList list = Parser.processColumnList(tokenizer,
 
566
            acceptAscDesc);
 
567
        int   size = list.size();
 
568
        int[] col  = new int[size];
593
569
 
594
570
        for (int i = 0; i < size; i++) {
595
 
            col[i] = t.getColumnNr((String) list.get(i));
 
571
            col[i] = t.getColumnNr((String) list.getKey(i));
596
572
        }
597
573
 
598
574
        return col;
599
575
    }
600
576
 
601
577
    /**
602
 
     *  Responsible for tail end of CREATE INDEX DDL. <p>
603
 
     *
604
 
     *  Indexes defined in DDL scripts are handled by this method. If the
605
 
     *  name of an existing index begins with "SYS_", the name is changed to
606
 
     *  begin with "USER_". The name should be unique within the database.
607
 
     *  For compatibility with old database, non-unique names are modified
608
 
     *  and assigned a new name. <p>
609
 
     *
610
 
     *  In 1.7.2 no new index is created if an equivalent already exists. <p>
611
 
     *
612
 
     *  (fredt@users) <p>
613
 
     *
614
 
     * @param  t table
615
 
     * @param  indexName index
616
 
     *
617
 
     * @param  indexNameQuoted is quoted
618
 
     * @param  unique is unique
619
 
     * @throws  HsqlException
620
 
     */
621
 
    private void addIndexOn(Table t, String indexName,
622
 
                            boolean indexNameQuoted,
623
 
                            boolean unique) throws HsqlException {
624
 
 
625
 
        HsqlName indexHsqlName;
626
 
        int[]    indexColumns;
627
 
 
628
 
        database.schemaManager.checkIndexExists(indexName, t.getSchemaName(),
629
 
                false);
630
 
 
631
 
        indexHsqlName = newIndexHsqlName(indexName, indexNameQuoted);
632
 
        indexColumns  = processColumnList(t);
633
 
 
634
 
        session.commit();
635
 
        session.setScripting(true);
636
 
 
637
 
        TableWorks tableWorks = new TableWorks(session, t);
638
 
 
639
 
        tableWorks.createIndex(indexColumns, indexHsqlName, unique, false,
640
 
                               false);
641
 
    }
642
 
 
643
 
    /**
644
578
     *  Responsible for handling the execution of CREATE TRIGGER SQL
645
579
     *  statements. <p>
646
580
     *
760
694
                // start the trigger thread
761
695
                td.start();
762
696
            } catch (Exception e) {
763
 
                throw Trace.error(Trace.UNKNOWN_FUNCTION, e.getMessage());
 
697
                throw Trace.error(Trace.UNKNOWN_FUNCTION, e.toString());
764
698
            }
765
699
        }
766
700
 
828
762
            Trace.check(Types.acceptsPrecisionCreateParam(type),
829
763
                        Trace.UNEXPECTED_TOKEN);
830
764
 
831
 
            if (type != Types.TIMESTAMP && length == 0) {
 
765
            if (type != Types.TIMESTAMP && type != Types.TIME
 
766
                    && length == 0) {
832
767
                throw Trace.error(Trace.INVALID_SIZE_PRECISION);
833
768
            }
834
769
 
835
770
            if (tokenizer.isGetThis(Token.T_COMMA)) {
836
 
                scale = tokenizer.getInt();
837
 
 
838
771
                Trace.check(Types.acceptsScaleCreateParam(type),
839
772
                            Trace.UNEXPECTED_TOKEN);
 
773
 
 
774
                scale = tokenizer.getInt();
840
775
            }
841
776
 
842
777
            tokenizer.getThis(Token.T_CLOSEBRACKET);
866
801
            }
867
802
        }
868
803
 
 
804
        if (type == Types.TIME) {
 
805
            if (length != 0) {
 
806
                throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE);
 
807
            }
 
808
        }
 
809
 
869
810
        token = tokenizer.getSimpleToken();
870
811
 
871
812
        if (token.equals(Token.T_DEFAULT)) {
963
904
 
964
905
        expr.resolveTypes(session);
965
906
 
966
 
        if (expr.getType() == Expression.VALUE
967
 
                || (expr.getType() == Expression.FUNCTION
 
907
        int newType = expr.getType();
 
908
 
 
909
        if (newType == Expression.VALUE || newType == Expression.TRUE
 
910
                || newType == Expression.FALSE
 
911
                || (newType == Expression.FUNCTION
968
912
                    && expr.function.isSimple)) {
969
913
            Object defValTemp;
970
914
 
1078
1022
                    // tony_lai@users 20020820 - patch 595099
1079
1023
                    pkHsqlName = cname;
1080
1024
 
1081
 
                    int[]      col = processColumnList(t);
 
1025
                    int[]      cols = processColumnList(t, false);
1082
1026
                    Constraint mainConst;
1083
1027
 
1084
1028
                    mainConst = (Constraint) tcList.get(0);
1085
1029
 
1086
1030
                    if (mainConst.core.mainColArray != null) {
1087
1031
                        if (!ArrayUtil.areEqual(mainConst.core.mainColArray,
1088
 
                                                col, col.length, true)) {
 
1032
                                                cols, cols.length, true)) {
1089
1033
                            throw Trace.error(Trace.SECOND_PRIMARY_KEY);
1090
1034
                        }
1091
1035
                    }
1092
1036
 
1093
 
                    mainConst.core.mainColArray = col;
 
1037
                    mainConst.core.mainColArray = cols;
1094
1038
                    mainConst.constName         = pkHsqlName;
1095
1039
 
1096
1040
                    break;
1097
1041
                }
1098
1042
                case Token.UNIQUE : {
1099
 
                    int[] col = processColumnList(t);
 
1043
                    int[] col = processColumnList(t, false);
1100
1044
 
1101
1045
                    if (cname == null) {
1102
1046
                        cname = database.nameManager.newAutoName("CT");
1142
1086
                    }
1143
1087
 
1144
1088
                    tempConst = new Constraint(cname, null, null, null,
1145
 
                                               Constraint.CHECK, 0, 0);
 
1089
                                               Constraint.CHECK,
 
1090
                                               Constraint.NO_ACTION,
 
1091
                                               Constraint.NO_ACTION);
1146
1092
 
1147
1093
                    processCreateCheckConstraintCondition(tempConst);
1148
1094
                    tcList.add(tempConst);
1257
1203
            throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
1258
1204
        }
1259
1205
 
 
1206
        HsqlArrayList tempConstraints = processCreateConstraints(t,
 
1207
            constraint, pkCols);
 
1208
 
1260
1209
        if (tokenizer.isGetThis(Token.T_ON)) {
1261
1210
            if (!t.isTemp) {
1262
1211
                throw Trace.error(Trace.UNEXPECTED_TOKEN, Token.T_ON);
1276
1225
            tokenizer.getThis(Token.T_ROWS);
1277
1226
        }
1278
1227
 
1279
 
        HsqlArrayList tempConstraints = processCreateConstraints(t,
1280
 
            constraint, pkCols);
1281
 
 
1282
1228
        try {
1283
1229
            session.commit();
1284
1230
 
1285
1231
            Constraint primaryConst = (Constraint) tempConstraints.get(0);
1286
1232
 
1287
 
            if (primaryConst.constName == null) {
1288
 
                primaryConst.constName = t.makeSysPKName();
 
1233
            t.createPrimaryKey(null, primaryConst.core.mainColArray, true);
 
1234
 
 
1235
            if (primaryConst.core.mainColArray != null) {
 
1236
                if (primaryConst.constName == null) {
 
1237
                    primaryConst.constName = t.makeSysPKName();
 
1238
                }
 
1239
 
 
1240
                Constraint newconstraint =
 
1241
                    new Constraint(primaryConst.constName,
 
1242
                                   primaryConst.core.mainColArray);
 
1243
 
 
1244
                t.addConstraint(newconstraint);
 
1245
                database.schemaManager.registerConstraintName(
 
1246
                    primaryConst.constName.name, t.getName());
1289
1247
            }
1290
1248
 
1291
 
            database.schemaManager.registerIndexName(
1292
 
                primaryConst.constName.name, t.getName());
1293
 
            database.schemaManager.registerConstraintName(
1294
 
                primaryConst.constName.name, t.getName());
1295
 
            t.createPrimaryKey(primaryConst.constName,
1296
 
                               primaryConst.core.mainColArray, true);
1297
 
 
1298
1249
            for (int i = 1; i < tempConstraints.size(); i++) {
1299
1250
                Constraint tempConst = (Constraint) tempConstraints.get(i);
1300
1251
 
1363
1314
        Table  expTable;
1364
1315
        String token;
1365
1316
 
1366
 
        localcol = processColumnList(t);
 
1317
        localcol = processColumnList(t, false);
1367
1318
 
1368
1319
        tokenizer.getThis(Token.T_REFERENCES);
1369
1320
 
1394
1345
        tokenizer.back();
1395
1346
 
1396
1347
        if (token.equals(Token.T_OPENBRACKET)) {
1397
 
            expcol = processColumnList(expTable);
 
1348
            expcol = processColumnList(expTable, false);
1398
1349
        } else {
1399
1350
            if (expTable.getPrimaryKey() == null) {
1400
1351
 
1513
1464
 
1514
1465
        HsqlName[] colList = null;
1515
1466
 
 
1467
        // fredt - a bug in 1.8.0.0 and previous versions causes view
 
1468
        // definitions to script without double quotes around column names
 
1469
        // in certain cases; the workaround here discards such scripted column
 
1470
        // lists when used in OOo
1516
1471
        if (tokenizer.isGetThis(Token.T_OPENBRACKET)) {
1517
 
            HsqlArrayList list = Parser.getColumnNames(database, null,
1518
 
                tokenizer, true);
1519
 
 
1520
 
            colList = new HsqlName[list.size()];
1521
 
            colList = (HsqlName[]) list.toArray(colList);
 
1472
            try {
 
1473
                HsqlArrayList list = Parser.getColumnNames(database, null,
 
1474
                    tokenizer, true);
 
1475
 
 
1476
                colList = new HsqlName[list.size()];
 
1477
                colList = (HsqlName[]) list.toArray(colList);
 
1478
 
 
1479
                //added lines to make sure all valid columns are quoted
 
1480
                if (database.isStoredFileAccess()) {
 
1481
                    for (int i = 0; i < colList.length; i++) {
 
1482
                        if (!colList[i].isNameQuoted) {
 
1483
                            colList = null;
 
1484
 
 
1485
                            break;
 
1486
                        }
 
1487
                    }
 
1488
                }
 
1489
            } catch (HsqlException e) {
 
1490
 
 
1491
                //added lines to catch unquoted names with spaces
 
1492
                if (database.isStoredFileAccess()) {
 
1493
                    while (!tokenizer.getString().equals(
 
1494
                            Token.T_CLOSEBRACKET)) {}
 
1495
                } else {
 
1496
                    throw e;
 
1497
                }
 
1498
            }
1522
1499
        }
1523
1500
 
1524
1501
        tokenizer.getThis(Token.T_AS);
1525
1502
        tokenizer.setPartMarker();
1526
1503
 
1527
1504
        Parser parser   = new Parser(session, database, tokenizer);
1528
 
        int    brackets = 0;
1529
 
 
1530
 
        if (tokenizer.isGetThis(Token.T_OPENBRACKET)) {
1531
 
            brackets += Parser.parseOpenBrackets(tokenizer) + 1;
1532
 
        }
1533
 
 
1534
 
        tokenizer.getThis(Token.T_SELECT);
1535
 
 
 
1505
        int    brackets = parser.parseOpenBracketsSelect();
1536
1506
        Select select;
1537
1507
 
1538
1508
        // accept ORDER BY or ORDRY BY with LIMIT - accept unions
1539
1509
        select = parser.parseSelect(brackets, true, false, true, true);
1540
1510
 
1541
1511
        if (select.sIntoTable != null) {
1542
 
            throw (Trace.error(Trace.TABLE_NOT_FOUND));
 
1512
            throw (Trace.error(Trace.INVALID_IDENTIFIER, Token.INTO));
1543
1513
        }
1544
1514
 
1545
1515
        select.prepareResult(session);
1559
1529
     */
1560
1530
    private void processAlterTableRename(Table t) throws HsqlException {
1561
1531
 
1562
 
        String  name   = t.getName().name;
1563
1532
        String  schema = t.getSchemaName();
1564
1533
        String  newName;
1565
1534
        boolean isquoted;
1566
1535
 
1567
1536
        // ensures that if temp table, it also belongs to this session
 
1537
/*
1568
1538
        if (!t.equals(session, name)) {
1569
1539
            throw Trace.error(Trace.TABLE_NOT_FOUND);
1570
1540
        }
1571
 
 
 
1541
*/
1572
1542
        tokenizer.getThis(Token.T_TO);
1573
1543
 
1574
1544
        newName = tokenizer.getName();
1588
1558
                schema);
1589
1559
        session.commit();
1590
1560
        session.setScripting(true);
1591
 
        t.rename(session, newName, isquoted);
 
1561
        database.schemaManager.renameTable(session, t, newName, isquoted);
1592
1562
    }
1593
1563
 
1594
1564
    /**
1771
1741
 
1772
1742
                        if (t.hasPrimaryKey()) {
1773
1743
                            processAlterTableDropConstraint(
1774
 
                                t, t.getIndexes()[0].getName().name);
 
1744
                                t, t.getPrimaryConstraint().getName().name);
1775
1745
                        } else {
1776
1746
                            throw Trace.error(Trace.CONSTRAINT_NOT_FOUND,
1777
1747
                                              Trace.TABLE_HAS_NO_PRIMARY_KEY,
1830
1800
            }
1831
1801
            case Token.DROP : {
1832
1802
                tokenizer.getThis(Token.T_DEFAULT);
1833
 
                t.setDefaultExpression(columnIndex, null);
 
1803
 
 
1804
                TableWorks tw = new TableWorks(session, t);
 
1805
 
 
1806
                tw.setColDefaultExpression(columnIndex, null);
1834
1807
 
1835
1808
                return;
1836
1809
            }
1852
1825
                } else if (token.equals(Token.T_DEFAULT)) {
1853
1826
 
1854
1827
                    //alter table alter column set default
1855
 
                    int type   = column.getType();
1856
 
                    int length = column.getSize();
1857
 
                    int scale  = column.getScale();
 
1828
                    TableWorks tw     = new TableWorks(session, t);
 
1829
                    int        type   = column.getType();
 
1830
                    int        length = column.getSize();
 
1831
                    int        scale  = column.getScale();
 
1832
                    Expression expr = processCreateDefaultExpression(type,
 
1833
                        length, scale);
1858
1834
 
1859
 
                    t.setDefaultExpression(
1860
 
                        columnIndex,
1861
 
                        processCreateDefaultExpression(type, length, scale));
 
1835
                    tw.setColDefaultExpression(columnIndex, expr);
1862
1836
                } else {
1863
1837
                    throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
1864
1838
                }
1912
1886
            throw Trace.error(Trace.COLUMN_ALREADY_EXISTS, newName);
1913
1887
        }
1914
1888
 
 
1889
        t.database.schemaManager.checkColumnIsInView(t,
 
1890
                column.columnName.name);
1915
1891
        session.commit();
1916
1892
        session.setScripting(true);
1917
1893
        t.renameColumn(column, newName, isquoted);
2514
2490
    private Table newTable(int type, String name, boolean quoted,
2515
2491
                           HsqlName schema) throws HsqlException {
2516
2492
 
2517
 
        int sid = session.getId();
2518
2493
        HsqlName tableHsqlName = database.nameManager.newHsqlName(name,
2519
2494
            quoted);
2520
2495
 
2546
2521
        boolean canAdd = true;
2547
2522
 
2548
2523
        if (t.findColumn(c.columnName.name) != -1) {
2549
 
            canAdd = false;
2550
 
        }
2551
 
 
2552
 
        if (c.isIdentity()) {
2553
 
 
2554
 
//            canAdd = false;
 
2524
            throw Trace.error(Trace.COLUMN_ALREADY_EXISTS);
2555
2525
        }
2556
2526
 
2557
2527
        if (c.isPrimaryKey() && t.hasPrimaryKey()) {
2558
2528
            canAdd = false;
2559
2529
        }
2560
2530
 
2561
 
        if (!t.isEmpty(session)) {
 
2531
        if (canAdd &&!t.isEmpty(session)) {
2562
2532
            canAdd = c.isNullable() || c.getDefaultExpression() != null;
2563
2533
        }
2564
2534
 
2584
2554
                if (defExpr == null) {
2585
2555
                    String columnName = column.columnName.name;
2586
2556
 
2587
 
                    throw Trace.error(Trace.COLUMN_TYPE_MISMATCH,
2588
 
                                      Trace.NO_DEFAULT_VALUE_FOR_COLUMN,
 
2557
                    throw Trace.error(Trace.NO_DEFAULT_VALUE_FOR_COLUMN,
2589
2558
                                      new Object[]{ columnName });
2590
2559
                }
2591
2560
            }
2758
2727
        return;
2759
2728
    }
2760
2729
 
 
2730
    /**
 
2731
     * If an invalid alias is encountered while processing an old script,
 
2732
     * simply discard it.
 
2733
     */
2761
2734
    private void processCreateAlias() throws HsqlException {
2762
2735
 
2763
2736
        String alias;
2764
2737
        String methodFQN;
2765
2738
 
2766
 
        alias = tokenizer.getSimpleName();
 
2739
        try {
 
2740
            alias = tokenizer.getSimpleName();
 
2741
        } catch (HsqlException e) {
 
2742
            if (session.isProcessingScript()) {
 
2743
                alias = null;
 
2744
            } else {
 
2745
                throw e;
 
2746
            }
 
2747
        }
2767
2748
 
2768
2749
        tokenizer.getThis(Token.T_FOR);
2769
2750
 
2770
2751
        methodFQN = upgradeMethodFQN(tokenizer.getSimpleName());
2771
2752
 
2772
 
        database.getAliasMap().put(alias, methodFQN);
 
2753
        if (alias != null) {
 
2754
            database.getAliasMap().put(alias, methodFQN);
 
2755
        }
2773
2756
    }
2774
2757
 
2775
2758
    private void processCreateIndex(boolean unique) throws HsqlException {
2776
2759
 
2777
2760
        Table   t;
2778
 
        String  name     = tokenizer.getName();
2779
 
        String  schema   = tokenizer.getLongNameFirst();
2780
 
        boolean isQuoted = tokenizer.wasQuotedIdentifier();
 
2761
        String  indexName       = tokenizer.getName();
 
2762
        String  schema          = tokenizer.getLongNameFirst();
 
2763
        boolean indexNameQuoted = tokenizer.wasQuotedIdentifier();
2781
2764
 
2782
2765
        tokenizer.getThis(Token.T_ON);
2783
2766
 
2791
2774
 
2792
2775
        t = database.schemaManager.getTable(session, tablename, tableschema);
2793
2776
 
2794
 
        addIndexOn(t, name, isQuoted, unique);
 
2777
        database.schemaManager.checkIndexExists(indexName, t.getSchemaName(),
 
2778
                false);
2795
2779
 
2796
 
        String extra = tokenizer.getSimpleToken();
 
2780
        HsqlName indexHsqlName = newIndexHsqlName(indexName, indexNameQuoted);
 
2781
        int[]    indexColumns  = processColumnList(t, true);
 
2782
        String   extra         = tokenizer.getSimpleToken();
2797
2783
 
2798
2784
        if (!Token.T_DESC.equals(extra) &&!Token.T_ASC.equals(extra)) {
2799
2785
            tokenizer.back();
2800
2786
        }
 
2787
 
 
2788
        session.commit();
 
2789
        session.setScripting(true);
 
2790
 
 
2791
        TableWorks tableWorks = new TableWorks(session, t);
 
2792
 
 
2793
        tableWorks.createIndex(indexColumns, indexHsqlName, unique, false,
 
2794
                               false);
2801
2795
    }
2802
2796
 
2803
2797
    /**
2864
2858
        }
2865
2859
 
2866
2860
        tokenizer.getThis(Token.T_AUTHORIZATION);
2867
 
        tokenizer.getThis(GranteeManager.ADMIN_ROLE_NAME);
 
2861
        tokenizer.getThis(GranteeManager.DBA_ADMIN_ROLE_NAME);
2868
2862
 
2869
2863
        if (database.schemaManager.schemaExists(name)) {
2870
2864
            if (!session.isProcessingScript) {
2874
2868
            database.schemaManager.createSchema(name, isquoted);
2875
2869
        }
2876
2870
 
 
2871
        HsqlName schemaName = database.schemaManager.getSchemaHsqlName(name);
 
2872
 
 
2873
        database.logger.writeToLog(session,
 
2874
                                   DatabaseScript.getSchemaCreateDDL(database,
 
2875
                                       schemaName));
 
2876
        database.logger.writeToLog(session,
 
2877
                                   "SET SCHEMA " + schemaName.statementName);
2877
2878
        session.startSchemaDefinition(name);
2878
2879
 
2879
2880
        session.loggedSchema = session.currentSchema;
2896
2897
 
2897
2898
        if (admin) {
2898
2899
            database.getGranteeManager().grant(
2899
 
                name, GranteeManager.ADMIN_ROLE_NAME);
 
2900
                name, GranteeManager.DBA_ADMIN_ROLE_NAME);
2900
2901
        }
2901
2902
    }
2902
2903
 
2945
2946
 
2946
2947
        session.checkAdmin();
2947
2948
        session.checkDDLWrite();
2948
 
        database.getUserManager().dropUser(getPassword());
 
2949
 
 
2950
        String userName = getPassword();
 
2951
 
 
2952
        if (database.getSessionManager().isUserActive(userName)) {
 
2953
 
 
2954
            // todo - new error message "cannot drop a user that is currently connected."    // NOI18N
 
2955
            throw Trace.error(Trace.ACCESS_IS_DENIED);
 
2956
        }
 
2957
 
 
2958
        database.getUserManager().dropUser(userName);
2949
2959
    }
2950
2960
 
2951
2961
    private void processDropSequence() throws HsqlException {
3030
3040
            tokenizer.isGetThis(Token.T_RESTRICT);
3031
3041
        }
3032
3042
 
 
3043
        processDropSchema(name, cascade);
 
3044
    }
 
3045
 
 
3046
    private void processDropSchema(String name,
 
3047
                                   boolean cascade) throws HsqlException {
 
3048
 
3033
3049
        if (!database.schemaManager.schemaExists(name)) {
3034
3050
            throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS);
3035
3051
        }
3066
3082
 
3067
3083
        switch (cmd) {
3068
3084
 
3069
 
            case Token.OPENBRACKET : {
3070
 
                brackets = Parser.parseOpenBrackets(tokenizer) + 1;
3071
 
 
3072
 
                tokenizer.getThis(Token.T_SELECT);
3073
 
            }
 
3085
            case Token.OPENBRACKET :
 
3086
                brackets = parser.parseOpenBracketsSelect() + 1;
3074
3087
            case Token.SELECT :
3075
3088
                cs = parser.compileSelectStatement(brackets);
3076
3089
                break;
3127
3140
        return fqn;
3128
3141
    }
3129
3142
 
 
3143
    /**
 
3144
     * Processes a SELECT INTO for a new table.
 
3145
     */
3130
3146
    Result processSelectInto(Result result, HsqlName intoHsqlName,
3131
3147
                             int intoType) throws HsqlException {
3132
3148
 
3213
3229
 
3214
3230
        int[] col;
3215
3231
 
3216
 
        col = processColumnList(t);
 
3232
        col = processColumnList(t, false);
3217
3233
 
3218
3234
        if (n == null) {
3219
3235
            n = database.nameManager.newAutoName("CT");
3237
3253
 
3238
3254
        tc = processCreateFK(t, n);
3239
3255
 
 
3256
        checkFKColumnDefaults(t, tc);
3240
3257
        t.checkColumnsMatch(tc.core.mainColArray, tc.core.refTable,
3241
3258
                            tc.core.refColArray);
3242
3259
        session.commit();
3258
3275
            name = database.nameManager.newAutoName("CT");
3259
3276
        }
3260
3277
 
3261
 
        check = new Constraint(name, null, null, null, Constraint.CHECK, 0,
3262
 
                               0);
 
3278
        check = new Constraint(name, null, null, null, Constraint.CHECK,
 
3279
                               Constraint.NO_ACTION, Constraint.NO_ACTION);
3263
3280
 
3264
3281
        processCreateCheckConstraintCondition(check);
3265
3282
        session.commit();
3274
3291
 
3275
3292
        int[] col;
3276
3293
 
3277
 
        col = processColumnList(t);
 
3294
        col = processColumnList(t, false);
3278
3295
 
3279
3296
        session.commit();
3280
3297