Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 545CD200BB8 for ; Thu, 29 Sep 2016 19:40:10 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 531E8160AEA; Thu, 29 Sep 2016 17:40:10 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D8B50160AEC for ; Thu, 29 Sep 2016 19:40:06 +0200 (CEST) Received: (qmail 42272 invoked by uid 500); 29 Sep 2016 17:38:55 -0000 Mailing-List: contact commits-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list commits@cayenne.apache.org Received: (qmail 42214 invoked by uid 99); 29 Sep 2016 17:38:54 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Sep 2016 17:38:54 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 47DD7EEE1B; Thu, 29 Sep 2016 17:38:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aadamchik@apache.org To: commits@cayenne.apache.org Date: Thu, 29 Sep 2016 17:38:51 -0000 Message-Id: <8a663a22e6fe432db50fd02acd425640@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [05/15] cayenne git commit: CAY-2116 Split schema synchronization code in a separate module archived-at: Thu, 29 Sep 2016 17:40:10 -0000 http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMerger.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMerger.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMerger.java deleted file mode 100644 index 3070cf1..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMerger.java +++ /dev/null @@ -1,405 +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.merge; - -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Types; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -import javax.sql.DataSource; - -import org.apache.cayenne.CayenneRuntimeException; -import org.apache.cayenne.access.DataNode; -import org.apache.cayenne.access.DbLoader; -import org.apache.cayenne.access.loader.DbLoaderConfiguration; -import org.apache.cayenne.access.loader.LoggingDbLoaderDelegate; -import org.apache.cayenne.access.loader.filters.FiltersConfig; -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.map.Attribute; -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbJoin; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.DetectedDbEntity; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Traverse a {@link DataNode} and a {@link DataMap} and create a group of - * {@link MergerToken}s to alter the {@link DataNode} data store to match the - * {@link DataMap}. - * - */ -public class DbMerger { - - private static final Log LOGGER = LogFactory.getLog(DbMerger.class); - - private final MergerFactory factory; - - private final ValueForNullProvider valueForNull; - - public DbMerger(MergerFactory factory) { - this(factory, null); - } - - public DbMerger(MergerFactory factory, ValueForNullProvider valueForNull) { - this.factory = factory; - this.valueForNull = valueForNull == null ? new EmptyValueForNullProvider() : valueForNull; - } - - /** - * Create and return a {@link List} of {@link MergerToken}s to alter the - * given {@link DataNode} to match the given {@link DataMap} - */ - public List createMergeTokens(DataSource dataSource, DbAdapter adapter, DataMap existingDataMap, - DbLoaderConfiguration config) { - return createMergeTokens(existingDataMap, loadDataMapFromDb(dataSource, adapter, config), config); - } - - /** - * Create and return a {@link List} of {@link MergerToken}s to alter the - * given {@link DataNode} to match the given {@link DataMap} - */ - public List createMergeTokens(DataMap existing, DataMap loadedFomDb, DbLoaderConfiguration config) { - - loadedFomDb.setQuotingSQLIdentifiers(existing.isQuotingSQLIdentifiers()); - - List tokens = createMergeTokens(filter(existing, config.getFiltersConfig()), - loadedFomDb.getDbEntities(), config); - - // sort. use a custom Comparator since only toDb tokens are comparable - // by now - Collections.sort(tokens, new Comparator() { - - public int compare(MergerToken o1, MergerToken o2) { - if (o1 instanceof AbstractToDbToken && o2 instanceof AbstractToDbToken) { - - return ((AbstractToDbToken) o1).compareTo(o2); - } - return 0; - } - }); - - return tokens; - } - - private Collection filter(DataMap existing, FiltersConfig filtersConfig) { - Collection existingFiltered = new LinkedList(); - for (DbEntity entity : existing.getDbEntities()) { - if (filtersConfig.tableFilter(entity.getCatalog(), entity.getSchema()).isIncludeTable(entity.getName()) != null) { - existingFiltered.add(entity); - } - } - return existingFiltered; - } - - private DataMap loadDataMapFromDb(DataSource dataSource, DbAdapter adapter, DbLoaderConfiguration config) { - try (Connection conn = dataSource.getConnection();) { - - return new DbLoader(conn, adapter, new LoggingDbLoaderDelegate(LOGGER)).load(config); - } catch (SQLException e) { - throw new CayenneRuntimeException("Can't doLoad dataMap from db.", e); - } - } - - public List createMergeTokens(Collection existing, Collection loadedFromDb, - DbLoaderConfiguration config) { - Collection dbEntitiesToDrop = new LinkedList(loadedFromDb); - - List tokens = new LinkedList(); - for (DbEntity dbEntity : existing) { - String tableName = dbEntity.getName(); - - // look for table - DbEntity detectedEntity = findDbEntity(loadedFromDb, tableName); - if (detectedEntity == null) { - tokens.add(factory.createCreateTableToDb(dbEntity)); - // TODO: does this work properly with createReverse? - for (DbRelationship rel : dbEntity.getRelationships()) { - tokens.add(factory.createAddRelationshipToDb(dbEntity, rel)); - } - continue; - } - - dbEntitiesToDrop.remove(detectedEntity); - - tokens.addAll(checkRelationshipsToDrop(dbEntity, detectedEntity)); - if (!config.isSkipRelationshipsLoading()) { - tokens.addAll(checkRelationshipsToAdd(dbEntity, detectedEntity)); - } - tokens.addAll(checkRows(dbEntity, detectedEntity)); - - if (!config.isSkipPrimaryKeyLoading()) { - MergerToken token = checkPrimaryKeyChange(dbEntity, detectedEntity); - if (token != null) { - tokens.add(token); - } - } - } - - // drop table - // TODO: support drop table. currently, too many tables are marked for - // drop - for (DbEntity e : dbEntitiesToDrop) { - tokens.add(factory.createDropTableToDb(e)); - for (DbRelationship relationship : e.getRelationships()) { - DbEntity detectedEntity = findDbEntity(existing, relationship.getTargetEntityName()); - if (detectedEntity != null) { - tokens.add(factory.createDropRelationshipToDb(detectedEntity, relationship.getReverseRelationship())); - } - } - } - - return tokens; - } - - private List checkRows(DbEntity existing, DbEntity loadedFromDb) { - List tokens = new LinkedList(); - - // columns to drop - for (DbAttribute detected : loadedFromDb.getAttributes()) { - if (findDbAttribute(existing, detected.getName()) == null) { - tokens.add(factory.createDropColumnToDb(existing, detected)); - } - } - - // columns to add or modify - for (DbAttribute attr : existing.getAttributes()) { - String columnName = attr.getName().toUpperCase(); - - DbAttribute detected = findDbAttribute(loadedFromDb, columnName); - - if (detected == null) { - tokens.add(factory.createAddColumnToDb(existing, attr)); - if (attr.isMandatory()) { - if (valueForNull.hasValueFor(existing, attr)) { - tokens.add(factory.createSetValueForNullToDb(existing, attr, valueForNull)); - } - tokens.add(factory.createSetNotNullToDb(existing, attr)); - } - continue; - } - - // check for not null - if (attr.isMandatory() != detected.isMandatory()) { - if (attr.isMandatory()) { - if (valueForNull.hasValueFor(existing, attr)) { - tokens.add(factory.createSetValueForNullToDb(existing, attr, valueForNull)); - } - tokens.add(factory.createSetNotNullToDb(existing, attr)); - } else { - tokens.add(factory.createSetAllowNullToDb(existing, attr)); - } - } - - // TODO: check more types than char/varchar - // TODO: psql report VARCHAR for text column, not clob - switch (detected.getType()) { - case Types.VARCHAR: - case Types.CHAR: - if (attr.getMaxLength() != detected.getMaxLength()) { - tokens.add(factory.createSetColumnTypeToDb(existing, detected, attr)); - } - break; - } - } - - return tokens; - } - - private List checkRelationshipsToDrop(DbEntity dbEntity, DbEntity detectedEntity) { - List tokens = new LinkedList(); - - // relationships to drop - for (DbRelationship detected : detectedEntity.getRelationships()) { - if (findDbRelationship(dbEntity, detected) == null) { - - // alter detected relationship to match entity and attribute - // names. - // (case sensitively) - - DbEntity targetEntity = findDbEntity(dbEntity.getDataMap().getDbEntities(), - detected.getTargetEntityName()); - if (targetEntity == null) { - continue; - } - - detected.setSourceEntity(dbEntity); - detected.setTargetEntityName(targetEntity); - - // manipulate the joins to match the DbAttributes in the model - for (DbJoin join : detected.getJoins()) { - DbAttribute sattr = findDbAttribute(dbEntity, join.getSourceName()); - if (sattr != null) { - join.setSourceName(sattr.getName()); - } - DbAttribute tattr = findDbAttribute(targetEntity, join.getTargetName()); - if (tattr != null) { - join.setTargetName(tattr.getName()); - } - } - - MergerToken token = factory.createDropRelationshipToDb(dbEntity, detected); - if (detected.isToMany()) { - // default toModel as we can not do drop a toMany in the db. - // only - // toOne are represented using foreign key - token = token.createReverse(factory); - } - tokens.add(token); - } - } - - return tokens; - } - - private List checkRelationshipsToAdd(DbEntity dbEntity, DbEntity detectedEntity) { - - List tokens = new LinkedList(); - - for (DbRelationship rel : dbEntity.getRelationships()) { - if (findDbRelationship(detectedEntity, rel) == null) { - AddRelationshipToDb token = (AddRelationshipToDb) factory.createAddRelationshipToDb(dbEntity, rel); - - if (token.shouldGenerateFkConstraint()) { - // TODO I guess we should add relationship always; in order - // to have ability - // TODO generate reverse relationship. If it doesn't have - // anything to execute it will be passed - // TODO through execution without any affect on db - tokens.add(token); - } - } - } - - return tokens; - } - - private MergerToken checkPrimaryKeyChange(DbEntity dbEntity, DbEntity detectedEntity) { - Collection primaryKeyOriginal = detectedEntity.getPrimaryKeys(); - Collection primaryKeyNew = dbEntity.getPrimaryKeys(); - - String primaryKeyName = null; - if (detectedEntity instanceof DetectedDbEntity) { - primaryKeyName = ((DetectedDbEntity) detectedEntity).getPrimaryKeyName(); - } - - if (upperCaseEntityNames(primaryKeyOriginal).equals(upperCaseEntityNames(primaryKeyNew))) { - return null; - } - - return factory.createSetPrimaryKeyToDb(dbEntity, primaryKeyOriginal, primaryKeyNew, primaryKeyName); - } - - private Set upperCaseEntityNames(Collection attrs) { - Set names = new HashSet(); - for (Attribute attr : attrs) { - names.add(attr.getName().toUpperCase()); - } - return names; - } - - /** - * case insensitive search for a {@link DbEntity} in a {@link DataMap} by - * name - */ - private DbEntity findDbEntity(Collection dbEntities, String caseInsensitiveName) { - // TODO: create a Map with upper case keys? - for (DbEntity e : dbEntities) { - if (e.getName().equalsIgnoreCase(caseInsensitiveName)) { - return e; - } - } - return null; - } - - /** - * case insensitive search for a {@link DbAttribute} in a {@link DbEntity} - * by name - */ - private DbAttribute findDbAttribute(DbEntity entity, String caseInsensitiveName) { - for (DbAttribute a : entity.getAttributes()) { - if (a.getName().equalsIgnoreCase(caseInsensitiveName)) { - return a; - } - } - return null; - } - - /** - * search for a {@link DbRelationship} like rel in the given - * {@link DbEntity} - */ - private DbRelationship findDbRelationship(DbEntity entity, DbRelationship rel) { - for (DbRelationship candidate : entity.getRelationships()) { - if (equalDbJoinCollections(candidate.getJoins(), rel.getJoins())) { - return candidate; - } - } - return null; - } - - /** - * Return true if the two unordered {@link Collection}s of {@link DbJoin}s - * are equal. Entity and Attribute names are compared case insensitively. - * - * TODO complexity n^2; sort both collection and go through them to compare - * = 2*n*log(n) + n - */ - private static boolean equalDbJoinCollections(Collection j1s, Collection j2s) { - if (j1s.size() != j2s.size()) { - return false; - } - - for (DbJoin j1 : j1s) { - if (!havePair(j2s, j1)) { - return false; - } - } - - return true; - } - - private static boolean havePair(Collection j2s, DbJoin j1) { - for (DbJoin j2 : j2s) { - if (!isNull(j1.getSource()) && !isNull(j1.getTarget()) && !isNull(j2.getSource()) - && !isNull(j2.getTarget()) - && j1.getSource().getEntity().getName().equalsIgnoreCase(j2.getSource().getEntity().getName()) - && j1.getTarget().getEntity().getName().equalsIgnoreCase(j2.getTarget().getEntity().getName()) - && j1.getSourceName().equalsIgnoreCase(j2.getSourceName()) - && j1.getTargetName().equalsIgnoreCase(j2.getTargetName())) { - - return true; - } - } - return false; - } - - private static boolean isNull(DbAttribute attribute) { - return attribute == null || attribute.getEntity() == null; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMergerConfig.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMergerConfig.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMergerConfig.java deleted file mode 100644 index e8df4b8..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DbMergerConfig.java +++ /dev/null @@ -1,63 +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.merge; - -import org.apache.cayenne.access.loader.filters.FiltersConfig; - -/** - * @since 4.0 - */ -public class DbMergerConfig { - - private FiltersConfig filtersConfig; - - private boolean skipRelationships; - - private boolean skipPrimaryKey; - - public DbMergerConfig(FiltersConfig filtersConfig, boolean skipRelationships, boolean skipPrimaryKey) { - this.filtersConfig = filtersConfig; - this.skipRelationships = skipRelationships; - this.skipPrimaryKey = skipPrimaryKey; - } - - public void setSkipRelationships(boolean skipRelationships) { - this.skipRelationships = skipRelationships; - } - - public boolean isSkipRelationships() { - return skipRelationships; - } - - public void setSkipPrimaryKey(boolean skipPrimaryKey) { - this.skipPrimaryKey = skipPrimaryKey; - } - - public boolean isSkipPrimaryKey() { - return skipPrimaryKey; - } - - public FiltersConfig getFiltersConfig() { - return filtersConfig; - } - - public void setFiltersConfig(FiltersConfig filtersConfig) { - this.filtersConfig = filtersConfig; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultModelMergeDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultModelMergeDelegate.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultModelMergeDelegate.java deleted file mode 100644 index e457f31..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultModelMergeDelegate.java +++ /dev/null @@ -1,89 +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.merge; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjAttribute; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; - -/** - * A default noop implementation of {@link ModelMergeDelegate}. - */ -public class DefaultModelMergeDelegate implements ModelMergeDelegate { - - @Override - public void dbAttributeAdded(DbAttribute att) { - } - - @Override - public void dbAttributeModified(DbAttribute att) { - } - - @Override - public void dbAttributeRemoved(DbAttribute att) { - } - - @Override - public void dbEntityAdded(DbEntity ent) { - } - - @Override - public void dbEntityRemoved(DbEntity ent) { - } - - @Override - public void dbRelationshipAdded(DbRelationship rel) { - } - - @Override - public void dbRelationshipRemoved(DbRelationship rel) { - } - - @Override - public void objAttributeAdded(ObjAttribute att) { - } - - @Override - public void objAttributeModified(ObjAttribute att) { - } - - @Override - public void objAttributeRemoved(ObjAttribute att) { - } - - @Override - public void objEntityAdded(ObjEntity ent) { - } - - @Override - public void objEntityRemoved(ObjEntity ent) { - } - - @Override - public void objRelationshipAdded(ObjRelationship rel) { - } - - @Override - public void objRelationshipRemoved(ObjRelationship rel) { - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultValueForNullProvider.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultValueForNullProvider.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultValueForNullProvider.java deleted file mode 100644 index f17f0fc..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DefaultValueForNullProvider.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.merge; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.cayenne.access.jdbc.SQLParameterBinding; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -public class DefaultValueForNullProvider implements ValueForNullProvider { - - private Map values = new HashMap<>(); - - public void set(DbEntity entity, DbAttribute column, Object value, int type) { - values.put(createKey(entity, column), new SQLParameterBinding(value, type, column - .getAttributePrecision())); - } - - protected SQLParameterBinding get(DbEntity entity, DbAttribute column) { - return values.get(createKey(entity, column)); - } - - public List createSql(DbEntity entity, DbAttribute column) { - SQLParameterBinding value = get(entity, column); - if (value == null) { - return Collections.emptyList(); - } - - // TODO: change things so it is possible to use prepared statements here - return Collections.singletonList("UPDATE " + entity.getFullyQualifiedName() - + " SET " + column.getName() + "='" + value.getValue() + "' WHERE " + column.getName() + " IS NULL"); - } - - public boolean hasValueFor(DbEntity entity, DbAttribute column) { - return values.containsKey(createKey(entity, column)); - } - - private String createKey(DbEntity entity, DbAttribute attribute) { - return (entity.getFullyQualifiedName() + "." + attribute.getName()).toUpperCase(); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java deleted file mode 100644 index 5cb0a05..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java +++ /dev/null @@ -1,51 +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.merge; - -import java.util.Collections; -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.QuotingStrategy; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -public class DropColumnToDb extends AbstractToDbToken.EntityAndColumn { - - public DropColumnToDb(DbEntity entity, DbAttribute column) { - super("Drop Column", entity, column); - } - - @Override - public List createSql(DbAdapter adapter) { - StringBuilder sqlBuffer = new StringBuilder(); - QuotingStrategy context = adapter.getQuotingStrategy(); - sqlBuffer.append("ALTER TABLE "); - sqlBuffer.append(context.quotedFullyQualifiedName(getEntity())); - sqlBuffer.append(" DROP COLUMN "); - sqlBuffer.append(context.quotedName(getColumn())); - - return Collections.singletonList(sqlBuffer.toString()); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createAddColumnToModel(getEntity(), getColumn()); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java deleted file mode 100644 index 37fb728..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java +++ /dev/null @@ -1,74 +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.merge; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbJoin; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjAttribute; -import org.apache.cayenne.map.ObjEntity; - -/** - * A {@link MergerToken} to remove a {@link DbAttribute} from a {@link DbEntity}. - * - */ -public class DropColumnToModel extends AbstractToModelToken.EntityAndColumn { - - public DropColumnToModel(DbEntity entity, DbAttribute column) { - super("Drop Column", entity, column); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createAddColumnToDb(getEntity(), getColumn()); - } - - public void execute(MergerContext mergerContext) { - - // remove relationships mapped to column. duplicate List to prevent - // ConcurrentModificationException - List dbRelationships = new ArrayList(getEntity() - .getRelationships()); - for (DbRelationship dbRelationship : dbRelationships) { - for (DbJoin join : dbRelationship.getJoins()) { - if (join.getSource() == getColumn() || join.getTarget() == getColumn()) { - remove(mergerContext.getModelMergeDelegate(), dbRelationship, true); - } - } - } - - // remove ObjAttribute mapped to same column - for (ObjEntity objEntity : getEntity().mappedObjEntities()) { - ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(getColumn()); - if (objAttribute != null) { - objEntity.removeAttribute(objAttribute.getName()); - mergerContext.getModelMergeDelegate().objAttributeRemoved(objAttribute); - } - - } - - // remove DbAttribute - getEntity().removeAttribute(getColumn().getName()); - - mergerContext.getModelMergeDelegate().dbAttributeRemoved(getColumn()); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToDb.java deleted file mode 100644 index 618aab3..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToDb.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.merge; - -import java.util.Collections; -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.QuotingStrategy; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.DbRelationshipDetected; - -public class DropRelationshipToDb extends AbstractToDbToken.Entity { - - private DbRelationship rel; - - public DropRelationshipToDb(DbEntity entity, DbRelationship rel) { - super("Drop foreign key", entity); - this.rel = rel; - } - - public String getFkName() { - if (rel instanceof DbRelationshipDetected) { - return ((DbRelationshipDetected) rel).getFkName(); - } - return null; - } - - @Override - public List createSql(DbAdapter adapter) { - String fkName = getFkName(); - if (fkName == null) { - return Collections.emptyList(); - } - - QuotingStrategy context = adapter.getQuotingStrategy(); - return Collections.singletonList( - "ALTER TABLE " + context.quotedFullyQualifiedName(getEntity()) + " DROP CONSTRAINT " + fkName); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createAddRelationshipToModel(getEntity(), rel); - } - - @Override - public String getTokenValue() { - return rel.getSourceEntity().getName() + "->" + rel.getTargetEntityName(); - } - - public DbRelationship getRelationship() { - return rel; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java deleted file mode 100644 index ee9359f..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java +++ /dev/null @@ -1,50 +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.merge; - -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; - -public class DropRelationshipToModel extends AbstractToModelToken.Entity { - - private final DbRelationship rel; - - public DropRelationshipToModel(DbEntity entity, DbRelationship rel) { - super("Drop db-relationship ", entity); - this.rel = rel; - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createAddRelationshipToDb(getEntity(), rel); - } - - public void execute(MergerContext mergerContext) { - remove(mergerContext.getModelMergeDelegate(), rel, true); - } - - @Override - public String getTokenValue() { - return AddRelationshipToModel.getTokenValue(rel); - } - - public DbRelationship getRelationship() { - return rel; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToDb.java deleted file mode 100644 index d0904e6..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToDb.java +++ /dev/null @@ -1,49 +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.merge; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.map.DbEntity; - -public class DropTableToDb extends AbstractToDbToken.Entity { - - public DropTableToDb(DbEntity entity) { - super("Drop Table", entity); - } - - @Override - public List createSql(DbAdapter adapter) { - List sqls = new ArrayList(); - // TODO: fix. some adapters drop the complete AUTO_PK_SUPPORT here - /* - sqls.addAll(adapter.getPkGenerator().dropAutoPkStatements( - Collections.singletonList(entity))); - */ - sqls.addAll(adapter.dropTableStatements(getEntity())); - return sqls; - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createCreateTableToModel(getEntity()); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToModel.java deleted file mode 100644 index 55fde14..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DropTableToModel.java +++ /dev/null @@ -1,48 +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.merge; - -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.ObjEntity; - -/** - * A {@link MergerToken} to remove a {@link DbEntity} from a {@link DataMap}. Any - * {@link ObjEntity} mapped to the {@link DbEntity} will also be removed. - * - */ -public class DropTableToModel extends AbstractToModelToken.Entity { - - public DropTableToModel(DbEntity entity) { - super("Drop Table", entity); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createCreateTableToDb(getEntity()); - } - - public void execute(MergerContext mergerContext) { - for (ObjEntity objEntity : getEntity().mappedObjEntities()) { - objEntity.getDataMap().removeObjEntity(objEntity.getName(), true); - mergerContext.getModelMergeDelegate().objEntityRemoved(objEntity); - } - getEntity().getDataMap().removeDbEntity(getEntity().getName(), true); - mergerContext.getModelMergeDelegate().dbEntityRemoved(getEntity()); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/DummyReverseToken.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/DummyReverseToken.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/DummyReverseToken.java deleted file mode 100644 index b99cc97..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/DummyReverseToken.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.merge; - -/** - * The reverse of a {@link MergerToken} that can not be reversed.. This will not execute - * any thing, but {@link #createReverse(MergerFactory)} will get back the reverse that - * this was made from. - */ -class DummyReverseToken implements MergerToken { - - private MergerToken reverse; - - public DummyReverseToken(MergerToken reverse) { - this.reverse = reverse; - } - - public MergerToken createReverse(MergerFactory factory) { - return reverse; - } - - public void execute(MergerContext mergerContext) { - // can not execute - } - - public MergeDirection getDirection() { - return reverse.getDirection().reverseDirection(); - } - - public String getTokenName() { - return "Can not execute the reverse of " + reverse.getTokenName(); - } - - public String getTokenValue() { - return reverse.getTokenValue(); - } - - public boolean isReversible() { - return true; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/EmptyValueForNullProvider.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/EmptyValueForNullProvider.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/EmptyValueForNullProvider.java deleted file mode 100644 index 88fb9f4..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/EmptyValueForNullProvider.java +++ /dev/null @@ -1,40 +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.merge; - -import java.util.Collections; -import java.util.List; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * A dummy {@link ValueForNullProvider} that are not able to provide any values - */ -class EmptyValueForNullProvider implements ValueForNullProvider { - - public List createSql(DbEntity entity, DbAttribute column) { - return Collections.emptyList(); - } - - public boolean hasValueFor(DbEntity entity, DbAttribute column) { - return false; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/MergeDirection.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergeDirection.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/MergeDirection.java deleted file mode 100644 index d0a95c5..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergeDirection.java +++ /dev/null @@ -1,70 +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.merge; - -/** - * Represent a merge direction that can be either from the model to the db or from the db to the model. - */ -public enum MergeDirection { - - /** - * TO_DB Token means that changes was made in object model and should be reflected at DB - */ - TO_DB("To DB"), - - /** - * TO_MODEL Token represent database changes that should be allayed to object model - */ - TO_MODEL("To Model"); - - private String name; - - MergeDirection(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public boolean isToDb() { - return (this == TO_DB); - } - - public boolean isToModel() { - return (this == TO_MODEL); - } - - @Override - public String toString() { - return getName(); - } - - public MergeDirection reverseDirection() { - switch (this) { - case TO_DB: - return TO_MODEL; - case TO_MODEL: - return TO_DB; - default: - throw new IllegalStateException("Invalid direction: " + this); - } - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerContext.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerContext.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerContext.java deleted file mode 100644 index f592ebe..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerContext.java +++ /dev/null @@ -1,118 +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.merge; - -import org.apache.cayenne.access.DataNode; -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.validation.ValidationResult; - -import javax.sql.DataSource; -import java.util.Objects; - -/** - * An object passed as an argument to {@link MergerToken#execute(MergerContext)}s that a - * {@link MergerToken} can do its work. - */ -public class MergerContext { - - private DataMap dataMap; - private DataNode dataNode; - private ValidationResult validationResult; - private ModelMergeDelegate delegate; - - protected MergerContext() { - } - - public static Builder builder(DataMap dataMap) { - return new Builder().dataMap(dataMap); - } - - /** - * @deprecated since 4.0 use {@link #getDataNode()} and its {@link DataNode#getAdapter()} method. - */ - @Deprecated - public DbAdapter getAdapter() { - return getDataNode().getAdapter(); - } - - /** - * Returns the DataMap that is the target of a the merge operation. - * - * @return the DataMap that is the target of a the merge operation. - */ - public DataMap getDataMap() { - return dataMap; - } - - public DataNode getDataNode() { - return dataNode; - } - - public ValidationResult getValidationResult() { - return validationResult; - } - - /** - * Returns a callback object that is invoked as the merge proceeds through tokens, modifying the DataMap. - * - * @return a callback object that is invoked as the merge proceeds through tokens, modifying the DataMap. - */ - public ModelMergeDelegate getModelMergeDelegate() { - return delegate; - } - - public static class Builder { - - private MergerContext context; - - private Builder() { - this.context = new MergerContext(); - this.context.validationResult = new ValidationResult(); - this.context.delegate = new DefaultModelMergeDelegate(); - this.context.dataNode = new DataNode(); - } - - public MergerContext build() { - return context; - } - - public Builder delegate(ModelMergeDelegate delegate) { - context.delegate = Objects.requireNonNull(delegate); - return this; - } - - public Builder dataNode(DataNode dataNode) { - this.context.dataNode = Objects.requireNonNull(dataNode); - return this; - } - - public Builder syntheticDataNode(DataSource dataSource, DbAdapter adapter) { - DataNode dataNode = new DataNode(); - dataNode.setDataSource(dataSource); - dataNode.setAdapter(adapter); - return dataNode(dataNode); - } - - public Builder dataMap(DataMap dataMap) { - context.dataMap = Objects.requireNonNull(dataMap); - return this; - } - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerFactory.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerFactory.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerFactory.java deleted file mode 100644 index 92e064b..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerFactory.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.merge; - -import java.util.Collection; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; - -/** - * All {@link MergerToken}s should be created from a {@link MergerFactory} obtained from - * {@link DbAdapter#mergerFactory()} so that the {@link DbAdapter} are able to provide - * {@link MergerToken} subclasses. - * - * @see DbAdapter#mergerFactory() - */ -public class MergerFactory { - - public MergerToken createCreateTableToModel(DbEntity entity) { - return new CreateTableToModel(entity); - } - - public MergerToken createCreateTableToDb(DbEntity entity) { - return new CreateTableToDb(entity); - } - - public MergerToken createDropTableToModel(DbEntity entity) { - return new DropTableToModel(entity); - } - - public MergerToken createDropTableToDb(DbEntity entity) { - return new DropTableToDb(entity); - } - - public MergerToken createAddColumnToModel(DbEntity entity, DbAttribute column) { - return new AddColumnToModel(entity, column); - } - - public MergerToken createAddColumnToDb(DbEntity entity, DbAttribute column) { - return new AddColumnToDb(entity, column); - } - - public MergerToken createDropColumnToModel(DbEntity entity, DbAttribute column) { - return new DropColumnToModel(entity, column); - } - - public MergerToken createDropColumnToDb(DbEntity entity, DbAttribute column) { - return new DropColumnToDb(entity, column); - } - - public MergerToken createSetNotNullToModel(DbEntity entity, DbAttribute column) { - return new SetNotNullToModel(entity, column); - } - - public MergerToken createSetNotNullToDb(DbEntity entity, DbAttribute column) { - return new SetNotNullToDb(entity, column); - } - - public MergerToken createSetAllowNullToModel(DbEntity entity, DbAttribute column) { - return new SetAllowNullToModel(entity, column); - } - - public MergerToken createSetAllowNullToDb(DbEntity entity, DbAttribute column) { - return new SetAllowNullToDb(entity, column); - } - - public MergerToken createSetValueForNullToDb(DbEntity entity, DbAttribute column, ValueForNullProvider valueForNullProvider){ - return new SetValueForNullToDb(entity, column, valueForNullProvider); - } - - public MergerToken createSetColumnTypeToModel( - DbEntity entity, - DbAttribute columnOriginal, - DbAttribute columnNew) { - return new SetColumnTypeToModel(entity, columnOriginal, columnNew); - } - - public MergerToken createSetColumnTypeToDb( - DbEntity entity, - DbAttribute columnOriginal, - DbAttribute columnNew) { - return new SetColumnTypeToDb(entity, columnOriginal, columnNew); - } - - public MergerToken createAddRelationshipToDb(DbEntity entity, DbRelationship rel) { - return new AddRelationshipToDb(entity, rel); - } - - public MergerToken createAddRelationshipToModel(DbEntity entity, DbRelationship rel) { - return new AddRelationshipToModel(entity, rel); - } - - public MergerToken createDropRelationshipToDb(DbEntity entity, DbRelationship rel) { - return new DropRelationshipToDb(entity, rel); - } - - public MergerToken createDropRelationshipToModel(DbEntity entity, DbRelationship rel) { - return new DropRelationshipToModel(entity, rel); - } - - public MergerToken createSetPrimaryKeyToDb( - DbEntity entity, - Collection primaryKeyOriginal, - Collection primaryKeyNew, - String detectedPrimaryKeyName) { - return new SetPrimaryKeyToDb( - entity, - primaryKeyOriginal, - primaryKeyNew, - detectedPrimaryKeyName); - } - - public MergerToken createSetPrimaryKeyToModel( - DbEntity entity, - Collection primaryKeyOriginal, - Collection primaryKeyNew, - String detectedPrimaryKeyName) { - return new SetPrimaryKeyToModel( - entity, - primaryKeyOriginal, - primaryKeyNew, - detectedPrimaryKeyName); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerToken.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerToken.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerToken.java deleted file mode 100644 index 99af419..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/MergerToken.java +++ /dev/null @@ -1,51 +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.merge; - -/** - * Represents a minimal atomic synchronization operation between database and Cayenne model. - */ -public interface MergerToken { - - String getTokenName(); - - String getTokenValue(); - - /** - * The direction of this token. One of {@link MergeDirection#TO_DB} or - * {@link MergeDirection#TO_MODEL} - */ - MergeDirection getDirection(); - - /** - * Create a complimentary token with the reverse direction. AddColumn in one direction becomes - * DropColumn in the other direction. - *

- * Not all tokens are reversible. - */ - MergerToken createReverse(MergerFactory factory); - - /** - * Executes synchronization operation. - * - * @param mergerContext operation context. - */ - void execute(MergerContext mergerContext); - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/ModelMergeDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/ModelMergeDelegate.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/ModelMergeDelegate.java deleted file mode 100644 index f003752..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/ModelMergeDelegate.java +++ /dev/null @@ -1,65 +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.merge; - -import org.apache.cayenne.access.DbLoaderDelegate; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjAttribute; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; - -/** - * A interface used to tell about modifications performed on the model by - * {@link MergerToken} with {@link MergeDirection#TO_MODEL} - * - * @see DbLoaderDelegate - */ -public interface ModelMergeDelegate { - - public void dbEntityAdded(DbEntity ent); - - public void dbEntityRemoved(DbEntity ent); - - public void objEntityAdded(ObjEntity ent); - - public void objEntityRemoved(ObjEntity ent); - - public void dbAttributeAdded(DbAttribute att); - - public void dbAttributeRemoved(DbAttribute att); - - public void dbAttributeModified(DbAttribute att); - - public void objAttributeAdded(ObjAttribute att); - - public void objAttributeRemoved(ObjAttribute att); - - public void objAttributeModified(ObjAttribute att); - - public void dbRelationshipAdded(DbRelationship rel); - - public void dbRelationshipRemoved(DbRelationship rel); - - public void objRelationshipAdded(ObjRelationship rel); - - public void objRelationshipRemoved(ObjRelationship rel); - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/ProxyModelMergeDelegate.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/ProxyModelMergeDelegate.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/ProxyModelMergeDelegate.java deleted file mode 100644 index ad6a4a9..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/ProxyModelMergeDelegate.java +++ /dev/null @@ -1,108 +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.merge; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjAttribute; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; - -/** - * @since 4.0 - */ -public class ProxyModelMergeDelegate implements ModelMergeDelegate { - - private final ModelMergeDelegate delegate; - - public ProxyModelMergeDelegate(ModelMergeDelegate delegate) { - this.delegate = delegate; - } - - @Override - public void dbEntityAdded(DbEntity ent) { - delegate.dbEntityAdded(ent); - } - - @Override - public void dbEntityRemoved(DbEntity ent) { - delegate.dbEntityRemoved(ent); - } - - @Override - public void objEntityAdded(ObjEntity ent) { - delegate.objEntityAdded(ent); - } - - @Override - public void objEntityRemoved(ObjEntity ent) { - delegate.objEntityRemoved(ent); - } - - @Override - public void dbAttributeAdded(DbAttribute att) { - delegate.dbAttributeAdded(att); - } - - @Override - public void dbAttributeRemoved(DbAttribute att) { - delegate.dbAttributeRemoved(att); - } - - @Override - public void dbAttributeModified(DbAttribute att) { - delegate.dbAttributeModified(att); - } - - @Override - public void objAttributeAdded(ObjAttribute att) { - delegate.objAttributeAdded(att); - } - - @Override - public void objAttributeRemoved(ObjAttribute att) { - delegate.objAttributeRemoved(att); - } - - @Override - public void objAttributeModified(ObjAttribute att) { - delegate.objAttributeModified(att); - } - - @Override - public void dbRelationshipAdded(DbRelationship rel) { - delegate.dbRelationshipAdded(rel); - } - - @Override - public void dbRelationshipRemoved(DbRelationship rel) { - delegate.dbRelationshipRemoved(rel); - } - - @Override - public void objRelationshipAdded(ObjRelationship rel) { - delegate.objRelationshipAdded(rel); - } - - @Override - public void objRelationshipRemoved(ObjRelationship rel) { - delegate.objRelationshipRemoved(rel); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java deleted file mode 100644 index 07f677c..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java +++ /dev/null @@ -1,56 +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.merge; - -import java.util.Collections; -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.QuotingStrategy; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * A {@link MergerToken} to add a "allow null" clause to a column. - * - */ -public class SetAllowNullToDb extends AbstractToDbToken.EntityAndColumn { - - public SetAllowNullToDb(DbEntity entity, DbAttribute column) { - super("Set Allow Null", entity, column); - } - - @Override - public List createSql(DbAdapter adapter) { - StringBuilder sqlBuffer = new StringBuilder(); - QuotingStrategy context = adapter.getQuotingStrategy(); - sqlBuffer.append("ALTER TABLE "); - sqlBuffer.append(context.quotedFullyQualifiedName(getEntity())); - sqlBuffer.append(" ALTER COLUMN "); - sqlBuffer.append(context.quotedName(getColumn())); - sqlBuffer.append(" DROP NOT NULL"); - - return Collections.singletonList(sqlBuffer.toString()); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetNotNullToModel(getEntity(), getColumn()); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToModel.java deleted file mode 100644 index 049240d..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetAllowNullToModel.java +++ /dev/null @@ -1,42 +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.merge; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * A {@link MergerToken} to set the mandatory field of a {@link DbAttribute} to false - * - */ -public class SetAllowNullToModel extends AbstractToModelToken.EntityAndColumn { - - public SetAllowNullToModel(DbEntity entity, DbAttribute column) { - super("Set Allow Null", entity, column); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetNotNullToDb(getEntity(), getColumn()); - } - - public void execute(MergerContext mergerContext) { - getColumn().setMandatory(false); - mergerContext.getModelMergeDelegate().dbAttributeModified(getColumn()); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java deleted file mode 100644 index 466a9d9..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java +++ /dev/null @@ -1,119 +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.merge; - -import java.util.Collections; -import java.util.List; - -import org.apache.cayenne.CayenneRuntimeException; -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.JdbcAdapter; -import org.apache.cayenne.dba.QuotingStrategy; -import org.apache.cayenne.dba.TypesMapping; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * An {@link MergerToken} to use to set type, length and precision. - */ -public class SetColumnTypeToDb extends AbstractToDbToken.Entity { - - private DbAttribute columnOriginal; - private DbAttribute columnNew; - - public SetColumnTypeToDb(DbEntity entity, DbAttribute columnOriginal, DbAttribute columnNew) { - super("Set Column Type", entity); - this.columnOriginal = columnOriginal; - this.columnNew = columnNew; - } - - /** - * append the part of the token before the actual column data type - * @param context - */ - protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy context) { - sqlBuffer.append("ALTER TABLE "); - sqlBuffer.append(context.quotedFullyQualifiedName(getEntity())); - sqlBuffer.append(" ALTER "); - sqlBuffer.append(context.quotedName(columnNew)); - sqlBuffer.append(" TYPE "); - } - - @Override - public List createSql(DbAdapter adapter) { - StringBuffer sqlBuffer = new StringBuffer(); - appendPrefix(sqlBuffer, adapter.getQuotingStrategy()); - - sqlBuffer.append(JdbcAdapter.getType(adapter, columnNew)); - sqlBuffer.append(JdbcAdapter.sizeAndPrecision(adapter, columnNew)); - - return Collections.singletonList(sqlBuffer.toString()); - } - - @Override - public String getTokenValue() { - StringBuilder sb = new StringBuilder(); - sb.append(getEntity().getName()); - sb.append("."); - sb.append(columnNew.getName()); - - if (columnOriginal.getType() != columnNew.getType()) { - sb.append(" type: "); - sb.append(TypesMapping.getSqlNameByType(columnOriginal.getType())); - sb.append(" -> "); - sb.append(TypesMapping.getSqlNameByType(columnNew.getType())); - } - - if (columnOriginal.getMaxLength() != columnNew.getMaxLength()) { - sb.append(" maxLength: "); - sb.append(columnOriginal.getMaxLength()); - sb.append(" -> "); - sb.append(columnNew.getMaxLength()); - } - - if (columnOriginal.getAttributePrecision() != columnNew.getAttributePrecision()) { - sb.append(" precision: "); - sb.append(columnOriginal.getAttributePrecision()); - sb.append(" -> "); - sb.append(columnNew.getAttributePrecision()); - } - - if (columnOriginal.getScale() != columnNew.getScale()) { - sb.append(" scale: "); - sb.append(columnOriginal.getScale()); - sb.append(" -> "); - sb.append(columnNew.getScale()); - } - - return sb.toString(); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetColumnTypeToModel(getEntity(), columnNew, columnOriginal); - } - - public DbAttribute getColumnOriginal() { - return columnOriginal; - } - - public DbAttribute getColumnNew() { - return columnNew; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToModel.java deleted file mode 100644 index 784bc53..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetColumnTypeToModel.java +++ /dev/null @@ -1,101 +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.merge; - -import org.apache.cayenne.dba.TypesMapping; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * A {@link MergerToken} that modifies one original {@link DbAttribute} to match another - * new {@link DbAttribute}s type, maxLength and precision. The name and mandatory fields - * are not modified by this token. - * - */ -public class SetColumnTypeToModel extends AbstractToModelToken.Entity { - - private DbAttribute columnOriginal; - private DbAttribute columnNew; - - public SetColumnTypeToModel(DbEntity entity, DbAttribute columnOriginal, - DbAttribute columnNew) { - super("Set Column Type", entity); - this.columnOriginal = columnOriginal; - this.columnNew = columnNew; - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetColumnTypeToDb(getEntity(), columnNew, columnOriginal); - } - - public void execute(MergerContext mergerContext) { - columnOriginal.setType(columnNew.getType()); - columnOriginal.setMaxLength(columnNew.getMaxLength()); - columnOriginal.setAttributePrecision(columnNew.getAttributePrecision()); - columnOriginal.setScale(columnNew.getScale()); - mergerContext.getModelMergeDelegate().dbAttributeModified(columnOriginal); - } - - @Override - public String getTokenValue() { - StringBuilder sb = new StringBuilder(); - sb.append(getEntity().getName()); - sb.append("."); - sb.append(columnNew.getName()); - - if (columnOriginal.getType() != columnNew.getType()) { - sb.append(" type: "); - sb.append(TypesMapping.getSqlNameByType(columnOriginal.getType())); - sb.append(" -> "); - sb.append(TypesMapping.getSqlNameByType(columnNew.getType())); - } - - if (columnOriginal.getMaxLength() != columnNew.getMaxLength()) { - sb.append(" maxLength: "); - sb.append(columnOriginal.getMaxLength()); - sb.append(" -> "); - sb.append(columnNew.getMaxLength()); - } - - if (columnOriginal.getAttributePrecision() != columnNew.getAttributePrecision()) { - sb.append(" precision: "); - sb.append(columnOriginal.getAttributePrecision()); - sb.append(" -> "); - sb.append(columnNew.getAttributePrecision()); - } - - if (columnOriginal.getScale() != columnNew.getScale()) { - sb.append(" scale: "); - sb.append(columnOriginal.getScale()); - sb.append(" -> "); - sb.append(columnNew.getScale()); - } - - return sb.toString(); - } - - public DbAttribute getColumnOriginal() { - return columnOriginal; - } - - public DbAttribute getColumnNew() { - return columnNew; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java deleted file mode 100644 index 60b5ad5..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java +++ /dev/null @@ -1,50 +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.merge; - -import java.util.Collections; -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.QuotingStrategy; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * A {@link MergerToken} to add a "not null" clause to a column. - */ -public class SetNotNullToDb extends AbstractToDbToken.EntityAndColumn { - - public SetNotNullToDb(DbEntity entity, DbAttribute column) { - super("Set Not Null", entity, column); - } - - @Override - public List createSql(DbAdapter adapter) { - QuotingStrategy context = adapter.getQuotingStrategy(); - - return Collections.singletonList("ALTER TABLE " + context.quotedFullyQualifiedName(getEntity()) - + " ALTER COLUMN " + context.quotedName(getColumn()) + " SET NOT NULL"); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetAllowNullToModel(getEntity(), getColumn()); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToModel.java deleted file mode 100644 index 767f9e5..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetNotNullToModel.java +++ /dev/null @@ -1,42 +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.merge; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * A {@link MergerToken} to set the mandatory field of a {@link DbAttribute} to true - * - */ -public class SetNotNullToModel extends AbstractToModelToken.EntityAndColumn { - - public SetNotNullToModel(DbEntity entity, DbAttribute column) { - super("Set Not Null", entity, column); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetAllowNullToDb(getEntity(), getColumn()); - } - - public void execute(MergerContext mergerContext) { - getColumn().setMandatory(true); - mergerContext.getModelMergeDelegate().dbAttributeModified(getColumn()); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java deleted file mode 100644 index 8f467fa..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java +++ /dev/null @@ -1,85 +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.merge; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.QuotingStrategy; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -public class SetPrimaryKeyToDb extends AbstractToDbToken.Entity { - - private Collection primaryKeyOriginal; - private Collection primaryKeyNew; - private String detectedPrimaryKeyName; - - public SetPrimaryKeyToDb(DbEntity entity, Collection primaryKeyOriginal, - Collection primaryKeyNew, String detectedPrimaryKeyName) { - super("Set Primary Key", entity); - - this.primaryKeyOriginal = primaryKeyOriginal; - this.primaryKeyNew = primaryKeyNew; - this.detectedPrimaryKeyName = detectedPrimaryKeyName; - } - - @Override - public List createSql(DbAdapter adapter) { - List sqls = new ArrayList(); - if (!primaryKeyOriginal.isEmpty()) { - appendDropOriginalPrimaryKeySQL(adapter, sqls); - } - appendAddNewPrimaryKeySQL(adapter, sqls); - return sqls; - } - - protected void appendDropOriginalPrimaryKeySQL(DbAdapter adapter, List sqls) { - if (detectedPrimaryKeyName == null) { - return; - } - sqls.add("ALTER TABLE " + adapter.getQuotingStrategy().quotedFullyQualifiedName(getEntity()) - + " DROP CONSTRAINT " + detectedPrimaryKeyName); - } - - protected void appendAddNewPrimaryKeySQL(DbAdapter adapter, List sqls) { - QuotingStrategy quotingStrategy = adapter.getQuotingStrategy(); - - StringBuilder sql = new StringBuilder(); - sql.append("ALTER TABLE "); - sql.append(quotingStrategy.quotedFullyQualifiedName(getEntity())); - sql.append(" ADD PRIMARY KEY ("); - for (Iterator it = primaryKeyNew.iterator(); it.hasNext();) { - sql.append(quotingStrategy.quotedName(it.next())); - if (it.hasNext()) { - sql.append(", "); - } - } - sql.append(")"); - sqls.add(sql.toString()); - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetPrimaryKeyToModel(getEntity(), primaryKeyNew, primaryKeyOriginal, - detectedPrimaryKeyName); - } -}