📄 table.java
字号:
* */ void dropIndex(Session session, String indexname) throws HsqlException { // find the array index for indexname and remove int todrop = getIndexIndex(indexname); indexList = (Index[]) ArrayUtil.toAdjustedArray(indexList, null, todrop, -1); setBestRowIdentifiers(); dropIndexFromRows(session, todrop); } void dropIndexFromRows(Session session, int index) throws HsqlException { RowIterator it = getPrimaryIndex().firstRow(session); while (it.hasNext()) { Row row = it.next(); int i = index - 1; Node backnode = row.getNode(0); while (i-- > 0) { backnode = backnode.nNext; } backnode.nNext = backnode.nNext.nNext; } } /** * Moves the data from table to table. * The colindex argument is the index of the column that was * added or removed. The adjust argument is {-1 | 0 | +1} */ void moveData(Session session, Table from, int colindex, int adjust) throws HsqlException { Object colvalue = null; Column column = null; if (adjust >= 0 && colindex != -1) { column = getColumn(colindex); colvalue = column.getDefaultValue(session); } RowIterator it = from.getPrimaryIndex().firstRow(session); while (it.hasNext()) { Row row = it.next(); Object[] o = row.getData(); Object[] data = getEmptyRowData(); if (adjust == 0 && colindex != -1) { colvalue = Column.convertObject(session, o[colindex], column.getType(), column.getSize(), column.getScale()); } ArrayUtil.copyAdjustArray(o, data, colvalue, colindex, adjust); setIdentityColumn(session, data); enforceNullConstraints(data); Row newrow = newRow(data); indexRow(session, newrow); } from.drop(); } /** * Highest level multiple row insert method. Corresponds to an SQL * INSERT INTO ... SELECT ... statement. */ int insert(Session session, Result ins) throws HsqlException { Record ni = ins.rRoot; int count = 0; fireAll(session, Trigger.INSERT_BEFORE); while (ni != null) { insertRow(session, ni.data); ni = ni.next; count++; } fireAll(session, Trigger.INSERT_AFTER); return count; } /** * Highest level method for inserting a single row. Corresponds to an * SQL INSERT INTO .... VALUES(,,) statement. * fires triggers. */ void insert(Session session, Object[] data) throws HsqlException { fireAll(session, Trigger.INSERT_BEFORE); insertRow(session, data); fireAll(session, Trigger.INSERT_AFTER); } /** * Mid level method for inserting rows. Performs constraint checks and * fires row level triggers. */ private void insertRow(Session session, Object[] data) throws HsqlException { if (triggerLists[Trigger.INSERT_BEFORE_ROW] != null) { fireAll(session, Trigger.INSERT_BEFORE_ROW, null, data); } setIdentityColumn(session, data); checkRowDataInsert(session, data); insertNoCheck(session, data); if (triggerLists[Trigger.INSERT_AFTER_ROW] != null) { fireAll(session, Trigger.INSERT_AFTER_ROW, null, data); checkRowDataInsert(session, data); } } /** * Multi-row insert method. Used for SELECT ... INTO tablename queries. * These tables are new, empty tables, with no constraints, triggers * column default values, column size enforcement whatsoever. * * Not used for INSERT INTO .... SELECT ... FROM queries */ void insertIntoTable(Session session, Result result) throws HsqlException { insertResult(session, result); if (!isLogged) { return; } Record r = result.rRoot; while (r != null) { database.logger.writeInsertStatement(session, this, r.data); r = r.next; } } /** * Low level method for row insert. * UNIQUE or PRIMARY constraints are enforced by attempting to * add the row to the indexes. */ private void insertNoCheck(Session session, Object[] data) throws HsqlException { Row row = newRow(data); // this handles the UNIQUE constraints indexRow(session, row); if (session != null) { session.addInsertAction(this, row); } if (isLogged) { database.logger.writeInsertStatement(session, this, data); } } /** * */ public void insertNoCheckFromLog(Session session, Object[] data) throws HsqlException { Row r = newRow(data); updateIdentityValue(data); indexRow(session, r); if (session != null) { session.addInsertAction(this, r); } } /** * Low level method for restoring deleted rows */ void insertNoCheckRollback(Session session, Row row, boolean log) throws HsqlException { Row newrow = restoreRow(row); // instead of new row, use new routine so that the row does not use // rowstore.add(), which will allocate new space and different pos indexRow(session, newrow); if (log && isLogged) { database.logger.writeInsertStatement(session, this, row.getData()); } } /** * Used for system table inserts. No checks. No identity * columns. */ int insertSys(Result ins) throws HsqlException { Record ni = ins.rRoot; int count = 0; while (ni != null) { insertData(null, ni.data); ni = ni.next; count++; } return count; } /** * Used for subquery inserts. No checks. No identity * columns. */ int insertResult(Session session, Result ins) throws HsqlException { Record ni = ins.rRoot; int count = 0; while (ni != null) { Object[] newData = (Object[]) ArrayUtil.resizeArrayIfDifferent(ni.data, columnCount); insertData(session, newData); ni = ni.next; count++; } return count; } /** * Not for general use. * Used by ScriptReader to unconditionally insert a row into * the table when the .script file is read. */ public void insertFromScript(Object[] data) throws HsqlException { updateIdentityValue(data); insertData(null, data); } /** * Used by the methods above. */ public void insertData(Session session, Object[] data) throws HsqlException { Row row = newRow(data); indexRow(session, row); commitRowToStore(row); } /** * Used by the system tables */ public void insertSys(Object[] data) throws HsqlException { Row row = newRow(data); indexRow(null, row); } /** * Used by TextCache to insert a row into the indexes when the source * file is first read. */ protected void insertFromTextSource(CachedRow row) throws HsqlException { Object[] data = row.getData(); updateIdentityValue(data); enforceFieldValueLimits(data, defaultColumnMap); enforceNullConstraints(data); int i = 0; try { for (; i < indexList.length; i++) { indexList[i].insert(null, row, i); } } catch (HsqlException e) { // unique index violation - rollback insert for (--i; i >= 0; i--) { Node n = row.getNode(i); indexList[i].delete(null, n); } row.delete(); removeRowFromStore(row); throw e; } } /** * Checks a row against NOT NULL constraints on columns. */ protected void enforceNullConstraints(Object[] data) throws HsqlException { for (int i = 0; i < columnCount; i++) { if (data[i] == null &&!colNullable[i]) { Trace.throwerror(Trace.TRY_TO_INSERT_NULL, "column: " + getColumn(i).columnName.name + " table: " + tableName.name); } } } /** * If there is an identity column (visible or hidden) on the table, sets * the value and/or adjusts the iIdentiy value for the table. */ protected void setIdentityColumn(Session session, Object[] data) throws HsqlException { if (identityColumn != -1) { Number id = (Number) data[identityColumn]; if (id == null) { if (colTypes[identityColumn] == Types.INTEGER) { id = ValuePool.getInt((int) identitySequence.getValue()); } else { id = ValuePool.getLong(identitySequence.getValue()); } data[identityColumn] = id; } else { identitySequence.getValue(id.longValue()); } if (session != null) { session.setLastIdentity(id); } } } /** * If there is an identity column (visible or hidden) on the table, sets * the max identity value. */ protected void updateIdentityValue(Object[] data) throws HsqlException { if (identityColumn != -1) { Number id = (Number) data[identityColumn]; if (id != null) { identitySequence.getValue(id.longValue()); } } } /** * Enforce max field sizes according to SQL column definition. * SQL92 13.8 */ void enforceFieldValueLimits(Object[] data, int[] cols) throws HsqlException { int i; int colindex; if (sqlEnforceSize) { if (cols == null) { cols = defaultColumnMap; } for (i = 0; i < cols.length; i++) { colindex = cols[i]; if ((colTypes[colindex] == Types.TIMESTAMP || colSizes[colindex] != 0) && data[colindex] != null) { data[colindex] = Column.enforceSize(data[colindex], colTypes[colindex], colSizes[colindex], colScales[colindex], true); } } } }// fredt@users 20020130 - patch 491987 by jimbag@users - modified /** * Fires all row-level triggers of the given set (trigger type) * */ void fireAll(Session session, int trigVecIndx, Object[] oldrow, Object[] newrow) { if (!database.isReferentialIntegrity()) { // isReferentialIntegrity is false when reloading db return; } HsqlArrayList trigVec = triggerLists[trigVecIndx]; if (trigVec == null) { return; } for (int i = 0, size = trigVec.siz
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -