📄 databasecommandinterpreter.java
字号:
try { session.commit(); Constraint primaryConst = (Constraint) tempConstraints.get(0); t.createPrimaryKey(null, primaryConst.core.mainColArray, true); if (primaryConst.core.mainColArray != null) { if (primaryConst.constName == null) { primaryConst.constName = t.makeSysPKName(); } Constraint newconstraint = new Constraint(primaryConst.constName, t, t.getPrimaryIndex(), Constraint.PRIMARY_KEY); t.addConstraint(newconstraint); database.schemaManager.registerConstraintName( primaryConst.constName.name, t.getName()); } for (int i = 1; i < tempConstraints.size(); i++) { Constraint tempConst = (Constraint) tempConstraints.get(i); if (tempConst.constType == Constraint.UNIQUE) { TableWorks tableWorks = new TableWorks(session, t); tableWorks.createUniqueConstraint( tempConst.core.mainColArray, tempConst.constName); t = tableWorks.getTable(); } if (tempConst.constType == Constraint.FOREIGN_KEY) { TableWorks tableWorks = new TableWorks(session, t); tableWorks.createForeignKey(tempConst.core.mainColArray, tempConst.core.refColArray, tempConst.constName, tempConst.core.refTable, tempConst.core.deleteAction, tempConst.core.updateAction); t = tableWorks.getTable(); } if (tempConst.constType == Constraint.CHECK) { TableWorks tableWorks = new TableWorks(session, t); tableWorks.createCheckConstraint(tempConst, tempConst.constName); t = tableWorks.getTable(); } } database.schemaManager.linkTable(t); } catch (HsqlException e) {// fredt@users 20020225 - comment// if a HsqlException is thrown while creating table, any foreign key that has// been created leaves it modification to the expTable in place// need to undo those modifications. This should not happen in practice. database.schemaManager.removeExportedKeys(t); database.schemaManager.removeIndexNames(t.tableName); database.schemaManager.removeConstraintNames(t.tableName); throw e; } }// fredt@users 20020221 - patch 520213 by boucherb@users - self reference FK// allows foreign keys that reference a column in the same table /** * @param t table * @param cname foreign key name * @throws HsqlException * @return constraint */ private Constraint processCreateFK(Table t, HsqlName cname) throws HsqlException { int[] localcol; int[] expcol; String expTableName; Table expTable; String token; localcol = processColumnList(t, false); tokenizer.getThis(Token.T_REFERENCES); expTableName = tokenizer.getName(); String constraintSchema = tokenizer.getLongNameFirst(); if (constraintSchema != null) { constraintSchema = session.getSchemaNameForWrite(tokenizer.getLongNameFirst()); if (!t.getSchemaName().equals(constraintSchema)) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS, constraintSchema); } } if (t.getName().name.equals(expTableName)) { expTable = t; } else { expTable = database.schemaManager.getTable(session, expTableName, t.getSchemaName()); } expcol = null; token = tokenizer.getSimpleToken(); tokenizer.back(); if (token.equals(Token.T_OPENBRACKET)) { expcol = processColumnList(expTable, false); } else { if (expTable.getPrimaryKey() == null) { // getPrimaryKey() == null is true while creating the table // fredt - FK statement is part of CREATE TABLE and is self-referencing // reference must be to same table being created // it is resolved in the calling method Trace.check(t == expTable, Trace.TABLE_HAS_NO_PRIMARY_KEY); } else { if (expTable.hasPrimaryKey()) { expcol = expTable.getPrimaryKey(); } else { throw Trace.error(Trace.CONSTRAINT_NOT_FOUND, Trace.TABLE_HAS_NO_PRIMARY_KEY); } } } token = tokenizer.getSimpleToken(); // -- In a while loop we parse a maximium of two // -- "ON" statements following the foreign key // -- definition this can be // -- ON [UPDATE|DELETE] [NO ACTION|RESTRICT|CASCADE|SET [NULL|DEFAULT]] int deleteAction = Constraint.NO_ACTION; int updateAction = Constraint.NO_ACTION; while (token.equals(Token.T_ON)) { token = tokenizer.getSimpleToken(); if (deleteAction == Constraint.NO_ACTION && token.equals(Token.T_DELETE)) { token = tokenizer.getSimpleToken(); if (token.equals(Token.T_SET)) { token = tokenizer.getSimpleToken(); if (token.equals(Token.T_DEFAULT)) { deleteAction = Constraint.SET_DEFAULT; } else if (token.equals(Token.T_NULL)) { deleteAction = Constraint.SET_NULL; } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } else if (token.equals(Token.T_CASCADE)) { deleteAction = Constraint.CASCADE; } else if (token.equals(Token.T_RESTRICT)) { // LEGACY compatibility/usability // - same as NO ACTION or nothing at all } else { tokenizer.matchThis(Token.T_NO); tokenizer.getThis(Token.T_ACTION); } } else if (updateAction == Constraint.NO_ACTION && token.equals(Token.T_UPDATE)) { token = tokenizer.getSimpleToken(); if (token.equals(Token.T_SET)) { token = tokenizer.getSimpleToken(); if (token.equals(Token.T_DEFAULT)) { updateAction = Constraint.SET_DEFAULT; } else if (token.equals(Token.T_NULL)) { updateAction = Constraint.SET_NULL; } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } else if (token.equals(Token.T_CASCADE)) { updateAction = Constraint.CASCADE; } else if (token.equals(Token.T_RESTRICT)) { // LEGACY compatibility/usability // - same as NO ACTION or nothing at all } else { tokenizer.matchThis(Token.T_NO); tokenizer.getThis(Token.T_ACTION); } } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } token = tokenizer.getSimpleToken(); } tokenizer.back(); if (cname == null) { cname = database.nameManager.newAutoName("FK"); } return new Constraint(cname, localcol, expTable, expcol, Constraint.FOREIGN_KEY, deleteAction, updateAction); } /** * Responsible for handling the execution CREATE VIEW SQL statements. * * @throws HsqlException */ private void processCreateView() throws HsqlException { String name = tokenizer.getName(); HsqlName schemaname = session.getSchemaHsqlNameForWrite(tokenizer.getLongNameFirst()); int logposition = tokenizer.getPartMarker(); database.schemaManager.checkUserViewNotExists(session, name, schemaname.name); HsqlName viewHsqlName = database.nameManager.newHsqlName(name, tokenizer.wasQuotedIdentifier()); viewHsqlName.schema = schemaname; HsqlName[] colList = null; // fredt - a bug in 1.8.0.0 and previous versions causes view // definitions to script without double quotes around column names // in certain cases; the workaround here discards such scripted column // lists when used in OOo if (tokenizer.isGetThis(Token.T_OPENBRACKET)) { try { HsqlArrayList list = Parser.getColumnNames(database, null, tokenizer, true); colList = new HsqlName[list.size()]; colList = (HsqlName[]) list.toArray(colList); //added lines to make sure all valid columns are quoted if (database.isStoredFileAccess()) { for (int i = 0; i < colList.length; i++) { if (!colList[i].isNameQuoted) { colList = null; break; } } } } catch (HsqlException e) { //added lines to catch unquoted names with spaces if (database.isStoredFileAccess()) { while (!tokenizer.getString().equals( Token.T_CLOSEBRACKET)) {} } else { throw e; } } } tokenizer.getThis(Token.T_AS); tokenizer.setPartMarker(); Parser parser = new Parser(session, database, tokenizer); int brackets = parser.parseOpenBracketsSelect(); Select select; // accept ORDER BY or ORDRY BY with LIMIT - accept unions select = parser.parseSelect(brackets, true, false, true, true); if (select.sIntoTable != null) { throw (Trace.error(Trace.INVALID_IDENTIFIER, Token.INTO)); } select.prepareResult(session); View view = new View(session, database, viewHsqlName, tokenizer.getLastPart(), colList); session.commit(); database.schemaManager.linkTable(view); tokenizer.setPartMarker(logposition); } /** * Responsible for handling tail of ALTER TABLE ... RENAME ... * @param t table * @throws HsqlException */ private void processAlterTableRename(Table t) throws HsqlException { String schema = t.getSchemaName(); String newName; boolean isquoted; // ensures that if temp table, it also belongs to this session/* if (!t.equals(session, name)) { throw Trace.error(Trace.TABLE_NOT_FOUND); }*/ tokenizer.getThis(Token.T_TO); newName = tokenizer.getName(); String newSchema = tokenizer.getLongNameFirst(); isquoted = tokenizer.wasQuotedIdentifier(); newSchema = newSchema == null ? schema : session.getSchemaNameForWrite( newSchema); if (!schema.equals(newSchema)) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS); } database.schemaManager.checkUserTableNotExists(session, newName, schema); session.commit(); session.setScripting(true); database.schemaManager.renameTable(session, t, newName, isquoted); } /** * Handles ALTER TABLE statements. <p> * * ALTER TABLE <name> RENAME TO <newname> * ALTER INDEX <name> RENAME TO <newname> * * ALTER TABLE <name> ADD CONSTRAINT <constname> FOREIGN KEY (<col>, ...) * REFERENCE <other table> (<col>, ...) [ON DELETE CASCADE] * * ALTER TABLE <name> ADD CONSTRAINT <constname> UNIQUE (<col>, ...) * * @throws HsqlException */ private void processAlter() throws HsqlException { String token; session.checkAdmin(); session.checkDDLWrite(); session.setScripting(true); token = tokenizer.getSimpleToken(); switch (Token.get(token)) { case Token.INDEX : { processAlterIndex(); break; } case Token.SCHEMA : { processAlterSchema(); break; } case Token.SEQUENCE : { processAlterSequence(); break; } case Token.TABLE : { processAlterTable(); break; } case Token.USER : { processAlterUser(); break; } default : { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } } /** * Handles ALTER TABLE DDL. * * @throws HsqlException */ private void processAlterTable() throws HsqlException { String tableName = tokenizer.getName(); String schema = session.getSchemaNameForWrite(tokenizer.getLongNameFirst()); Table t = database.schemaManager.getUserTable(session, tableName, schema); String token; if (t.isView()) { throw Trace.error(Trace.NOT_A_TABLE); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -