MetaResult.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
com.quantum.core.metadata |
![]() |
![]() |
Quantum Database Utility |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* File: MetaResult.java Created: 03/04/2007 10:22:58 AM
*
* Copyright 2007 the original author or authors.
*
* Licensed under the CPL, Version 1.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.opensource.org/licenses/cpl1.0.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.quantum.core.metadata;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.quantum.adapters.DatabaseAdapter;
import com.quantum.core.metadata.impl.Schema;
import com.quantum.core.metadata.model.ICheck;
import com.quantum.core.metadata.model.IColumn;
import com.quantum.core.metadata.model.IDbObjectQ;
import com.quantum.core.metadata.model.IEntity;
import com.quantum.core.metadata.model.IForeignKey;
import com.quantum.core.metadata.model.IIndex;
import com.quantum.core.metadata.model.IMetadata;
import com.quantum.core.metadata.model.IPrivilege;
import com.quantum.core.metadata.model.ITable;
import com.quantum.core.metadata.model.ITrigger;
import com.quantum.model.Bookmark;
import com.quantum.util.ListUtils;
import com.quantum.util.connection.NotConnectedException;
/**
* Metadata results class. It's the class that does the metadata job for the IMetadataService
* It's structured as a list of IMetadata objects. Most methods return itself, to allow chaining, like:
* forSchema("SCOTT").getTables().startWith("B").getColumns().asArrayOf(IColumn.class)
* This allow flexibility, but it's not very efficient. Other more efficient functions are also available.
* It'll usually delegate to the adapter for accessing the JDBC, in case the adapter has a better
* way of getting the data than the standard JDBC
*
* @author Julen Parra
*/
public class MetaResult extends ArrayList<IMetadata> {
private static final long serialVersionUID = -7138696658829882743L;
private Bookmark bookmark = null;
private String schema = null;
private boolean useCache = true;
/**
* Main constructor. All methods consider that they'll have access to the bookmark member.
* It'll set the schema to the default schema of the bookmark
*/
public MetaResult(Bookmark bookmark) throws NotConnectedException, SQLException {
this.bookmark = bookmark;
this.schema = bookmark.getDefaultSchema().getName().toUpperCase();
this.useCache = true;
}
public void setUseCache(boolean b) {
useCache = b;
}
/**
* Sets the schema, to restrict actions to that schema only
*/
public MetaResult forSchema(String schema) {
this.schema = schema;
return this;
}
/*************************************************************
* <CHAINED GETTERS>
*************************************************************/
/**
* Gets all the schemas of the current bookmark, as a chained MetaResult
*/
public MetaResult getSchemas()
throws NotConnectedException, SQLException {
clear();
addAll(getAllSchemas());
return this;
}
/**
* Gets all the tables for the current bookmark, as a chained MetaResult
*/
public MetaResult getTables() throws NotConnectedException, SQLException {
// If there is anything in our list, they should be schemas, we store them
List<Schema> schemaList = ListUtils.getFilteredList(this, Schema.class);
clear();
if (schemaList.size() > 0) {
for (Schema schema : schemaList) {
addTables(schema.getName());
}
} else {
addTables(schema);
}
return this;
}
/**
* Gets all the tables for the current bookmark, as a chained MetaResult
*/
public MetaResult getViews() throws NotConnectedException, SQLException {
// If there is anything in our list, they should be schemas, we store them
List<Schema> schemaList = ListUtils.getFilteredList(this, Schema.class);
clear();
if (schemaList.size() > 0) {
for (Schema schema : schemaList) {
addViews(schema.getName());
}
} else {
addViews(schema);
}
return this;
}
public MetaResult getColumns() throws NotConnectedException, SQLException {
// If there is anything in our list, they should be entities, we store them
List<IEntity> tableList = ListUtils.getFilteredList(this, IEntity.class);
clear();
for (IEntity entity : tableList) {
addColumns(entity);
}
return this;
}
/*************************************************************
* </chained getters>
*************************************************************/
/*************************************************************
* <CHAINED FILTERS>
*************************************************************/
/**
* Leaves in the current metaresult just the elements whose name starts with "prefix"
*/
public MetaResult startWith(String prefix) {
// TODO: This is probably slower than making a copy of the object and returning it
for (Iterator<IMetadata> iter = this.iterator(); iter.hasNext();) {
IMetadata element = iter.next();
if (!element.getName().startsWith(prefix)) {
iter.remove();
}
}
return this;
}
/**
* Leaves in the current metaresult just the elements whose name is precisely the given one
* (Comparation is case sensitive)
*/
public MetaResult withName(String name) {
// TODO: This is probably slower than making a copy of the object and returning it
for (Iterator<IMetadata> iter = this.iterator(); iter.hasNext();) {
IMetadata element = iter.next();
if (!element.getName().equals(name)) {
iter.remove();
}
}
return this;
}
public List<String> asNames() {
List<String> result = new ArrayList<String>();
for (IMetadata object : this) {
result.add(object.getName());
}
return result;
}
public String[] asNameArray() {
return asNames().toArray(new String[this.size()]);
}
/*
* Returns the objects in the list an array of elements of class T
* This is done for compatibility only and is better avoided if possible.
* Just use typed lists for all purposes
*/
@SuppressWarnings("unchecked")
public <T> T[] asArrayOf(Class<T> clazz) {
// Filter the list to avoid ClassCast errors
List<T> selected = ListUtils.getFilteredList(this, clazz);
// return the list as an array of type T.
// Note that you have to do it this way if you want to prevent generics erasure to return an Object[]
// If you wanted to use a typed array as input instead of the Class<T> , you should have to extract
// the correct type from the input array using reflection, like:
// Class<?> componentType = inputArray.getClass().getComponentType();
// T[] result = T[].class.cast(Array.newInstance(componentType, length));
// Nasty, isn't it?
return selected.toArray((T[])Array.newInstance(clazz, this.size()));
}
/*
* Returns the objects in the list an list of elements of class T
*/
public <T> List<T> asListOf(Class<T> clazz) {
// Filter the list to avoid ClassCast errors
return ListUtils.getFilteredList(this, clazz);
}
/*************************************************************
* <CONVENIENCE METHODS> These methods don't return a MetaResult, and so end the chain
*************************************************************/
/**
* Convenience method to just get all schemas as a list of schemas
*/
public List<Schema> getAllSchemas()
throws NotConnectedException, SQLException {
List<Schema> schemas = null;
if (useCache) {
schemas = MetadataCache.getInstance().getSchemas(bookmark);
}
if (schemas == null) {
schemas = bookmark.getAdapter().getSchemas(bookmark);
MetadataCache.getInstance().putSchemas(bookmark, schemas);
}
return schemas;
}
/**
* Returns all the objects in the schema of the given type
*/
public List<IDbObjectQ> getObjectsOfType(String schema, String type)
throws NotConnectedException, SQLException {
List<IDbObjectQ> objects = null;
if (useCache) {
objects = MetadataCache.getInstance().getObjects(bookmark, schema, type);
}
if (objects == null) {
objects = bookmark.getAdapter().getObjects(bookmark, schema, type);
MetadataCache.getInstance().putObjects(bookmark, schema, type, objects);
}
return objects;
}
/**
* Returns a list of strings with the names of the columns that matches the column pattern
*/
//TODO: At the moment, in the "NamesLike" functions, caching is per-call, so a call for all columns like "A%"
// and another for all columns like "AT%" will result in two different cache keys
// stored, and two calls to the JDBC. Other possibility could be to cache all column names the first time they
// are used and the do the selection using java regexp's. This could have negative impact in
// cases where bandwitdh to the database is thin.
public List<String> getColumnNamesLike(String columnPattern)
throws NotConnectedException, SQLException {
List<String> columnNames = null;
if (useCache) {
columnNames = MetadataCache.getInstance().getColumnNamesLike(bookmark, schema, columnPattern);
}
if (columnNames == null) {
columnNames = bookmark.getAdapter().getColumnNamesLike(bookmark, schema, columnPattern);
MetadataCache.getInstance().putColumnNamesLike(bookmark, schema, columnPattern, columnNames);
}
return columnNames;
}
/**
* Returns a list of strings with the names of the columns that matches the column pattern, for a table or tablePattern
*/
public List<String> getColumnNamesLike(String tablePattern, String columnPattern)
throws NotConnectedException, SQLException {
List<String> columnNames = null;
if (useCache) {
columnNames = MetadataCache.getInstance().getColumnNamesLike(bookmark, schema, tablePattern, columnPattern);
}
if (columnNames == null) {
columnNames = bookmark.getAdapter().getColumnNamesLike(bookmark, schema, tablePattern, columnPattern);
MetadataCache.getInstance().putColumnNamesLike(bookmark, schema, tablePattern, columnPattern, columnNames);
}
return columnNames;
}
/**
* Returns a boolean indicating that the given column name belongs to the given tablename
*/
public boolean columnBelongsToTable(String tableName, String columnName)
throws NotConnectedException, SQLException {
return bookmark.getAdapter().columnBelongsToTable(bookmark, schema, tableName, columnName);
}
/**
* Gets a list of IColumn objects
*/
public List<IColumn> getColumns(IEntity entity)
throws NotConnectedException, SQLException {
List<IColumn> columns = null;
if (useCache) {
columns = MetadataCache.getInstance().getColumns(entity);
}
if (columns == null) {
columns = bookmark.getAdapter().getColumns(entity);
MetadataCache.getInstance().putColumns(entity, columns);
}
return columns;
}
public boolean tableExists(String name)
throws NotConnectedException, SQLException {
clear();
addTables(this.schema);
return nameExists(name);
}
public boolean viewExists(String name)
throws NotConnectedException, SQLException {
clear();
addViews(this.schema);
return nameExists(name);
}
private boolean nameExists(String name) {
// should this be a preference? Or should we check the MetaData?
for (IMetadata object : this) {
if (object.getName().equalsIgnoreCase(name)) {
return true;
}
}
return false;
}
/**
* Gets the indexes of the given table
*/
public List<IIndex> getIndexes(ITable entity)
throws SQLException, NotConnectedException {
List<IIndex> indexes = null;
if (useCache) {
indexes = MetadataCache.getInstance().getIndexes(entity);
}
if (indexes == null) {
indexes = bookmark.getAdapter().getIndexes(entity);
MetadataCache.getInstance().putIndexes(entity, indexes);
}
return indexes;
}
public List<IPrivilege> getPrivileges(IEntity entity)
throws NotConnectedException, SQLException {
List<IPrivilege> privileges = null;
if (useCache) {
privileges = MetadataCache.getInstance().getPrivileges(entity);
}
if (privileges == null) {
privileges = bookmark.getAdapter().getPrivileges(entity);
MetadataCache.getInstance().putPrivileges(entity, privileges);
}
return privileges;
}
public List<ICheck> getChecks(ITable table)
throws NotConnectedException, SQLException {
List<ICheck> checks = null;
if (useCache) {
checks = MetadataCache.getInstance().getChecks(table);
}
if (checks == null) {
checks = bookmark.getAdapter().getChecks(table);
MetadataCache.getInstance().putChecks(table, checks);
}
return checks;
}
public List<ITrigger> getTriggers(IDbObjectQ object)
throws NotConnectedException, SQLException {
List<ITrigger> triggers = null;
if (useCache) {
triggers = MetadataCache.getInstance().getTriggers(object);
}
if (triggers == null) {
triggers = bookmark.getAdapter().getTriggers(object);
MetadataCache.getInstance().putTriggers(object, triggers);
}
return triggers;
}
public String getCreateStatement(IEntity entity)
throws NotConnectedException, SQLException {
DatabaseAdapter adapter = entity.getBookmark().getAdapter();
if (entity.getType() == IEntity.VIEW_TYPE) {
return adapter.getCreateView(entity);
} else if (entity.getType() == IEntity.TABLE_TYPE) {
return adapter.buildCreateTable(entity.getBookmark(), (ITable)entity, null, false);
}
return "";
}
public List<IForeignKey> getImportedKeys(IEntity entity)
throws NotConnectedException, SQLException {
return getImportedKeys(entity.getSchema(), entity.getName());
}
public List<IForeignKey> getImportedKeys(String schemaName, String tableName)
throws NotConnectedException, SQLException {
List<IForeignKey> iKeys = null;
if (useCache) {
iKeys = MetadataCache.getInstance().getImportedKeys(bookmark, schemaName, tableName);
}
if (iKeys == null) {
iKeys = bookmark.getAdapter().getImportedKeys(bookmark, schemaName, tableName);
MetadataCache.getInstance().putImportedKeys(bookmark, schemaName, tableName, iKeys);
}
return iKeys;
}
public List<IForeignKey> getExportedKeys(IEntity entity)
throws NotConnectedException, SQLException {
return getExportedKeys(entity.getSchema(), entity.getName());
}
public List<IForeignKey> getExportedKeys(String schemaName, String tableName)
throws NotConnectedException, SQLException {
List<IForeignKey> iKeys = null;
if (useCache) {
iKeys = MetadataCache.getInstance().getExportedKeys(bookmark, schemaName, tableName);
}
if (iKeys == null) {
iKeys = bookmark.getAdapter().getExportedKeys(bookmark, schemaName, tableName);
MetadataCache.getInstance().putExportedKeys(bookmark, schemaName, tableName, iKeys);
}
return iKeys;
}
/*************************************************************
* </convenience methods>
*************************************************************/
/*************************************************************
* <PRIVATE UTILITIES>
*************************************************************/
private void addColumns(IEntity entity) throws NotConnectedException, SQLException {
this.addAll(entity.getColumns());
}
private void addTables(String schema) throws SQLException, NotConnectedException {
addObjectsOfType(schema, IEntity.TABLE_TYPE);
}
private void addViews(String schema) throws SQLException, NotConnectedException {
addObjectsOfType(schema, IEntity.VIEW_TYPE);
}
private void addObjectsOfType(String schema, String type) throws SQLException, NotConnectedException {
if(useCache){
List<IDbObjectQ> tables = null;
tables = MetadataCache.getInstance().getObjects(bookmark, schema, type);
if(tables == null){
tables = bookmark.getAdapter().getObjects(bookmark, schema, type );
MetadataCache.getInstance().putObjects(bookmark, schema, type, tables);
}
this.addAll(tables);
}else{
this.addAll(bookmark.getAdapter().getObjects(bookmark, schema, type ));
}
}
/*************************************************************
* </private utilities>
* @throws NotConnectedException
*************************************************************/
/*************************************************************
* <REFRESH>
*************************************************************/
public void refresh() {
MetadataCache.getInstance().setDirty(bookmark.getName(), null, null, null);
}
public void refresh(IEntity entity) {
MetadataCache.getInstance().setDirty(bookmark.getName(), entity.getSchema(), entity.getType(), entity.getName());
}
/*************************************************************
* </refresh>
*************************************************************/
}
The table below shows all metrics for MetaResult.java.




