📄 database.java
字号:
boolean identity = false; boolean primarykey = false; String sToken = c.getString(); String sColumn = sToken; boolean isnamequoted = c.wasQuotedIdentifier(); String typestring = c.getString(); int iType = Column.getTypeNr(typestring); Trace.check(!sColumn.equals(Table.DEFAULT_PK), Trace.COLUMN_ALREADY_EXISTS, sColumn); if (typestring.equals("IDENTITY")) { identity = true; primarykey = true; } if (iType == Types.VARCHAR && bIgnoreCase) { iType = Column.VARCHAR_IGNORECASE; } sToken = c.getString(); if (iType == Types.DOUBLE && sToken.equals("PRECISION")) { sToken = c.getString(); }// fredt@users 20020130 - patch 491987 by jimbag@users String sLen = ""; if (sToken.equals("(")) { // read length do { sToken = c.getString(); if (!sToken.equals(")")) { sLen += sToken; } } while (!sToken.equals(")")); sToken = c.getString(); } int iLen = 0; int iScale = 0; // see if we have a scale specified int index; if ((index = sLen.indexOf(",")) != -1) { String sScale = sLen.substring(index + 1, sLen.length()); sLen = sLen.substring(0, index); try { iScale = Integer.parseInt(sScale.trim()); } catch (NumberFormatException ne) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sLen); } } // convert the length if (sLen.trim().length() > 0) { try { iLen = Integer.parseInt(sLen.trim()); } catch (NumberFormatException ne) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sLen); } } String defaultvalue = null; if (sToken.equals("DEFAULT")) { String s = c.getString(); if (c.wasValue() && iType != Types.BINARY && iType != Types.OTHER) { Object sv = c.getAsValue(); if (sv != null) { defaultvalue = String.valueOf(sv); try { Column.convertObject(defaultvalue, iType); } catch (Exception e) { throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE, defaultvalue); } String testdefault = (String) Parser.enforceSize(defaultvalue, iType, iLen, false); if (defaultvalue.equals(testdefault) == false) { throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE, defaultvalue); } } } else { throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE, s); } sToken = c.getString(); } boolean nullable = true; if (sToken.equals("NULL")) { sToken = c.getString(); } else if (sToken.equals("NOT")) { c.getThis("NULL"); nullable = false; sToken = c.getString(); } if (sToken.equals("IDENTITY")) { identity = true; sToken = c.getString(); primarykey = true; } if (sToken.equals("PRIMARY")) { c.getThis("KEY"); primarykey = true; } else { c.back(); } return new Column(new HsqlName(sColumn, isnamequoted), nullable, iType, iLen, iScale, identity, primarykey, defaultvalue); }// fredt@users 20020225 - patch 509002 by fredt// temporary attributes for constraints used in processCreateTable() /** * temporary attributes for constraints used in processCreateTable() */ private class TempConstraint { HsqlName name; int[] localCol; Table expTable; int[] expCol; int type; boolean cascade; TempConstraint(HsqlName name, int[] localCol, Table expTable, int[] expCol, int type, boolean cascade) { this.name = name; this.type = type; this.localCol = localCol; this.expTable = expTable; this.expCol = expCol; this.cascade = cascade; } }// fredt@users 20020225 - patch 509002 by fredt// process constraints after parsing to include primary keys defined as// constraints// fredt@users 20020225 - patch 489777 by fredt// better error trapping// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) /** * Responsible for handling the execution CREATE TABLE SQL statements. * * @param c * @param session * @param type Description of the Parameter * @throws SQLException */ private void processCreateTable(Tokenizer c, Session session, int type) throws SQLException { Table t; String sToken = c.getName(); boolean isnamequoted = c.wasQuotedIdentifier(); if (DatabaseInformation.isSystemTable(sToken) || findUserTable(sToken, session) != null) { throw Trace.error(Trace.TABLE_ALREADY_EXISTS, sToken); } if (type == Table.TEMP_TEXT_TABLE || type == Table.TEXT_TABLE) { t = new TextTable(this, new HsqlName(sToken, isnamequoted), type, session); } else { t = new Table(this, new HsqlName(sToken, isnamequoted), type, session); } c.getThis("("); int[] primarykeycolumn = null; int column = 0; boolean constraint = false; while (true) { sToken = c.getString(); isnamequoted = c.wasQuotedIdentifier();// fredt@users 20020225 - comment// we can check here for reserved words used with quotes as column names if (sToken.equals("CONSTRAINT") || sToken.equals("PRIMARY") || sToken.equals("FOREIGN") || sToken.equals("UNIQUE")) { c.back(); constraint = true; break; } c.back(); Column newcolumn = processCreateColumn(c, t); t.addColumn(newcolumn); if (newcolumn.isPrimaryKey()) { Trace.check(primarykeycolumn == null, Trace.SECOND_PRIMARY_KEY, "column " + column); primarykeycolumn = new int[]{ column }; } sToken = c.getString(); if (sToken.equals(")")) { break; } if (!sToken.equals(",")) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } column++; } try {// 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 Vector tempConstraints = new Vector(); TempConstraint tempConst = new TempConstraint(null, primarykeycolumn, null, null, Constraint.MAIN, false);// tony_lai@users 20020820 - patch 595099 HsqlName pkName = null; tempConstraints.addElement(tempConst); if (constraint) { int i = 0; while (true) { sToken = c.getString(); HsqlName cname = null; i++; if (sToken.equals("CONSTRAINT")) { cname = new HsqlName(c.getName(), c.wasQuotedIdentifier()); sToken = c.getString(); } if (sToken.equals("PRIMARY")) { c.getThis("KEY");// tony_lai@users 20020820 - patch 595099 pkName = cname; int col[] = processColumnList(c, t); TempConstraint mainConst = (TempConstraint) tempConstraints.elementAt(0); Trace.check(mainConst.localCol == null, Trace.SECOND_PRIMARY_KEY); mainConst.localCol = col; } else if (sToken.equals("UNIQUE")) { int col[] = processColumnList(c, t); if (cname == null) { cname = HsqlName.makeAutoName("CT"); } tempConst = new TempConstraint(cname, col, null, null, Constraint.UNIQUE, false); tempConstraints.addElement(tempConst); } else if (sToken.equals("FOREIGN")) { c.getThis("KEY"); tempConst = processCreateFK(c, session, t, cname); if (tempConst.expCol == null) { TempConstraint mainConst = (TempConstraint) tempConstraints.elementAt(0); tempConst.expCol = mainConst.localCol; if (tempConst.expCol == null) { throw Trace.error(Trace.INDEX_NOT_FOUND, "table has no primary key"); } } t.checkColumnsMatch(tempConst.localCol, tempConst.expTable, tempConst.expCol); tempConstraints.addElement(tempConst); } sToken = c.getString(); if (sToken.equals(")")) { break; } if (!sToken.equals(",")) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } } } session.commit();// fredt@users 20020225 - patch 509002 by fredt// it is essential to stay compatible with existing cached tables// so we create all constraints and indexes (even duplicates) for cached// tables// CONSTRAINT PRIMARY KEY can appear in user scripts and new tables only so// we can safely apply it correctly// first apply any primary key constraint// then set all the constriants// also, duplicate indexes can be avoided if we choose to in the future but// currently we have to accept them to stay compatible with existing cached// tables that include them tempConst = (TempConstraint) tempConstraints.elementAt(0);// tony_lai@users 20020820 - patch 595099 t.createPrimaryKey(pkName, tempConst.localCol); boolean logDDL = false; for (int i = 1; i < tempConstraints.size(); i++) { tempConst = (TempConstraint) tempConstraints.elementAt(i); if (tempConst.type == Constraint.UNIQUE) { TableWorks tw = new TableWorks(t); tw.createUniqueConstraint(tempConst.localCol, tempConst.name); t = tw.getTable(); } if (tempConst.type == Constraint.FOREIGN_KEY) { TableWorks tw = new TableWorks(t); tw.createForeignKey(tempConst.localCol, tempConst.expCol, tempConst.name, tempConst.expTable, tempConst.cascade); t = tw.getTable(); } } linkTable(t); } catch (SQLException e) {// fredt@users 20020225 - comment// if a SQLException 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. removeExportedKeys(t); throw e; } } TempConstraint processCreateFK(Tokenizer c, Session session, Table t,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -