Compare commits

...

16 Commits

4 changed files with 175 additions and 49 deletions

View File

@ -26,6 +26,8 @@ dependencies {
//jdbc drivers
implementation 'org.postgresql:postgresql:42.5.0'
implementation 'net.sf.jt400:jt400:11.0'
implementation 'com.microsoft.sqlserver:mssql-jdbc:9.2.0.jre8'
implementation 'com.microsoft.sqlserver:mssql-jdbc_auth:9.2.0.x64'
}
application {

View File

@ -4,6 +4,7 @@ import java.util.*;
import java.nio.file.Files;
import java.nio.file.Path ;
import java.nio.file.Paths;
import java.time.*;
public class jrunner {
//static final String QUERY = "SELECT * from rlarp.osm LIMIT 100";
@ -19,6 +20,7 @@ public class jrunner {
String sq = "";
String dt = "";
Boolean trim = true;
Boolean clear = true;
Integer r = 0;
Integer t = 0;
String sql = "";
@ -34,8 +36,10 @@ public class jrunner {
String[] getv = null;
Integer cols = null;
String[] dtn = null;
Timestamp tsStart = null;
Timestamp tsEnd = null;
msg = "jrunner version 0.32";
msg = "jrunner version 0.41";
msg = msg + nl + "-scu source jdbc url";
msg = msg + nl + "-scn source username";
msg = msg + nl + "-scp source passowrd";
@ -45,6 +49,7 @@ public class jrunner {
msg = msg + nl + "-sq path to source query";
msg = msg + nl + "-dt fully qualified name of destination table";
msg = msg + nl + "-t trim text";
msg = msg + nl + "-c clear target table";
msg = msg + nl + "--help info";
//---------------------------------------parse args into variables-------------------------------------------------
@ -60,6 +65,7 @@ public class jrunner {
scn = args[i+1];
break;
//source password
//import java.time.*;
case "-scp":
scp = args[i+1];
break;
@ -95,6 +101,9 @@ public class jrunner {
case "-t":
trim = true;
break;
case "-c":
clear = true;
break;
case "-v":
System.out.println(msg);
return;
@ -140,6 +149,7 @@ public class jrunner {
//-------------------------------------------establish connections-------------------------------------------------
//source database
System.out.println("------------open database connections---------------------");
try {
scon = DriverManager.getConnection(scu, scn, scp);
} catch (SQLException e) {
@ -147,18 +157,22 @@ public class jrunner {
e.printStackTrace();
System.exit(0);
}
System.out.println(" ✅ source database");
//destination database
try {
dcon = DriverManager.getConnection(dcu, dcn, dcp);
} catch (SQLException e) {
System.out.println("issue connecting to desctination:");
System.out.println("issue connecting to destination:");
e.printStackTrace();
System.exit(0);
}
System.out.println(" ✅ destination database");
//----------------------------------------open resultset------------------------------------------------------------
System.out.println("issue connecting to desctination:");
try {
stmt = scon.createStatement();
stmt.setFetchSize(10000);
tsStart = Timestamp.from(Instant.now());
System.out.println(tsStart);
rs = stmt.executeQuery(sq);
//while (rs.next()) {
// System.out.println(rs.getString("x"));
@ -183,65 +197,118 @@ public class jrunner {
try {
for (int i = 1; i <= cols; i++){
dtn[i] = rs.getMetaData().getColumnTypeName(i);
System.out.println(rs.getMetaData().getColumnName(i) + ": " + dtn[i]);
System.out.println(" * " + rs.getMetaData().getColumnName(i) + ": " + dtn[i]);
}
} catch (SQLException e) {
e.printStackTrace();
System.exit(0);
}
//-------------------------clear the target table if requeted----------------------------------------------------
if (clear) {
System.out.println("------------clear target table----------------------------");
sql = "DELETE FROM " + dt;
try {
stmtd = dcon.createStatement();
System.out.println(" " + sql);
stmtd.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(sql);
System.exit(0);
}
}
System.out.println("------------row count-------------------------------------");
//-------------------------------build & execute sql-------------------------------------------------------------
try {
sql = "";
while (rs.next()) {
r++;
t++;
nr = "";
for (int i = 1; i <= cols; i++){
nc = rs.getString(i);
if (nc == null) {
nc = "NULL";
}
else {
if (rs.wasNull()) {
nc = "NULL";
} else {
switch (dtn[i]){
case "VARCHAR":
nc = rs.getString(i).replace("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "CLOB":
nc = rs.getString(i).replace("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "CHAR":
nc = rs.getString(i).replace("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "DATE":
nc = "'" + rs.getString(i) + "'";
if (nc == "'1/1/0001 12:00:00 AM'") {
nc = "NULL";
}
break;
case "TIME":
nc = "'" + rs.getString(i).replace("'","''") + "'";
break;
case "BIGINT":
nc = rs.getString(i);
default:
if (rs.getString(i) != "") {
nc = rs.getString(i);
}
else {
nc = "NULL";
}
break;
switch (dtn[i].toUpperCase()){
case "VARCHAR":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
}
nc = nc.replaceAll("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "TEXT":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
nc = nc.replaceAll("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "CHAR":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
nc = nc.replaceAll("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "CLOB":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
nc = nc.replaceAll("'","''");
if (trim) { nc = nc.trim();}
nc = "'" + nc + "'";
break;
case "DATE":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
nc = "'" + nc + "'";
if (nc == "'1/1/0001 12:00:00 AM'") {
nc = "NULL";
}
break;
case "TIME":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
nc.replaceAll("'","''");
nc = "'" + nc + "'";
break;
case "TIMESTAMP":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
nc.replaceAll("'","''");
nc = "'" + nc + "'";
break;
case "BIGINT":
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
default:
nc = rs.getString(i);
if (rs.wasNull() || nc == null) {
nc = "NULL";
break;
}
break;
}
if (i != 1){
nr = nr + ",";
@ -297,6 +364,12 @@ public class jrunner {
System.out.println("issue closing connections");
e.printStackTrace();
}
System.out.println("");
System.out.println(" rows written");
tsEnd = Timestamp.from(Instant.now());
System.out.println(tsStart);
System.out.println(tsEnd);
//long time = Duration.between(tsStart, tsEnd).toMillis();
//System.out.println("time elapsed: " + time);
System.out.println();
}
}

6
copy_to_apt.sh Executable file
View File

@ -0,0 +1,6 @@
sudo cp app/build/distributions/app.zip /opt/
sudo rm -rf /opt/app/
sudo unzip /opt/app.zip -d /opt/
sudo rm /opt/app.zip
sudo chown $USER:$USER -R /opt/app/
export JR="/opt/app/bin/app"

45
readme.md Normal file
View File

@ -0,0 +1,45 @@
## install java jdk.
find downloads page and get latest tarball.
https://www.oracle.com/java/technologies/downloads/
```
wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz
tar -xvf downloaded_file
```
move the extracted folder to /opt
put the extracted location in your path variable
```
export JAVA_HOME=/opt/jdk-19.0.1
export PATH=$PATH:$JAVA_HOME/bin
```
`java --version` to test
## install gradle
https://docs.gradle.org/current/userguide/installation.html
go to gradle.org and find the latest bin.zip file (burried in the `direct link` hyperlink)
```
wget https://services.gradle.org/distributions/gradle-7.6-bin.zip
unzip -d /opt/gradle gradle-7.6-bin.zip
```
point to the gradle install
```
export PATH=$PATH:/opt/gradle/gradle-7.6/bin
gradle -v` to validate
```
## clone this repo
clone
`git clone https://gitea.hptrow.me/pt/jrunner.git`
build
gradle build`
copy to opt for use
```
sudo cp app/build/distributions/app.zip /opt/
sudo rm -rf /opt/app/
sudo unzip /opt/app.zip -d /opt/
sudo rm /opt/app.zip
sudo chown ptrowbridge:ptrowbridge -R /opt/app/
```