dotnet_runner/runner.cs

289 lines
11 KiB
C#
Raw Normal View History

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
2018-09-11 11:37:38 -04:00
string scs = "";
string dcs = "";
string sq = "";
string dt = "";
2018-08-23 13:38:58 -04:00
Boolean trim = false;
2018-08-17 16:22:24 -04:00
int r = 0;
int t = 0;
string sql = "";
string nr = "";
string nc = "";
string nl = Environment.NewLine;
2018-09-20 11:50:16 -04:00
System.Data.Odbc.OdbcConnection ibmc = new System.Data.Odbc.OdbcConnection();
NpgsqlConnection pgc = new NpgsqlConnection();
2018-08-15 14:34:31 -04:00
2018-08-17 16:34:37 -04:00
string msg = "Help:";
2019-04-30 12:05:23 -04:00
msg = msg + nl + "version 0.25";
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";
2018-08-23 13:38:58 -04:00
msg = msg + nl + "-t trim text";
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":
2018-09-20 11:39:15 -04:00
try {
sq = System.IO.File.ReadAllText(args[i+1]);
}
catch (Exception e) {
Console.Write(nl + "error reasing source sql file: " + e.Message);
return;
}
2018-08-17 16:22:24 -04:00
break;
//destination table name
case "-dt":
dt = "INSERT INTO " + args[i+1] + " VALUES ";
break;
2018-08-23 13:38:58 -04:00
case "-t":
trim = true;
break;
2018-08-17 16:34:37 -04:00
case "--help":
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:34:37 -04:00
Console.Write(msg);
return;
case "-help":
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:34:37 -04:00
Console.Write(msg);
return;
case "-h":
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:34:37 -04:00
Console.Write(msg);
return;
case @"\?":
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:34:37 -04:00
Console.Write(msg);
return;
2018-08-17 16:22:24 -04:00
default:
break;
}
}
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:22:24 -04:00
Console.Write(scs);
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:22:24 -04:00
Console.Write(dcs);
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:22:24 -04:00
Console.Write(sq);
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-17 16:22:24 -04:00
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-09-20 11:39:15 -04:00
try {
2018-09-20 11:50:16 -04:00
ibmc.ConnectionString = scs;
2018-09-20 11:39:35 -04:00
}
catch (Exception e) {
Console.Write(nl + "bad source connection string: " + e.Message);
2018-09-20 11:56:04 -04:00
return;
2018-09-20 11:39:35 -04:00
}
try {
2018-09-20 11:50:16 -04:00
pgc.ConnectionString = dcs;
2018-09-20 11:39:35 -04:00
}
catch (Exception e) {
Console.Write(nl + "bad source connection string: " + e.Message);
2018-09-20 11:56:04 -04:00
return;
2018-09-20 11:39:35 -04:00
}
try {
ibmc.Open();
2018-09-20 11:39:15 -04:00
}
catch (Exception e) {
Console.Write(nl + "issue connection to source: " + e.Message);
return;
}
try {
2018-09-20 11:39:35 -04:00
pgc.Open();
2018-09-20 11:39:15 -04:00
}
catch (Exception e) {
ibmc.Close();
Console.Write(nl + "issue connecting to destination: "+ e.Message);
}
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
2018-09-20 11:39:15 -04:00
Console.Write(nl);
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();
ibmcmd.CommandTimeout = 6000;
2018-08-20 11:23:22 -04:00
System.Data.Odbc.OdbcDataReader ibmdr;
try {
ibmdr = ibmcmd.ExecuteReader();
}
catch (Exception e) {
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-20 11:23:22 -04:00
Console.Write("error on source sql:");
2018-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-20 11:23:22 -04:00
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-21 16:21:21 -04:00
//getv hold an array of datareader row
2018-08-16 02:42:48 -04:00
var getv = new object[ibmdr.FieldCount];
int cols = ibmdr.FieldCount;
2018-08-21 16:21:21 -04:00
//dtn holds list of data types per column
var dtn = new string[ibmdr.FieldCount];
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 = "";
//populate all the data type names into a string array instead of calling against ibmdr in every iteration
2018-08-21 16:21:21 -04:00
if (t == 1 ) {
for (int i = 0; i < cols; i++){
dtn[i] = ibmdr.GetDataTypeName(i);
}
}
for (int i = 0 ; i < cols;i++) {
//Console.Write(nl);
//Console.Write(DBNull.Value.Equals(ibmdr.GetValue(i)).ToString());
//Console.Write(ibmdr.GetValue(i).ToString());
Boolean dnull = false;
if (dtn[i] == "BIGINT") {
dnull = DBNull.Value.Equals(ibmdr.GetInt64(i));
}
else {
dnull = DBNull.Value.Equals(ibmdr.GetValue(i));
}
if (! dnull) {
switch (dtn[i]){
2018-08-16 02:42:48 -04:00
case "VARCHAR":
2018-08-23 13:38:58 -04:00
if (trim) {
nc = "'" + ibmdr.GetValue(i).ToString().Replace("'","''").TrimEnd() + "'";
2018-08-23 13:38:58 -04:00
}
else {
nc = "'" + ibmdr.GetValue(i).ToString().Replace("'","''") + "'";
2018-08-23 13:38:58 -04:00
}
2018-08-16 02:42:48 -04:00
break;
case "CHAR":
2018-08-23 13:38:58 -04:00
if (trim) {
nc = "'" + ibmdr.GetValue(i).ToString().Replace("'","''").TrimEnd() + "'";
2018-08-23 13:38:58 -04:00
}
else {
nc = "'" + ibmdr.GetValue(i).ToString().Replace("'","''") + "'";
2018-08-23 13:38:58 -04:00
}
2018-08-16 02:42:48 -04:00
break;
case "DATE":
if (ibmdr.GetValue(i).ToString() == "1/1/0001 12:00:00 AM" || ibmdr.GetValue(i).ToString() == "") {
nc = "NULL";
2018-08-16 02:42:48 -04:00
}
else {
nc = "'" + ibmdr.GetValue(i).ToString() + "'";
2018-08-16 02:42:48 -04:00
}
break;
case "TIME":
nc = "'" + ibmdr.GetValue(i).ToString() + "'";
2018-08-16 02:42:48 -04:00
break;
case "TIMESTAMP":
2018-09-17 09:41:52 -04:00
nc = "'" + ibmdr.GetDateTime(i).ToString() + "'";
break;
case "BIGINT":
nc = ibmdr.GetInt64(i).ToString();
2018-08-16 02:42:48 -04:00
break;
default:
if (ibmdr.GetValue(i).ToString() != "") {
nc = ibmdr.GetValue(i).ToString();
2018-08-16 12:01:10 -04:00
}
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) {
2018-09-20 11:39:15 -04:00
Console.Write(nl);
Console.Write(e.Message);
2018-08-20 12:04:05 -04:00
System.IO.File.WriteAllText(@"C:\Users\ptrowbridge\Downloads\runner_error.sql",sql);
ibmc.Close();
2018-08-20 11:23:56 -04:00
pgt.Rollback();
pgc.Close();
2018-08-20 11:43:54 -04:00
return;
}
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;
try {
pgcom.ExecuteNonQuery();
}
catch (Exception e) {
2018-09-20 11:39:15 -04:00
Console.Write(nl);
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();
pgc.Close();
2018-08-20 11:43:54 -04:00
return;
}
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-09-20 11:39:15 -04:00
Console.Write(nl);
2018-08-16 02:42:48 -04:00
Console.Write("etl end:" + DateTime.Now.ToString());
2018-08-15 14:34:31 -04:00
}
}
}