219 lines
8.1 KiB
C#
219 lines
8.1 KiB
C#
|
using Microsoft.Win32;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Configuration;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace UpdateHelper
|
|||
|
{
|
|||
|
class Program
|
|||
|
{
|
|||
|
static void Main(string[] args)
|
|||
|
{
|
|||
|
#region filename argument
|
|||
|
var sql_path = "";
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
switch(args[0])
|
|||
|
{
|
|||
|
case "-f":
|
|||
|
case "-w":
|
|||
|
sql_path = AppDomain.CurrentDomain.BaseDirectory + args[1];
|
|||
|
break;
|
|||
|
case "-h":
|
|||
|
case "--help":
|
|||
|
WriteLine("-w <SQL Func filename>", ConsoleColor.Yellow); // run whole file
|
|||
|
WriteLine("-f <SQL Query filename>", ConsoleColor.Yellow); // run line by line
|
|||
|
WriteLine("-h help", ConsoleColor.Yellow);
|
|||
|
WriteLine("--help help", ConsoleColor.Yellow);
|
|||
|
WriteLine("NOTE: If you wish to set the pgsql password, please execute: SET PGPASSWORD=<PGPASSWORD STRING>", ConsoleColor.Cyan);
|
|||
|
Console.WriteLine("\nPress any key to exit...");
|
|||
|
Console.ReadKey();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
WriteLine("Invalid argument exception", ConsoleColor.Red);
|
|||
|
Console.WriteLine("\nPress any key to exit...");
|
|||
|
Console.ReadLine();
|
|||
|
return;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region readfile & config
|
|||
|
string pg_path = ConfigurationManager.AppSettings["pg_path"];
|
|||
|
if (!pg_path.EndsWith("\\"))
|
|||
|
pg_path += "\\";
|
|||
|
|
|||
|
string hostname = ConfigurationManager.AppSettings["hostname"];
|
|||
|
string username = ConfigurationManager.AppSettings["username"];
|
|||
|
string dbname = ConfigurationManager.AppSettings["dbname"];
|
|||
|
string port = ConfigurationManager.AppSettings["port"];
|
|||
|
string PGPASSWORD = ConfigurationManager.AppSettings["PGPASSWORD"];
|
|||
|
int cntOK = 0, cntKO = 0, cntWarning = 0, total = 0, cmd_sleep = 100;
|
|||
|
int.TryParse(ConfigurationManager.AppSettings["cmd_sleep"], out cmd_sleep);
|
|||
|
if(!File.Exists(sql_path))
|
|||
|
{
|
|||
|
WriteLine("Invalid sql_path", ConsoleColor.Red);
|
|||
|
Console.ReadLine();
|
|||
|
return;
|
|||
|
}
|
|||
|
if (!File.Exists(pg_path + "psql.exe"))
|
|||
|
{
|
|||
|
WriteLine("Invalid pg_path", ConsoleColor.Red);
|
|||
|
Console.ReadLine();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
|
|||
|
string arguments = string.Empty;
|
|||
|
|
|||
|
if (args[0] == "-w")
|
|||
|
{
|
|||
|
arguments = string.Format("-h {0} -U {1} -d {2} -p {3} -w -f \"{4}\" ", hostname, username, dbname, port, sql_path);
|
|||
|
RunFile(pg_path, arguments);
|
|||
|
|
|||
|
Console.WriteLine(pg_path + "psql.exe " + arguments);
|
|||
|
Console.ReadKey();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (args[0] == "-f")
|
|||
|
{
|
|||
|
var reader = File.ReadLines(sql_path);
|
|||
|
StringBuilder fileOK = new StringBuilder();
|
|||
|
StringBuilder fileKO = new StringBuilder();
|
|||
|
StringBuilder fileWarning = new StringBuilder();
|
|||
|
StreamWriter swOK = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Succes.txt", false);
|
|||
|
StreamWriter swKO = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Failed.txt", false);
|
|||
|
StreamWriter swWarning = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Warning.txt", false);
|
|||
|
|
|||
|
|
|||
|
Process p = new Process();
|
|||
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
|||
|
arguments = string.Empty;
|
|||
|
|
|||
|
#region execute commands
|
|||
|
foreach (string line in reader)
|
|||
|
{
|
|||
|
if (!line.StartsWith("--"))
|
|||
|
{
|
|||
|
total++;
|
|||
|
|
|||
|
arguments = string.Format("-h {0} -U {1} -d {2} -p {3} -c \"{4}\" -w", hostname, username, dbname, port, line);
|
|||
|
p.StartInfo = RunQuery(startInfo, pg_path, arguments);
|
|||
|
|
|||
|
p.Start();
|
|||
|
|
|||
|
// add statistics
|
|||
|
string output = p.StandardOutput.ReadToEnd();
|
|||
|
string error = p.StandardError.ReadToEnd();
|
|||
|
if (output == "")
|
|||
|
{
|
|||
|
if (error.Contains("already"))
|
|||
|
{
|
|||
|
WriteLine(line, ConsoleColor.Yellow);
|
|||
|
WriteLine(error.Trim(), ConsoleColor.White);
|
|||
|
fileWarning.AppendLine(line);
|
|||
|
cntWarning++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
WriteLine(line, ConsoleColor.Red);
|
|||
|
WriteLine(error, ConsoleColor.White);
|
|||
|
fileKO.AppendLine(line);
|
|||
|
cntKO++;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
WriteLine(line, ConsoleColor.Green);
|
|||
|
fileOK.AppendLine(line);
|
|||
|
cntOK++;
|
|||
|
}
|
|||
|
Thread.Sleep(cmd_sleep);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region statistics
|
|||
|
WriteLine("-------------------------", ConsoleColor.Cyan);
|
|||
|
WriteLine("| Finish |", ConsoleColor.Cyan);
|
|||
|
WriteLine("-------------------------", ConsoleColor.Cyan);
|
|||
|
WriteLine(string.Format("| Total | {0,10}|", total), ConsoleColor.Cyan);
|
|||
|
WriteLine(string.Format("| Succes | {0,10}|", cntOK), ConsoleColor.Cyan);
|
|||
|
WriteLine(string.Format("| Warning | {0,10}|", cntWarning), ConsoleColor.Cyan);
|
|||
|
WriteLine(string.Format("| Failed | {0,10}|", cntKO), ConsoleColor.Cyan);
|
|||
|
WriteLine("-------------------------", ConsoleColor.Cyan);
|
|||
|
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region write streams
|
|||
|
swOK.Write(fileOK.ToString());
|
|||
|
swOK.Close();
|
|||
|
swKO.Write(fileKO.ToString());
|
|||
|
swKO.Close();
|
|||
|
swWarning.Write(fileWarning.ToString());
|
|||
|
swWarning.Close();
|
|||
|
#endregion
|
|||
|
|
|||
|
Console.ReadKey();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
static void RunFile(string path, string arguments)
|
|||
|
{
|
|||
|
Process p = new Process();
|
|||
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
|||
|
|
|||
|
p.StartInfo = RunQuery(startInfo, path, arguments);
|
|||
|
|
|||
|
p.Start();
|
|||
|
|
|||
|
string output = p.StandardOutput.ReadToEnd();
|
|||
|
string error = p.StandardError.ReadToEnd();
|
|||
|
|
|||
|
Console.WriteLine(output);
|
|||
|
Console.WriteLine(error);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
static ProcessStartInfo RunQuery(ProcessStartInfo startInfo, string path, string arguments)
|
|||
|
{
|
|||
|
|
|||
|
startInfo.FileName = path + "psql.exe";
|
|||
|
startInfo.Arguments = arguments;
|
|||
|
startInfo.Verb = "runas";
|
|||
|
startInfo.UseShellExecute = false;
|
|||
|
startInfo.RedirectStandardOutput = true;
|
|||
|
startInfo.RedirectStandardError = true;
|
|||
|
|
|||
|
return startInfo;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static void WriteLine(string str, ConsoleColor color)
|
|||
|
{
|
|||
|
Console.ForegroundColor = ConsoleColor.DarkGray;
|
|||
|
Console.Write(String.Format("[### {0:H:mm:ss} ###] ", DateTime.Now));
|
|||
|
Console.ForegroundColor = color;
|
|||
|
Console.Write(String.Format("{0}\n", str));
|
|||
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|