Skip to content

Commit

Permalink
sqlrun: refactoring: removing '_procid' property
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrugz committed Aug 15, 2023
1 parent 656b8f8 commit 4fd0b00
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src_run/tbrugz/sqldump/sqlrun/SQLRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class SQLRun implements tbrugz.sqldump.def.Executor {
List<String> allAuxSuffixes = new ArrayList<String>();

//other/reserved props
static final String PROP_PROCID = "_procid";
//static final String PROP_PROCID = "_procid"; //XXX: remove

public enum ProcType {
EXEC, ASSERT;
Expand Down Expand Up @@ -301,15 +301,15 @@ boolean doExec(String procId, int sqlrunCounter) throws IOException, SQLExceptio
log.warn("no exec suffix for key '"+key+"' [id="+procId+",action="+action+"]");
return false;
}
papp.setProperty(PROP_PROCID, procId);
//papp.setProperty(PROP_PROCID, procId);

boolean splitBySemicolon = Utils.getPropBool(papp, Constants.PREFIX_EXEC+procId+SUFFIX_SPLIT, true);

// .file
if(key.endsWith(SUFFIX_FILE)) {
try {
setExecProperties(srproc, papp, execFailOnError);
srproc.execFile(execValue, Constants.PREFIX_EXEC+procId+SUFFIX_LOGINVALIDSTATEMENTS, splitBySemicolon);
srproc.execFile(execValue, procId, splitBySemicolon);
}
catch(FileNotFoundException e) {
log.warn("file not found: "+e);
Expand Down Expand Up @@ -347,7 +347,7 @@ else if(key.endsWith(SUFFIX_FILES_REGEX)) {
if(files!=null && files.size()>0) {
for(String file: files) {
if((fileCount>0) && (commitStrategy==CommitStrategy.FILE)) { doCommit(); }
srproc.execFile(file, Constants.PREFIX_EXEC+procId+SUFFIX_LOGINVALIDSTATEMENTS, splitBySemicolon);
srproc.execFile(file, procId, splitBySemicolon);
fileCount++;
}
log.info(fileCount + " files processed");
Expand All @@ -365,7 +365,7 @@ else if(key.endsWith(SUFFIX_FILES_REGEX)) {
else if(key.endsWith(SUFFIX_STATEMENT)) {
setExecProperties(srproc, papp, execFailOnError);
@SuppressWarnings("unused")
int urows = srproc.execStatement(execValue);
int urows = srproc.execStatement(execValue, procId);
}
// .import
else if(key.endsWith(Constants.SUFFIX_IMPORT)) {
Expand Down Expand Up @@ -455,7 +455,7 @@ boolean doAssert(String procId, int sqlrunCounter) throws IOException, SQLExcept
log.warn("no assert suffix for key '"+key+"' [id="+procId+",action="+action+"]");
return false;
}
papp.setProperty(PROP_PROCID, procId);
//papp.setProperty(PROP_PROCID, procId);

String sql = null;

Expand Down
33 changes: 17 additions & 16 deletions src_run/tbrugz/sqldump/sqlrun/StmtProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public class StmtProc extends AbstractFailable implements Executor {
Statement batchStmt = null;

//@SuppressWarnings("deprecation")
public void execFile(String filePath, String errorLogKey, boolean split) throws IOException {
setupProperties();
public void execFile(String filePath, String procId, boolean split) throws IOException {
String errorLogKey = Constants.PREFIX_EXEC + procId + SQLRun.SUFFIX_LOGINVALIDSTATEMENTS;
setupProperties(procId);
//String errorLogFilePath = papp.getProperty(errorLogKey);
File file = new File(filePath);
//FIXedME: SQLStmtTokenizer not working (on big files?)
Expand Down Expand Up @@ -127,7 +128,7 @@ public void execFile(String filePath, String errorLogKey, boolean split) throws
//replacing ${...} parameters
stmtStr = ParametrizedProperties.replaceProps(stmtStr, papp);
}
urowsTotal += execStatementInternal(stmtStr);
urowsTotal += execStatementInternal(stmtStr, procId);
countOk++;
}
catch(SQLException e) {
Expand Down Expand Up @@ -214,12 +215,12 @@ Writer getErrorLogHandler(String errorLogKey) {
return logerror;
}

public int execStatement(String stmtStr) throws IOException {
setupProperties();
public int execStatement(String stmtStr, String procId) throws IOException {
setupProperties(procId);
try {
long initTime = System.currentTimeMillis();

int urows = execStatementInternal(stmtStr);
int urows = execStatementInternal(stmtStr, procId);
urows += closeStatement();

long totalTime = System.currentTimeMillis() - initTime;
Expand All @@ -236,7 +237,7 @@ public int execStatement(String stmtStr) throws IOException {
}
}

int execStatementInternal(String stmtStr) throws IOException, SQLException {
int execStatementInternal(String stmtStr, String procId) throws IOException, SQLException {
if(stmtStr==null) { throw new IllegalArgumentException("null parameter"); }
stmtStr = stmtStr.trim();
if(stmtStr.equals("")) { throw new IllegalArgumentException("null parameter"); }
Expand All @@ -253,7 +254,7 @@ int execStatementInternal(String stmtStr) throws IOException, SQLException {
if(batchStmt==null) {
batchStmt = conn.createStatement();
}
batchStmt.addBatch(replaceParameters(stmtStr));
batchStmt.addBatch(replaceParameters(stmtStr, procId));
batchExecCounter++;

if((batchExecCounter%batchSize)==0) {
Expand All @@ -272,7 +273,7 @@ int execStatementInternal(String stmtStr) throws IOException, SQLException {
if(usePreparedStatement){
PreparedStatement stmt = conn.prepareStatement(stmtStr);
try {
setParameters(stmt);
setParameters(stmt, procId);
updateCount = stmt.executeUpdate();
//if(log.isInfoEnabled()) { SQLUtils.logWarningsInfo(stmt.getWarnings(), log); }
}
Expand All @@ -283,7 +284,7 @@ int execStatementInternal(String stmtStr) throws IOException, SQLException {
else {
Statement stmt = conn.createStatement();
try {
updateCount = stmt.executeUpdate(replaceParameters(stmtStr));
updateCount = stmt.executeUpdate(replaceParameters(stmtStr, procId));
//if(log.isInfoEnabled()) { SQLUtils.logWarningsInfo(stmt.getWarnings(), log); }
}
finally {
Expand Down Expand Up @@ -312,10 +313,10 @@ else if(logUpdates.isDebugEnabled() && updateCount>0) {
}
}

void setParameters(PreparedStatement stmt) throws SQLException {
void setParameters(PreparedStatement stmt, String procId) throws SQLException {
int i=1;
while(true) {
String key = Constants.PREFIX_EXEC+papp.getProperty(SQLRun.PROP_PROCID)+SQLRun.SUFFIX_PARAM+"."+i;
String key = Constants.PREFIX_EXEC+procId+SQLRun.SUFFIX_PARAM+"."+i;
String param = papp.getProperty(key);
if(param!=null) {
log.debug("param #"+i+"/"+key+": "+param);
Expand All @@ -328,11 +329,11 @@ void setParameters(PreparedStatement stmt) throws SQLException {

static final String questionMarkPattern = Pattern.quote("?");

String replaceParameters(String stmt) throws SQLException {
String replaceParameters(String stmt, String procId) throws SQLException {
int i=1;
String retStmt = stmt;
while(true) {
String key = Constants.PREFIX_EXEC+papp.getProperty(SQLRun.PROP_PROCID)+SQLRun.SUFFIX_PARAM+"."+i;
String key = Constants.PREFIX_EXEC+procId+SQLRun.SUFFIX_PARAM+"."+i;
String param = papp.getProperty(key);
if(param!=null) {
log.debug("param #"+i+"/"+key+": "+param);
Expand All @@ -343,12 +344,12 @@ String replaceParameters(String stmt) throws SQLException {
}
}

void setupProperties() {
void setupProperties(String execId) {
if(papp==null) {
log.warn("null properties!");
return;
}
String execId = papp.getProperty(SQLRun.PROP_PROCID);
//String execId = papp.getProperty(SQLRun.PROP_PROCID);
String prefix = Constants.PREFIX_EXEC + execId + ".";
useBatchUpdate = Utils.getPropBool(papp, prefix + Constants.SUFFIX_BATCH_MODE, DEFAULT_USE_BATCH_UPDATE);
batchSize = Utils.getPropLong(papp, prefix + Constants.SUFFIX_BATCH_SIZE, DEFAULT_BATCH_SIZE);
Expand Down
4 changes: 2 additions & 2 deletions src_test/tbrugz/sqldump/sqlrun/StmtProcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public void testReplaceParameters() throws SQLException {
StmtProc proc = new StmtProc();
Properties prop = new Properties();
prop.setProperty("sqlrun.exec.a.param.1", "x");
prop.setProperty("_procid", "a");
//prop.setProperty("_procid", "a");
proc.setProperties(prop);
String replaced = proc.replaceParameters("abc ? cde");
String replaced = proc.replaceParameters("abc ? cde", "a");
Assert.assertEquals("abc x cde", replaced);
}
}

0 comments on commit 4fd0b00

Please sign in to comment.