Files
panaceantech/QuickFTP/gQuery/Program.cs

141 lines
4.6 KiB
C#

using Sdaleo;
using Sdaleo.Systems.Advantage;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Yaulw.Assembly;
using Yaulw.File;
using Yaulw.Registry;
namespace quickftp
{
class Program
{
public static string DataRet_Retrieve(object o)
{
if(o != null && !String.IsNullOrEmpty(o.ToString()))
{
return o.ToString().Trim();
}
return "";
}
static void Main(string[] args)
{
string path = gQuery.Properties.Settings.Default.AdvantagePath;
string user = gQuery.Properties.Settings.Default.AdvantageUser;
string pass = gQuery.Properties.Settings.Default.AdvantagePass;
string fpath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyPath(AssemblyW.AssemblyST.Executing);
//string fname = DateTime.Now.ToShortDateString().Replace('/', '_');
if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(user) || String.IsNullOrEmpty(pass))
{
// If no db configuration found (* RUN WIZARD * TO DO )
Console.WriteLine("Advantage settings blank or null");
return;
}
AdvantageCredential cred = new AdvantageCredential(path, user, pass, AdvantageCredential.ServerType.REMOTE_LOCAL);
if(!cred.IsValid)
{
Console.WriteLine("Advantage connection invalid");
return;
}
// Find Queries
string[] queries = Directory.GetFiles(fpath, "query*.*");
if(queries == null || queries.Length < 1)
{
Console.WriteLine("No queries found");
return;
}
foreach (string q in queries)
{
ExecuteForEveryFileFound(q, cred, fpath, q.ToLower().Replace("query","result"));
}
Process.GetCurrentProcess().Close();
}
public static void ExecuteForEveryFileFound(string filename, AdvantageCredential cred, string fpath, string outfile)
{
string query1 = ReadQuery(filename);
if (String.IsNullOrEmpty(query1))
return;
DB db = DB.Create(cred);
DBRetVal retVal = db.FillDataTable(query1);
if (!retVal.IsValid)
{
Console.WriteLine(retVal.ErrorMsg);
return;
}
try
{
DataTable dt = retVal.GetDataTableRetVal();
StringBuilder sbColumns = new StringBuilder();
foreach (DataColumn c in dt.Columns)
{
sbColumns.Append(c.ColumnName);
sbColumns.Append(',');
}
String columnHeader = sbColumns.ToString().Substring(0, sbColumns.Length - 1);
String[] columns = columnHeader.Split(',');
FileWriter fw = new FileWriter(outfile, "csv", fpath);
fw.WriteLineA(columnHeader);
foreach (DataRow r in dt.Rows)
{
StringBuilder sbRow = new StringBuilder();
for (int i = 0; i < columns.Length; ++i)
{
sbRow.Append(DataRet_Retrieve(r[columns[i]]));
sbRow.Append(',');
}
String rowValues = sbRow.ToString().Substring(0, sbRow.Length - 1);
fw.WriteLineA(rowValues);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static string ReadQuery(string filename)
{
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(filename))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
return line;
}
}
catch (Exception e)
{
Console.WriteLine(String.Format("The file could not be read: {0}", filename));
Console.WriteLine(e.Message);
}
return "";
}
}
}