📄 table.java
字号:
* Method declaration * * @param result * @param c * @throws SQLException */ void insert(Result result, Session c) throws SQLException { // if violation of constraints can occur, insert must be rolled back // outside of this function! Record r = result.rRoot; int len = result.getColumnCount(); while (r != null) { Object row[] = getNewRow(); for (int i = 0; i < len; i++) { row[i] = r.data[i]; } insert(row, c); r = r.next; } } /** * Method declaration * * @param row * @param c * @throws SQLException */ void insert(Object row[], Session c) throws SQLException { Trace.check(!isReadOnly, Trace.DATA_IS_READONLY); fireAll(TriggerDef.INSERT_BEFORE, row); if (dDatabase.isReferentialIntegrity()) { for (int i = 0; i < vConstraint.size(); i++) { ((Constraint) vConstraint.elementAt(i)).checkInsert(row); } } insertNoCheck(row, c, true); fireAll(TriggerDef.INSERT_AFTER, row); } /** * Method declaration * * @param row * @param c * @param log * @throws SQLException */ void insertNoCheck(Object row[], Session c, boolean log) throws SQLException { for (int i = 0; i < iColumnCount; i++) { if (row[i] == null) { Column col = getColumn(i); boolean nullOK = col.isNullable() || col.isIdentity(); if (!nullOK) { throw Trace.error(Trace.TRY_TO_INSERT_NULL); } } } int nextId = iIdentityId; if (iIdentityColumn != -1) { Number id = (Number) row[iIdentityColumn]; if (id == null) { row[iIdentityColumn] = new Integer(iIdentityId); } else { int columnId = id.intValue(); if (iIdentityId < columnId) { iIdentityId = nextId = columnId; } } } Row r = Row.newRow(this, row); if (isText) { //-- Always inserted at end of file. nextId = ((CachedRow) r).iPos + ((CachedRow) r).storageSize; } else { nextId++; } indexRow(r, true); if (c != null) { c.setLastIdentity(iIdentityId); c.addTransactionInsert(this, row); } iIdentityId = nextId; if (log &&!isTemp &&!isReadOnly && dDatabase.logger.hasLog()) { dDatabase.logger.writeToLog(c, getInsertStatement(row)); } } /** * Method declaration * * @param trigVecIndx * @param row */ void fireAll(int trigVecIndx, Object row[]) { if (!dDatabase.isReferentialIntegrity()) { // reloading db return; } Vector trigVec = vTrigs[trigVecIndx]; int trCount = trigVec.size(); for (int i = 0; i < trCount; i++) { TriggerDef td = (TriggerDef) trigVec.elementAt(i); td.push(row); // tell the trigger thread to fire with this row } }// statement-level triggers /** * Method declaration * * @param trigVecIndx */ void fireAll(int trigVecIndx) { Object row[] = new Object[1]; row[0] = new String("Statement-level"); fireAll(trigVecIndx, row); } /** * Method declaration * * @param trigDef */ void addTrigger(TriggerDef trigDef) { if (Trace.TRACE) { Trace.trace("Trigger added " + String.valueOf(trigDef.vectorIndx)); } vTrigs[trigDef.vectorIndx].addElement(trigDef); }// fredt@users 20020225 - patch 1.7.0 - CASCADING DELETES /** * Method is called recursively on a tree of tables from the current one * until no referring foreign-key table is left. In the process, if a * non-cascading foreign-key referring table contains data, an exception * is thrown. Parameter delete indicates whether to delete refering rows. * The method is called first to check if the row can be deleted, then to * delete the row and all the refering rows. (fredt@users) * * @param row * @param session * @param delete * @throws SQLException */ void checkCascadeDelete(Object[] row, Session session, boolean delete) throws SQLException { for (int i = 0; i < vConstraint.size(); i++) { Constraint c = (Constraint) vConstraint.elementAt(i); if (c.getType() != Constraint.MAIN || c.getRef() == null) { continue; } Node refnode = c.findFkRef(row); if (refnode == null) { // no referencing row found continue; } Table reftable = c.getRef(); // shortcut when deltable has no imported constraint boolean hasref = reftable.getNextConstraintIndex(0, Constraint.MAIN) != -1; if (delete == false && hasref == false) { return; } Index refindex = c.getRefIndex(); int maincolumns[] = c.getMainColumns(); Object[] mainobjects = new Object[maincolumns.length]; ArrayUtil.copyColumnValues(row, maincolumns, mainobjects); // walk the index for all the nodes that reference delnode for (Node n = refnode; refindex.comparePartialRowNonUnique( mainobjects, n.getData()) == 0; ) { // get the next node before n is deleted Node nextn = refindex.next(n); if (hasref) { reftable.checkCascadeDelete(n.getData(), session, delete); } if (delete) { reftable.deleteNoRefCheck(n.getData(), session); // foreign key referencing own table if (reftable == this) { nextn = c.findFkRef(row); } } if (nextn == null) { break; } n = nextn; } } } /** * Method declaration * * @param row * @param session Description of the Parameter * @throws SQLException */ void delete(Object row[], Session session) throws SQLException { fireAll(TriggerDef.DELETE_BEFORE_ROW, row); if (dDatabase.isReferentialIntegrity()) { checkCascadeDelete(row, session, false); checkCascadeDelete(row, session, true); } deleteNoCheck(row, session, true); // fire the delete after statement trigger fireAll(TriggerDef.DELETE_AFTER_ROW, row); } /** * Method declaration * * @param row * @param session Description of the Parameter * @throws SQLException */ private void deleteNoRefCheck(Object row[], Session session) throws SQLException { fireAll(TriggerDef.DELETE_BEFORE_ROW, row); deleteNoCheck(row, session, true); // fire the delete after statement trigger fireAll(TriggerDef.DELETE_AFTER_ROW, row); } /** * Method declaration * * @param row * @param c * @param log * @throws SQLException */ void deleteNoCheck(Object row[], Session c, boolean log) throws SQLException { for (int i = 1; i < iIndexCount; i++) { getIndex(i).delete(row, false); } // must delete data last getIndex(0).delete(row, true); if (c != null) { c.addTransactionDelete(this, row); } if (log &&!isTemp &&!isReadOnly && dDatabase.logger.hasLog()) { dDatabase.logger.writeToLog(c, getDeleteStatement(row)); } } /** * Method declaration * * @param row * @return * @throws SQLException */ String getInsertStatement(Object row[]) throws SQLException { StringBuffer a = new StringBuffer(128); a.append("INSERT INTO "); a.append(tableName.statementName); a.append(" VALUES("); for (int i = 0; i < iVisibleColumns; i++) { a.append(Column.createSQLString(row[i], getColumn(i).getType())); a.append(','); } a.setCharAt(a.length() - 1, ')'); return a.toString(); } /** * Method declaration * * @return */ boolean isCached() { return isCached; } /** * Method declaration * * @return */ boolean isIndexCached() { return isCached; } /** * Method declaration * * @param s * @return */ Index getIndex(String s) { for (int i = 0; i < iIndexCount; i++) { Index h = getIndex(i); if (s.equals(h.getName().name)) { return h; } } // no such index return null; } /** * Return the position of the constraint within the list * * @param s * @return */ int getConstraintIndex(String s) { for (int j = 0; j < vConstraint.size(); j++) { Constraint tempc = (Constraint) vConstraint.elementAt(j); if (tempc.getName().name.equals(s)) { return j; } } return -1; } /** * return the named constriant * * @param s * @return */ Constraint getConstraint(String s) { int j = getConstraintIndex(s); if (j >= 0) { return (Constraint) vConstraint.elementAt(j); } else { return null; } } /** * Method declaration * * @param i * @return */ Column getColumn(int i) { return (Column) vColumn.elementAt(i); } /** * Method declaration * * @return */ int[] getColumnTypes() { return colTypes; } /** * Method declaration * * @param i * @return */ protected Index getIndex(int i) { return (Index) vIndex.elementAt(i); } /** * Method declaration * * @param row * @return * @throws SQLException */ private String getDeleteStatement(Object row[]) throws SQLException { StringBuffer a = new StringBuffer(128); a.append("DELETE FROM "); a.append(tableName.statementName); a.append(" WHERE "); if (iVisibleColumns < iColumnCount) { for (int i = 0; i < iVisibleColumns; i++) { Column c = getColumn(i); a.append(c.columnName.statementName); a.append('='); a.append(Column.createSQLString(row[i], c.getType())); if (i < iVisibleColumns - 1) { a.append(" AND "); } } } else { for (int i = 0; i < iPrimaryKey.length; i++) { Column c = getColumn(iPrimaryKey[i]); a.append(c.columnName.statementName); a.append('='); a.append(Column.createSQLString(row[iPrimaryKey[i]], c.getType())); if (i < iPrimaryKey.length - 1) { a.append(" AND "); } } } return a.toString(); } /** * Method declaration * * @param pos * @return * @throws SQLException */ Row getRow(int pos) throws SQLException { if (isCached) { return (cCache.getRow(pos, this)); } return null; } void putRow(CachedRow r) throws SQLException { int size = 0; if (cCache != null) { cCache.add(r); } } void removeRow(CachedRow r) throws SQLException { if (cCache != null) { cCache.free(r); } } void cleanUp() throws SQLException { if (cCache != null) { cCache.cleanUp(); } } void indexRow(Row r, boolean inserted) throws SQLException { if (inserted) { int i = 0; try { Node n = null; for (; i < iIndexCount; i++) { n = r.getNextNode(n); getIndex(i).insert(n); } } catch (SQLException e) { // rollback insert for (--i; i >= 0; i--) { getIndex(i).delete(r.getData(), i == 0); } throw e; // and throw error again } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -