tbrugz.sqldiff.datadiff.SQLDataDiffSyntax Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sqldump Show documentation
Show all versions of sqldump Show documentation
Utility to dump schema and data from a RDBMS
package tbrugz.sqldiff.datadiff;
import java.io.IOException;
import java.io.Writer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import tbrugz.sqldump.datadump.DataDumpUtils;
import tbrugz.sqldump.datadump.InsertIntoDataDump;
import tbrugz.sqldump.util.SQLUtils;
import tbrugz.sqldump.util.Utils;
public class SQLDataDiffSyntax extends InsertIntoDataDump implements DiffSyntax {
static final Log log = LogFactory.getLog(SQLDataDiffSyntax.class);
static final String SYNTAX_ID = "sqldatadiff";
static final String DATADIFF_PREFIX = "sqldiff.datadiff";
static final String PROP_DATADIFF_ADDCOMMENTS = DATADIFF_PREFIX+".addcomments";
boolean shouldFlush = false;
boolean addComments = false;
@Override
public void procProperties(Properties prop) {
super.procProperties(prop);
addComments = Utils.getPropBool(prop, PROP_DATADIFF_ADDCOMMENTS, addComments);
}
@Override
public String getSyntaxId() {
return SYNTAX_ID;
}
@Override
public void dumpRow(ResultSet rs, long count, Writer fos)
throws IOException, SQLException {
super.dumpRow(rs, count, fos);
if(shouldFlush) { fos.flush(); }
}
//XXX: option to select update strategy: updatealways/if modified, update changed cols/all cols ?
//XXX: rename to something like 'dumpRowWithMatchedKey' ?
@Override
public boolean dumpUpdateRowIfNotEquals(ResultSet rsSource,
ResultSet rsTarget, long count, boolean alsoDumpIfEquals, Writer w) throws IOException,
SQLException {
List valsS = (List) DataDumpUtils.values4sql( SQLUtils.getRowObjectListFromRS(rsSource, lsColTypes, numCol), dateFormatter );
List valsT = (List) DataDumpUtils.values4sql( SQLUtils.getRowObjectListFromRS(rsTarget, lsColTypes, numCol), dateFormatter );
List changedCols = getChangedCols(lsColNames, valsS, valsT);
if(changedCols.size()>0) {
//dumpUpdateRowInternal(rsTarget, null, count, w); //updates all cols
dumpUpdateRowInternal(valsS, valsT, changedCols, count, w); //updates changed cols
return true;
}
else {
//XXX: alsoDumpIfEquals??
return false;
}
}
@Override
public void dumpUpdateRow(ResultSet rsSource, ResultSet rsTarget,
long count, Writer w) throws IOException, SQLException {
//dumpUpdateRowInternal(rsTarget, null, count, w);
//XXX: valsS: not needed?
List valsS = (List) DataDumpUtils.values4sql( SQLUtils.getRowObjectListFromRS(rsSource, lsColTypes, numCol), dateFormatter );
List valsT = (List) DataDumpUtils.values4sql( SQLUtils.getRowObjectListFromRS(rsTarget, lsColTypes, numCol), dateFormatter );
dumpUpdateRowInternal(valsS, valsT, null, count, w);
}
@Override
public void dumpDeleteRow(ResultSet rs, long count, Writer w)
throws IOException, SQLException {
List vals = (List) DataDumpUtils.values4sql( SQLUtils.getRowObjectListFromRS(rs, lsColTypes, numCol), dateFormatter );
List wheres = new ArrayList();
for(int i = 0;i colsToUpdate, long count, Writer w) throws IOException, SQLException {
List vals = (List) DataDumpUtils.values4sql( SQLUtils.getRowObjectListFromRS(rs, lsColTypes, numCol), dateFormatter );
List sets = new ArrayList();
List wheres = new ArrayList();
for(int i = 0;i valsS, List valsT, List colsToUpdate, long count, Writer w) throws IOException, SQLException {
List sets = new ArrayList();
List wheres = new ArrayList();
List comments = new ArrayList();
for(int i = 0;i getChangedCols(List lsColNames, List vals1, List vals2) {
int comp = 0;
int size1 = vals1.size();
int size2 = vals2.size();
if(size1!=size2) {
log.warn("different list size: vals1="+vals1+" ; vals2="+vals2);
return null;
}
List changedColNames = new ArrayList();
for(int i=0;i