Class Schema
- All Implemented Interfaces:
Serializable,Iterable<ColumnInfo>
A schema is an ordered collection of column names and associated types.
Many different entities in the database code can have schema associated with them. Both tables and tuples have schemas, for obvious reasons. SELECT and FROM clauses also have schemas, used by the database engine to verify the semantics of database queries. Finally, relational algebra plan nodes also have schemas, which specify the kinds of tuples that they generate.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classThis helper class is used for the internal hashed column structure, so that we can do fast lookups based on table names and column names. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ArrayList<KeyColumnRefs> A collection of all candidate-key objects specifying the sets of columns that comprise candidate keys on this table.private HashMap<String, ArrayList<Schema.IndexedColumnInfo>> A mapping that provides fast lookups for columns based only on column name.private HashMap<String, HashMap<String, Schema.IndexedColumnInfo>> A mapping that provides fast lookups for columns based on table name and column name.private ArrayList<ColumnInfo> The collection of the column-info objects describing the columns in the schema.private ArrayList<ForeignKeyColumnRefs> A collection of foreign-key objects specifying other tables that this table references.private HashMap<String, IndexColumnRefs> This collection provides easy access to all the indexes defined on this table, including those for candidate keys and the primary key.A set recording the indexes of the columns have NOT NULL constraints on them.A set of the tables that reference this table. -
Constructor Summary
ConstructorsConstructorDescriptionSchema()Construct an empty schema.Schema(ColumnInfo... colInfos) Construct a schema containing the specified array of column-info objects.Create a schema that is the concatenation of one or more other schemas.Schema(Iterable<ColumnInfo> colInfos) Construct a schema whose columns are taken from the specified iterable. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds another candidate key to the schema.intaddColumnInfo(ColumnInfo colInfo) Add a column to the schema.voidvoidaddIndex(IndexColumnRefs index) booleanaddNotNull(int colIndex) Adds a column with given index to list of NOT NULL constrained columns.booleanaddReferencingTable(String referencingTableName) Adds the specified table to the set of tables that reference this table.voidAppend another schema to this schema.findColumns(ColumnName colName) Given a (possibly wildcard) column-name, this method returns the collection of all columns that match the specified column name.getAllKeysOnColumns(int[] colIndexes) Returns all candidate-keys from this schema that have the specified set of columns.getAllKeysOnColumns(ColumnRefs colRefs) Returns all candidate-keys from this schema that have the specified set of columns.Returns an unmodifiable list of candidate keys present on the schema.intgetColumnIndex(ColumnName colName) Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name.intgetColumnIndex(ColumnInfo colInfo) Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name.intgetColumnIndex(String colName) Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name.intgetColumnIndex(String tblName, String colName) Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name.int[]getColumnIndexes(List<String> columnNames) Given a list of column names, this method returns an array containing the indexes of the specified columns.getColumnInfo(int i) Returns the ColumnInfo object describing the column at the specified index.getColumnInfo(String colName) Presuming that exactly one column has the specified name, this method returns the column information for that column name.Returns an unmodifiable list of all the columns in the schemagetColumnInfos(int[] colIndexes) Constructs and returns a list ofColumnInfoobjects for the columns at the specified indexes.Returns a set containing all column names that appear in this schema.Returns the names of columns that are common between this schema and the specified schema.This helper method returns the names of all tables that appear in both this schema and the specified schema.getKeyOnColumns(int[] colIndexes) Returns any candidate-key from this schema that has the specified set of columns.getKeyOnColumns(ColumnRefs colRefs) Returns any candidate-key from this schema that has the specified set of columns.Returns a set of columns that have a NOT NULL constraint, specified by the indexes of the columns in the table schema.Returns the primary key on this table, ornullif there is no primary key.Returns a set containing all table names that appear in this schema.booleanhasKeyOnColumns(int[] colIndexes) Returnstrueif this schema has a candidate key on the set of columns specified in the argument.booleanhasKeyOnColumns(ColumnRefs colRefs) Returnstrueif this schema has a candidate key on the set of columns specified in the argument.booleanThis helper method returns true if this schema contains any columns with the same column name but different table names.booleanisNullable(int colIndex) iterator()Provides support for iteration over the columns in the schema.intReturns a count of how many candidate keys are present on the schema.intReturns the number of columns in the schema.intnumColumnsWithName(String colName) Returns the number of columns that have the specified column name.intbooleanremoveNotNull(int colIndex) Removes a column with given index from the list of NOT NULL constrained columns.voidremoveReferencingTable(String referencingTableName) voidsetTableName(String tableName) This method iterates through all columns in the schema, setting them to all have the specified table name.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
columnInfos
The collection of the column-info objects describing the columns in the schema. -
colsHashedByTable
A mapping that provides fast lookups for columns based on table name and column name. The outer hash-map has table names for keys; "no table" is indicated with anullkey, whichHashMapsupports. The inner hash-map has column names for keys, and maps to column information objects. -
colsHashedByColumn
A mapping that provides fast lookups for columns based only on column name. Because multiple columns could have the same column name (but different table names) in a single schema, the values in the mapping are lists. -
notNullCols
A set recording the indexes of the columns have NOT NULL constraints on them. -
candidateKeys
A collection of all candidate-key objects specifying the sets of columns that comprise candidate keys on this table. One of these keys may be specified as the primary key. -
foreignKeys
A collection of foreign-key objects specifying other tables that this table references. -
referencingTables
A set of the tables that reference this table. -
indexes
This collection provides easy access to all the indexes defined on this table, including those for candidate keys and the primary key.
-
-
Constructor Details
-
Schema
public Schema()Construct an empty schema. -
Schema
Construct a schema whose columns are taken from the specified iterable. Note that this does not work with arrays of column-info objects.- Throws:
SchemaNameException
-
Schema
Construct a schema containing the specified array of column-info objects.- Throws:
SchemaNameException
-
Schema
Create a schema that is the concatenation of one or more other schemas. Schemas are copied in the order they are given. If a column name appears multiple times in the input, an exception will be generated.
Keys will not be copied by this constructor.
- Parameters:
schemas- one or more schema objects to copy into this schema
-
-
Method Details
-
numColumns
public int numColumns()Returns the number of columns in the schema.- Returns:
- the number of columns in the schema.
-
getColumnInfo
Returns the ColumnInfo object describing the column at the specified index. Column indexes are numbered from 0.- Parameters:
i- the index to retrieve the column-info for- Returns:
- the ColumnInfo object describing the name and type of the column
-
getColumnInfos
Returns an unmodifiable list of all the columns in the schema- Returns:
- an unmodifiable list of all the columns in the schema
-
getColumnInfos
Constructs and returns a list ofColumnInfoobjects for the columns at the specified indexes. This method can be useful withColumnRefsobjects,to retrieve the columns referenced by a key.- Parameters:
colIndexes- an array of zero-based column indexes to retrieve- Returns:
- a list of
ColumnInfoobjects for the specified columns
-
iterator
Provides support for iteration over the columns in the schema.- Specified by:
iteratorin interfaceIterable<ColumnInfo>- Returns:
- an iterator over the columns in this schema.
-
addColumnInfo
Add a column to the schema.- Parameters:
colInfo- the name and type of the column being added to the schema- Returns:
- the zero-based index of the column in the schema
- Throws:
IllegalArgumentException- ifcolInfoisnull
-
append
Append another schema to this schema.- Throws:
SchemaNameException- if any of the input column-info objects overlap the names of columns already in the schema.
-
getTableNames
Returns a set containing all table names that appear in this schema. Note that this may includenullif there are columns with no table name specified! -
getCommonTableNames
This helper method returns the names of all tables that appear in both this schema and the specified schema. Note that not all columns of a given table must be present for the table to be included in the result; there just has to be at least one column from the table in both schemas for it to be included in the result.- Parameters:
s- the other schema to compare this schema to- Returns:
- a set containing the names of all tables that appear in both schemas
-
getColumnNames
Returns a set containing all column names that appear in this schema. Note that a column-name may be used by multiple columns, if it is associated with multiple table names in this schema.- Returns:
- a set containing all column names that appear in this schema.
-
getCommonColumnNames
Returns the names of columns that are common between this schema and the specified schema. This kind of operation is mainly used for resolving NATURAL joins.- Parameters:
s- the schema to compare to this schema- Returns:
- a set of the common column names
-
numColumnsWithName
Returns the number of columns that have the specified column name. Note that multiple columns can have the same column name but different table names.- Parameters:
colName- the column name to return the count for- Returns:
- the number of columns with the specified column name
-
hasMultipleColumnsWithSameName
public boolean hasMultipleColumnsWithSameName()This helper method returns true if this schema contains any columns with the same column name but different table names. If so, the schema is not valid for use on one side of a NATURAL join.- Returns:
- true if the schema has multiple columns with the same column name but different table names, or false otherwise.
-
getColumnInfo
Presuming that exactly one column has the specified name, this method returns the column information for that column name.- Parameters:
colName- the name of the column to retrieve the information for- Returns:
- the column information for the specified column
- Throws:
SchemaNameException- if the specified column name doesn't appear in this schema, or if it appears multiple times
-
setTableName
This method iterates through all columns in the schema, setting them to all have the specified table name. An exception will be thrown if the result would be an invalid schema with duplicate column names; in this case, the schema object will remain unchanged.- Throws:
SchemaNameException- if the schema contains columns with the same column name but different table names. In this case, resetting the table name will produce an invalid schema with ambiguous column names.- Design Note:
- (donnie) At present, this method does this by replacing each
ColumnInfoobject with a new object with updated information. This is becauseColumnInfois immutable.
-
getColumnIndex
Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name. The column-name object is not required to specify a table name; however, if the table name is unspecified and the column name is ambiguous then an exception will be thrown.- Parameters:
colName- column-name object to use for looking up the column in the schema- Returns:
- the zero-based index of the column, or -1 if the schema does not contain a column of the specified name.
- Throws:
IllegalArgumentException- ifcolNameisnullSchemaNameException- ifcolNamedoesn't specify a table name, and multiple columns have the specified column name
-
getColumnIndex
Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name. The column-info object is not required to specify a table name; however, if the table name is unspecified and the column name is ambiguous then an exception will be thrown.- Parameters:
colInfo- column-info object to use for looking up the column in the schema- Returns:
- the zero-based index of the column, or -1 if the schema does not contain a column of the specified name.
- Throws:
IllegalArgumentException- ifcolInfoisnullSchemaNameException- ifcolInfodoesn't specify a table name, and multiple columns have the specified column name
-
getColumnIndex
Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name. The table name is unspecified; if the column name is ambiguous then an exception will be thrown.- Parameters:
colName- the column name to look up- Returns:
- the zero-based index of the column, or -1 if the schema does not contain a column of the specified name.
- Throws:
IllegalArgumentException- ifcolNameisnullSchemaNameException- if multiple columns have the specified column name
-
getColumnIndex
Finds the index of the specified column in this schema, or returns -1 if the schema contains no column of the specified name. The table name may be specified or it may benull; ifnulland the column name is ambiguous then an exception will be thrown.- Parameters:
tblName- the table name, ornullif the table name is not known or unspecifiedcolName- the column name to look up- Returns:
- the zero-based index of the column, or -1 if the schema does not contain a column of the specified name.
- Throws:
IllegalArgumentException- ifcolNameisnullSchemaNameException- iftblNameisnulland multiple columns have the specified column name
-
getColumnIndexes
Given a list of column names, this method returns an array containing the indexes of the specified columns.- Parameters:
columnNames- a list of column names in the schema- Returns:
- an array containing the indexes of the columns specified in the input
- Throws:
SchemaNameException- if a column name is specified multiple times in the input list, or if a column name doesn't appear in the schema
-
findColumns
Given a (possibly wildcard) column-name, this method returns the collection of all columns that match the specified column name. The collection is a mapping from integer indexes (the keys) toColumnInfoobjects from the schema.Any valid column-name object will work, so all of these options are available:
- No table, only a column name - to resolve an unqualified column name, e.g. in an expression or predicate
- A table and column name - to check whether the schema contains such a column
- A wildcard without a table name - to retrieve all columns in the schema
- A wildcard with a table name - to retrieve all columns associated with a particular table name
-
addNotNull
public boolean addNotNull(int colIndex) Adds a column with given index to list of NOT NULL constrained columns.- Parameters:
colIndex- the integer index of the column to NOT NULL constrain.- Returns:
- true if the column previous was NULLable, or false if the column already had a NOT NULL constraint on it before this call.
-
removeNotNull
public boolean removeNotNull(int colIndex) Removes a column with given index from the list of NOT NULL constrained columns.- Parameters:
colIndex- the integer index of the column to remove the NOT NULL constraint from.
-
isNullable
public boolean isNullable(int colIndex) -
getNotNull
Returns a set of columns that have a NOT NULL constraint, specified by the indexes of the columns in the table schema.- Returns:
- set of integers - indexes of columns with NOT NULL constraint
-
getPrimaryKey
Returns the primary key on this table, ornullif there is no primary key.- Returns:
- the primary key on this table, or
nullif there is no primary key.
-
addCandidateKey
Adds another candidate key to the schema.- Parameters:
ck- the candidate key to add to the schema.- Throws:
IllegalArgumentException- ifckisnull, or ifckis a primary key and the schema already contains a primary key.
-
numCandidateKeys
public int numCandidateKeys()Returns a count of how many candidate keys are present on the schema.- Returns:
- a count of how many candidate keys are present on the schema.
-
getCandidateKeys
Returns an unmodifiable list of candidate keys present on the schema.- Returns:
- an unmodifiable list of candidate keys present on the schema.
-
hasKeyOnColumns
public boolean hasKeyOnColumns(int[] colIndexes) Returnstrueif this schema has a candidate key on the set of columns specified in the argument. The columns are specified by an array of zero-based indexes.- Parameters:
colIndexes- the set of columns to check against this table to see if it's a candidate key- Returns:
trueif this table has a candidate key on the specified columns;falseotherwise
-
hasKeyOnColumns
Returnstrueif this schema has a candidate key on the set of columns specified in the argument. The columns are specified by aColumnRefsobject.- Parameters:
colRefs- the set of columns to check against this table to see if it's a candidate key- Returns:
trueif this table has a candidate key on the specified columns;falseotherwise
-
getKeyOnColumns
Returns any candidate-key from this schema that has the specified set of columns. Note that the key's columns may be in a different order than those specified in the argument.- Parameters:
colIndexes- the set of columns to check against this table to see if it's a candidate key- Returns:
- a candidate key on the specified columns, or
nullif the schema contains no key on the specified columns
-
getKeyOnColumns
Returns any candidate-key from this schema that has the specified set of columns. Note that the key's columns may be in a different order than those specified in the argument.- Parameters:
colRefs- the set of columns to check against this table to see if it's a candidate key- Returns:
- a candidate key on the specified columns, or
nullif the schema contains no key on the specified columns
-
getAllKeysOnColumns
Returns all candidate-keys from this schema that have the specified set of columns. Note that keys may specify columns in a different order than those specified in the argument. If there are no keys on the specified columns, this method will return an empty list.- Parameters:
colIndexes- the set of columns to check against this table to see if it's a candidate key- Returns:
- a list of candidate keys on the specified columns
-
getAllKeysOnColumns
Returns all candidate-keys from this schema that have the specified set of columns. Note that keys may specify columns in a different order than those specified in the argument. If there are no keys on the specified columns, this method will return an empty list.- Parameters:
colRefs- the set of columns to check against this table to see if it's a candidate key- Returns:
- a list of candidate keys on the specified columns
-
addReferencingTable
Adds the specified table to the set of tables that reference this table.- Parameters:
referencingTableName- the name of the table that references this table- Returns:
trueif the table wasn't previously in this table's set of referencing tables, orfalseif the table was already in the set of referencing tables
-
removeReferencingTable
-
addIndex
-
addForeignKey
-
numForeignKeys
public int numForeignKeys() -
getForeignKeys
-
getReferencingTables
-
getIndexes
-
getIndex
-
getIndexNames
-
toString
-