📄 database.java
字号:
c.back();// fredt@users - patch 1.7.0 - no DROP TABLE statements with SCRIPT command// try to script all but drop, insert; but no positions for cached tables return getScript(false, true, false, session); } } /** * Responsible for handling the parse and execution of CREATE SQL * statements. * * @param c the tokenized representation of the statement being processed * @param session * @return * @throws SQLException */ private Result processCreate(Tokenizer c, Session session) throws SQLException { session.checkReadWrite(); session.checkAdmin(); String sToken = c.getString();// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) boolean isTemp = false; if (sToken.equals("TEMP")) { isTemp = true; sToken = c.getString(); Trace.check(sToken.equals("TABLE") || sToken.equals("MEMORY") || sToken.equals("TEXT"), Trace.UNEXPECTED_TOKEN, sToken); session.setScripting(false); } else { session.checkReadWrite(); session.checkAdmin(); session.setScripting(true); }// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) if (sToken.equals("TABLE")) { int tableType = isTemp ? Table.TEMP_TABLE : Table.MEMORY_TABLE; processCreateTable(c, session, tableType); } else if (sToken.equals("MEMORY")) { c.getThis("TABLE"); int tableType = isTemp ? Table.TEMP_TABLE : Table.MEMORY_TABLE; processCreateTable(c, session, tableType); } else if (sToken.equals("CACHED")) { c.getThis("TABLE"); processCreateTable(c, session, Table.CACHED_TABLE); } else if (sToken.equals("TEXT")) { c.getThis("TABLE"); int tableType = isTemp ? Table.TEMP_TEXT_TABLE : Table.TEXT_TABLE; processCreateTable(c, session, tableType); } else if (sToken.equals("VIEW")) { processCreateView(c, session); } else if (sToken.equals("TRIGGER")) { processCreateTrigger(c, session); } else if (sToken.equals("USER")) { String u = c.getStringToken(); c.getThis("PASSWORD"); String p = c.getStringToken(); boolean admin; if (c.getString().equals("ADMIN")) { admin = true; } else { admin = false; } aAccess.createUser(u, p, admin); } else if (sToken.equals("ALIAS")) { String name = c.getString(); sToken = c.getString(); Trace.check(sToken.equals("FOR"), Trace.UNEXPECTED_TOKEN, sToken); sToken = c.getString();// fredt@users 20010701 - patch 1.6.1 by fredt - open <1.60 db files// convert org.hsql.Library aliases from versions < 1.60 to org.hsqldb// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) - ABS function if (sToken.startsWith("org.hsql.Library.")) { sToken = "org.hsqldb.Library." + sToken.substring("org.hsql.Library.".length()); } else if (sToken.equals("java.lang.Math.abs")) { sToken = "org.hsqldb.Library.abs"; } hAlias.put(name, sToken); } else { boolean unique = false; if (sToken.equals("UNIQUE")) { unique = true; sToken = c.getString(); } if (!sToken.equals("INDEX")) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } String name = c.getName(); boolean isnamequoted = c.wasQuotedIdentifier(); c.getThis("ON"); Table t = getTable(c.getName(), session); addIndexOn(c, session, name, isnamequoted, t, unique); } return new Result(); } /** * 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 c * @param t table that contains the columns * @return * @throws SQLException if a column is not found or is duplicated */ private int[] processColumnList(Tokenizer c, Table t) throws SQLException { Vector v = new Vector(); Hashtable h = new Hashtable(); c.getThis("("); while (true) { String colname = c.getName(); v.addElement(colname); h.put(colname, colname); String sToken = c.getString(); if (sToken.equals(")")) { break; } if (!sToken.equals(",")) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } } int s = v.size(); if (s != h.size()) { throw Trace.error(Trace.COLUMN_ALREADY_EXISTS, "duplicate column in list"); } int col[] = new int[s]; for (int i = 0; i < s; i++) { col[i] = t.getColumnNr((String) v.elementAt(i)); } return col; } /** * Indexes defined in DDL scripts are handled by this method. If the * name of an existing index begins with "SYS_", the name is changed to * begin with "USER_". The name should be unique within the database. * For compatibility with old database, non-unique names are modified * and assigned a new name<br> * (fredt@users) * * @param c * @param session * @param name * @param t * @param unique * @param namequoted The feature to be added to the IndexOn attribute * @throws SQLException */ private void addIndexOn(Tokenizer c, Session session, String name, boolean namequoted, Table t, boolean unique) throws SQLException { HsqlName indexname; int col[] = processColumnList(c, t); if (HsqlName.isReservedName(name)) { indexname = HsqlName.makeAutoName("USER", name); } else { indexname = new HsqlName(name, namequoted); }// fredt@users - to check further - this is confined only to old scripts// rename duplicate indexes/* if (findIndex(name) != null && session == sysSession && databaseProperties.getProperty("hsqldb.compatible_version") .equals("1.6.0")) { indexname = HsqlName.makeAutoName("USER", name); name = indexname.name; }*/ if (findIndex(name) != null) { throw Trace.error(Trace.INDEX_ALREADY_EXISTS); } session.commit(); session.setScripting(!t.isTemp()); TableWorks tw = new TableWorks(t); tw.createIndex(col, indexname, unique); } /** * Finds an index with the given name in the whole database. * * @param name Description of the Parameter * @return Description of the Return Value */ private Index findIndex(String name) { Table t = findTableForIndex(name); if (t == null) { return null; } else { return t.getIndex(name); } } /** * Finds the table that has an index with the given name in the * whole database. * * @param name Description of the Parameter * @return Description of the Return Value */ private Table findTableForIndex(String name) { for (int i = 0, tsize = tTable.size(); i < tsize; i++) { Table t = (Table) tTable.elementAt(i); if (t.getIndex(name) != null) { return t; } } return null; } /** * Retrieves the index of a table or view in the Vector that contains * these objects for a Database. * * @param table the Table object * @return the index of the specified table or view, or -1 if not found */ int getTableIndex(Table table) { for (int i = 0, tsize = tTable.size(); i < tsize; i++) { Table t = (Table) tTable.elementAt(i); if (t == table) { return i; } } return -1; } /** * Responsible for handling the execution of CREATE TRIGGER SQL * statements. <p> * * typical sql is: CREATE TRIGGER tr1 AFTER INSERT ON tab1 CALL "pkg.cls" * * @param c the tokenized representation of the statement being processed * @param session * @throws SQLException */ private void processCreateTrigger(Tokenizer c, Session session) throws SQLException { Table t; boolean bForEach = false; boolean bNowait = false; int nQueueSize = TriggerDef.getDefaultQueueSize(); String sTrigName = c.getName(); String sWhen = c.getString(); String sOper = c.getString(); c.getThis("ON"); String sTableName = c.getString(); t = getTable(sTableName, session); if (t.isView()) { throw Trace.error(Trace.NOT_A_TABLE); }// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) session.setScripting(!t.isTemp()); // "FOR EACH ROW" or "CALL" String tok = c.getString(); if (tok.equals("FOR")) { tok = c.getString(); if (tok.equals("EACH")) { tok = c.getString(); if (tok.equals("ROW")) { bForEach = true; tok = c.getString(); // should be 'NOWAIT' or 'QUEUE' or 'CALL' } else { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, tok); } } else { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, tok); } } if (tok.equals("NOWAIT")) { bNowait = true; tok = c.getString(); // should be 'CALL' or 'QUEUE' } if (tok.equals("QUEUE")) { nQueueSize = Integer.parseInt(c.getString()); tok = c.getString(); // should be 'CALL' } if (!tok.equals("CALL")) { throw Trace.error(Trace.UNEXPECTED_END_OF_COMMAND, tok); } String sClassName = c.getString(); // double quotes have been stripped TriggerDef td; Trigger o; try { Class cl = Class.forName(sClassName); // dynamically load class o = (Trigger) cl.newInstance(); // dynamically instantiate it td = new TriggerDef(sTrigName, sWhen, sOper, bForEach, t, o, "\"" + sClassName + "\"", bNowait, nQueueSize); if (td.isValid()) { t.addTrigger(td); td.start(); // start the trigger thread } else { String msg = "Error in parsing trigger command "; throw Trace.error(Trace.UNEXPECTED_TOKEN, msg); } } catch (Exception e) { String msg = "Exception in loading trigger class " + e.getMessage(); throw Trace.error(Trace.UNKNOWN_FUNCTION, msg); } } /** * Responsible for handling the creation of table columns during the * process of executing CREATE TABLE statements. * * @param c the tokenized representation of the statement being processed * @param t target table * @return * @throws SQLException */ private Column processCreateColumn(Tokenizer c, Table t) throws SQLException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -