Advertisement
Guest User

TextSQL

a guest
Feb 11th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 33.74 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. using System.Data;
  8. using MySql.Data.MySqlClient;
  9.  
  10. namespace TextSQL
  11. {
  12.     /// <summary>
  13.     /// Summary description for TextSQLMainWindow.
  14.     /// </summary>
  15.     public class TextSQLMainWindow : System.Windows.Forms.Form
  16.     {
  17.         private System.Windows.Forms.TextBox LogBox1;
  18.         private System.ComponentModel.IContainer components;
  19.  
  20.         private MySqlConnection conn;
  21.         private Label SchemaLabel;
  22.         private Label UserLabel;
  23.         private Label HostLabel;
  24.         private Label FileInLabel;
  25.         private Label DirOutLabel;
  26.         private Label TQueryCountLabel;
  27.         private Label NQueryCountLabel;
  28.         private Label QueryCountLabel;
  29.         public Label TQueryCountVarLabel;
  30.         public Label NQueryCountVarLabel;
  31.         public Label QueryCountVarLabel;
  32.         public Label DirInVarLabel;
  33.         public Label DirOutVarLabel;
  34.         public Label UserVarLabel;
  35.         public Label SchemaVarLabel;
  36.         public Label HostVarLabel;
  37.         public Label PasswordVarLabel;
  38.         private Label PasswordLabel;
  39.         private GroupBox GroupStatus1;
  40.         private Timer QueryTimer;
  41.         private Button ReloadCFG;
  42.         private Button QueryPause;
  43.         private Button ClearLogBox;
  44.         private StatusStrip statusStrip1;
  45.         private ToolStripStatusLabel AutoScrollLabel;
  46.         private ToolStripStatusLabel DebugLvlLabel;
  47.  
  48.         public TextSQLMainWindow()
  49.         {
  50.             //
  51.             // Required for Windows Form Designer support
  52.             //
  53.             InitializeComponent();
  54.            
  55.             ReadCFG();
  56.            
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Clean up any resources being used.
  61.         /// </summary>
  62.         protected override void Dispose( bool disposing )
  63.         {
  64.             if( disposing )
  65.             {
  66.                 if (components != null)
  67.                 {
  68.                     components.Dispose();
  69.                 }
  70.             }
  71.             base.Dispose( disposing );
  72.         }
  73.  
  74.         #region Windows Form Designer generated code
  75.         /// <summary>
  76.         /// Required method for Designer support - do not modify
  77.         /// the contents of this method with the code editor.
  78.         /// </summary>
  79.         private void InitializeComponent()
  80.         {
  81.             this.components = new System.ComponentModel.Container();
  82.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TextSQLMainWindow));
  83.             this.LogBox1 = new System.Windows.Forms.TextBox();
  84.             this.TQueryCountVarLabel = new System.Windows.Forms.Label();
  85.             this.TQueryCountLabel = new System.Windows.Forms.Label();
  86.             this.NQueryCountVarLabel = new System.Windows.Forms.Label();
  87.             this.NQueryCountLabel = new System.Windows.Forms.Label();
  88.             this.QueryCountVarLabel = new System.Windows.Forms.Label();
  89.             this.QueryCountLabel = new System.Windows.Forms.Label();
  90.             this.DirInVarLabel = new System.Windows.Forms.Label();
  91.             this.FileInLabel = new System.Windows.Forms.Label();
  92.             this.DirOutVarLabel = new System.Windows.Forms.Label();
  93.             this.DirOutLabel = new System.Windows.Forms.Label();
  94.             this.PasswordVarLabel = new System.Windows.Forms.Label();
  95.             this.UserVarLabel = new System.Windows.Forms.Label();
  96.             this.SchemaLabel = new System.Windows.Forms.Label();
  97.             this.SchemaVarLabel = new System.Windows.Forms.Label();
  98.             this.PasswordLabel = new System.Windows.Forms.Label();
  99.             this.UserLabel = new System.Windows.Forms.Label();
  100.             this.HostVarLabel = new System.Windows.Forms.Label();
  101.             this.HostLabel = new System.Windows.Forms.Label();
  102.             this.GroupStatus1 = new System.Windows.Forms.GroupBox();
  103.             this.QueryTimer = new System.Windows.Forms.Timer(this.components);
  104.             this.ReloadCFG = new System.Windows.Forms.Button();
  105.             this.QueryPause = new System.Windows.Forms.Button();
  106.             this.ClearLogBox = new System.Windows.Forms.Button();
  107.             this.statusStrip1 = new System.Windows.Forms.StatusStrip();
  108.             this.AutoScrollLabel = new System.Windows.Forms.ToolStripStatusLabel();
  109.             this.DebugLvlLabel = new System.Windows.Forms.ToolStripStatusLabel();
  110.             this.GroupStatus1.SuspendLayout();
  111.             this.statusStrip1.SuspendLayout();
  112.             this.SuspendLayout();
  113.             //
  114.             // LogBox1
  115.             //
  116.             this.LogBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  117.                         | System.Windows.Forms.AnchorStyles.Left)
  118.                         | System.Windows.Forms.AnchorStyles.Right)));
  119.             this.LogBox1.Location = new System.Drawing.Point(3, 216);
  120.             this.LogBox1.Multiline = true;
  121.             this.LogBox1.Name = "LogBox1";
  122.             this.LogBox1.ReadOnly = true;
  123.             this.LogBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  124.             this.LogBox1.Size = new System.Drawing.Size(500, 292);
  125.             this.LogBox1.TabIndex = 0;
  126.             //
  127.             // TQueryCountVarLabel
  128.             //
  129.             this.TQueryCountVarLabel.Location = new System.Drawing.Point(115, 175);
  130.             this.TQueryCountVarLabel.Name = "TQueryCountVarLabel";
  131.             this.TQueryCountVarLabel.Size = new System.Drawing.Size(112, 23);
  132.             this.TQueryCountVarLabel.TabIndex = 6;
  133.             this.TQueryCountVarLabel.Text = "0";
  134.             //
  135.             // TQueryCountLabel
  136.             //
  137.             this.TQueryCountLabel.Location = new System.Drawing.Point(6, 175);
  138.             this.TQueryCountLabel.Name = "TQueryCountLabel";
  139.             this.TQueryCountLabel.Size = new System.Drawing.Size(103, 23);
  140.             this.TQueryCountLabel.TabIndex = 6;
  141.             this.TQueryCountLabel.Text = "Total Query Count:";
  142.             //
  143.             // NQueryCountVarLabel
  144.             //
  145.             this.NQueryCountVarLabel.Location = new System.Drawing.Point(115, 152);
  146.             this.NQueryCountVarLabel.Name = "NQueryCountVarLabel";
  147.             this.NQueryCountVarLabel.Size = new System.Drawing.Size(112, 23);
  148.             this.NQueryCountVarLabel.TabIndex = 5;
  149.             this.NQueryCountVarLabel.Text = "0";
  150.             //
  151.             // NQueryCountLabel
  152.             //
  153.             this.NQueryCountLabel.Location = new System.Drawing.Point(6, 152);
  154.             this.NQueryCountLabel.Name = "NQueryCountLabel";
  155.             this.NQueryCountLabel.Size = new System.Drawing.Size(103, 23);
  156.             this.NQueryCountLabel.TabIndex = 5;
  157.             this.NQueryCountLabel.Text = "Non Query Count:";
  158.             //
  159.             // QueryCountVarLabel
  160.             //
  161.             this.QueryCountVarLabel.Location = new System.Drawing.Point(115, 129);
  162.             this.QueryCountVarLabel.Name = "QueryCountVarLabel";
  163.             this.QueryCountVarLabel.Size = new System.Drawing.Size(112, 23);
  164.             this.QueryCountVarLabel.TabIndex = 4;
  165.             this.QueryCountVarLabel.Text = "0";
  166.             //
  167.             // QueryCountLabel
  168.             //
  169.             this.QueryCountLabel.Location = new System.Drawing.Point(6, 129);
  170.             this.QueryCountLabel.Name = "QueryCountLabel";
  171.             this.QueryCountLabel.Size = new System.Drawing.Size(103, 23);
  172.             this.QueryCountLabel.TabIndex = 4;
  173.             this.QueryCountLabel.Text = "Query Count:";
  174.             //
  175.             // DirInVarLabel
  176.             //
  177.             this.DirInVarLabel.Location = new System.Drawing.Point(341, 37);
  178.             this.DirInVarLabel.Name = "DirInVarLabel";
  179.             this.DirInVarLabel.Size = new System.Drawing.Size(96, 23);
  180.             this.DirInVarLabel.TabIndex = 3;
  181.             this.DirInVarLabel.Text = "Invalid File In";
  182.             //
  183.             // FileInLabel
  184.             //
  185.             this.FileInLabel.Location = new System.Drawing.Point(223, 37);
  186.             this.FileInLabel.Name = "FileInLabel";
  187.             this.FileInLabel.Size = new System.Drawing.Size(112, 23);
  188.             this.FileInLabel.TabIndex = 3;
  189.             this.FileInLabel.Text = "Text Directory In:";
  190.             //
  191.             // DirOutVarLabel
  192.             //
  193.             this.DirOutVarLabel.Location = new System.Drawing.Point(341, 60);
  194.             this.DirOutVarLabel.Name = "DirOutVarLabel";
  195.             this.DirOutVarLabel.Size = new System.Drawing.Size(96, 23);
  196.             this.DirOutVarLabel.TabIndex = 2;
  197.             this.DirOutVarLabel.Text = "Invalid Directory Out:";
  198.             //
  199.             // DirOutLabel
  200.             //
  201.             this.DirOutLabel.Location = new System.Drawing.Point(223, 60);
  202.             this.DirOutLabel.Name = "DirOutLabel";
  203.             this.DirOutLabel.Size = new System.Drawing.Size(112, 23);
  204.             this.DirOutLabel.TabIndex = 2;
  205.             this.DirOutLabel.Text = "Text Directory Out:";
  206.             //
  207.             // PasswordVarLabel
  208.             //
  209.             this.PasswordVarLabel.Location = new System.Drawing.Point(83, 83);
  210.             this.PasswordVarLabel.Name = "PasswordVarLabel";
  211.             this.PasswordVarLabel.Size = new System.Drawing.Size(99, 23);
  212.             this.PasswordVarLabel.TabIndex = 1;
  213.             this.PasswordVarLabel.Text = "Invalid Password";
  214.             //
  215.             // UserVarLabel
  216.             //
  217.             this.UserVarLabel.Location = new System.Drawing.Point(83, 60);
  218.             this.UserVarLabel.Name = "UserVarLabel";
  219.             this.UserVarLabel.Size = new System.Drawing.Size(99, 23);
  220.             this.UserVarLabel.TabIndex = 1;
  221.             this.UserVarLabel.Text = "Invalid User";
  222.             //
  223.             // SchemaLabel
  224.             //
  225.             this.SchemaLabel.Location = new System.Drawing.Point(9, 37);
  226.             this.SchemaLabel.Name = "SchemaLabel";
  227.             this.SchemaLabel.Size = new System.Drawing.Size(49, 23);
  228.             this.SchemaLabel.TabIndex = 1;
  229.             this.SchemaLabel.Text = "Schema:";
  230.             //
  231.             // SchemaVarLabel
  232.             //
  233.             this.SchemaVarLabel.Location = new System.Drawing.Point(80, 36);
  234.             this.SchemaVarLabel.Name = "SchemaVarLabel";
  235.             this.SchemaVarLabel.Size = new System.Drawing.Size(99, 23);
  236.             this.SchemaVarLabel.TabIndex = 1;
  237.             this.SchemaVarLabel.Text = "Invalid Schema";
  238.             //
  239.             // PasswordLabel
  240.             //
  241.             this.PasswordLabel.Location = new System.Drawing.Point(9, 83);
  242.             this.PasswordLabel.Name = "PasswordLabel";
  243.             this.PasswordLabel.Size = new System.Drawing.Size(64, 23);
  244.             this.PasswordLabel.TabIndex = 1;
  245.             this.PasswordLabel.Text = "Password:";
  246.             //
  247.             // UserLabel
  248.             //
  249.             this.UserLabel.Location = new System.Drawing.Point(9, 60);
  250.             this.UserLabel.Name = "UserLabel";
  251.             this.UserLabel.Size = new System.Drawing.Size(74, 23);
  252.             this.UserLabel.TabIndex = 1;
  253.             this.UserLabel.Text = "User:";
  254.             //
  255.             // HostVarLabel
  256.             //
  257.             this.HostVarLabel.Location = new System.Drawing.Point(83, 14);
  258.             this.HostVarLabel.Name = "HostVarLabel";
  259.             this.HostVarLabel.Size = new System.Drawing.Size(99, 23);
  260.             this.HostVarLabel.TabIndex = 1;
  261.             this.HostVarLabel.Text = "Invalid Host";
  262.             //
  263.             // HostLabel
  264.             //
  265.             this.HostLabel.Location = new System.Drawing.Point(9, 14);
  266.             this.HostLabel.Name = "HostLabel";
  267.             this.HostLabel.Size = new System.Drawing.Size(74, 23);
  268.             this.HostLabel.TabIndex = 1;
  269.             this.HostLabel.Text = "Server/Host:";
  270.             //
  271.             // GroupStatus1
  272.             //
  273.             this.GroupStatus1.Controls.Add(this.SchemaVarLabel);
  274.             this.GroupStatus1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  275.             this.GroupStatus1.Location = new System.Drawing.Point(3, 1);
  276.             this.GroupStatus1.Name = "GroupStatus1";
  277.             this.GroupStatus1.Size = new System.Drawing.Size(490, 116);
  278.             this.GroupStatus1.TabIndex = 7;
  279.             this.GroupStatus1.TabStop = false;
  280.             //
  281.             // QueryTimer
  282.             //
  283.             this.QueryTimer.Enabled = true;
  284.             this.QueryTimer.Interval = 250;
  285.             this.QueryTimer.Tick += new System.EventHandler(this.QueryTimer_Tick);
  286.  
  287.             //
  288.             // ReloadCFG
  289.             //
  290.             this.ReloadCFG.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  291.             this.ReloadCFG.Location = new System.Drawing.Point(387, 170);
  292.             this.ReloadCFG.Name = "ReloadCFG";
  293.             this.ReloadCFG.Size = new System.Drawing.Size(105, 23);
  294.             this.ReloadCFG.TabIndex = 8;
  295.             this.ReloadCFG.Text = "Reload Config";
  296.             this.ReloadCFG.Click += new System.EventHandler(this.ReloadCFG_Click);
  297.             //
  298.             // QueryPause
  299.             //
  300.             this.QueryPause.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  301.             this.QueryPause.Location = new System.Drawing.Point(294, 170);
  302.             this.QueryPause.Name = "QueryPause";
  303.             this.QueryPause.Size = new System.Drawing.Size(87, 23);
  304.             this.QueryPause.TabIndex = 1;
  305.             this.QueryPause.Text = "Pause";
  306.             this.QueryPause.Click += new System.EventHandler(this.QueryPause_Click);
  307.             //
  308.             // ClearLogBox
  309.             //
  310.             this.ClearLogBox.Location = new System.Drawing.Point(387, 141);
  311.             this.ClearLogBox.Name = "ClearLogBox";
  312.             this.ClearLogBox.Size = new System.Drawing.Size(105, 23);
  313.             this.ClearLogBox.TabIndex = 10;
  314.             this.ClearLogBox.Text = "Clear LogBox";
  315.             this.ClearLogBox.UseVisualStyleBackColor = true;
  316.             this.ClearLogBox.Click += new System.EventHandler(this.ClearLogBox_Click);
  317.             //
  318.             // statusStrip1
  319.             //
  320.             this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  321.             this.AutoScrollLabel,
  322.             this.DebugLvlLabel});
  323.             this.statusStrip1.Location = new System.Drawing.Point(0, 511);
  324.             this.statusStrip1.Name = "statusStrip1";
  325.             this.statusStrip1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
  326.             this.statusStrip1.Size = new System.Drawing.Size(504, 22);
  327.             this.statusStrip1.SizingGrip = false;
  328.             this.statusStrip1.TabIndex = 11;
  329.             this.statusStrip1.Text = "statusStrip1";
  330.             //
  331.             // AutoScrollLabel
  332.             //
  333.             this.AutoScrollLabel.BorderSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Right)));
  334.             this.AutoScrollLabel.Name = "AutoScrollLabel";
  335.             this.AutoScrollLabel.Size = new System.Drawing.Size(81, 17);
  336.             this.AutoScrollLabel.Text = "AutoScroll: ON";
  337.             this.AutoScrollLabel.Click += new System.EventHandler(this.AutoScrollLabel_Click);
  338.             //
  339.             // DebugLvlLabel
  340.             //
  341.             this.DebugLvlLabel.BorderSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Right)));
  342.             this.DebugLvlLabel.Name = "DebugLvlLabel";
  343.             this.DebugLvlLabel.Size = new System.Drawing.Size(94, 17);
  344.             this.DebugLvlLabel.Text = "DebugLevel: OFF";
  345.             this.DebugLvlLabel.Click += new System.EventHandler(this.DebugLvlLabel_Click);
  346.             //
  347.             // TextSQLMainWindow
  348.             //
  349.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  350.             this.ClientSize = new System.Drawing.Size(504, 533);
  351.             this.Controls.Add(this.statusStrip1);
  352.             this.Controls.Add(this.ClearLogBox);
  353.             this.Controls.Add(this.ReloadCFG);
  354.             this.Controls.Add(this.TQueryCountVarLabel);
  355.             this.Controls.Add(this.TQueryCountLabel);
  356.             this.Controls.Add(this.NQueryCountVarLabel);
  357.             this.Controls.Add(this.NQueryCountLabel);
  358.             this.Controls.Add(this.QueryCountVarLabel);
  359.             this.Controls.Add(this.QueryCountLabel);
  360.             this.Controls.Add(this.DirInVarLabel);
  361.             this.Controls.Add(this.FileInLabel);
  362.             this.Controls.Add(this.DirOutVarLabel);
  363.             this.Controls.Add(this.DirOutLabel);
  364.             this.Controls.Add(this.PasswordVarLabel);
  365.             this.Controls.Add(this.UserVarLabel);
  366.             this.Controls.Add(this.SchemaLabel);
  367.             this.Controls.Add(this.PasswordLabel);
  368.             this.Controls.Add(this.UserLabel);
  369.             this.Controls.Add(this.HostVarLabel);
  370.             this.Controls.Add(this.HostLabel);
  371.             this.Controls.Add(this.GroupStatus1);
  372.             this.Controls.Add(this.QueryPause);
  373.             this.Controls.Add(this.LogBox1);
  374.             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  375.             this.Name = "TextSQLMainWindow";
  376.             this.Text = "TextSQL";
  377.             this.GroupStatus1.ResumeLayout(false);
  378.             this.statusStrip1.ResumeLayout(false);
  379.             this.statusStrip1.PerformLayout();
  380.             this.ResumeLayout(false);
  381.             this.PerformLayout();
  382.             this.WindowState = GlobalVars.Mini;
  383.  
  384.         }
  385.         #endregion
  386.  
  387.         /// <summary>
  388.         /// The main entry point for the application.
  389.         /// </summary>
  390.         [STAThread]
  391.         static void Main(string[] args) // Entry point, --mini
  392.               {
  393.            
  394.            foreach(string arg in args)
  395.            {
  396.                if (arg == "--minimized")
  397.                {
  398.                    GlobalVars.Mini = FormWindowState.Minimized;
  399.                }
  400.            }
  401.                      
  402.             Application.Run(new TextSQLMainWindow());
  403.         }
  404.  
  405.         public void TextReturnLines(String returnfile, String Line, int entireline) // Text Files out
  406.         {
  407.             if (!File.Exists(returnfile)) //File Doesn't exist
  408.             {
  409.                 using (StreamWriter FileOut = File.CreateText(returnfile))
  410.                 {
  411.                     if (entireline == 0) // We are just writing some text, not entire line
  412.                     {
  413.                         FileOut.Write(Line);
  414.                     }
  415.                     else if (entireline == 1) // Go for a line
  416.                     {
  417.                         FileOut.WriteLine(Line);
  418.                     }
  419.                 }
  420.             }
  421.             else
  422.             {
  423.                 using (StreamWriter FileOut = File.AppendText(returnfile))
  424.                 {
  425.                     if (entireline == 0) // We are just writing some text, not entire line
  426.                     {
  427.                         FileOut.Write(Line);
  428.                     }
  429.                     else if (entireline == 1) // Go for a line
  430.                     {
  431.                         FileOut.WriteLine(Line);
  432.                     }
  433.                 }
  434.  
  435.             }
  436.         }
  437.        
  438.         public void LogBoxEntry(String LogEntry) // Drops entries to LogBox and Log File
  439.         {
  440.             if (LogEntry == "Clear LogBox")
  441.             {
  442.                 this.LogBox1.Text = "";
  443.             }
  444.             else
  445.             {
  446.                 this.LogBox1.AppendText(LogEntry + Environment.NewLine);
  447.                 TextReturnLines("log.txt", DateTime.Now + ": " + LogEntry, 1);
  448.                 if (GlobalVars.CarrotScroll)
  449.                 {
  450.                     this.LogBox1.SelectionStart = LogBox1.Text.Length;
  451.                     this.LogBox1.ScrollToCaret();
  452.                 }
  453.             }
  454.         }
  455.  
  456.         public void QueryCountUpdate() // Updates The query Counters
  457.         {
  458.             this.NQueryCountVarLabel.Text = Convert.ToString(GlobalVars.NQCount);
  459.             this.QueryCountVarLabel.Text = Convert.ToString(GlobalVars.QCount);
  460.             this.TQueryCountVarLabel.Text = Convert.ToString(GlobalVars.NQCount + GlobalVars.QCount);
  461.         }
  462.  
  463.         public void QueryBuild(FileInfo file, MySqlConnection conn, String[] FileTextLine, String[] FormatingReturn) // Executes The Query and Return Lines
  464.         {
  465.             try
  466.             {
  467.                 String QueryLine = FileTextLine[1];
  468.  
  469.                 MySqlCommand cmd = new MySqlCommand(QueryLine, conn);
  470.  
  471.                 if (Convert.ToInt32(FileTextLine[0]) >= 1)
  472.                 {
  473.                     //int QueryBack = Convert.ToInt32(FileTextLine[0]);
  474.  
  475.                     MySqlDataReader reader = null;
  476.  
  477.                     reader = cmd.ExecuteReader();
  478.  
  479.                     int QueryBack = reader.FieldCount;
  480.  
  481.                     String[,] QueryLinesBack = new String[256, QueryBack + 1];
  482.                     int m = 0;
  483.                     while (reader.Read())
  484.                     {
  485.                         for (int i = 0; i < QueryBack; i++)
  486.                         {
  487.                             if (reader.IsDBNull(i))
  488.                             {
  489.                                 QueryLinesBack[m, i] = GlobalVars.NullString;
  490.                             }
  491.                             else
  492.                             {
  493.                                 QueryLinesBack[m, i] = reader.GetString(i);
  494.                             }
  495.  
  496.  
  497.                         }
  498.                         m++;
  499.                     }
  500.  
  501.                     String QueryLineBack = "";
  502.  
  503.                     //No Need to confuse Lua, get rid of the old file if one was once there
  504.                     if (File.Exists(GlobalVars.OutputDir + "\\" + file.Name)) File.Delete(GlobalVars.OutputDir + "\\" + file.Name);
  505.  
  506.                     TextReturnLines(GlobalVars.OutputDir + "\\" + file.Name, "1", 1);
  507.                     for (int p = 0; p < m; p++)
  508.                     {
  509.                         for (int k = 0; k < QueryBack; k++)
  510.                         {
  511.                             if (k == 0)
  512.                             {
  513.                                 QueryLineBack = GlobalVars.QSep + QueryLinesBack[p, k] + GlobalVars.QSep;
  514.                             }
  515.                             else if ((k > 0))
  516.                             {
  517.                                 QueryLineBack += " " + GlobalVars.QSep + QueryLinesBack[p, k] + GlobalVars.QSep;
  518.                             }
  519.                         }
  520.  
  521.                         TextReturnLines(GlobalVars.OutputDir + "\\" + file.Name, QueryLineBack, 1);
  522.                         if (GlobalVars.DebugLvl >= 3) LogBoxEntry("QueryLine: " + QueryLineBack);
  523.                     }
  524.                     //TextReturnLines(GlobalVars.OutputDir + file.Name, GlobalVars.StopString, 1);
  525.  
  526.                     if (GlobalVars.DebugLvl >= 3) LogBoxEntry("Query: Completed");
  527.                     GlobalVars.QCount++;
  528.                     QueryCountUpdate();
  529.                     if (reader != null) reader.Close();
  530.                 }
  531.                 if (FileTextLine[0] == "0")
  532.                 {
  533.                     cmd.ExecuteNonQuery();
  534.  
  535.                     TextReturnLines(GlobalVars.OutputDir + "\\" + file.Name, "1", 1);
  536.                     //TextReturnLines(GlobalVars.OutputDir + file.Name, GlobalVars.StopString, 1);
  537.  
  538.                     if (GlobalVars.DebugLvl >= 3) LogBoxEntry("NonQuery: Completed");
  539.  
  540.                     GlobalVars.NQCount++;
  541.                     QueryCountUpdate();
  542.                 }
  543.  
  544.                 //Delete the Query File
  545.                 file.Delete(); // (Path.ChangeExtension(, ".old"));
  546.                 if (GlobalVars.DebugLvl >= 3) LogBoxEntry("QueryFile Deleted!");
  547.  
  548.  
  549.             }
  550.             catch (MySqlException ex)
  551.             {
  552.                 //If we had a problem let's not screw around and possibly send it back fed up
  553.                 if (File.Exists(GlobalVars.OutputDir + "\\" + file.Name)) File.Delete(GlobalVars.OutputDir + file.Name);
  554.  
  555.                 TextReturnLines(GlobalVars.OutputDir + "\\" + file.Name, "0", 1);
  556.                 TextReturnLines(GlobalVars.OutputDir + "\\"  + file.Name, ex.Message, 1);
  557.  
  558.                 LogBoxEntry("Query Failed to Execute: " + ex.Message);
  559.             }
  560.             finally
  561.             {
  562.                 TextReturnLines(GlobalVars.OutputDir + "\\" + file.Name, GlobalVars.StopString, 1);
  563.             }
  564.         }
  565.  
  566.         public void CheckTextStop(FileInfo file, String[] FormatingReturn) // Parses in Files in sends to QueryBuild
  567.         {
  568.             try
  569.             {
  570.                 //Read from file
  571.                 StreamReader FileText =
  572.                 new StreamReader(file.FullName);
  573.  
  574.                 //Single line from config.cfg
  575.                 //string strLine;
  576.                 string[] FileTextLine = new string[4];
  577.  
  578.                 //Parse the Text, 3 lines only
  579.                 for (int x = 0; x < 4; x++)
  580.                 {
  581.  
  582.                     FileTextLine[x] = FileText.ReadLine();
  583.  
  584.                 }
  585.  
  586.                 //Cleanup
  587.                 //Close the Config File
  588.                 FileText.Close();
  589.  
  590.                 //We need to make sure our stop String is there, //
  591.                 //we don't want to execute half queries now do we //
  592.                 if (FileTextLine[2] == GlobalVars.StopString)
  593.                 {
  594.  
  595.                     //Lets get the database Connection going
  596.                     if (GlobalVars.DebugLvl >= 1)
  597.                     {
  598.                         LogBoxEntry("Query Found: " + file.Name);
  599.                         if (GlobalVars.DebugLvl >= 2)
  600.                         {
  601.                             LogBoxEntry(FileTextLine[1]);
  602.                         }
  603.                     }
  604.  
  605.                     string connStr = String.Format("server={0};uid={1};pwd={2};database={3}",
  606.                         GlobalVars.Host, GlobalVars.User, GlobalVars.Password, GlobalVars.Schema);
  607.  
  608.                     conn = new MySqlConnection(connStr);
  609.                     try
  610.                     {
  611.                         conn.Open();
  612.                         QueryBuild(file, conn, FileTextLine, FormatingReturn);
  613.  
  614.                     }
  615.                     catch (MySqlException ex)
  616.                     {
  617.                         LogBoxEntry("Failed to Connect to Database: " + ex.Message);
  618.                     }
  619.                     finally
  620.                     {
  621.                         conn.Close();
  622.                     }
  623.                 }
  624.  
  625.             }
  626.             catch (System.IO.IOException ex)
  627.             {
  628.                 LogBoxEntry(ex.Message);
  629.             }
  630.         }
  631.  
  632.         public void ReadCFG() // Called when ReloadCFG Button is clicked, and at Start
  633.         {
  634.             String config = "config.cfg";
  635.             if (! File.Exists(config))
  636.             {
  637.                 //File.Create("config.cfg");
  638.                 LogBoxEntry("Exception: No ConfigFile, Default created");
  639.                
  640.                 using (StreamWriter sw = new StreamWriter(config))
  641.                 {
  642.                     sw.WriteLine("Server/Host");
  643.                     sw.WriteLine("User");
  644.                     sw.WriteLine("Password");
  645.                     sw.WriteLine("Schema");
  646.                     sw.WriteLine("Directory In");
  647.                     sw.WriteLine("Directory Out");
  648.                     sw.WriteLine("StopString");
  649.                     sw.WriteLine("QuerySeperator");
  650.                     sw.WriteLine("NullString");
  651.                 }
  652.             }
  653.             //Read from file
  654.             StreamReader ConfigFile =
  655.             new StreamReader(config);
  656.    
  657.             //Single line from config.cfg
  658.             //string strLine;
  659.             string[] Conn_Details = new string[9];
  660.  
  661.             for (int x = 0; x < 9; x++)
  662.             {
  663.                     Conn_Details[x] = ConfigFile.ReadLine();
  664.             }
  665.  
  666.             //Assign the Config to The GlobalVars
  667.             GlobalVars.Host = Conn_Details[0];
  668.             GlobalVars.User = Conn_Details[1];
  669.             GlobalVars.Password = Conn_Details[2];
  670.             GlobalVars.Schema = Conn_Details[3];
  671.             GlobalVars.InputDir = Conn_Details[4];
  672.             GlobalVars.OutputDir = Conn_Details[5];
  673.             GlobalVars.StopString = Conn_Details[6];
  674.             GlobalVars.QSep = Conn_Details[7];
  675.             GlobalVars.NullString = Conn_Details[8];
  676.  
  677.             //Sets the text box crap
  678.             this.HostVarLabel.Text = GlobalVars.Host;
  679.             this.UserVarLabel.Text = GlobalVars.User;
  680.             this.PasswordVarLabel.Text = GlobalVars.Password;
  681.             this.SchemaVarLabel.Text = GlobalVars.Schema;
  682.             this.DirInVarLabel.Text = GlobalVars.InputDir;
  683.             this.DirOutVarLabel.Text = GlobalVars.OutputDir;
  684.  
  685.             //The Better way
  686.             //this.LogBox1.Text += "Using Stop String: \"" + GlobalVars.StopString + "\"" + Environment.NewLine;
  687.            
  688.             //Even Better Way
  689.             if (GlobalVars.DebugLvl >= 1)
  690.             {
  691.                 LogBoxEntry("Using Stop String: \"" + GlobalVars.StopString + "\"");
  692.                 LogBoxEntry("Using Query Seperator: \"" + GlobalVars.QSep + "\"");
  693.                 LogBoxEntry("Using Null String: \"" + GlobalVars.NullString + "\"");
  694.             }
  695.             //Cleanup
  696.             //Close the Config File
  697.             ConfigFile.Close();
  698.            
  699.             //We need to make sure our Directories Exist, Right?
  700.             if (!Directory.Exists(GlobalVars.InputDir))
  701.             {
  702.                 Directory.CreateDirectory(GlobalVars.InputDir);
  703.                 LogBoxEntry("Input Directory Not Found, Creating");
  704.             }
  705.             if (!Directory.Exists(GlobalVars.OutputDir))
  706.             {
  707.                 Directory.CreateDirectory(GlobalVars.OutputDir);
  708.                 LogBoxEntry("Output Directory Not Found, Creating");
  709.             }
  710.  
  711.            
  712.         }
  713.  
  714.         public void QueryCheck() // Calls CheckTextStop - For each file *.txt found
  715.         {
  716.            
  717.             DirectoryInfo InDir = new DirectoryInfo(GlobalVars.InputDir);
  718.            
  719.             FileInfo[] InFiles = InDir.GetFiles("*.txt");
  720.  
  721.             foreach ( FileInfo f in InFiles)
  722.             {
  723.                 String[] FormatingReturn = new String[8];
  724.  
  725.                 CheckTextStop(f, FormatingReturn);
  726.             }
  727.  
  728.         }
  729.  
  730.         private void QueryPause_Click(object sender, System.EventArgs e)
  731.         {
  732.             GlobalVars.Running = (!GlobalVars.Running);
  733.  
  734.             if (GlobalVars.Running)
  735.             {
  736.                 this.QueryPause.Text = "Pause";
  737.                 if (GlobalVars.DebugLvl >= 1) LogBoxEntry("Querying: Resumed");
  738.  
  739.             }
  740.             else
  741.             {
  742.                 this.QueryPause.Text = "Resume";
  743.                 if (GlobalVars.DebugLvl >= 1) LogBoxEntry("Querying: Paused");
  744.                
  745.             }
  746.         }
  747.  
  748.         private void QueryTimer_Tick(object sender, System.EventArgs e)
  749.         {
  750.  
  751.             QueryTimer.Enabled = false;
  752.            
  753.             if (GlobalVars.Running)
  754.             QueryCheck();
  755.  
  756.             QueryTimer.Enabled = true;
  757.            
  758.         }
  759.  
  760.         private void ReloadCFG_Click(object sender, EventArgs e)
  761.         {
  762.             LogBoxEntry("Clear LogBox");
  763.             LogBoxEntry("Reloaded config file, Log Box Cleared!");
  764.             ReadCFG();
  765.         }
  766.  
  767.         private void AutoScrollLabel_Click(object sender, EventArgs e) //Toggle AutoScrolling on the LogBox
  768.         {
  769.             GlobalVars.CarrotScroll = (!GlobalVars.CarrotScroll);
  770.             if (GlobalVars.CarrotScroll) //True Scroll
  771.             {
  772.                 this.AutoScrollLabel.Text = "AutoScroll: ON";
  773.             }
  774.             else
  775.             {
  776.                 this.AutoScrollLabel.Text = "AutoScroll: OFF";
  777.             }
  778.         }
  779.  
  780.         private void ClearLogBox_Click(object sender, EventArgs e)
  781.         {
  782.             LogBoxEntry("Clear LogBox");
  783.             LogBoxEntry("LogBox Cleared!");
  784.         }
  785.  
  786.         private void DebugLvlLabel_Click(object sender, EventArgs e)
  787.         {
  788.             if (GlobalVars.DebugLvl == 3)
  789.             {
  790.                 this.DebugLvlLabel.Text = "DebugLevel: OFF";
  791.                 GlobalVars.DebugLvl = 0;
  792.                 LogBoxEntry("DebugMode: OFF");
  793.  
  794.             }
  795.             else if (GlobalVars.DebugLvl == 0)
  796.             {
  797.                 this.DebugLvlLabel.Text = "DebugLevel: 1";
  798.                 GlobalVars.DebugLvl = 1;
  799.                 LogBoxEntry("DebugMode: Level 1");
  800.  
  801.             }
  802.             else if (GlobalVars.DebugLvl == 1)
  803.             {
  804.                 this.DebugLvlLabel.Text = "DebugLevel: 2";
  805.                 GlobalVars.DebugLvl = 2;
  806.                 LogBoxEntry("DebugMode: Level 2");
  807.  
  808.             }
  809.             else if (GlobalVars.DebugLvl == 2)
  810.             {
  811.                 this.DebugLvlLabel.Text = "DebugLevel: 3";
  812.                 GlobalVars.DebugLvl = 3;
  813.                 LogBoxEntry("DebugMode: Level 3");
  814.  
  815.             }
  816.  
  817.         }
  818.  
  819.     }
  820.     public class GlobalVars
  821.     {
  822.        
  823.         public static String Host;
  824.         public static String StopString;
  825.         public static String InputDir;
  826.         public static String OutputDir;
  827.         public static String Password;
  828.         public static String User;
  829.         public static String Schema;
  830.         public static String QSep;
  831.         public static String NullString;
  832.         public static FormWindowState Mini = FormWindowState.Normal;
  833.         public static bool CarrotScroll = true;
  834.         public static bool Running = true;
  835.         public static int DebugLvl = 0;
  836.         public static int QCount = 0;
  837.         public static int NQCount = 0;
  838.     }
  839. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement