📄 databasecommandinterpreter.java
字号:
identityIncrement = tokenizer.getBigint(); } tokenizer.getThis(Token.T_CLOSEBRACKET); } isIdentity = true; isPrimaryKey = true; token = tokenizer.getSimpleToken(); } // fredt@users - accept IDENTITY before or after NOT NULL if (token.equals(Token.T_IDENTITY)) { isIdentity = true; isPrimaryKey = true; token = tokenizer.getSimpleToken(); } if (token.equals(Token.T_NULL)) { token = tokenizer.getSimpleToken(); } else if (token.equals(Token.T_NOT)) { tokenizer.getThis(Token.T_NULL); isNullable = false; token = tokenizer.getSimpleToken(); } if (token.equals(Token.T_IDENTITY)) { if (isIdentity) { throw Trace.error(Trace.SECOND_PRIMARY_KEY, Token.T_IDENTITY); } isIdentity = true; isPrimaryKey = true; token = tokenizer.getSimpleToken(); } if (token.equals(Token.T_PRIMARY)) { tokenizer.getThis(Token.T_KEY); isPrimaryKey = true; } else { tokenizer.back(); } // make sure IDENTITY and DEFAULT are not used together if (isIdentity && defaultExpr != null) { throw Trace.error(Trace.UNEXPECTED_TOKEN, Token.T_DEFAULT); } Column column = new Column(hsqlName, isNullable, type, length, scale, isPrimaryKey, defaultExpr); column.setIdentity(isIdentity, identityStart, identityIncrement); return column; } /** * @param type data type of column * @param length maximum length of column * @throws HsqlException * @return new Expression */ private Expression processCreateDefaultExpression(int type, int length, int scale) throws HsqlException { if (type == Types.OTHER) { throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE); } Parser parser = new Parser(session, database, tokenizer); Expression expr = parser.readDefaultClause(type); expr.resolveTypes(session); int newType = expr.getType(); if (newType == Expression.VALUE || newType == Expression.TRUE || newType == Expression.FALSE || (newType == Expression.FUNCTION && expr.function.isSimple)) { Object defValTemp; try { defValTemp = expr.getValue(session, type); } catch (HsqlException e) { throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE); } if (defValTemp != null && database.sqlEnforceStrictSize) { try { Column.enforceSize(defValTemp, type, length, scale, true); } catch (HsqlException e) { // default value is too long for fixed size column throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE); } } return expr; } throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE); } public static void checkBooleanDefault(String s, int type) throws HsqlException { if (type != Types.BOOLEAN || s == null) { return; } s = s.toUpperCase(); if (s.equals(Token.T_TRUE) || s.equals(Token.T_FALSE)) { return; } if (s.equals("0") || s.equals("1")) { return; } throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE, s); } /** * Responsible for handling constraints section of CREATE TABLE ... * * @param t table * @param constraint CONSTRAINT keyword used * @param primarykeycolumn primary columns * @throws HsqlException * @return list of constraints */ private HsqlArrayList processCreateConstraints(Table t, boolean constraint, int[] primarykeycolumn) throws HsqlException { String token; HsqlArrayList tcList; Constraint tempConst; HsqlName pkHsqlName;// fredt@users 20020225 - comment// HSQLDB relies on primary index to be the first one defined// and needs original or system added primary key before any// non-unique index is created tcList = new HsqlArrayList(); tempConst = new Constraint(null, primarykeycolumn, null, null, Constraint.MAIN, Constraint.NO_ACTION, Constraint.NO_ACTION);// tony_lai@users 20020820 - patch 595099 pkHsqlName = null; tcList.add(tempConst); if (!constraint) { return tcList; } while (true) { HsqlName cname = null; if (tokenizer.isGetThis(Token.T_CONSTRAINT)) { token = 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); } } cname = database.nameManager.newHsqlName(token, tokenizer.wasQuotedIdentifier()); } token = tokenizer.getSimpleToken(); switch (Token.get(token)) { case Token.PRIMARY : { tokenizer.getThis(Token.T_KEY); // tony_lai@users 20020820 - patch 595099 pkHsqlName = cname; int[] cols = processColumnList(t, false); Constraint mainConst; mainConst = (Constraint) tcList.get(0); if (mainConst.core.mainColArray != null) { if (!ArrayUtil.areEqual(mainConst.core.mainColArray, cols, cols.length, true)) { throw Trace.error(Trace.SECOND_PRIMARY_KEY); } } mainConst.core.mainColArray = cols; mainConst.constName = pkHsqlName; break; } case Token.UNIQUE : { int[] col = processColumnList(t, false); if (cname == null) { cname = database.nameManager.newAutoName("CT"); } tempConst = new Constraint(cname, col, null, null, Constraint.UNIQUE, Constraint.NO_ACTION, Constraint.NO_ACTION); tcList.add(tempConst); break; } case Token.FOREIGN : { tokenizer.getThis(Token.T_KEY); tempConst = processCreateFK(t, cname); if (tempConst.core.refColArray == null) { Constraint mainConst = (Constraint) tcList.get(0); tempConst.core.refColArray = mainConst.core.mainColArray; if (tempConst.core.refColArray == null) { throw Trace.error(Trace.CONSTRAINT_NOT_FOUND, Trace.TABLE_HAS_NO_PRIMARY_KEY); } } checkFKColumnDefaults(t, tempConst); t.checkColumnsMatch(tempConst.core.mainColArray, tempConst.core.refTable, tempConst.core.refColArray); tcList.add(tempConst); break; } case Token.CHECK : { if (cname == null) { cname = database.nameManager.newAutoName("CT"); } tempConst = new Constraint(cname, null, null, null, Constraint.CHECK, Constraint.NO_ACTION, Constraint.NO_ACTION); processCreateCheckConstraintCondition(tempConst); tcList.add(tempConst); break; } } token = tokenizer.getSimpleToken(); if (token.equals(Token.T_COMMA)) { continue; } if (token.equals(Token.T_CLOSEBRACKET)) { break; } throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } return tcList; } /** * Responsible for handling check constraints section of CREATE TABLE ... * * @param c check constraint * @throws HsqlException */ private void processCreateCheckConstraintCondition(Constraint c) throws HsqlException { tokenizer.getThis(Token.T_OPENBRACKET); Parser parser = new Parser(session, database, tokenizer); Expression condition = parser.parseExpression(); tokenizer.getThis(Token.T_CLOSEBRACKET); c.core.check = condition; } /** * Responsible for handling the execution CREATE TABLE SQL statements. * * @param type Description of the Parameter * @throws HsqlException */ private void processCreateTable(int type) throws HsqlException { String token = tokenizer.getName(); HsqlName schemaname = session.getSchemaHsqlNameForWrite(tokenizer.getLongNameFirst()); database.schemaManager.checkUserTableNotExists(session, token, schemaname.name); boolean isnamequoted = tokenizer.wasQuotedIdentifier(); int[] pkCols = null; int colIndex = 0; boolean constraint = false; Table t = newTable(type, token, isnamequoted, schemaname); tokenizer.getThis(Token.T_OPENBRACKET); while (true) { token = tokenizer.getString(); switch (Token.get(token)) { case Token.CONSTRAINT : case Token.PRIMARY : case Token.FOREIGN : case Token.UNIQUE : case Token.CHECK : // fredt@users : check for quoted reserved words used as column names constraint = !tokenizer.wasQuotedIdentifier() && !tokenizer.wasLongName(); } tokenizer.back(); if (constraint) { break; } Column newcolumn = processCreateColumn(); t.addColumn(newcolumn); if (newcolumn.isPrimaryKey()) { Trace.check(pkCols == null, Trace.SECOND_PRIMARY_KEY, newcolumn.columnName.name); pkCols = new int[]{ colIndex }; } token = tokenizer.getSimpleToken(); if (token.equals(Token.T_COMMA)) { colIndex++; continue; } if (token.equals(Token.T_CLOSEBRACKET)) { break; } throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } HsqlArrayList tempConstraints = processCreateConstraints(t, constraint, pkCols); if (tokenizer.isGetThis(Token.T_ON)) { if (!t.isTemp) { throw Trace.error(Trace.UNEXPECTED_TOKEN, Token.T_ON); } tokenizer.getThis(Token.T_COMMIT); token = tokenizer.getSimpleToken(); if (token.equals(Token.T_DELETE)) {} else if (token.equals(Token.T_PRESERVE)) { t.onCommitPreserve = true; } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } tokenizer.getThis(Token.T_ROWS); } 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(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -