dotnet_runner/runner.cs

340 lines
13 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 sdt = "";
string ddt = "";
2018-09-11 11:37:38 -04:00
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-11-07 12:56:47 -05:00
//declaring these objects globally will asume these libraries exist, they may not
System.Data.Odbc.OdbcConnection s_odbc = new System.Data.Odbc.OdbcConnection();
System.Data.Odbc.OdbcConnection d_odbc = new System.Data.Odbc.OdbcConnection();
NpgsqlConnection s_npgsql = new NpgsqlConnection();
NpgsqlConnection d_npgsql = new NpgsqlConnection();
System.Data.Common.DbConnection sc; //source connection
System.Data.Common.DbConnection dc; //destination connection
2018-08-15 14:34:31 -04:00
2018-08-17 16:34:37 -04:00
string msg = "Help:";
msg = msg + nl + "version 0.25";
msg = msg + nl;
msg = msg + nl + "options:";
msg = msg + nl + "-sdt sourc driver type";
msg = msg + nl + "-scs source connection string";
msg = msg + nl + "-ddt destination driver type";
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";
msg = msg + nl;
msg = msg + nl + "available driver types:";
msg = msg + nl + "-------------------------";
msg = msg + nl + "* odbc";
msg = msg + nl + "* npgsql";
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]) {
//sourc driver type
case "-sdt":
sdt = args[i+1];
break;
2018-08-17 16:22:24 -04:00
//source connection string
case "-scs":
scs = args[i+1];
break;
//destination driver type
case "-ddt":
ddt = args[i+1];
break;
2018-08-17 16:22:24 -04:00
//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);
Console.Write(sdt);
2018-08-17 16:22:24 -04:00
Console.Write(scs);
2018-09-20 11:39:15 -04:00
Console.Write(nl);
Console.Write(ddt);
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-11-07 12:56:47 -05:00
//setup source
switch (sdt){
case "odbc":
try {
s_odbc.ConnectionString = scs;
}
catch (Exception e) {
Console.Write(nl + "bad source connection string: " + e.Message);
return;
}
sc = s_odbc;
return;
default:
break;
2018-09-20 11:39:35 -04:00
}
2018-11-07 12:56:47 -05:00
//setup destination
switch (ddt) {
case "npgsql":
try {
d_npgsql.ConnectionString = dcs;
}
catch (Exception e) {
Console.Write(nl + "bad source connection string: " + e.Message);
return;
}
dc = d_npgsql;
return;
default:
break;
2018-09-20 11:39:35 -04:00
}
2018-11-07 12:56:47 -05:00
//polymorph open
2018-09-20 11:39:35 -04:00
try {
2018-11-07 12:56:47 -05:00
sc.Open();
2018-09-20 11:39:15 -04:00
}
catch (Exception e) {
Console.Write(nl + "issue connection to source: " + e.Message);
return;
}
try {
2018-11-07 12:56:47 -05:00
dc.Open();
2018-09-20 11:39:15 -04:00
}
catch (Exception e) {
2018-11-07 12:56:47 -05:00
sc.Close();
2018-09-20 11:39:15 -04:00
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-11-07 12:56:47 -05:00
var s_cmd = new System.Data.Odbc.OdbcCommand();
var pgcom = d_npgsql.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();
2018-11-07 12:56:47 -05:00
s_odbcommandTimeout = 6000;
s_odbcommandTimeout = 6000;
system.Data.Odbc.OdbcDataReader ibmdr;
2018-08-20 11:23:22 -04:00
try {
2018-11-07 12:56:47 -05:00
ibmdr = s_odbcxecuteReader();
ibmdr = s_odbcxecuteReader();
d }
2018-08-20 11:23:22 -04:00
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);
2018-11-07 12:56:47 -05:00
s_odbcse();
s_odbcse();
pgc.Cldse();
2018-08-20 11:23:22 -04:00
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);
2018-11-07 12:56:47 -05:00
s_odbcse();
s_odbcse();
pgt.Rollbdck();
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-11-07 12:56:47 -05:00
//s_odbcse();
//s_odbcse();
pgt.Rodlback();
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-11-07 12:56:47 -05:00
s_odbcse();
s_odbcse();
pgc.Cldse();
2018-08-15 14:34:31 -04:00
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
}
}
}