dotnet_runner/runner.cs

174 lines
6.4 KiB
C#
Raw Normal View History

2018-08-15 14:34:31 -04:00
using System;
using Npgsql;
using FileHelpers;
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-15 14:34:31 -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;
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-15 23:26:00 -04:00
var ibmdr = ibmcmd.ExecuteReader();
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 = "";
for ( int i = 0 ; i < ibmdr.GetValues(getv);i++) {
if (getv[i] != null) {
switch (ibmdr.GetDataTypeName(i)){
case "VARCHAR":
nc = "'" + getv[i].ToString().Replace("'","''") + "'";
break;
case "CHAR":
nc = "'" + getv[i].ToString().Replace("'","''") + "'";
break;
case "DATE":
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 {
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;
try {
pgcom.ExecuteNonQuery();
}
catch (Exception e) {
Console.Write(Environment.NewLine);
Console.Write(e.Message);
ibmc.Close();
pgc.Close();
}
2018-08-16 02:42:48 -04:00
sql = "";
}
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;
try {
pgcom.ExecuteNonQuery();
}
catch (Exception e) {
Console.Write(Environment.NewLine);
Console.Write(e.Message);
ibmc.Close();
2018-08-16 12:23:18 -04:00
pgt.Rollback();
pgc.Close();
2018-08-16 12:23:18 -04:00
}
2018-08-16 02:42:48 -04:00
sql = "";
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
}
}
}