http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java b/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java deleted file mode 100644 index ec31e10..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java +++ /dev/null @@ -1,58 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access; - -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; - -/** - * DbLoaderDelegate defines API that allows to control the behavior of DbLoader - * during the database reverse-engineering. Delegate is also notified of the - * progress of reverse-engineering. - */ -public interface DbLoaderDelegate { - - void dbEntityAdded(DbEntity entity); - - void dbEntityRemoved(DbEntity entity); - - /** - * Called before relationship loading for db-entity - * @param entity - * - * @return true in case you want process relationships for this entity - * false otherwise - */ - boolean dbRelationship(DbEntity entity); - - /** - * Called before relationship will be added into db-entity but after it was loaded from db - * @param entity - * - * @return true in case you want add this relationship into entity - * false otherwise - */ - boolean dbRelationshipLoaded(DbEntity entity, DbRelationship relationship); - - void objEntityAdded(ObjEntity entity); - - void objEntityRemoved(ObjEntity entity); -}
http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesBaseLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesBaseLoader.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesBaseLoader.java deleted file mode 100644 index 57e35ac..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesBaseLoader.java +++ /dev/null @@ -1,107 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.TypesMapping; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; -import java.util.Set; - -/** -* @since 4.0. -*/ -public abstract class DbAttributesBaseLoader implements DbAttributesLoader { - private final String catalog; - private final String schema; - - private final DatabaseMetaData metaData; - private final DbAdapter adapter; - - public DbAttributesBaseLoader(String catalog, String schema, DatabaseMetaData metaData, DbAdapter adapter) { - this.catalog = catalog; - this.schema = schema; - this.metaData = metaData; - this.adapter = adapter; - } - - protected DbAttribute loadDbAttribute(Set<String> columns, ResultSet rs) throws SQLException { - - // gets attribute's (column's) information - int columnType = rs.getInt("DATA_TYPE"); - - // ignore precision of non-decimal columns - int decimalDigits = -1; - if (TypesMapping.isDecimal(columnType)) { - decimalDigits = rs.getInt("DECIMAL_DIGITS"); - if (rs.wasNull()) { - decimalDigits = -1; - } - } - - // create attribute delegating this task to adapter - DbAttribute attr = adapter.buildAttribute( - rs.getString("COLUMN_NAME"), - rs.getString("TYPE_NAME"), - columnType, - rs.getInt("COLUMN_SIZE"), - decimalDigits, - rs.getBoolean("NULLABLE")); - - if (columns.contains("IS_AUTOINCREMENT")) { - String autoIncrement = rs.getString("IS_AUTOINCREMENT"); - if ("YES".equals(autoIncrement)) { - attr.setGenerated(true); - } - } - return attr; - } - - @Override - public void loadDbAttributes(DbEntity entity) { - for (DbAttribute attr : loadDbAttributes(entity.getName())) { - attr.setEntity(entity); - - // override existing attributes if it comes again - if (entity.getAttribute(attr.getName()) != null) { - entity.removeAttribute(attr.getName()); - } - entity.addAttribute(attr); - } - } - - protected abstract List<DbAttribute> loadDbAttributes(String tableName); - - protected String getCatalog() { - return catalog; - } - - protected String getSchema() { - return schema; - } - - protected DatabaseMetaData getMetaData() { - return metaData; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesLoader.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesLoader.java deleted file mode 100644 index e4113b4..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesLoader.java +++ /dev/null @@ -1,43 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import org.apache.cayenne.map.DbEntity; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Interface responsible for attributes loading. Several options possible here - * 1) load attributes for each table separately - * 2) load attributes for schema and group it by table names - * - * here is a trade of between count of queries and amount af calculation. - * - * - * @since 4.0 - */ -public interface DbAttributesLoader { - - // TODO use instant field for logging - Log LOGGER = LogFactory.getLog(DbTableLoader.class); - - void loadDbAttributes(DbEntity entity); - -} - http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesPerSchemaLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesPerSchemaLoader.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesPerSchemaLoader.java deleted file mode 100644 index a7871ad..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbAttributesPerSchemaLoader.java +++ /dev/null @@ -1,130 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.cayenne.access.loader.filters.PatternFilter; -import org.apache.cayenne.access.loader.filters.TableFilter; -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.map.DbAttribute; - -/** - * Load all attributes for schema and return it for each table - * */ -public class DbAttributesPerSchemaLoader extends DbAttributesBaseLoader { - - private final TableFilter filter; - - private Map<String, List<DbAttribute>> attributes; - - public DbAttributesPerSchemaLoader(String catalog, String schema, DatabaseMetaData metaData, DbAdapter adapter, - TableFilter filter) { - super(catalog, schema, metaData, adapter); - - this.filter = filter; - } - - private Map<String, List<DbAttribute>> loadDbAttributes() throws SQLException { - Map<String, List<DbAttribute>> attributes = new HashMap<>(); - - try (ResultSet rs = getMetaData().getColumns(getCatalog(), getSchema(), "%", "%");) { - Set<String> columns = new HashSet<String>(); - - while (rs.next()) { - if (columns.isEmpty()) { - ResultSetMetaData rsMetaData = rs.getMetaData(); - for (int i = 1; i <= rsMetaData.getColumnCount(); i++) { - columns.add(rsMetaData.getColumnLabel(i)); - } - } - - // for a reason not quiet apparent to me, Oracle sometimes - // returns duplicate record sets for the same table, messing up - // table - // names. E.g. for the system table "WK$_ATTR_MAPPING" columns - // are - // returned twice - as "WK$_ATTR_MAPPING" and - // "WK$$_ATTR_MAPPING"... Go figure - String tableName = rs.getString("TABLE_NAME"); - String columnName = rs.getString("COLUMN_NAME"); - - PatternFilter columnFilter = filter.isIncludeTable(tableName); - /* - * Here is possible optimization if filter will contain - * map<tableName, columnFilter> we can replace it after tables - * loading since already done pattern matching once and exactly - * know all tables that we want to process - */ - if (columnFilter == null || !columnFilter.isInclude(columnName)) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Skip column '" + tableName + "." + columnName + "' (Path: " + getCatalog() + "/" - + getSchema() + "; Filter: " + columnFilter + ")"); - } - continue; - } - - List<DbAttribute> attrs = attributes.get(tableName); - if (attrs == null) { - attrs = new LinkedList<DbAttribute>(); - - attributes.put(tableName, attrs); - } - - attrs.add(loadDbAttribute(columns, rs)); - } - } - - return attributes; - } - - @Override - protected List<DbAttribute> loadDbAttributes(String tableName) { - Map<String, List<DbAttribute>> attributes = getAttributes(); - if (attributes != null) { - List<DbAttribute> dbAttributes = attributes.get(tableName); - if (dbAttributes != null) { - return dbAttributes; - } - } - - return new LinkedList<DbAttribute>(); - } - - public Map<String, List<DbAttribute>> getAttributes() { - if (attributes == null) { - try { - attributes = loadDbAttributes(); - } catch (SQLException e) { - LOGGER.error(e); - attributes = new HashMap<>(); - } - } - return attributes; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbLoaderConfiguration.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbLoaderConfiguration.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbLoaderConfiguration.java deleted file mode 100644 index 59365cc..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbLoaderConfiguration.java +++ /dev/null @@ -1,150 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import org.apache.cayenne.access.loader.filters.TableFilter; -import org.apache.cayenne.access.loader.filters.FiltersConfig; -import org.apache.cayenne.access.loader.filters.PatternFilter; - -/** - * @since 4.0 - */ -public class DbLoaderConfiguration { - - /** - * Returns a name of a generic class that should be used for all - * ObjEntities. The most common generic class is - * {@link org.apache.cayenne.CayenneDataObject}. If generic class name is - * null (which is the default), DbLoader will assign each entity a unique - * class name derived from the table name. - * - */ - private String genericClassName; - -/* - // TODO: Andrus, 10/29/2005 - this type of filtering should be delegated to adapter - TODO by default should skip name.startsWith("BIN$") - - private NameFilter tableFilter = NamePatternMatcher.build(null, null, "BIN$"); - - private NameFilter columnFilter; - - private NameFilter proceduresFilter = new NameFilter() { - private final Collection<String> excludedProcedures = Arrays.asList( - "auto_pk_for_table", - "auto_pk_for_table;1" // the last name is some Mac OS X Sybase artifact - ); - - @Override - public boolean isIncluded(String string) { - return !excludedProcedures.contains(string); - } - }; -*/ - - - /** - * Java class implementing org.apache.cayenne.map.naming.NamingStrategy. - * This is used to specify how ObjEntities will be mapped from the imported - * DB schema. - */ - private String namingStrategy; - - private Boolean skipRelationshipsLoading; - - private Boolean skipPrimaryKeyLoading; - - private String[] tableTypes; - - private FiltersConfig filtersConfig; - - public String getGenericClassName() { - return genericClassName; - } - - public void setGenericClassName(String genericClassName) { - this.genericClassName = genericClassName; - } - - public String[] getTableTypes() { - return tableTypes; - } - - public void setTableTypes(String[] tableTypes) { - this.tableTypes = tableTypes; - } - - public String getNamingStrategy() { - return namingStrategy; - } - - public void setNamingStrategy(String namingStrategy) { - this.namingStrategy = namingStrategy; - } - - public FiltersConfig getFiltersConfig() { - if (filtersConfig == null) { - // this case is used often in tests where config not initialized properly - return FiltersConfig.create(null, null, TableFilter.everything(), PatternFilter.INCLUDE_NOTHING); - } - return filtersConfig; - } - - public void setFiltersConfig(FiltersConfig filtersConfig) { - this.filtersConfig = filtersConfig; - } - - public boolean isSkipRelationshipsLoading() { - return skipRelationshipsLoading != null && skipRelationshipsLoading; - } - - public Boolean getSkipRelationshipsLoading() { - return skipRelationshipsLoading; - } - - public void setSkipRelationshipsLoading(Boolean skipRelationshipsLoading) { - this.skipRelationshipsLoading = skipRelationshipsLoading; - } - - public void setSkipPrimaryKeyLoading(Boolean skipPrimaryKeyLoading) { - this.skipPrimaryKeyLoading = skipPrimaryKeyLoading; - } - - public boolean getSkipPrimaryKeyLoading() { - return skipPrimaryKeyLoading; - } - - public boolean isSkipPrimaryKeyLoading() { - return skipPrimaryKeyLoading != null && skipPrimaryKeyLoading; - } - - @Override - public String toString() { - String res = "EntitiesFilters: " + getFiltersConfig(); - if (isSkipRelationshipsLoading()) { - res += "\n Skip Loading Relationships! \n"; - } - - if (isSkipPrimaryKeyLoading()) { - res += "\n Skip Loading PrimaryKeys! \n"; - } - - return res; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbTableLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbTableLoader.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbTableLoader.java deleted file mode 100644 index baad305..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DbTableLoader.java +++ /dev/null @@ -1,196 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import org.apache.cayenne.access.DbLoaderDelegate; -import org.apache.cayenne.access.loader.filters.PatternFilter; -import org.apache.cayenne.access.loader.filters.TableFilter; -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DetectedDbEntity; -import org.apache.cayenne.map.ObjEntity; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * @since 4.0 - */ -public class DbTableLoader { - - private static final Log LOGGER = LogFactory.getLog(DbTableLoader.class); - - private static final String WILDCARD = "%"; - - private final String catalog; - private final String schema; - - private final DatabaseMetaData metaData; - private final DbLoaderDelegate delegate; - - private final DbAttributesLoader attributesLoader; - - public DbTableLoader(String catalog, String schema, DatabaseMetaData metaData, DbLoaderDelegate delegate, - DbAttributesLoader attributesLoader) { - this.catalog = catalog; - this.schema = schema; - this.metaData = metaData; - this.delegate = delegate; - - this.attributesLoader = attributesLoader; - } - - /** - * Returns all tables for given combination of the criteria. Tables returned - * as DbEntities without any attributes or relationships. - * - * @param types - * The types of table names to retrieve, null returns all types. - * @return - * @since 4.0 - */ - public List<DetectedDbEntity> getDbEntities(TableFilter filters, String[] types) throws SQLException { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Read tables: catalog=" + catalog + ", schema=" + schema + ", types=" + Arrays.toString(types)); - } - - List<DetectedDbEntity> tables = new LinkedList<DetectedDbEntity>(); - try (ResultSet rs = metaData.getTables(catalog, schema, WILDCARD, types);) { - while (rs.next()) { - // Oracle 9i and newer has a nifty recycle bin feature... but we - // don't - // want dropped tables to be included here; in fact they may - // even result - // in errors on reverse engineering as their names have special - // chars like - // "/", etc. So skip them all together - - String name = rs.getString("TABLE_NAME"); - if (name == null) { - continue; - } - - DetectedDbEntity table = new DetectedDbEntity(name); - - String catalog = rs.getString("TABLE_CAT"); - table.setCatalog(catalog); - - String schema = rs.getString("TABLE_SCHEM"); - table.setSchema(schema); - if (!(this.catalog == null || this.catalog.equals(catalog)) - || !(this.schema == null || this.schema.equals(schema))) { - - LOGGER.error(catalog + "." + schema + "." + name + " wrongly loaded for catalog/schema : " - + this.catalog + "." + this.schema); - - continue; - } - - PatternFilter includeTable = filters.isIncludeTable(table.getName()); - if (includeTable != null) { - tables.add(table); - } - } - } - return tables; - } - - /** - * Loads dbEntities for the specified tables. - * - * @param config - * @param types - */ - public List<DbEntity> loadDbEntities(DataMap map, DbLoaderConfiguration config, String[] types) throws SQLException { - /** List of db entities to process. */ - - List<DetectedDbEntity> tables = getDbEntities(config.getFiltersConfig().tableFilter(catalog, schema), types); - - List<DbEntity> dbEntities = new ArrayList<DbEntity>(); - for (DbEntity dbEntity : tables) { - DbEntity oldEnt = map.getDbEntity(dbEntity.getName()); - if (oldEnt != null) { - Collection<ObjEntity> oldObjEnt = map.getMappedEntities(oldEnt); - if (!oldObjEnt.isEmpty()) { - for (ObjEntity objEntity : oldObjEnt) { - LOGGER.debug("Delete ObjEntity: " + objEntity.getName()); - map.removeObjEntity(objEntity.getName(), true); - delegate.objEntityRemoved(objEntity); - } - } - - LOGGER.debug("Overwrite DbEntity: " + oldEnt.getName()); - map.removeDbEntity(oldEnt.getName(), true); - delegate.dbEntityRemoved(oldEnt); - } - - map.addDbEntity(dbEntity); - - delegate.dbEntityAdded(dbEntity); - - // delegate might have thrown this entity out... so check if it is - // still - // around before continuing processing - if (map.getDbEntity(dbEntity.getName()) == dbEntity) { - dbEntities.add(dbEntity); - attributesLoader.loadDbAttributes(dbEntity); - if (!config.isSkipPrimaryKeyLoading()) { - loadPrimaryKey(dbEntity); - } - } - } - - return dbEntities; - } - - private void loadPrimaryKey(DbEntity dbEntity) throws SQLException { - - try (ResultSet rs = metaData.getPrimaryKeys(dbEntity.getCatalog(), dbEntity.getSchema(), dbEntity.getName());) { - while (rs.next()) { - String columnName = rs.getString("COLUMN_NAME"); - DbAttribute attribute = dbEntity.getAttribute(columnName); - - if (attribute != null) { - attribute.setPrimaryKey(true); - } else { - // why an attribute might be null is not quiet clear - // but there is a bug report 731406 indicating that it is - // possible - // so just print the warning, and ignore - LOGGER.warn("Can't locate attribute for primary key: " + columnName); - } - - String pkName = rs.getString("PK_NAME"); - if (pkName != null && dbEntity instanceof DetectedDbEntity) { - ((DetectedDbEntity) dbEntity).setPrimaryKeyName(pkName); - } - - } - } - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java deleted file mode 100644 index 70a0230..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java +++ /dev/null @@ -1,60 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import org.apache.cayenne.access.DbLoaderDelegate; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; - -/** - * @since 4.0. - */ -public class DefaultDbLoaderDelegate implements DbLoaderDelegate { - - @Override - public void dbEntityAdded(DbEntity entity) { - - } - - @Override - public void dbEntityRemoved(DbEntity entity) { - - } - - @Override - public boolean dbRelationship(DbEntity entity) { - return true; - } - - @Override - public boolean dbRelationshipLoaded(DbEntity entity, DbRelationship relationship) { - return true; - } - - @Override - public void objEntityAdded(ObjEntity entity) { - - } - - @Override - public void objEntityRemoved(ObjEntity entity) { - - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java deleted file mode 100644 index 8c77a87..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; -import org.apache.commons.logging.Log; - -/** - * @since 4.0 - */ -public class LoggingDbLoaderDelegate extends DefaultDbLoaderDelegate { - - private final Log logger; - - public LoggingDbLoaderDelegate(Log logger) { - this.logger = logger; - } - - @Override - public void dbEntityAdded(DbEntity entity) { - logger.info(" Table: " + entity.getFullyQualifiedName()); - } - - @Override - public void dbEntityRemoved(DbEntity entity) { - logger.info(" Table removed: " + entity.getFullyQualifiedName()); - } - - @Override - public boolean dbRelationship(DbEntity entity) { - if (logger.isDebugEnabled()) { - logger.debug(" Relationships for " + entity.getFullyQualifiedName()); - } - - return true; - } - - @Override - public boolean dbRelationshipLoaded(DbEntity entity, DbRelationship relationship) { - logger.info(" " + relationship); - - return true; - } - - @Override - public void objEntityAdded(ObjEntity entity) { - if (logger.isDebugEnabled()) { - logger.debug(" Class: " + entity.getName()); - } - } - - @Override - public void objEntityRemoved(ObjEntity entity) { - if (logger.isDebugEnabled()) { - logger.debug(" Class removed: " + entity.getName()); - } - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntity.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntity.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntity.java deleted file mode 100644 index 6b93f19..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntity.java +++ /dev/null @@ -1,142 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; -import org.apache.cayenne.map.naming.DefaultUniqueNameGenerator; -import org.apache.cayenne.map.naming.ExportedKey; -import org.apache.cayenne.map.naming.NameCheckers; -import org.apache.cayenne.map.naming.ObjectNameGenerator; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.util.ArrayList; -import java.util.List; - -/** - * Class represent ObjEntity that may be optimized using flattened relationships - * as many to many table - */ -public class ManyToManyCandidateEntity { - - private static final Log LOG = LogFactory.getLog(ManyToManyCandidateEntity.class); - - private final ObjEntity joinEntity; - - private final DbRelationship dbRel1; - private final DbRelationship dbRel2; - - private final ObjEntity entity1; - private final ObjEntity entity2; - - private final DbRelationship reverseRelationship1; - private final DbRelationship reverseRelationship2; - - private ManyToManyCandidateEntity(ObjEntity entityValue, List<ObjRelationship> relationships) { - joinEntity = entityValue; - - ObjRelationship rel1 = relationships.get(0); - ObjRelationship rel2 = relationships.get(1); - - dbRel1 = rel1.getDbRelationships().get(0); - dbRel2 = rel2.getDbRelationships().get(0); - - reverseRelationship1 = dbRel1.getReverseRelationship(); - reverseRelationship2 = dbRel2.getReverseRelationship(); - - entity1 = rel1.getTargetEntity(); - entity2 = rel2.getTargetEntity(); - } - - /** - * Method check - if current entity represent many to many temporary table - * @return true if current entity is represent many to many table; otherwise returns false - */ - public static ManyToManyCandidateEntity build(ObjEntity joinEntity) { - ArrayList<ObjRelationship> relationships = new ArrayList<ObjRelationship>(joinEntity.getRelationships()); - if (relationships.size() != 2 || (relationships.get(0).getDbRelationships().isEmpty() || relationships.get(1).getDbRelationships().isEmpty())) { - return null; - } - - ManyToManyCandidateEntity candidateEntity = new ManyToManyCandidateEntity(joinEntity, relationships); - if (candidateEntity.isManyToMany()) { - return candidateEntity; - } - - return null; - } - - private boolean isManyToMany() { - boolean isNotHaveAttributes = joinEntity.getAttributes().size() == 0; - - return isNotHaveAttributes - && reverseRelationship1 != null && reverseRelationship1.isToDependentPK() - && reverseRelationship2 != null && reverseRelationship2.isToDependentPK() - && entity1 != null && entity2 != null; - } - - private void addFlattenedRelationship(ObjectNameGenerator nameGenerator, ObjEntity srcEntity, ObjEntity dstEntity, - DbRelationship rel1, DbRelationship rel2) { - - if (rel1.getSourceAttributes().isEmpty() && rel2.getTargetAttributes().isEmpty()) { - LOG.warn("Wrong call ManyToManyCandidateEntity.addFlattenedRelationship(... , " + srcEntity.getName() - + ", " + dstEntity.getName() + ", ...)"); - - return; - } - - ExportedKey key = new ExportedKey( - rel1.getSourceEntity().getName(), - rel1.getSourceAttributes().iterator().next().getName(), - null, - rel2.getTargetEntity().getName(), - rel2.getTargetAttributes().iterator().next().getName(), - null, - (short) 1); - - ObjRelationship newRelationship = new ObjRelationship(); - newRelationship.setName(DefaultUniqueNameGenerator.generate(NameCheckers.objRelationship, srcEntity, - nameGenerator.createDbRelationshipName(key, true))); - - newRelationship.setSourceEntity(srcEntity); - newRelationship.setTargetEntityName(dstEntity); - - newRelationship.addDbRelationship(rel1); - newRelationship.addDbRelationship(rel2); - - srcEntity.addRelationship(newRelationship); - } - - /** - * Method make direct relationships between 2 entities and remove relationships to - * many to many entity - * - * @param nameGenerator - */ - public void optimizeRelationships(ObjectNameGenerator nameGenerator) { - entity1.removeRelationship(reverseRelationship1.getName()); - entity2.removeRelationship(reverseRelationship2.getName()); - - addFlattenedRelationship(nameGenerator, entity1, entity2, reverseRelationship1, dbRel2); - addFlattenedRelationship(nameGenerator, entity2, entity1, reverseRelationship2, dbRel1); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NameFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NameFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NameFilter.java deleted file mode 100644 index b0269d4..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NameFilter.java +++ /dev/null @@ -1,27 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -/** - * @since 4.0. - */ -public interface NameFilter { - - boolean isIncluded(String string); -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NamePatternMatcher.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NamePatternMatcher.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NamePatternMatcher.java deleted file mode 100644 index 6faa7e4..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/NamePatternMatcher.java +++ /dev/null @@ -1,225 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import org.apache.cayenne.util.CayenneMapEntry; -import org.apache.commons.logging.Log; - -/** - * Provides name pattern matching functionality. - * - * @since 1.2 - */ -public class NamePatternMatcher implements NameFilter { - - private static final String[] EMPTY_ARRAY = new String[0]; - private static final Pattern COMMA = Pattern.compile(","); - - private final Pattern[] itemIncludeFilters; - private final Pattern[] itemExcludeFilters; - - public static NamePatternMatcher build(Log logger, String includePattern, String excludePattern) { - return new NamePatternMatcher(createPatterns(logger, includePattern), createPatterns(logger, excludePattern)); - } - - public NamePatternMatcher(Pattern[] itemIncludeFilters, Pattern[] itemExcludeFilters) { - this.itemIncludeFilters = itemIncludeFilters; - this.itemExcludeFilters = itemExcludeFilters; - } - - /** - * Applies preconfigured list of filters to the list, removing entities that do not - * pass the filter. - * - * @deprecated since 3.0 still used by AntDataPortDelegate, which itself should - * probably be deprecated - */ - @Deprecated - public List<?> filter(List<?> items) { - if (items == null || items.isEmpty()) { - return items; - } - - if (itemIncludeFilters.length == 0 && itemExcludeFilters.length == 0) { - return items; - } - - Iterator<?> it = items.iterator(); - while (it.hasNext()) { - CayenneMapEntry entity = (CayenneMapEntry) it.next(); - - if (!passedIncludeFilter(entity.getName())) { - it.remove(); - continue; - } - - if (!passedExcludeFilter(entity.getName())) { - it.remove(); - } - } - - return items; - } - - /** - * Returns an array of Patterns. Takes a comma-separated list of patterns, attempting - * to convert them to the java.util.regex.Pattern syntax. E.g. - * <p> - * <code>"billing_*,user?"</code> will become an array of two expressions: - * <p> - * <code>^billing_.*$</code><br> - * <code>^user.?$</code><br> - */ - public static Pattern[] createPatterns(Log logger, String patternString) { - if (patternString == null) { - return new Pattern[0]; - } - String[] patternStrings = tokenizePattern(patternString); - List<Pattern> patterns = new ArrayList<Pattern>(patternStrings.length); - - for (String patternString1 : patternStrings) { - - // test the pattern - try { - patterns.add(Pattern.compile(patternString1)); - } catch (PatternSyntaxException e) { - - if (logger != null) { - logger.warn("Ignoring invalid pattern [" + patternString1 + "], reason: " + e.getMessage()); - } - } - } - - return patterns.toArray(new Pattern[patterns.size()]); - } - - /** - * Returns an array of valid regular expressions. Takes a comma-separated list of - * patterns, attempting to convert them to the java.util.regex.Pattern syntax. E.g. - * <p> - * <code>"billing_*,user?"</code> will become an array of two expressions: - * <p> - * <code>^billing_.*$</code><br> - * <code>^user.?$</code><br> - */ - public static String[] tokenizePattern(String pattern) { - if (pattern == null || pattern.isEmpty()) { - return EMPTY_ARRAY; - } - - String[] patterns = COMMA.split(pattern); - if (patterns.length == 0) { - return EMPTY_ARRAY; - } - - for (int i = 0; i < patterns.length; i++) { - // convert * into regex syntax - // e.g. abc*x becomes ^abc.*x$ - // or abc?x becomes ^abc.?x$ - patterns[i] = "^" + patterns[i].replaceAll("[*?]", ".$0") + "$"; - } - - return patterns; - } - - /** - * Returns true if a given object property satisfies the include/exclude patterns. - * - * @since 3.0 - */ - @Override - public boolean isIncluded(String string) { - return passedIncludeFilter(string) && passedExcludeFilter(string); - } - - /** - * Returns true if an object matches any one of the "include" patterns, or if there is - * no "include" patterns defined. - * - * @since 3.0 - */ - private boolean passedIncludeFilter(String item) { - if (itemIncludeFilters.length == 0) { - return true; - } - - for (Pattern itemIncludeFilter : itemIncludeFilters) { - if (itemIncludeFilter.matcher(item).find()) { - return true; - } - } - - return false; - } - - /** - * Returns true if an object does not match any one of the "exclude" patterns, or if - * there is no "exclude" patterns defined. - * - * @since 3.0 - */ - private boolean passedExcludeFilter(String item) { - if (itemExcludeFilters.length == 0) { - return true; - } - - for (Pattern itemExcludeFilter : itemExcludeFilters) { - if (itemExcludeFilter.matcher(item).find()) { - return false; - } - } - - return true; - } - - public static String replaceWildcardInStringWithString( - String wildcard, - String pattern, - String replacement) { - - if (pattern == null || wildcard == null) { - return pattern; - } - - StringBuilder buffer = new StringBuilder(); - int lastPos = 0; - int wildCardPos = pattern.indexOf(wildcard); - while (wildCardPos != -1) { - if (lastPos != wildCardPos) { - buffer.append(pattern.substring(lastPos, wildCardPos)); - } - buffer.append(replacement); - lastPos += wildCardPos + wildcard.length(); - wildCardPos = pattern.indexOf(wildcard, lastPos); - } - - if (lastPos < pattern.length()) { - buffer.append(pattern.substring(lastPos)); - } - - return buffer.toString(); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/CatalogFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/CatalogFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/CatalogFilter.java deleted file mode 100644 index 830c9c8..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/CatalogFilter.java +++ /dev/null @@ -1,62 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -import java.util.Arrays; - -/** -* @since 4.0. -*/ -public class CatalogFilter { - public final String name; - public final SchemaFilter[] schemas; - - public CatalogFilter(String name, SchemaFilter... schemas) { - if (schemas == null || schemas.length == 0) { - throw new IllegalArgumentException("schemas(" + Arrays.toString(schemas) + ") can't be null or empty"); - } - - this.name = name; - this.schemas = schemas; - } - - public SchemaFilter getSchema(String schema) { - for (SchemaFilter schemaFilter : schemas) { - if (schemaFilter.name == null || schemaFilter.name.equals(schema)) { - return schemaFilter; - } - } - - return null; - } - - @Override - public String toString() { - return toString(new StringBuilder(), "").toString(); - } - - public StringBuilder toString(StringBuilder res, String prefix) { - res.append(prefix).append("Catalog: ").append(name).append("\n"); - for (SchemaFilter schema : schemas) { - schema.toString(res, prefix + " "); - } - - return res; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/FiltersConfig.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/FiltersConfig.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/FiltersConfig.java deleted file mode 100644 index 7a6823e..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/FiltersConfig.java +++ /dev/null @@ -1,81 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -import java.util.Arrays; - -/** - * @since 4.0. - */ -public class FiltersConfig { - - public final CatalogFilter[] catalogs; - - public FiltersConfig(CatalogFilter ... catalogs) { - if (catalogs == null || catalogs.length == 0) { - throw new IllegalArgumentException("catalogs(" + Arrays.toString(catalogs) + ") can't be null or empty"); - } - - this.catalogs = catalogs; - } - - public PatternFilter proceduresFilter(String catalog, String schema) { - return getSchemaFilter(catalog, schema).procedures; - } - - public TableFilter tableFilter(String catalog, String schema) { - return getSchemaFilter(catalog, schema).tables; - } - - protected SchemaFilter getSchemaFilter(String catalog, String schema) { - CatalogFilter catalogFilter = getCatalog(catalog); - if (catalogFilter == null) { - return null; - } - - return catalogFilter.getSchema(schema); - } - - protected CatalogFilter getCatalog(String catalog) { - for (CatalogFilter catalogFilter : catalogs) { - if (catalogFilter.name == null || catalogFilter.name.equals(catalog)) { - return catalogFilter; - } - } - - return null; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - for (CatalogFilter catalog : catalogs) { - catalog.toString(builder, ""); - } - - return builder.toString(); - } - - public static FiltersConfig create(String catalog, String schema, TableFilter tableFilter, PatternFilter procedures) { - return new FiltersConfig( - new CatalogFilter(catalog, - new SchemaFilter(schema, tableFilter, procedures))); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/IncludeTableFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/IncludeTableFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/IncludeTableFilter.java deleted file mode 100644 index dcb144b..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/IncludeTableFilter.java +++ /dev/null @@ -1,71 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -import java.util.regex.Pattern; - -/** -* @since 4.0. -*/ -public class IncludeTableFilter implements Comparable<IncludeTableFilter> { - public final Pattern pattern; - - public final PatternFilter columnsFilter; - - public IncludeTableFilter(String pattern) { - this(pattern, PatternFilter.INCLUDE_EVERYTHING); - } - - public IncludeTableFilter(String pattern, PatternFilter columnsFilter) { - this.pattern = PatternFilter.pattern(pattern); - this.columnsFilter = columnsFilter; - } - - public boolean isIncludeColumn (String name) { - return columnsFilter.isInclude(name); - } - - @Override - public int compareTo(IncludeTableFilter o) { - if (pattern == null && o.pattern == null) { - return 0; - } else if (pattern == null) { - return 1; - } else if (o.pattern == null) { - return -1; - } else { - return pattern.pattern().compareTo(o.pattern.pattern()); - } - - } - - - @Override - public String toString() { - return toString(new StringBuilder(), "").toString(); - } - - protected StringBuilder toString(StringBuilder res, String prefix) { - res.append(prefix).append("Include: ").append(String.valueOf(pattern)).append(" Columns: "); - columnsFilter.toString(res); - res.append("\n"); - - return res; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/LegacyFilterConfigBridge.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/LegacyFilterConfigBridge.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/LegacyFilterConfigBridge.java deleted file mode 100644 index 90545c6..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/LegacyFilterConfigBridge.java +++ /dev/null @@ -1,150 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -import static org.apache.commons.lang.StringUtils.isBlank; - -/** - * @since 4.0 - */ -public class LegacyFilterConfigBridge { - - private String catalog; - private String schema; - - private String includeTableFilters; - private String includeColumnFilters; - private String includeProceduresFilters; - private String excludeTableFilters; - private String excludeColumnFilters; - private String excludeProceduresFilters; - - private boolean loadProcedures; - - public LegacyFilterConfigBridge() { - } - - public LegacyFilterConfigBridge catalog(String catalog) { - this.catalog = catalog; - return this; - } - - public String catalog() { - return catalog; - } - - public LegacyFilterConfigBridge schema(String schema) { - this.schema = schema; - return this; - } - - public String schema() { - return schema; - } - - public LegacyFilterConfigBridge includeTables(String tableFilters) { - if (isBlank(tableFilters)) { - return this; - } - - this.includeTableFilters = transform(tableFilters); - return this; - } - - public LegacyFilterConfigBridge includeColumns(String columnFilters) { - if (isBlank(columnFilters)) { - return this; - } - - this.includeColumnFilters = transform(columnFilters); - return this; - } - - public LegacyFilterConfigBridge includeProcedures(String proceduresFilters) { - if (isBlank(proceduresFilters)) { - return this; - } - - this.includeProceduresFilters = transform(proceduresFilters); - return this; - } - - public LegacyFilterConfigBridge excludeTables(String tableFilters) { - if (isBlank(tableFilters)) { - return this; - } - - this.excludeTableFilters = transform(tableFilters); - return this; - } - - public LegacyFilterConfigBridge excludeColumns(String columnFilters) { - if (isBlank(columnFilters)) { - return this; - } - - this.excludeColumnFilters = transform(columnFilters); - return this; - } - - public LegacyFilterConfigBridge excludeProcedures(String proceduresFilters) { - if (isBlank(proceduresFilters)) { - return this; - } - - this.excludeProceduresFilters = transform(proceduresFilters); - return this; - } - - private static String transform(String pattern) { - return "^" + pattern.replaceAll("[*?]", ".$0") + "$"; - } - - public void setProceduresFilters(boolean loadProcedures) { - this.loadProcedures = loadProcedures; - } - - public String getIncludeTableFilters() { - return includeTableFilters; - } - - public String getIncludeColumnFilters() { - return includeColumnFilters; - } - - public String getIncludeProceduresFilters() { - return includeProceduresFilters; - } - - public String getExcludeTableFilters() { - return excludeTableFilters; - } - - public String getExcludeColumnFilters() { - return excludeColumnFilters; - } - - public String getExcludeProceduresFilters() { - return excludeProceduresFilters; - } - - public boolean isLoadProcedures() { - return loadProcedures; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/PatternFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/PatternFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/PatternFilter.java deleted file mode 100644 index 82c45cc..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/PatternFilter.java +++ /dev/null @@ -1,169 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -import org.apache.commons.lang.StringUtils; - -import java.util.Comparator; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.regex.Pattern; - -/** - * @since 4.0 - */ -public class PatternFilter { - - public static final PatternFilter INCLUDE_EVERYTHING = new PatternFilter() { - - @Override - public boolean isInclude(String obj) { - return true; - } - - @Override - public StringBuilder toString(StringBuilder res) { - return res.append("ALL"); - } - }; - - public static final PatternFilter INCLUDE_NOTHING = new PatternFilter() { - @Override - public boolean isInclude(String obj) { - return false; - } - - @Override - public StringBuilder toString(StringBuilder res) { - return res.append("NONE"); - } - }; - - public static final Comparator<Pattern> PATTERN_COMPARATOR = new Comparator<Pattern>() { - @Override - public int compare(Pattern o1, Pattern o2) { - if (o1 != null && o2 != null) { - return o1.pattern().compareTo(o2.pattern()); - } - else { - return -1; - } - } - }; - - private final SortedSet<Pattern> includes; - private final SortedSet<Pattern> excludes; - - public PatternFilter() { - this.includes = new TreeSet<>(PATTERN_COMPARATOR); - this.excludes = new TreeSet<>(PATTERN_COMPARATOR); - } - - public PatternFilter include(Pattern p) { - includes.add(p); - - return this; - } - - public PatternFilter exclude(Pattern p) { - excludes.add(p); - - return this; - } - - public static Pattern pattern(String pattern) { - if (pattern == null) { - return null; - } - return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); - } - - public PatternFilter include(String p) { - return include(pattern(p)); - } - - public PatternFilter exclude(String p) { - return exclude(pattern(p)); - } - - public boolean isInclude(String obj) { - boolean include = includes.isEmpty(); - for (Pattern p : includes) { - if (p != null) { - if (p.matcher(obj).matches()) { - include = true; - break; - } - } - } - - if (!include) { - return false; - } - - for (Pattern p : excludes) { - if (p.matcher(obj).matches()) { - return false; - } - } - - return true; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (o == null || getClass() != o.getClass()) { - return false; - } - - - PatternFilter filter = (PatternFilter) o; - return includes.equals(filter.includes) - && excludes.equals(filter.excludes); - } - - @Override - public int hashCode() { - return includes.hashCode(); - } - - public StringBuilder toString(StringBuilder res) { - if (includes.isEmpty()) { - // Do nothing. - } else if (includes.size() > 1) { - res.append("(").append(StringUtils.join(includes, " OR ")).append(")"); - } else { - res.append(includes.first().pattern()); - } - - if (!excludes.isEmpty()) { - res.append(" AND NOT (").append(StringUtils.join(includes, " OR ")).append(")"); - } - - return res; - } - - public boolean isEmpty() { - return includes.isEmpty() && excludes.isEmpty(); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/SchemaFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/SchemaFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/SchemaFilter.java deleted file mode 100644 index c294bc4..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/SchemaFilter.java +++ /dev/null @@ -1,44 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -/** -* @since 4.0. -*/ -public class SchemaFilter { - public final String name; - public final TableFilter tables; - public final PatternFilter procedures; - - public SchemaFilter(String name, TableFilter tables, PatternFilter procedures) { - this.name = name; - this.tables = tables; - this.procedures = procedures; - } - - protected StringBuilder toString(StringBuilder res, String prefix) { - res.append(prefix).append("Schema: ").append(name).append("\n"); - tables.toString(res, prefix + " "); - - res.append(prefix).append(" Procedures: "); - procedures.toString(res).append("\n"); - - return res; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/TableFilter.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/TableFilter.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/TableFilter.java deleted file mode 100644 index 7d3c8f6..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/filters/TableFilter.java +++ /dev/null @@ -1,133 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.filters; - -import org.apache.commons.lang.StringUtils; - -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.regex.Pattern; - -/** - * TableFilter contain at least one IncludeTable always - * - */ -public class TableFilter { - - private final SortedSet<IncludeTableFilter> includes; - private final SortedSet<Pattern> excludes; - - /** - * Includes can contain only One includetable - * - * @param includes - * @param excludes - */ - public TableFilter(SortedSet<IncludeTableFilter> includes, SortedSet<Pattern> excludes) { - if (includes.isEmpty()) { - throw new IllegalArgumentException("TableFilter should contain at least one IncludeTableFilter always " + - "and it is builder responsibility. If you need table filter without includes, use EmptyTableFilter"); - } - - this.includes = includes; - this.excludes = excludes; - } - - /** - * Return filter for columns in case we should take this table - * - * @param tableName - * @return - */ - public PatternFilter isIncludeTable(String tableName) { - IncludeTableFilter include = null; - for (IncludeTableFilter p : includes) { - if (p.pattern == null || p.pattern.matcher(tableName).matches()) { - include = p; - break; - } - } - - if (include == null) { - return null; - } - - for (Pattern p : excludes) { - if (p != null) { - if (p.matcher(tableName).matches()) { - return null; - } - } - } - - return include.columnsFilter; - } - - public static TableFilter include(String tablePattern) { - TreeSet<IncludeTableFilter> includes = new TreeSet<IncludeTableFilter>(); - includes.add(new IncludeTableFilter(tablePattern == null ? null : tablePattern.replaceAll("%", ".*"))); - - return new TableFilter(includes, new TreeSet<Pattern>()); - } - - public static TableFilter everything() { - TreeSet<IncludeTableFilter> includes = new TreeSet<IncludeTableFilter>(); - includes.add(new IncludeTableFilter(null)); - - return new TableFilter(includes, new TreeSet<Pattern>()); - } - - protected StringBuilder toString(StringBuilder res, String prefix) { - res.append(prefix).append("Tables: ").append("\n"); - - for (IncludeTableFilter include : includes) { - include.toString(res, prefix + " "); - } - - if (!excludes.isEmpty()) { - res.append(prefix).append(" ").append(StringUtils.join(excludes, " OR ")).append("\n"); - } - - return res; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (!(o instanceof TableFilter)) { - return false; - } - - TableFilter that = (TableFilter) o; - - return excludes.equals(that.excludes) - && includes.equals(that.includes); - - } - - @Override - public int hashCode() { - int result = includes.hashCode(); - result = 31 * result + excludes.hashCode(); - return result; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/access/loader/mapper/DbType.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/mapper/DbType.java b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/mapper/DbType.java deleted file mode 100644 index 806b437..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/mapper/DbType.java +++ /dev/null @@ -1,194 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 - * - * 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 org.apache.cayenne.access.loader.mapper; - -import org.apache.cayenne.util.EqualsBuilder; -import org.apache.cayenne.util.HashCodeBuilder; -import org.apache.commons.lang.builder.CompareToBuilder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import static org.apache.commons.lang.StringUtils.isBlank; - -/** - * @since 4.0 - */ -public class DbType implements Comparable<DbType> { - - private static final Log LOG = LogFactory.getLog(DbType.class); - - public final String jdbc; - - public final Integer length; - public final Integer precision; - public final Integer scale; - public final Boolean notNull; - - public DbType(String jdbc) { - this(jdbc, null, null, null, null); - } - - public DbType(String jdbc, Integer length, Integer precision, Integer scale, Boolean notNull) { - if (isBlank(jdbc)) { - throw new IllegalArgumentException("Jdbc type can't be null"); - } - this.jdbc = jdbc; - - this.length = getValidInt(length); - this.precision = getValidInt(precision); - this.scale = getValidInt(scale); - this.notNull = notNull; - } - - public String getJdbc() { - return jdbc; - } - - public Integer getLength() { - return length; - } - - public Integer getPrecision() { - return precision; - } - - public Integer getScale() { - return scale; - } - - public Boolean getNotNull() { - return notNull; - } - - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (obj == this) { - return true; - } - if (obj.getClass() != getClass()) { - return false; - } - DbType rhs = (DbType) obj; - return new EqualsBuilder() - .append(this.jdbc, rhs.jdbc) - .append(this.length, rhs.length) - .append(this.precision, rhs.precision) - .append(this.scale, rhs.scale) - .append(this.notNull, rhs.notNull) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder() - .append(jdbc) - .append(length) - .append(precision) - .append(scale) - .append(notNull) - .toHashCode(); - } - - - @Override - public String toString() { - String res = jdbc; - - String len = "*"; - if (isPositive(length)) { - len = length.toString(); - } - if (isPositive(precision)) { - len = precision.toString(); - } - - res += " (" + len; - if (isPositive(scale)) { - res += ", " + scale; - } - res += ")"; - - if (notNull != null && notNull) { - res += " NOT NULL"; - } - - return res; - } - - private boolean isPositive(Integer num) { - return num != null && num > 0; - } - - private Integer getValidInt(Integer num) { - if (num == null || num > 0) { - return num; - } - - LOG.warn("Invalid int value '" + num + "'"); - return null; - } - - /** - * Compare by specificity the most specific DbPath should be first in ordered list - */ - @Override - public int compareTo(DbType dbType) { - return new CompareToBuilder() - .append(dbType.jdbc, jdbc) - .append(dbType.getSpecificity(), getSpecificity()) - .append(dbType.length, length) - .append(dbType.precision, precision) - .append(dbType.scale, scale) - .append(dbType.notNull, notNull) - .toComparison(); - } - - private int getSpecificity() { - int res = 0; - if (isPositive(length)) { - res += 100; - } - if (isPositive(precision)) { - res += 100; - } - if (isPositive(scale)) { - res += 10; - } - if (this.notNull != null) { - res += 5; - } - - return res; - } - - public boolean isCover(DbType type) { - return this.jdbc.equals(type.jdbc) - && (isCover(length, type.length) || length == null && type.length == null && isCover(precision, type.precision)) - && isCover(scale, type.scale) - && isCover(notNull, type.notNull); - } - - private boolean isCover(Object a, Object b) { - return a == null || a.equals(b); - } -}