public boolean setDistinctColumnsOnIndex(int[] colMap) { if (joinConditions.length != 1) { return false; } int[] indexColMap = joinConditions[0].rangeIndex.getColumns(); if (colMap.length > indexColMap.length) { return false; } if (colMap.length == indexColMap.length) { if (ArrayUtil.haveEqualSets(colMap, indexColMap, colMap.length)) { indexDistinctCount = colMap.length; return true; } } if (ArrayUtil.haveEqualArrays(colMap, indexColMap, colMap.length)) { indexDistinctCount = colMap.length; return true; } return false; }
/** * Used to update constrains to reflect structural changes in a table. Prior checks must ensure * that this method does not throw. * * @param session Session * @param oldTable reference to the old version of the table * @param newTable referenct to the new version of the table * @param colIndex index at which table column is added or removed * @param adjust -1, 0, +1 to indicate if column is added or removed @ */ void updateTable(Session session, Table oldTable, Table newTable, int colIndex, int adjust) { if (oldTable == core.mainTable) { core.mainTable = newTable; if (core.mainIndex != null) { core.mainIndex = core.mainTable.getIndex(core.mainIndex.getName().name); core.mainCols = ArrayUtil.toAdjustedColumnArray(core.mainCols, colIndex, adjust); } } if (oldTable == core.refTable) { core.refTable = newTable; if (core.refIndex != null) { core.refIndex = core.refTable.getIndex(core.refIndex.getName().name); core.refCols = ArrayUtil.toAdjustedColumnArray(core.refCols, colIndex, adjust); } } // CHECK if (constType == CHECK) { recompile(session, newTable); } }
public ResultMetaData getNewMetaData(int[] columnMap) { ResultMetaData newMeta = newResultMetaData(columnMap.length); ArrayUtil.projectRow(columnLabels, columnMap, newMeta.columnLabels); ArrayUtil.projectRow(columnTypes, columnMap, newMeta.columnTypes); ArrayUtil.projectRow(columns, columnMap, newMeta.columns); return newMeta; }
/** * Compares this with another constraint column set. This implementation only checks FOREIGN KEY * constraints. */ boolean isEquivalent(Table mainTable, int[] mainCols, Table refTable, int[] refCols) { if (constType != Constraint.MAIN && constType != Constraint.FOREIGN_KEY) { return false; } if (mainTable != core.mainTable || refTable != core.refTable) { return false; } return ArrayUtil.areEqualSets(core.mainCols, mainCols) && ArrayUtil.areEqualSets(core.refCols, refCols); }
boolean hasColumn(int paramInt) { switch (this.constType) { case 3: return this.rangeVariable.usedColumns[paramInt]; case 1: case 2: case 4: return ArrayUtil.find(this.core.mainCols, paramInt) != -1; case 0: return ArrayUtil.find(this.core.refCols, paramInt) != -1; } throw Error.runtimeError(201, "Constraint"); }
boolean isEquivalent( Table paramTable1, int[] paramArrayOfInt1, Table paramTable2, int[] paramArrayOfInt2) { switch (this.constType) { case 0: case 1: if ((paramTable1 != this.core.mainTable) || (paramTable2 != this.core.refTable)) return false; if ((this.core.mainCols.length == paramArrayOfInt1.length) && (this.core.refCols.length == paramArrayOfInt2.length)) return (ArrayUtil.areEqualSets(this.core.mainCols, paramArrayOfInt1)) && (ArrayUtil.areEqualSets(this.core.refCols, paramArrayOfInt2)); break; } return false; }
String getStatement(int startPosition, short[] startTokens) { int semiPosition = 0; while (true) { if (token.tokenType == Tokens.SEMICOLON) { semiPosition = scanner.getPosition(); } else if (token.tokenType == Tokens.X_ENDPARSE) { if (semiPosition == 0) { break; } else { rewind(semiPosition); break; } } else { semiPosition = 0; if (ArrayUtil.find(startTokens, token.tokenType) != -1) { break; } } read(); } String sql = scanner.getPart(startPosition, scanner.getPosition()); return sql; }
/** * Constructor declaration * * @param name HsqlName of the index * @param table table of the index * @param column array of column indexes * @param type array of column types * @param unique is this a unique index * @param constraint does this index belonging to a constraint * @param forward is this an auto-index for an FK that refers to a table defined after this table * @param visColumns count of visible columns */ Index( Database database, HsqlName name, Table table, int[] column, int[] type, boolean isPk, boolean unique, boolean constraint, boolean forward, int[] pkcols, int[] pktypes, boolean temp) { indexName = name; colIndex = column; colType = type; pkCols = pkcols; pkTypes = pktypes; isUnique = unique; isConstraint = constraint; isForward = forward; useRowId = (!isUnique && pkCols.length == 0) || (colIndex.length == 0); colCheck = table.getNewColumnCheckList(); ArrayUtil.intIndexesToBooleanArray(colIndex, colCheck); updatableIterators = Index.emptyIterator; updatableIterators.next = updatableIterators.last = updatableIterators; collation = database.collation; isTemp = temp; onCommitPreserve = table.onCommitPreserve; }
boolean hasColumnOnly(int colIndex) { switch (constType) { case CHECK: return rangeVariable.usedColumns[colIndex] && ArrayUtil.countTrueElements(rangeVariable.usedColumns) == 1; case PRIMARY_KEY: case UNIQUE: return core.mainCols.length == 1 && core.mainCols[0] == colIndex; case MAIN: return core.mainCols.length == 1 && core.mainCols[0] == colIndex && core.mainTable == core.refTable; case FOREIGN_KEY: return core.refCols.length == 1 && core.refCols[0] == colIndex && core.mainTable == core.refTable; default: throw Error.runtimeError(ErrorCode.U_S0500, "Constraint"); } }
RowIterator findFkRef(Session paramSession, Object[] paramArrayOfObject) { if ((paramArrayOfObject == null) || (ArrayUtil.hasNull(paramArrayOfObject, this.core.mainCols))) return this.core.refIndex.emptyIterator(); PersistentStore localPersistentStore = this.core.refTable.getRowStore(paramSession); return this.core.refIndex.findFirstRow( paramSession, localPersistentStore, paramArrayOfObject, this.core.mainCols); }
/** * Chooses certain query conditions and assigns a copy of them to this filter. The original * condition is set to Expression.TRUE once assigned. * * @param condition * @throws HsqlException */ void setConditions(Expression condition) throws HsqlException { setCondition(condition); if (filterIndex == null) { filterIndex = filterTable.getPrimaryIndex(); } if (filterIndex.getVisibleColumns() == 1 || eStart == null || eAnd == null || eStart.exprType != Expression.EQUAL) { return; } boolean[] check = filterTable.getNewColumnCheckList(); Expression[] expr = new Expression[check.length]; int colindex = eStart.getArg().getColumnNr(); check[colindex] = true; expr[colindex] = eStart.getArg2(); eAnd.getEquiJoinColumns(this, check, expr); if (ArrayUtil.containsAllTrueElements(check, filterIndex.colCheck)) { isMultiFindFirst = true; findFirstExpressions = expr; } }
public Object[] getSingleRowData() { Object[] data = (Object[]) initialiseNavigator().getNext(); data = (Object[]) ArrayUtil.resizeArrayIfDifferent(data, metaData.getColumnCount()); return data; }
public void release() { destroy(); setTimestamp(0); ArrayUtil.fillArray(accessorList, null); elementCount = 0; }
public void removeAll() { destroy(); elementCount = 0; ArrayUtil.fillArray(accessorList, null); }
/** Compares this with another constraint column set. This is used only for UNIQUE constraints. */ boolean isUniqueWithColumns(int[] cols) { if (constType != UNIQUE || core.mainCols.length != cols.length) { return false; } return ArrayUtil.haveEqualSets(core.mainCols, cols, cols.length); }
boolean hasColumn(int colIndex) { switch (constType) { case CHECK: return rangeVariable.usedColumns[colIndex]; case PRIMARY_KEY: case UNIQUE: case MAIN: return ArrayUtil.find(core.mainCols, colIndex) != -1; case FOREIGN_KEY: return ArrayUtil.find(core.refCols, colIndex) != -1; default: throw Error.runtimeError(ErrorCode.U_S0500, "Constraint"); } }
boolean hasColumnPlus(int paramInt) { switch (this.constType) { case 3: return (this.rangeVariable.usedColumns[paramInt] != 0) && (ArrayUtil.countTrueElements(this.rangeVariable.usedColumns) > 1); case 2: case 4: return (this.core.mainCols.length != 1) && (ArrayUtil.find(this.core.mainCols, paramInt) != -1); case 1: return (ArrayUtil.find(this.core.mainCols, paramInt) != -1) && ((this.core.mainCols.length != 1) || (this.core.mainTable != this.core.refTable)); case 0: return (ArrayUtil.find(this.core.refCols, paramInt) != -1) && ((this.core.mainCols.length != 1) || (this.core.mainTable != this.core.refTable)); } throw Error.runtimeError(201, "Constraint"); }
/** * Checks for foreign key or check constraint violation when inserting a row into the child table. */ void checkInsert(Session session, Table table, Object[] row) { switch (constType) { case CHECK: if (!isNotNull) { checkCheckConstraint(session, table, row); } return; case FOREIGN_KEY: PersistentStore store = session.sessionData.getRowStore(core.mainTable); if (ArrayUtil.hasNull(row, core.refCols)) { if (core.matchType == OpTypes.MATCH_SIMPLE) { return; } if (core.refCols.length == 1) { return; } if (ArrayUtil.hasAllNull(row, core.refCols)) { return; } // core.matchType == OpTypes.MATCH_FULL } else if (core.mainIndex.exists(session, store, row, core.refCols)) { return; } else if (core.mainTable == core.refTable) { // special case: self referencing table and self referencing row int compare = core.mainIndex.compareRowNonUnique(row, core.refCols, row); if (compare == 0) { return; } } String[] info = new String[] {core.refName.name, core.mainTable.getName().name}; throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info); } }
boolean[] getInsertOrUpdateColumnCheckList() { switch (type) { case StatementTypes.INSERT: return insertCheckColumns; case StatementTypes.UPDATE_WHERE: return updateCheckColumns; case StatementTypes.MERGE: boolean[] check = (boolean[]) ArrayUtil.duplicateArray(insertCheckColumns); ArrayUtil.orBooleanArray(updateCheckColumns, check); return check; } return null; }
public void setRangeIterator(RangeIterator iterator) { int position = iterator.getRangePosition(); if (position >= rangeIterators.length) { rangeIterators = (RangeIterator[]) ArrayUtil.resizeArray(rangeIterators, position + 1); } rangeIterators[iterator.getRangePosition()] = iterator; }
void checkInsert( Session paramSession, Table paramTable, Object[] paramArrayOfObject, boolean paramBoolean) { switch (this.constType) { case 3: if (!this.isNotNull) checkCheckConstraint(paramSession, paramTable, paramArrayOfObject); return; case 0: PersistentStore localPersistentStore = this.core.mainTable.getRowStore(paramSession); if (ArrayUtil.hasNull(paramArrayOfObject, this.core.refCols)) { if (this.core.matchType == 59) return; if (this.core.refCols.length == 1) return; if (!ArrayUtil.hasAllNull(paramArrayOfObject, this.core.refCols)) ; } else if (this.core.mainIndex.existsParent( paramSession, localPersistentStore, paramArrayOfObject, this.core.refCols)) { return; } throw getException(paramArrayOfObject); } }
/** * New method to find any referencing row for a foreign key (finds row in child table). If ON * DELETE CASCADE is specified for this constraint, then the method finds the first row among the * rows of the table ordered by the index and doesn't throw. Without ON DELETE CASCADE, the method * attempts to finds any row that exists. If no row is found, null is returned. (fredt@users) * * @param session Session * @param row array of objects for a database row * @param delete should we allow 'ON DELETE CASCADE' or 'ON UPDATE CASCADE' * @return iterator @ */ RowIterator findFkRef(Session session, Object[] row, boolean delete) { if (row == null || ArrayUtil.hasNull(row, core.mainCols)) { return core.refIndex.emptyIterator(); } PersistentStore store = session.sessionData.getRowStore(core.refTable); return core.refIndex.findFirstRow(session, store, row, core.mainCols); }
public void removeSpecificRoutine(Routine routine) { for (int i = 0; i < this.routines.length; i++) { if (routines[i] == routine) { routines = (Routine[]) ArrayUtil.toAdjustedArray(routines, null, i, -1); break; } } }
boolean isUniqueWithColumns(int[] paramArrayOfInt) { switch (this.constType) { case 2: case 4: if (this.core.mainCols.length == paramArrayOfInt.length) return ArrayUtil.haveEqualSets( this.core.mainCols, paramArrayOfInt, paramArrayOfInt.length); break; } return false; }
/** * Check used before creating a new foreign key cosntraint, this method checks all rows of a table * to ensure they all have a corresponding row in the main table. */ void checkReferencedRows(Session session, Table table, int[] rowColArray) { Index mainIndex = getMainIndex(); PersistentStore store = session.sessionData.getRowStore(table); RowIterator it = table.rowIterator(session); while (true) { Row row = it.getNextRow(); if (row == null) { break; } Object[] rowData = row.getData(); if (ArrayUtil.hasNull(rowData, rowColArray)) { if (core.matchType == OpTypes.MATCH_SIMPLE) { continue; } } else if (mainIndex.exists(session, store, rowData, rowColArray)) { continue; } if (ArrayUtil.hasAllNull(rowData, rowColArray)) { continue; } String colValues = ""; for (int i = 0; i < rowColArray.length; i++) { Object o = rowData[rowColArray[i]]; colValues += table.getColumnTypes()[i].convertToString(o); colValues += ","; } String[] info = new String[] {getName().name, getMain().getName().name}; throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info); } }
static void setTableIndexesForSubquery( Table paramTable, boolean paramBoolean1, boolean paramBoolean2) { int[] arrayOfInt = null; if (paramBoolean1) { arrayOfInt = new int[paramTable.getColumnCount()]; ArrayUtil.fillSequence(arrayOfInt); } paramTable.createPrimaryKey(null, paramBoolean2 ? arrayOfInt : null, false); if (paramBoolean2) paramTable.fullIndex = paramTable.getPrimaryIndex(); else if (paramBoolean1) paramTable.fullIndex = paramTable.createIndexForColumns(null, arrayOfInt); }
public static ResultMetaData newUpdateResultMetaData(Type[] types) { ResultMetaData md = new ResultMetaData(UPDATE_RESULT_METADATA); md.columnTypes = new Type[types.length]; md.columnCount = types.length; md.extendedColumnCount = types.length; ArrayUtil.copyArray(types, md.columnTypes, types.length); return md; }
void updateTable( Session paramSession, Table paramTable1, Table paramTable2, int paramInt1, int paramInt2) { if (paramTable1 == this.core.mainTable) { this.core.mainTable = paramTable2; if (this.core.mainIndex != null) { this.core.mainIndex = this.core.mainTable.getIndex(this.core.mainIndex.getName().name); this.core.mainCols = ArrayUtil.toAdjustedColumnArray(this.core.mainCols, paramInt1, paramInt2); this.core.mainIndex.setTable(paramTable2); } } if (paramTable1 == this.core.refTable) { this.core.refTable = paramTable2; if (this.core.refIndex != null) { this.core.refIndex = this.core.refTable.getIndex(this.core.refIndex.getName().name); this.core.refCols = ArrayUtil.toAdjustedColumnArray(this.core.refCols, paramInt1, paramInt2); this.core.refIndex.setTable(paramTable2); } } if (this.constType == 3) recompile(paramSession, paramTable2); }
boolean hasColumnPlus(int colIndex) { switch (constType) { case CHECK: return rangeVariable.usedColumns[colIndex] && ArrayUtil.countTrueElements(rangeVariable.usedColumns) > 1; case PRIMARY_KEY: case UNIQUE: return core.mainCols.length != 1 && ArrayUtil.find(core.mainCols, colIndex) != -1; case MAIN: return ArrayUtil.find(core.mainCols, colIndex) != -1 && (core.mainCols.length != 1 || core.mainTable != core.refTable); case FOREIGN_KEY: return ArrayUtil.find(core.refCols, colIndex) != -1 && (core.mainCols.length != 1 || core.mainTable == core.refTable); default: throw Error.runtimeError(ErrorCode.U_S0500, "Constraint"); } }
Result executeSetStatement(Session session) { Table table = targetTable; int[] colMap = updateColumnMap; // column map Expression[] colExpressions = updateExpressions; Type[] colTypes = table.getColumnTypes(); int index = targetRangeVariables[TriggerDef.NEW_ROW].rangePosition; Object[] oldData = session.sessionContext.rangeIterators[index].getCurrent(); Object[] data = StatementDML.getUpdatedData(session, table, colMap, colExpressions, colTypes, oldData); ArrayUtil.copyArray(data, oldData, data.length); return Result.updateOneResult; }