use of org.apache.derby.iapi.sql.dictionary.TableDescriptor in project derby by apache.
the class SYSTABLESRowFactory method makeRow.
// ///////////////////////////////////////////////////////////////////////////
//
// METHODS
//
// ///////////////////////////////////////////////////////////////////////////
/**
* Make a SYSTABLES row
*
* @return Row suitable for inserting into SYSTABLES.
*
* @exception StandardException thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
UUID oid;
String tabSType = null;
int tabIType;
ExecRow row;
String lockGranularity = null;
String tableID = null;
String schemaID = null;
String tableName = null;
if (td != null) {
/*
** We only allocate a new UUID if the descriptor doesn't already have one.
** For descriptors replicated from a Source system, we already have an UUID.
*/
TableDescriptor descriptor = (TableDescriptor) td;
SchemaDescriptor schema = (SchemaDescriptor) parent;
oid = descriptor.getUUID();
if (oid == null) {
oid = getUUIDFactory().createUUID();
descriptor.setUUID(oid);
}
tableID = oid.toString();
if (SanityManager.DEBUG) {
SanityManager.ASSERT(schema != null, "Schema should not be null unless empty row is true");
if (schema.getUUID() == null) {
SanityManager.THROWASSERT("schema " + schema + " has a null OID");
}
}
schemaID = schema.getUUID().toString();
tableName = descriptor.getName();
/* RESOLVE - Table Type should really be a char in the descriptor
* T, S, V, S instead of 0, 1, 2, 3
*/
tabIType = descriptor.getTableType();
switch(tabIType) {
case TableDescriptor.BASE_TABLE_TYPE:
tabSType = "T";
break;
case TableDescriptor.SYSTEM_TABLE_TYPE:
tabSType = "S";
break;
case TableDescriptor.VIEW_TYPE:
tabSType = "V";
break;
case TableDescriptor.SYNONYM_TYPE:
tabSType = "A";
break;
default:
if (SanityManager.DEBUG)
SanityManager.THROWASSERT("invalid table type");
}
char[] lockGChar = new char[1];
lockGChar[0] = descriptor.getLockGranularity();
lockGranularity = new String(lockGChar);
}
/* Insert info into systables */
/* RESOLVE - It would be nice to require less knowledge about systables
* and have this be more table driven.
*/
/* Build the row to insert */
row = getExecutionFactory().getValueRow(SYSTABLES_COLUMN_COUNT);
/* 1st column is TABLEID (UUID - char(36)) */
row.setColumn(SYSTABLES_TABLEID, new SQLChar(tableID));
/* 2nd column is NAME (varchar(30)) */
row.setColumn(SYSTABLES_TABLENAME, new SQLVarchar(tableName));
/* 3rd column is TABLETYPE (char(1)) */
row.setColumn(SYSTABLES_TABLETYPE, new SQLChar(tabSType));
/* 4th column is SCHEMAID (UUID - char(36)) */
row.setColumn(SYSTABLES_SCHEMAID, new SQLChar(schemaID));
/* 5th column is LOCKGRANULARITY (char(1)) */
row.setColumn(SYSTABLES_LOCKGRANULARITY, new SQLChar(lockGranularity));
return row;
}
use of org.apache.derby.iapi.sql.dictionary.TableDescriptor in project derby by apache.
the class DMLModStatementNode method generateFKInfo.
/**
* Generate the FKInfo structures used during code generation.
* For each constraint that isn't a check constraint, add another
* one of these FKInfo structures and then package them up into
* a single array.
*
* @param cdl The constraint descriptor list
* @param dd The DataDictionary
* @param td The TableDescriptor
* @param readColsBitSet columns read
*
* @exception StandardException Thrown on failure
*/
private void generateFKInfo(ConstraintDescriptorList cdl, DataDictionary dd, TableDescriptor td, FormatableBitSet readColsBitSet) throws StandardException {
ArrayList<FKInfo> fkList = new ArrayList<FKInfo>();
int type;
UUID[] uuids;
long[] conglomNumbers;
String[] fkNames;
ConstraintDescriptorList fkcdl;
ReferencedKeyConstraintDescriptor refcd;
boolean[] isSelfReferencingFK;
ConstraintDescriptorList activeList = dd.getActiveConstraintDescriptors(cdl);
int[] rowMap = getRowMap(readColsBitSet, td);
int[] raRules;
boolean[] deferrable;
UUID[] fkIds;
ArrayList<String> refSchemaNames = new ArrayList<String>(1);
ArrayList<String> refTableNames = new ArrayList<String>(1);
ArrayList<Long> refIndexConglomNum = new ArrayList<Long>(1);
ArrayList<Integer> refActions = new ArrayList<Integer>(1);
ArrayList<ColumnDescriptorList> refColDescriptors = new ArrayList<ColumnDescriptorList>(1);
ArrayList<int[]> fkColMap = new ArrayList<int[]>(1);
int activeSize = activeList.size();
for (int index = 0; index < activeSize; index++) {
ConstraintDescriptor cd = activeList.elementAt(index);
if (cd instanceof ForeignKeyConstraintDescriptor) {
/*
** We are saving information for checking the
** primary/unique key that is referenced by this
** foreign key, so type is FOREIGN KEY.
*/
type = FKInfo.FOREIGN_KEY;
refcd = ((ForeignKeyConstraintDescriptor) cd).getReferencedConstraint();
uuids = new UUID[1];
deferrable = new boolean[1];
fkIds = new UUID[1];
conglomNumbers = new long[1];
fkNames = new String[1];
isSelfReferencingFK = new boolean[1];
raRules = new int[1];
fkSetupArrays(dd, (ForeignKeyConstraintDescriptor) cd, 0, uuids, conglomNumbers, fkNames, isSelfReferencingFK, raRules, deferrable, fkIds);
// oops, get the right constraint name -- for error
// handling we want the FK name, not refcd name
fkNames[0] = cd.getConstraintName();
} else if (cd instanceof ReferencedKeyConstraintDescriptor) {
refcd = (ReferencedKeyConstraintDescriptor) cd;
/*
** We are saving information for checking the
** foreign key(s) that is dependent on this referenced
** key, so type is REFERENCED KEY.
*/
type = FKInfo.REFERENCED_KEY;
fkcdl = dd.getActiveConstraintDescriptors(((ReferencedKeyConstraintDescriptor) cd).getForeignKeyConstraints(ConstraintDescriptor.ENABLED));
int size = fkcdl.size();
if (size == 0) {
continue;
}
uuids = new UUID[size];
deferrable = new boolean[size];
fkIds = new UUID[size];
fkNames = new String[size];
conglomNumbers = new long[size];
isSelfReferencingFK = new boolean[size];
raRules = new int[size];
TableDescriptor fktd;
ColumnDescriptorList coldl;
int[] refColumns;
ColumnDescriptor cold;
int[] colArray = remapReferencedColumns(cd, rowMap);
for (int inner = 0; inner < size; inner++) {
ForeignKeyConstraintDescriptor fkcd = (ForeignKeyConstraintDescriptor) fkcdl.elementAt(inner);
fkSetupArrays(dd, fkcd, inner, uuids, conglomNumbers, fkNames, isSelfReferencingFK, raRules, deferrable, fkIds);
if ((raRules[inner] == StatementType.RA_CASCADE) || (raRules[inner] == StatementType.RA_SETNULL)) {
// find the referencing table Name
fktd = fkcd.getTableDescriptor();
refSchemaNames.add(fktd.getSchemaName());
refTableNames.add(fktd.getName());
refActions.add(Integer.valueOf(raRules[inner]));
// find the referencing column name required for update null.
refColumns = fkcd.getReferencedColumns();
coldl = fktd.getColumnDescriptorList();
ColumnDescriptorList releventColDes = new ColumnDescriptorList();
for (int i = 0; i < refColumns.length; i++) {
cold = coldl.elementAt(refColumns[i] - 1);
releventColDes.add(cold);
}
refColDescriptors.add(releventColDes);
refIndexConglomNum.add(Long.valueOf(conglomNumbers[inner]));
fkColMap.add(colArray);
}
}
} else {
continue;
}
final TableDescriptor pktd = refcd.getTableDescriptor();
final UUID pkIndexId = refcd.getIndexId();
final ConglomerateDescriptor pkIndexConglom = pktd.getConglomerateDescriptor(pkIndexId);
final TableDescriptor refTd = cd.getTableDescriptor();
fkList.add(new FKInfo(// foreign key names
fkNames, cd.getSchemaDescriptor().getSchemaName(), // table being modified
refTd.getName(), // INSERT|UPDATE|DELETE
statementType, // FOREIGN_KEY|REFERENCED_KEY
type, // referenced backing index uuid
pkIndexId, pkIndexConglom.getConglomerateNumber(), // referenced backing index conglom
refcd.getUUID(), // referenced constraint is
refcd.deferrable(), // fk backing index uuids
uuids, // fk backing index congloms
conglomNumbers, // is self ref array of bool
isSelfReferencingFK, remapReferencedColumns(cd, rowMap), // columns referenced by key
dd.getRowLocationTemplate(getLanguageConnectionContext(), refTd), // referential action rules
raRules, // deferrable flags
deferrable, // UUID of fks
fkIds));
}
// Now convert the list into an array.
if (!fkList.isEmpty()) {
fkInfo = fkList.toArray(new FKInfo[fkList.size()]);
}
// Convert the ref action info lists to arrays.
int size = refActions.size();
if (size > 0) {
fkTableNames = new String[size];
fkSchemaNames = new String[size];
fkRefActions = new int[size];
fkColDescriptors = new ColumnDescriptorList[size];
fkIndexConglomNumbers = new long[size];
fkColArrays = new int[size][];
for (int i = 0; i < size; i++) {
fkTableNames[i] = refTableNames.get(i);
fkSchemaNames[i] = refSchemaNames.get(i);
fkRefActions[i] = (refActions.get(i)).intValue();
fkColDescriptors[i] = refColDescriptors.get(i);
fkIndexConglomNumbers[i] = (refIndexConglomNum.get(i)).longValue();
fkColArrays[i] = (fkColMap.get(i));
}
}
}
use of org.apache.derby.iapi.sql.dictionary.TableDescriptor in project derby by apache.
the class FromVTI method bindVTITables.
// end of getVTIName
/**
* Bind this VTI that appears in the FROM list.
*
* @param fromListParam FromList to use/append to.
*
* @return ResultSetNode The bound FromVTI.
*
* @exception StandardException Thrown on error
*/
@Override
ResultSetNode bindVTITables(FromList fromListParam) throws StandardException {
ResultColumnList derivedRCL = getResultColumns();
LanguageConnectionContext lcc = getLanguageConnectionContext();
/* NOTE - setting of table number moved to FromList.bindTables()
* in order to avoid an ordering problem with join columns in
* parameters.
*/
/* Bind the constructor or static method - does basic error checking.
* Correlated subqueries are not allowed as parameters to
* a VTI, so pass an empty FromList.
*/
ArrayList<AggregateNode> aggregates = new ArrayList<AggregateNode>();
methodCall.bindExpression(fromListParam, subqueryList, aggregates);
// Is the parameter list to the constructor valid for a VTI?
methodParms = methodCall.getMethodParms();
RoutineAliasInfo routineInfo = methodCall.getRoutineInfo();
if ((routineInfo != null) && routineInfo.getReturnType().isRowMultiSet() && (routineInfo.getParameterStyle() == RoutineAliasInfo.PS_DERBY_JDBC_RESULT_SET)) {
isDerbyStyleTableFunction = true;
}
if (isDerbyStyleTableFunction) {
Method boundMethod = (Method) methodCall.getResolvedMethod();
isRestrictedTableFunction = RestrictedVTI.class.isAssignableFrom(boundMethod.getReturnType());
}
if (isConstructor()) {
NewInvocationNode constructor = (NewInvocationNode) methodCall;
if (!constructor.assignableTo("java.sql.PreparedStatement")) {
if (version2) {
throw StandardException.newException(SQLState.LANG_DOES_NOT_IMPLEMENT, getVTIName(), "java.sql.PreparedStatement");
} else if (!constructor.assignableTo("java.sql.ResultSet")) {
throw StandardException.newException(SQLState.LANG_DOES_NOT_IMPLEMENT, getVTIName(), "java.sql.ResultSet");
}
} else {
version2 = true;
}
/* If this is a version 2 VTI */
if (version2) {
// Does it support predicates
implementsPushable = constructor.assignableTo("org.apache.derby.vti.IQualifyable");
}
// Remember whether or not the VTI implements the VTICosting interface
implementsVTICosting = constructor.assignableTo(ClassName.VTICosting);
}
if (isDerbyStyleTableFunction) {
implementsVTICosting = implementsDerbyStyleVTICosting(methodCall.getJavaClassName());
}
/* Build the RCL for this VTI. We instantiate an object in order
* to get the ResultSetMetaData.
*
* If we have a special trigger vti, then we branch off and get
* its rcl from the trigger table that is waiting for us in
* the compiler context.
*/
UUID triggerTableId;
if ((isConstructor()) && ((triggerTableId = getSpecialTriggerVTITableName(lcc, methodCall.getJavaClassName())) != null)) {
TableDescriptor td = getDataDictionary().getTableDescriptor(triggerTableId);
setResultColumns(genResultColList(td));
// costing info
vtiCosted = true;
estimatedCost = 50d;
estimatedRowCount = 5d;
supportsMultipleInstantiations = true;
} else {
setResultColumns(new ResultColumnList((getContextManager())));
if (isDerbyStyleTableFunction) {
createResultColumnsForTableFunction(routineInfo.getReturnType());
} else {
ResultSetMetaData rsmd = getResultSetMetaData();
/* Wouldn't it be nice if we knew that the class/object would never
* return a null ResultSetMetaData.
*/
if (rsmd == null) {
throw StandardException.newException(SQLState.LANG_NULL_RESULT_SET_META_DATA, getVTIName());
}
// Remember how many columns VTI returns for partial row calculation
try {
numVTICols = rsmd.getColumnCount();
} catch (SQLException sqle) {
numVTICols = 0;
}
getResultColumns().createListFromResultSetMetaData(rsmd, exposedName, getVTIName());
}
}
numVTICols = getResultColumns().size();
/* Propagate the name info from the derived column list */
if (derivedRCL != null) {
getResultColumns().propagateDCLInfo(derivedRCL, correlationName);
}
return this;
}
use of org.apache.derby.iapi.sql.dictionary.TableDescriptor in project derby by apache.
the class FKConstraintDefinitionNode method bind.
/**
* Bind this constraint definition. Figure out some
* information about the table we are binding against.
*
* @param dd DataDictionary
*
* @exception StandardException on error
*/
@Override
void bind(DDLStatementNode ddlNode, DataDictionary dd) throws StandardException {
super.bind(ddlNode, dd);
refTableSd = getSchemaDescriptor(refTableName.getSchemaName());
if (refTableSd.isSystemSchema()) {
throw StandardException.newException(SQLState.LANG_NO_FK_ON_SYSTEM_SCHEMA);
}
// check the referenced table, unless this is a self-referencing constraint
if (refTableName.equals(ddlNode.getObjectName()))
return;
// error when the referenced table does not exist
TableDescriptor td = getTableDescriptor(refTableName.getTableName(), refTableSd);
if (td == null)
throw StandardException.newException(SQLState.LANG_INVALID_FK_NO_REF_TAB, getConstraintMoniker(), refTableName.getTableName());
// Verify if REFERENCES_PRIV is granted to columns referenced
getCompilerContext().pushCurrentPrivType(getPrivType());
// Indicate that this statement has a dependency on the
// table which is referenced by this foreign key:
getCompilerContext().createDependency(td);
// If references clause doesn't have columnlist, get primary key info
if (refRcl.size() == 0 && (td.getPrimaryKey() != null)) {
// Get the primary key columns
int[] refCols = td.getPrimaryKey().getReferencedColumns();
for (int i = 0; i < refCols.length; i++) {
ColumnDescriptor cd = td.getColumnDescriptor(refCols[i]);
// Set tableDescriptor for this column descriptor. Needed for adding required table
// access permission. Column descriptors may not have this set already.
cd.setTableDescriptor(td);
if (isPrivilegeCollectionRequired())
getCompilerContext().addRequiredColumnPriv(cd);
}
} else {
for (ResultColumn rc : refRcl) {
ColumnDescriptor cd = td.getColumnDescriptor(rc.getName());
if (cd != null) {
// Set tableDescriptor for this column descriptor. Needed for adding required table
// access permission. Column descriptors may not have this set already.
cd.setTableDescriptor(td);
if (isPrivilegeCollectionRequired())
getCompilerContext().addRequiredColumnPriv(cd);
}
}
}
getCompilerContext().popCurrentPrivType();
}
use of org.apache.derby.iapi.sql.dictionary.TableDescriptor in project derby by apache.
the class DefaultNode method bindExpression.
/**
* Bind this expression. This means binding the sub-expressions,
* as well as figuring out what the return type is for this expression.
* In this case, there are no sub-expressions, and the return type
* is already known, so this is just a stub.
*
* @param fromList The FROM list for the query this
* expression is in, for binding columns.
* @param subqueryList The subquery list being built as we find SubqueryNodes
* @param aggregates The aggregate list being built as we find AggregateNodes
*
* @return The new top of the expression tree.
*
* @exception StandardException Thrown on failure
*/
@Override
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
ColumnDescriptor cd;
TableDescriptor td;
if (SanityManager.DEBUG) {
SanityManager.ASSERT(fromList.size() != 0, "fromList expected to be non-empty");
if (!(fromList.elementAt(0) instanceof FromBaseTable)) {
SanityManager.THROWASSERT("fromList.elementAt(0) expected to be instanceof FromBaseTable, not " + fromList.elementAt(0).getClass().getName());
}
}
// Get the TableDescriptor for the target table
td = ((FromBaseTable) fromList.elementAt(0)).getTableDescriptor();
// Get the ColumnDescriptor for the column
cd = td.getColumnDescriptor(columnName);
if (SanityManager.DEBUG) {
SanityManager.ASSERT(cd != null, "cd expected to be non-null");
}
/* If we have the default text, then parse and bind it and
* return the tree.
*/
DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
if (defaultInfo != null) {
String defaultTxt = defaultInfo.getDefaultText();
ValueNode defaultTre = parseDefault(defaultTxt, getLanguageConnectionContext(), getCompilerContext());
/* Query is dependent on the DefaultDescriptor */
DefaultDescriptor defaultDescriptor = cd.getDefaultDescriptor(getDataDictionary());
getCompilerContext().createDependency(defaultDescriptor);
return defaultTre.bindExpression(fromList, subqueryList, aggregates);
} else {
// Default is null
return new UntypedNullConstantNode(getContextManager());
}
}
Aggregations