📄 databasecommandinterpreter.java
字号:
} else { tokenizer.back(); return DatabaseScript.getScript(database, false); } } finally { if (dsw != null) { dsw.close(); } } } /** * Responsible for handling CREATE ... * * All CREATE command require an ADMIN user except: <p> * * <pre> * CREATE TEMP [MEMORY] TABLE * </pre> * * @throws HsqlException */ private void processCreate() throws HsqlException { boolean unique = false; int tableType; boolean isTempTable = false; String token; session.checkAdmin(); session.checkDDLWrite(); session.setScripting(true); if (tokenizer.isGetThis(Token.T_GLOBAL)) { tokenizer.getThis(Token.T_TEMPORARY); isTempTable = true; } else if (tokenizer.isGetThis(Token.T_TEMP)) { isTempTable = true; } else if (tokenizer.isGetThis(Token.T_TEMPORARY)) { isTempTable = true; } token = tokenizer.getSimpleToken(); switch (Token.get(token)) { // table case Token.MEMORY : tokenizer.getThis(Token.T_TABLE); case Token.TABLE : tableType = isTempTable ? Table.TEMP_TABLE : database.getDefaultTableType(); processCreateTable(tableType); return; case Token.CACHED : if (isTempTable) { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } tokenizer.getThis(Token.T_TABLE); processCreateTable(Table.CACHED_TABLE); return; case Token.TEXT : if (isTempTable) { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } tokenizer.getThis(Token.T_TABLE); processCreateTable(Table.TEXT_TABLE); return; default : if (isTempTable) { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } switch (Token.get(token)) { // other objects case Token.ALIAS : processCreateAlias(); break; case Token.SEQUENCE : processCreateSequence(); break; case Token.SCHEMA : session.setScripting(false); processCreateSchema(); break; case Token.TRIGGER : processCreateTrigger(); break; case Token.USER : processCreateUser(); break; case Token.ROLE : database.getGranteeManager().addRole(getUserIdentifier()); break; case Token.VIEW : processCreateView(); break; // index case Token.UNIQUE : unique = true; tokenizer.getThis(Token.T_INDEX); //fall thru case Token.INDEX : processCreateIndex(unique); break; default : { throw Trace.error(Trace.UNEXPECTED_TOKEN, token); } } } /** * Process a bracketed column list as used in the declaration of SQL * CONSTRAINTS and return an array containing the indexes of the columns * within the table. * * @param t table that contains the columns * @return column index map * @throws HsqlException if a column is not found or is duplicate */ private int[] processColumnList(Table t, boolean acceptAscDesc) throws HsqlException { HashMappedList list = Parser.processColumnList(tokenizer, acceptAscDesc); int size = list.size(); int[] col = new int[size]; for (int i = 0; i < size; i++) { col[i] = t.getColumnNr((String) list.getKey(i)); } return col; } /** * Responsible for handling the execution of CREATE TRIGGER SQL * statements. <p> * * typical sql is: CREATE TRIGGER tr1 AFTER INSERT ON tab1 CALL "pkg.cls" * * @throws HsqlException */ private void processCreateTrigger() throws HsqlException { Table t; boolean isForEach; boolean isNowait; int queueSize; String triggerName; boolean isQuoted; String sWhen; String sOper; String tableName; String token; String className; TriggerDef td; Trigger o; triggerName = tokenizer.getName(); String schemaname = tokenizer.getLongNameFirst(); database.schemaManager.checkTriggerExists(triggerName, session.getSchemaNameForWrite(schemaname), false); isQuoted = tokenizer.wasQuotedIdentifier(); isForEach = false; isNowait = false; queueSize = TriggerDef.getDefaultQueueSize(); sWhen = tokenizer.getSimpleToken(); sOper = tokenizer.getSimpleToken(); tokenizer.getThis(Token.T_ON); tableName = tokenizer.getName(); if (schemaname == null) { schemaname = session.getSchemaNameForWrite(tokenizer.getLongNameFirst()); } else if (!schemaname.equals( session.getSchemaNameForWrite(tokenizer.getLongNameFirst()))) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS); } t = database.schemaManager.getUserTable(session, tableName, schemaname); if (t.isView()) { throw Trace.error(Trace.NOT_A_TABLE); } session.setScripting(true); // "FOR EACH ROW" or "CALL" token = tokenizer.getSimpleToken(); if (token.equals(Token.T_FOR)) { token = tokenizer.getSimpleToken(); if (token.equals(Token.T_EACH)) { token = tokenizer.getSimpleToken(); if (token.equals(Token.T_ROW)) { isForEach = true; // should be 'NOWAIT' or 'QUEUE' or 'CALL' token = tokenizer.getSimpleToken(); } else { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, token); } } else { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, token); } } if (token.equals(Token.T_NOWAIT)) { isNowait = true; // should be 'CALL' or 'QUEUE' token = tokenizer.getSimpleToken(); } if (token.equals(Token.T_QUEUE)) { queueSize = tokenizer.getInt(); // should be 'CALL' token = tokenizer.getSimpleToken(); } if (!token.equals(Token.T_CALL)) { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, token); } className = tokenizer.getSimpleName(); if (!tokenizer.wasQuotedIdentifier()) { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, className); } HsqlName name = database.nameManager.newHsqlName(triggerName, isQuoted); td = new TriggerDef(name, sWhen, sOper, isForEach, t, className, isNowait, queueSize, database.classLoader); t.addTrigger(td); if (td.isValid()) { try { // start the trigger thread td.start(); } catch (Exception e) { throw Trace.error(Trace.UNKNOWN_FUNCTION, e.toString()); } } database.schemaManager.registerTriggerName(triggerName, t.getName());// -- } private Column processCreateColumn() throws HsqlException { String token = tokenizer.getSimpleName(); boolean isQuoted = tokenizer.wasQuotedIdentifier(); HsqlName hsqlName = database.nameManager.newHsqlName(token, isQuoted); return processCreateColumn(hsqlName); } /** * Responsible for handling the creation of table columns during the * process of executing CREATE TABLE DDL statements. * * @param hsqlName name of the column * @return a Column object with indicated attributes * @throws HsqlException */ private Column processCreateColumn(HsqlName hsqlName) throws HsqlException { boolean isIdentity = false; long identityStart = database.firstIdentity; long identityIncrement = 1; boolean isPrimaryKey = false; String typeName; int type; int length = 0; int scale = 0; boolean hasLength = false; boolean isNullable = true; Expression defaultExpr = null; String token; typeName = tokenizer.getSimpleToken(); type = Types.getTypeNr(typeName); if (type == Types.CHAR) { if (tokenizer.isGetThis(Token.T_VARYING)) { type = Types.VARCHAR; } } if (typeName.equals(Token.T_IDENTITY)) { isIdentity = true; isPrimaryKey = true; } // fredt - when SET IGNORECASE is in effect, all new VARCHAR columns are defined as VARCHAR_IGNORECASE if (type == Types.DOUBLE) { tokenizer.isGetThis(Token.T_PRECISION); } if (tokenizer.isGetThis(Token.T_OPENBRACKET)) { hasLength = true; length = tokenizer.getInt(); Trace.check(Types.acceptsPrecisionCreateParam(type), Trace.UNEXPECTED_TOKEN); if (type != Types.TIMESTAMP && type != Types.TIME && length == 0) { throw Trace.error(Trace.INVALID_SIZE_PRECISION); } if (tokenizer.isGetThis(Token.T_COMMA)) { Trace.check(Types.acceptsScaleCreateParam(type), Trace.UNEXPECTED_TOKEN); scale = tokenizer.getInt(); } tokenizer.getThis(Token.T_CLOSEBRACKET); } else if (type == Types.CHAR && database.sqlEnforceStrictSize) { length = 1; } else if (type == Types.VARCHAR && database.sqlEnforceStrictSize) { throw Trace.error(Trace.COLUMN_SIZE_REQUIRED); } /** * @todo fredt - drop support for SET IGNORECASE and replace the * type name with a qualifier specifying the case sensitivity of VARCHAR */ if (type == Types.VARCHAR && database.isIgnoreCase()) { type = Types.VARCHAR_IGNORECASE; } if (type == Types.FLOAT && length > 53) { throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE); } if (type == Types.TIMESTAMP) { if (!hasLength) { length = 6; } else if (length != 0 && length != 6) { throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE); } } if (type == Types.TIME) { if (length != 0) { throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE); } } token = tokenizer.getSimpleToken(); if (token.equals(Token.T_DEFAULT)) { defaultExpr = processCreateDefaultExpression(type, length, scale); token = tokenizer.getSimpleToken(); } else if (token.equals(Token.T_GENERATED)) { tokenizer.getThis(Token.T_BY); tokenizer.getThis(Token.T_DEFAULT); tokenizer.getThis(Token.T_AS); tokenizer.getThis(Token.T_IDENTITY); if (tokenizer.isGetThis(Token.T_OPENBRACKET)) { tokenizer.getThis(Token.T_START); tokenizer.getThis(Token.T_WITH); identityStart = tokenizer.getBigint(); if (tokenizer.isGetThis(Token.T_COMMA)) { tokenizer.getThis(Token.T_INCREMENT); tokenizer.getThis(Token.T_BY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -