internal class MySqlSchema
{
public bool GetSchema(Database database)
{
string str;
string connectionString = database.ConnectionString;
Match match = Regex.Match(connectionString, @"Database=(?<Database>[^\;]*);");
if (match.Success)
{
str = match.Groups["Database"].Value;
connectionString = connectionString.Replace(string.Format("Database={0};", str), "Database=information_schema;");
}
else
{
return false;
}
DBHelper helper = new DBHelper(DBHelper.DatabaseTypes.MySql, connectionString);
try
{
DataSet set = helper.ExecuteQuery(CommandType.Text, string.Format("select distinct TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='{0}' and TABLE_TYPE='BASE TABLE'", str), null);
foreach (DataRow row in set.Tables[0].Rows)
{
string str3 = row[0].ToString();
Table table = new Table();
table.Name = str3;
DataSet set2 = helper.ExecuteQuery(CommandType.Text, string.Format("select * from COLUMNS where TABLE_SCHEMA='{0}' and TABLE_NAME='{1}'", str, str3), null);
foreach (DataRow row2 in set2.Tables[0].Rows)
{
Field field = new Field();
field.AllowNull = row2["IS_NULLABLE"].ToString().ToLower() == "yes";
field.MySqlTypeString = ObjectHelper.GetString(row2["DATA_TYPE"]);
field.DefaultValue = ObjectHelper.GetString(row2["COLUMN_DEFAULT"]);
field.FieldDescn = ObjectHelper.GetString(row2["COLUMN_COMMENT"]);
field.FieldLength = ObjectHelper.GetLong(row2["CHARACTER_MAXIMUM_LENGTH"]);
field.FieldName = ObjectHelper.GetString(row2["COLUMN_NAME"]);
field.FieldNumber = ObjectHelper.GetInt(row2["ORDINAL_POSITION"]);
table.Fields.Add(field);
}
database.Tables.Add(table);
}
DataSet set3 = helper.ExecuteQuery(CommandType.Text, string.Format("select distinct TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='{0}' and TABLE_TYPE='VIEW'", str), null);
foreach (DataRow row3 in set3.Tables[0].Rows)
{
database.Views.Add(row3[0].ToString());
}
DataSet set4 = helper.ExecuteQuery(CommandType.Text, string.Format("select distinct SPECIFIC_NAME from ROUTINES where ROUTINE_SCHEMA='{0}'", str), null);
foreach (DataRow row3 in set4.Tables[0].Rows)
{
database.StoreProcedures.Add(row3[0].ToString());
}
return true;
}
catch
{
return false;
}
}
}