dotnet_runner/runner.cs

135 lines
5.7 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-16 02:42:48 -04:00
//var i = new Int32();
2018-08-15 14:34:31 -04:00
//-------------------------------------------establis connections-------------------------------------------------
//var ibmc = new System.Data.Odbc.OdbcConnection("Driver={iSeries Access ODBC Driver};System=TEST400;Uid=tstdillen;Pwd=tstdillen");
var ibmc = new System.Data.Odbc.OdbcConnection("Driver={iSeries Access ODBC Driver};System=S7830956;Uid=PTROWBRIDG;Pwd=QQQX53@027");
var pgc = new NpgsqlConnection("Host=ushcc10091;Port=5432;Username=ptrowbridge;Password=qqqx53!026;Database=ubm;ApplicationName=runner");
ibmc.Open();
pgc.Open();
//----------------------------------------------setup commands---------------------------------------------------
var ibmcmd = new System.Data.Odbc.OdbcCommand();
ibmcmd.Connection = ibmc;
//ibmcmd.CommandText = "SELECT cast(ID as int) ID, TBLN, ACTN, TS, SU, COL, REPLACE(OLDV,X'00',CHR(32)) OLDV, REPLACE(NEWV,X'00',CHR(32)) NEWV, DTYPE, LENG, SCAL FROM QGPL.TRIG_LOG_EAV WHERE ID > 236832";
2018-08-15 23:26:00 -04:00
//ibmcmd.CommandText = "SELECT * FROM RLARP.OSM WHERE ITER >= '2018-08-15-11.23.42.009654'";
2018-08-15 14:34:31 -04:00
//ibmcmd.CommandText = "SELECT * FROM RLARP.OSMP";
//ibmcmd.CommandText = "SELECT * FROM LGDAT.GLDATE";
2018-08-15 23:26:00 -04:00
//ibmcmd.CommandText = "SELECT * FROM LGDAT.STKMM";
2018-08-16 11:59:13 -04:00
ibmcmd.CommandText = System.IO.File.ReadAllText(@"C:\Users\ptrowbridge\Documents\runner\osm.sql");
2018-08-15 23:26:00 -04:00
2018-08-15 14:34:31 -04:00
var pgcmd = new NpgsqlCommand();
pgcmd.Connection = pgc;
//pgcmd.CommandText = "SELECT * FROM rlarp.trig_log_eav WHERE 0=1";
2018-08-16 11:59:13 -04:00
pgcmd.CommandText = "SELECT * FROM rlarp.osmi WHERE 0=1";
2018-08-15 14:34:31 -04:00
//pgcmd.CommandText = "SELECT * FROM lgdat.gldate WHERE 0=1";
2018-08-16 11:59:13 -04:00
//pgcmd.CommandText = "SELECT * FROM lgdat.stkmm WHERE 0=1";
2018-08-15 14:34:31 -04:00
//---------------------------------------------setup adapters---------------------------------------------------------
2018-08-15 23:26:00 -04:00
//var ibmds = new System.Data.DataSet();
//var ibmda = new System.Data.Odbc.OdbcDataAdapter(ibmcmd);
//ibmda.Fill(ibmds);
2018-08-15 14:34:31 -04:00
var pgds = new System.Data.DataSet();
var pgda = new NpgsqlDataAdapter(pgcmd);
pgda.Fill(pgds);
2018-08-16 02:42:48 -04:00
//pgda.UpdateBatchSize = 100;
Console.Write("etl start:" + DateTime.Now.ToString());
Console.Write(Environment.NewLine);
2018-08-16 11:59:13 -04:00
pgc.BeginTransaction();
2018-08-15 14:34:31 -04:00
//--------------------------------------------move to target--------------------------------------------------------
2018-08-15 23:26:00 -04:00
var ibmdr = ibmcmd.ExecuteReader();
2018-08-16 02:42:48 -04:00
var getv = new object[ibmdr.FieldCount];
int r = 0;
2018-08-16 11:59:13 -04:00
int t = 0;
2018-08-16 02:42:48 -04:00
string sql = "";
string nr = "";
string nc = "";
var pgcom = pgc.CreateCommand();
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") {
nc = "'" + getv[i].ToString() + "'";
}
else {
nc = "NULL";
}
break;
case "TIME":
nc = "'" + getv[i].ToString() + "'";
break;
case "TIMESTAMP":
nc = "'" + getv[i].ToString() + "'";
break;
default:
nc = getv[i].ToString();
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-16 11:59:13 -04:00
sql = "INSERT INTO rlarp.osmi VALUES " + sql;
pgcom.CommandText = sql;
System.IO.File.WriteAllText(@"C:\Users\ptrowbridge\Downloads\"+ t.ToString()+".sql",sql);
pgcom.ExecuteNonQuery();
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-16 11:59:13 -04:00
sql = "INSERT INTO rlarp.osmi VALUES " + sql;
pgcom.CommandText = sql;
System.IO.File.WriteAllText(@"C:\Users\ptrowbridge\Downloads\"+ t.ToString()+".sql",sql);
pgcom.ExecuteNonQuery();
2018-08-16 02:42:48 -04:00
sql = "";
2018-08-15 14:34:31 -04:00
}
ibmc.Close();
pgc.Close();
2018-08-16 02:42:48 -04:00
Console.Write("etl end:" + DateTime.Now.ToString());
2018-08-15 14:34:31 -04:00
}
}
}