![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
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 MySql.Data.MySqlClient; 00010 00011 namespace RoutineAnalyzer 00012 { 00017 class RoutineReader 00018 { 00019 #region Fields 00020 00021 MySqlConnectionStringBuilder _builder; 00022 00023 #endregion 00024 00025 #region Constants 00026 00027 //These are all parameterized MySQL statement that pull meta-data from the database on routines 00028 //NOTE: These are intended to only dissect procedures for now 00029 const string showProcQuery = "SHOW CREATE PROCEDURE {0}.{1}"; 00030 const string showProcs = "select specific_name from INFORMATION_SCHEMA.routines where routine_schema = '{0}' and routine_type = 'PROCEDURE'"; 00031 const string procedureMarker = "PROCEDURE"; 00032 00033 #endregion 00034 00035 #region Methods 00036 00042 public RoutineReader(MySqlConnectionStringBuilder builder) 00043 { 00044 _builder = builder; 00045 } 00046 00053 static string GetShowProcQuery(string schema, string procName) 00054 { 00055 return string.Format(showProcQuery, schema, procName); 00056 } 00057 00063 static string GetShowAllProcsQuery(string schema) 00064 { 00065 return string.Format(showProcs, schema); 00066 } 00067 00073 private static DataTable CreateTableFromCommand(MySqlCommand command) 00074 { 00075 using (MySqlDataReader reader = command.ExecuteReader()) 00076 { 00077 //use a datatable to find the third column of the first row, that is the definition 00078 DataTable table = new DataTable(); 00079 table.Load(reader); 00080 return table; 00081 } 00082 } 00083 00089 DataTable GetAllProcNames(MySqlConnection connection) 00090 { 00091 using (MySqlCommand command = connection.CreateCommand()) 00092 { 00093 command.CommandText = GetShowAllProcsQuery(_builder.Database); 00094 return CreateTableFromCommand(command); 00095 } 00096 } 00097 00104 string GetProcDefintion(MySqlConnection connection, string procName) 00105 { 00106 //create a command and query the db for the proc definition 00107 using (MySqlCommand command = connection.CreateCommand()) 00108 { 00109 command.CommandText = GetShowProcQuery(_builder.Database, procName); 00110 //use a datatable to find the third column of the first row, that is the definition 00111 DataTable table = CreateTableFromCommand(command); 00112 return table.Rows[0][2].ToString(); 00113 } 00114 } 00115 00121 public List<string> GetProcedureDefintions() 00122 { 00123 using (MySqlConnection connection = new MySqlConnection(_builder.ToString())) 00124 { 00125 connection.Open(); 00126 DataTable table = GetAllProcNames(connection); 00127 List<string> procedures = new List<string>(); 00128 foreach (DataRow row in table.Rows) 00129 { 00130 //retrieve the whole definition 00131 string definition = GetProcDefintion(connection, row[0].ToString()); 00132 //pull out just the first line 00133 int firstNewline = definition.IndexOf("\n"); 00134 string paramters = definition.Substring(0, firstNewline + 1); 00135 //remove all of the special single quotes 00136 paramters = paramters.Replace("`", ""); 00137 //remove from the beginning to the end of the marker, plus one for the extra space 00138 int startOfProcedure = paramters.IndexOf(procedureMarker); 00139 paramters = paramters.Remove(0, startOfProcedure + procedureMarker.Length +1); 00140 //add this to the list of procedures 00141 procedures.Add(paramters); 00142 } 00143 00144 return procedures; 00145 } 00146 } 00147 00148 public DataTable GetProcedureDefinitionAsDataTable() 00149 { 00150 using (MySqlConnection connection = new MySqlConnection(_builder.ToString())) 00151 { 00152 connection.Open(); 00153 return GetAllProcNames(connection); 00154 } 00155 } 00156 00157 #endregion 00158 } 00159 }
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.