Shadowrun: Awakened 29 September 2011 - Build 871
AnalyzerForm.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.ComponentModel;
00004 using System.Data;
00005 using System.Drawing;
00006 using System.Linq;
00007 using System.Text;
00008 using System.Windows.Forms;
00009 using Microsoft.Win32;
00010 using MySql.Data.MySqlClient;
00011 
00012 namespace RoutineAnalyzer
00013 {
00017     public partial class AnalyzerForm : Form
00018     {
00022         public AnalyzerForm()
00023         {
00024             //required call
00025             InitializeComponent();
00026 
00027             //performed read from persistence
00028             try
00029             {
00030                 //load the form values from the registry (using last params used)
00031                 RegistryKey key = GetRegistryKey("Software\\MySQL Routine Analyzer");
00032                 _login.Text = key.GetValue("Login").ToString();
00033                 _password.Text = key.GetValue("Password").ToString();
00034                 _schema.Text = key.GetValue("Schema").ToString();
00035             }
00036             catch
00037             {
00038                 //if it fails, we don't really care
00039             }
00040         }
00041 
00047         private RegistryKey GetRegistryKey(string path)
00048         {
00049             //otherwise create the key and return it
00050             return Registry.CurrentUser.CreateSubKey(path);
00051         }
00052 
00059         private RoutineBase CreateRoutineDesc(string decl, bool isStreamed)
00060         {
00061             if (_cppRadioBtn.Checked)
00062             {
00063                 if (isStreamed)
00064                     return new CppGetRoutine(decl);
00065                 else
00066                     return new CppSetRoutine(decl);
00067             }
00068             else if (_uscriptRadioBtn.Checked)
00069             {
00070                 if (isStreamed)
00071                     return new UScriptGetRoutine(decl);
00072                 else
00073                     return new UScriptSetRoutine(decl);
00074             }
00075             else
00076             {
00077                 if (isStreamed)
00078                     return new CppWrapperGetRoutine(decl);
00079                 else
00080                     return new CppWrapperSetRoutine(decl);
00081             }
00082         }
00083 
00087         void FormatCode()
00088         {
00089             string[] lines = _code.Text.Split('\n');
00090             string tabs = "";
00091             if (_cppRadioBtn.Checked)
00092                 tabs = "\t\t";
00093             int increaseForNextIter = 0;
00094             StringBuilder builder = new StringBuilder();
00095             foreach (string line in lines)
00096             {
00097                 //when an open brace is found, we actually wait until
00098                 //the next line to increase the tabs
00099                 //when it's a closed brace, we do it immediately
00100                 if (increaseForNextIter == 1)
00101                     tabs += "\t";
00102 
00103                 increaseForNextIter = 0;
00104                 if (line == "{")
00105                     increaseForNextIter = 1;
00106                 else if (line == "}")
00107                     tabs = tabs.Remove(0, 1);
00108 
00109                 builder.AppendLine(line.Insert(0, tabs));
00110             }
00111 
00112             _code.Text = builder.ToString();
00113         }
00114 
00119         private MySqlConnectionStringBuilder CreateBuilder()
00120         {
00121             //build the connection string to be returned
00122             MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
00123             builder.Server = "localhost";
00124             builder.UserID = _login.Text;
00125             builder.Password = _password.Text;
00126             builder.Database = _schema.Text;
00127 
00128             ParseHelper.SqlBuilder = builder;
00129 
00130             try
00131             {
00132                 //save the current values to the registry, so next time we start the app we can use them
00133                 using (RegistryKey key = GetRegistryKey("Software\\MySQL Routine Analyzer"))
00134                 {
00135                     key.SetValue("Login", _login.Text);
00136                     key.SetValue("Password", _password.Text);
00137                     key.SetValue("Schema", _schema.Text);
00138                     key.Flush();
00139                 }
00140             }
00141             catch
00142             {
00143                 //if this fails, we don't really care (it's not a critical feature)
00144             }
00145 
00146             return builder;
00147         }
00148 
00152         private void GenerateCode()
00153         {
00154             try
00155             {
00156                 //create the connection criteria
00157                 MySqlConnectionStringBuilder builder = CreateBuilder();
00158                 //pass it off to the routine reader to retrieve the procedure code
00159                 RoutineReader reader = new RoutineReader(builder);
00160                 //get a list of the definitions for further parsing
00161                 List<string> definitions = reader.GetProcedureDefintions();
00162 
00163                 //each definition must be parsed and added to the textbox
00164                 StringBuilder b = new StringBuilder();
00165 
00166                 //build the code
00167                 DataTable table = _sprocGrid.DataSource as DataTable;
00168                 for (int i = 0; i < definitions.Count; ++i)
00169                 {
00170                     bool isStreamed = false;
00171                     if (table != null)
00172                     {
00173                         string tableRowsToString = table.Rows[i][1].ToString();
00174                         isStreamed = tableRowsToString == "True";
00175                     }
00176 
00177                     //create a routine descriptor to pull apart the definition
00178                     RoutineBase desc = CreateRoutineDesc(definitions[i], isStreamed);
00179                     //forward engineer the C++ and add it
00180                     b.Append(desc.GetDeclaration());
00181                     b.AppendLine(";");
00182                 }
00183 
00184                 b.AppendLine();
00185                 b.AppendLine();
00186 
00187                 for (int i = 0; i < definitions.Count; ++i)
00188                 {
00189                     bool isStreamed = false;
00190                     if (table != null)
00191                     {
00192                         string tableRowsToString = table.Rows[i][1].ToString();
00193                         isStreamed = tableRowsToString == "True";
00194                     }
00195 
00196                     //create a routine descriptor to pull apart the definition
00197                     RoutineBase desc = CreateRoutineDesc(definitions[i], isStreamed);
00198                     //forward engineer the C++ and add it
00199                     b.AppendLine(desc.GetCode());
00200                     b.AppendLine();
00201                 }
00202 
00203                 _code.Text = b.ToString();
00204 
00205                 //format the code for tab spacing
00206                 FormatCode();
00207             }
00208             catch (Exception ex)
00209             {
00210                 MessageBox.Show("Error during analysis: " + ex.Message);
00211             }
00212         }
00213 
00214         #region Event Handlers
00215 
00222         private void OnAnalyzeBtnClick(object sender, EventArgs e)
00223         {
00224             GenerateCode();
00225         }
00226 
00227         private void OnScanDatabaseClick(object sender, EventArgs e)
00228         {
00229             MySqlConnectionStringBuilder builder = CreateBuilder();
00230             //pass it off to the routine reader to retrieve the procedure code
00231             RoutineReader reader = new RoutineReader(builder);
00232             DataTable table = reader.GetProcedureDefinitionAsDataTable();
00233             table.Columns.Add("isStream");
00234             foreach (DataRow row in table.Rows)
00235             {
00236                 string sprocName = row[0].ToString();
00237                 row[1] = sprocName.Contains("get");
00238             }
00239             _sprocGrid.DataSource = table;
00240         }
00241 
00242         #endregion
00243     }
00244 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net