2018-08-15 14:34:31 -04:00
|
|
|
|
using System;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2018-08-17 16:22:24 -04:00
|
|
|
|
|
|
|
|
|
string scs = ""; //= "Driver={iSeries Access ODBC Driver};System=S7830956;Uid=PTROWBRIDG;Pwd=QQQX53@027";
|
|
|
|
|
//string scs = ""; //= "Driver={iSeries Access ODBC Driver};System=TEST400;Uid=tstdillen;Pwd=tstdillen"
|
|
|
|
|
string dcs = ""; //= "Host=ushcc10091;Port=5432;Username=ptrowbridge;Password=qqqx53!026;Database=ubm;ApplicationName=runner";
|
|
|
|
|
string sq = ""; //= System.IO.File.ReadAllText(@"C:\Users\ptrowbridge\Documents\runner\osm.sql");
|
|
|
|
|
string dt = ""; //= "rlarp.omsi";
|
|
|
|
|
int r = 0;
|
|
|
|
|
int t = 0;
|
|
|
|
|
string sql = "";
|
|
|
|
|
string nr = "";
|
|
|
|
|
string nc = "";
|
2018-08-21 09:41:51 -04:00
|
|
|
|
string nl = Environment.NewLine;
|
2018-08-15 14:34:31 -04:00
|
|
|
|
|
2018-08-17 16:34:37 -04:00
|
|
|
|
string msg = "Help:";
|
2018-08-21 15:56:54 -04:00
|
|
|
|
msg = msg + nl + "version 0.15";
|
2018-08-21 09:41:51 -04:00
|
|
|
|
msg = msg + nl + "-scs source connection string";
|
|
|
|
|
msg = msg + nl + "-dcs destination connection string";
|
|
|
|
|
msg = msg + nl + "-sq path to source query";
|
|
|
|
|
msg = msg + nl + "-dt fully qualified name of destination table";
|
|
|
|
|
msg = msg + nl + "--help info";
|
2018-08-17 16:34:37 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
//---------------------------------------parse args into variables-------------------------------------------------
|
2018-08-15 14:34:31 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
for (int i = 0; i < args.Length; i = i +1 ){
|
|
|
|
|
switch (args[i]) {
|
|
|
|
|
//source connection string
|
|
|
|
|
case "-scs":
|
|
|
|
|
scs = args[i+1];
|
|
|
|
|
break;
|
|
|
|
|
//destination connection string
|
|
|
|
|
case "-dcs":
|
|
|
|
|
dcs = args[i+1];
|
|
|
|
|
break;
|
|
|
|
|
//source query path
|
|
|
|
|
case "-sq":
|
|
|
|
|
sq = System.IO.File.ReadAllText(args[i+1]);
|
|
|
|
|
break;
|
|
|
|
|
//destination table name
|
|
|
|
|
case "-dt":
|
|
|
|
|
dt = "INSERT INTO " + args[i+1] + " VALUES ";
|
|
|
|
|
break;
|
2018-08-17 16:34:37 -04:00
|
|
|
|
case "--help":
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(msg);
|
|
|
|
|
return;
|
|
|
|
|
case "-help":
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(msg);
|
|
|
|
|
return;
|
|
|
|
|
case "-h":
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(msg);
|
|
|
|
|
return;
|
|
|
|
|
case @"\?":
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(msg);
|
|
|
|
|
return;
|
2018-08-17 16:22:24 -04:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(scs);
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(dcs);
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(sq);
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(dt);
|
2018-08-15 23:26:00 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
//return;
|
2018-08-15 23:26:00 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
//-------------------------------------------establish connections-------------------------------------------------
|
2018-08-15 23:26:00 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
var ibmc = new System.Data.Odbc.OdbcConnection(scs);
|
|
|
|
|
var pgc = new NpgsqlConnection(dcs);
|
|
|
|
|
ibmc.Open();
|
|
|
|
|
pgc.Open();
|
2018-08-15 14:34:31 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
//----------------------------------------------setup commands---------------------------------------------------
|
2018-08-15 14:34:31 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
var ibmcmd = new System.Data.Odbc.OdbcCommand(sq,ibmc);
|
|
|
|
|
var pgcom = pgc.CreateCommand();
|
2018-08-15 14:34:31 -04:00
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
//---------------------------------------------begin transaction---------------------------------------------------------
|
2018-08-16 02:42:48 -04:00
|
|
|
|
|
|
|
|
|
Console.Write(Environment.NewLine);
|
2018-08-17 16:22:24 -04:00
|
|
|
|
Console.Write("etl start:" + DateTime.Now.ToString());
|
2018-08-16 12:23:18 -04:00
|
|
|
|
NpgsqlTransaction pgt = pgc.BeginTransaction();
|
2018-08-21 09:41:51 -04:00
|
|
|
|
ibmcmd.CommandTimeout = 6000;
|
2018-08-20 11:23:22 -04:00
|
|
|
|
System.Data.Odbc.OdbcDataReader ibmdr;
|
|
|
|
|
try {
|
|
|
|
|
ibmdr = ibmcmd.ExecuteReader();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write("error on source sql:");
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(e.Message);
|
|
|
|
|
ibmc.Close();
|
|
|
|
|
pgc.Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-08-17 16:22:24 -04:00
|
|
|
|
//setup getv object array dimensioned to number of columns for scenario
|
2018-08-16 02:42:48 -04:00
|
|
|
|
var getv = new object[ibmdr.FieldCount];
|
2018-08-17 16:22:24 -04:00
|
|
|
|
|
2018-08-16 02:42:48 -04:00
|
|
|
|
while (ibmdr.Read()) {
|
|
|
|
|
r = r + 1;
|
2018-08-16 11:59:13 -04:00
|
|
|
|
t = t +1 ;
|
2018-08-16 02:42:48 -04:00
|
|
|
|
nr = "";
|
2018-08-21 15:56:54 -04:00
|
|
|
|
var dtn = new string[ibmdr.FieldCount];
|
|
|
|
|
//populate all the data type names into a string array instead of calling against ibmdr in every iteration
|
|
|
|
|
if (t ==1 ) {
|
2018-08-21 16:13:28 -04:00
|
|
|
|
for (int i = 0; i < ibmdr.GetValues(getv); i++){
|
2018-08-21 15:56:54 -04:00
|
|
|
|
dtn[i] = ibmdr.GetDataTypeName(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-16 02:42:48 -04:00
|
|
|
|
for ( int i = 0 ; i < ibmdr.GetValues(getv);i++) {
|
|
|
|
|
if (getv[i] != null) {
|
2018-08-21 15:56:54 -04:00
|
|
|
|
switch (dtn[i]){
|
2018-08-16 02:42:48 -04:00
|
|
|
|
case "VARCHAR":
|
|
|
|
|
nc = "'" + getv[i].ToString().Replace("'","''") + "'";
|
|
|
|
|
break;
|
|
|
|
|
case "CHAR":
|
|
|
|
|
nc = "'" + getv[i].ToString().Replace("'","''") + "'";
|
|
|
|
|
break;
|
|
|
|
|
case "DATE":
|
2018-08-16 12:22:57 -04:00
|
|
|
|
if (getv[i].ToString() == "1/1/0001 12:00:00 AM" || getv[i].ToString() == "") {
|
|
|
|
|
nc = "NULL";
|
2018-08-16 02:42:48 -04:00
|
|
|
|
}
|
|
|
|
|
else {
|
2018-08-16 12:22:57 -04:00
|
|
|
|
nc = "'" + getv[i].ToString() + "'";
|
2018-08-16 02:42:48 -04:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "TIME":
|
|
|
|
|
nc = "'" + getv[i].ToString() + "'";
|
|
|
|
|
break;
|
|
|
|
|
case "TIMESTAMP":
|
|
|
|
|
nc = "'" + getv[i].ToString() + "'";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2018-08-16 12:01:10 -04:00
|
|
|
|
if (getv[i].ToString() != "") {
|
|
|
|
|
nc = getv[i].ToString();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
nc = "NULL";
|
|
|
|
|
}
|
2018-08-16 02:42:48 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
nc = "NULL";
|
|
|
|
|
}
|
|
|
|
|
if (i!=0) {
|
|
|
|
|
nr = nr + ",";
|
|
|
|
|
}
|
|
|
|
|
nr = nr + nc;
|
2018-08-15 14:34:31 -04:00
|
|
|
|
}
|
2018-08-16 02:42:48 -04:00
|
|
|
|
if (sql!="") {
|
|
|
|
|
sql = sql + ",";
|
|
|
|
|
}
|
|
|
|
|
sql = sql + "(" + nr + ")";
|
2018-08-16 11:59:13 -04:00
|
|
|
|
if (r == 250) {
|
2018-08-16 02:42:48 -04:00
|
|
|
|
r = 0;
|
2018-08-17 16:22:24 -04:00
|
|
|
|
sql = dt + sql;
|
2018-08-16 11:59:13 -04:00
|
|
|
|
pgcom.CommandText = sql;
|
2018-08-16 12:22:57 -04:00
|
|
|
|
try {
|
|
|
|
|
pgcom.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(e.Message);
|
2018-08-20 12:04:05 -04:00
|
|
|
|
System.IO.File.WriteAllText(@"C:\Users\ptrowbridge\Downloads\runner_error.sql",sql);
|
2018-08-16 12:22:57 -04:00
|
|
|
|
ibmc.Close();
|
2018-08-20 11:23:56 -04:00
|
|
|
|
pgt.Rollback();
|
2018-08-16 12:22:57 -04:00
|
|
|
|
pgc.Close();
|
2018-08-20 11:43:54 -04:00
|
|
|
|
return;
|
2018-08-16 12:22:57 -04:00
|
|
|
|
}
|
2018-08-16 02:42:48 -04:00
|
|
|
|
sql = "";
|
2018-08-21 16:03:49 -04:00
|
|
|
|
Console.Write(nl + t.ToString());
|
2018-08-16 02:42:48 -04:00
|
|
|
|
}
|
2018-08-15 14:34:31 -04:00
|
|
|
|
}
|
2018-08-16 02:42:48 -04:00
|
|
|
|
if (r != 0) {
|
2018-08-17 16:22:24 -04:00
|
|
|
|
sql = dt + sql;
|
2018-08-16 11:59:13 -04:00
|
|
|
|
pgcom.CommandText = sql;
|
2018-08-16 12:22:57 -04:00
|
|
|
|
try {
|
|
|
|
|
pgcom.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
Console.Write(e.Message);
|
2018-08-20 11:43:54 -04:00
|
|
|
|
System.IO.File.WriteAllText(@"C:\Users\ptrowbridge\Downloads\runner_error.sql",sql);
|
2018-08-20 11:23:22 -04:00
|
|
|
|
//ibmc.Close();
|
2018-08-16 12:23:18 -04:00
|
|
|
|
pgt.Rollback();
|
2018-08-16 12:22:57 -04:00
|
|
|
|
pgc.Close();
|
2018-08-20 11:43:54 -04:00
|
|
|
|
return;
|
2018-08-16 12:22:57 -04:00
|
|
|
|
}
|
2018-08-21 16:03:49 -04:00
|
|
|
|
sql = "";
|
|
|
|
|
Console.Write(nl + t.ToString());
|
2018-08-15 14:34:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-16 12:23:18 -04:00
|
|
|
|
pgt.Commit();
|
2018-08-15 14:34:31 -04:00
|
|
|
|
ibmc.Close();
|
|
|
|
|
pgc.Close();
|
|
|
|
|
|
2018-08-17 16:22:24 -04:00
|
|
|
|
Console.Write(Environment.NewLine);
|
2018-08-16 02:42:48 -04:00
|
|
|
|
Console.Write("etl end:" + DateTime.Now.ToString());
|
2018-08-15 14:34:31 -04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|