From: <fu...@us...> - 2010-02-18 00:09:55
|
Revision: 1038 http://cishell.svn.sourceforge.net/cishell/?rev=1038&view=rev Author: fugu13 Date: 2010-02-18 00:09:49 +0000 (Thu, 18 Feb 2010) Log Message: ----------- Made merging faster. Made merging progresstrackable. Reviewed by Joseph. Modified Paths: -------------- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ColumnPair.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/DatabaseTable.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ForeignKey.java Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Remover.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Repointer.java Modified: trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF =================================================================== --- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-02-17 23:25:26 UTC (rev 1037) +++ trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-02-18 00:09:49 UTC (rev 1038) @@ -4,7 +4,10 @@ Bundle-SymbolicName: org.cishell.utilities Bundle-Version: 1.0.0 Bundle-RequiredExecutionEnvironment: J2SE-1.5 -Import-Package: org.cishell.framework;version="1.0.0", +Import-Package: com.google.common.annotations, + com.google.common.base, + com.google.common.collect, + org.cishell.framework;version="1.0.0", org.cishell.framework.algorithm;version="1.0.0", org.cishell.framework.data, org.cishell.reference.service.metatype, Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ColumnPair.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ColumnPair.java 2010-02-17 23:25:26 UTC (rev 1037) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ColumnPair.java 2010-02-18 00:09:49 UTC (rev 1038) @@ -20,4 +20,5 @@ public int hashCode() { return local.hashCode() * 31 + foreign.hashCode(); } + } Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/DatabaseTable.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/DatabaseTable.java 2010-02-17 23:25:26 UTC (rev 1037) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/DatabaseTable.java 2010-02-18 00:09:49 UTC (rev 1038) @@ -1,6 +1,7 @@ package org.cishell.utilities.database; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -10,9 +11,15 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedSet; import org.cishell.utilities.DatabaseUtilities; +import org.cishell.utilities.StringUtilities; +import com.google.common.collect.ImmutableSortedMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + public final class DatabaseTable { public final String catalog; public final String schema; @@ -147,4 +154,37 @@ List<Map<String, Object>> otherEntities) { return "DELETE FROM " + this.toString() + " WHERE " + DatabaseUtilities.createSQLInExpression(columns, otherEntities); } + + private String formatDeleteEquals(Connection connection, String separator) throws SQLException { + //sorting is necessary to ensure keys and values match up + SortedSet<String> keys = Sets.newTreeSet(); + String[] primaryKeys = getPrimaryKeyColumns(connection); + for(String key : primaryKeys) { + keys.add(key + " = ?"); + } + return StringUtilities.implodeList(Lists.newArrayList(keys), separator); + } + + public Remover constructRemover(Connection connection) throws SQLException { + String deleteSql = "DELETE FROM " + this.toString() + " WHERE " + formatDeleteEquals(connection, " AND "); + final PreparedStatement statement = connection.prepareStatement(deleteSql); + return new Remover() { + public void remove(Map<String, Object> values) throws SQLException { + int index = 1; + for(Object value : ImmutableSortedMap.copyOf(values).values()) { + statement.setObject(index, value); + index++; + } + statement.addBatch(); + } + public int apply() throws SQLException { + int removed = 0; + int[] updates = statement.executeBatch(); + for(int ii = 0; ii < updates.length; ii++) { + removed += updates[ii]; + } + return removed; + } + }; + } } Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ForeignKey.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ForeignKey.java 2010-02-17 23:25:26 UTC (rev 1037) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/ForeignKey.java 2010-02-18 00:09:49 UTC (rev 1038) @@ -1,17 +1,22 @@ package org.cishell.utilities.database; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.SQLException; -import java.sql.Statement; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; -import org.cishell.utilities.DatabaseUtilities; import org.cishell.utilities.StringUtilities; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + public final class ForeignKey { final public DatabaseTable localTable; @@ -21,38 +26,20 @@ public ForeignKey(DatabaseTable localTable, DatabaseTable otherTable, Set<ColumnPair> pairs) { this.localTable = localTable; this.otherTable = otherTable; - this.pairs = Collections.unmodifiableSet(pairs); + this.pairs = ImmutableSet.copyOf(pairs); } - public void repoint(List<Map<String, Object>> from, - Map<String, Object> to, Statement statement) throws SQLException { - - String updateQuery = constructUpdateQuery(from, to); - statement.addBatch(updateQuery); - - } - - private String constructUpdateQuery(List<Map<String, Object>> from, - Map<String, Object> to) { - return "UPDATE " + otherTable.toString() + " SET "+ formatUpdates(to) + " WHERE " - + DatabaseUtilities.createSQLInExpression(getForeignColumnNames(), translateToForeignNames(from)); - } - - private List<Map<String, Object>> translateToForeignNames( - List<Map<String, Object>> from) { - List<Map<String, Object>> output = new ArrayList<Map<String, Object>>(); - for(Map<String, Object> fromValues : from) { - Map<String, Object> toValues = new HashMap<String, Object>(); - for(ColumnPair pair : pairs) { - toValues.put(pair.foreign, fromValues.get(pair.local)); - } - output.add(toValues); - } + private Map<String, Object> translateToForeignNames( + Map<String, Object> from) { + //sorting is necessary to ensure keys and values match up + SortedMap<String, Object> to = Maps.newTreeMap(); + for(ColumnPair pair : pairs) { + to.put(pair.foreign, from.get(pair.local)); + } - - return output; + return to; } private List<String> getForeignColumnNames() { @@ -65,15 +52,38 @@ - private String formatUpdates(Map<String, Object> to) { - List<String> updateStatements = new ArrayList<String>(); + private String formatUpdateEquals(String separator) { + //sorting is necessary to ensure keys and values match up + SortedSet<String> updateStatements = Sets.newTreeSet(); for(ColumnPair pair : pairs) { String foreignColumn = pair.foreign; - Object newValue = to.get(pair.local); - updateStatements.add(foreignColumn + " = " + DatabaseUtilities.formatValue(newValue)); + updateStatements.add(foreignColumn + " = ?"); } - return StringUtilities.implodeList(updateStatements, ", "); + return StringUtilities.implodeList(Lists.newArrayList(updateStatements), separator); } + + public Repointer constructRepointer(Connection connection) throws SQLException { + final PreparedStatement statement = connection.prepareStatement("UPDATE " + otherTable.toString() + " SET " + formatUpdateEquals(", ") + " WHERE " + formatUpdateEquals(" AND ")); + return new Repointer() { + + public void repoint(Map<String, Object> primary, Map<String, Object> secondary) throws SQLException { + int index = 1; + for(Object primaryValue : primary.values()) { + statement.setObject(index, primaryValue); + index++; + } + for(Object secondaryValue : secondary.values()) { + statement.setObject(index, secondaryValue); + index++; + } + statement.addBatch(); + } + + public void apply() throws SQLException { + statement.executeBatch(); + } + }; + } Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Remover.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Remover.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Remover.java 2010-02-18 00:09:49 UTC (rev 1038) @@ -0,0 +1,9 @@ +package org.cishell.utilities.database; + +import java.sql.SQLException; +import java.util.Map; + +public interface Remover { + public void remove(Map<String, Object> values) throws SQLException; + public int apply() throws SQLException; +} Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Repointer.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Repointer.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/database/Repointer.java 2010-02-18 00:09:49 UTC (rev 1038) @@ -0,0 +1,10 @@ +package org.cishell.utilities.database; + +import java.sql.SQLException; +import java.util.Map; + +public interface Repointer { + public void repoint(Map<String, Object> primary, Map<String, Object> secondary) throws SQLException; + + public void apply() throws SQLException; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |