static public string GetObjectCreate(MySqlConnector conn, DbObjectType type, string objectName, bool ifNotExists) { string resultColumn = ""; switch (type) { case DbObjectType.PROCEDURE: resultColumn = "Create Procedure"; break; case DbObjectType.FUNCTION: resultColumn = "Create Function"; break; case DbObjectType.EVENT: resultColumn = "Create Event"; break; case DbObjectType.VIEW: resultColumn = "Create View"; break; case DbObjectType.TABLE: resultColumn = "Create Table"; break; } string sql = string.Format(@"SHOW CREATE {0} {1}", type.ToString(), objectName); using (DataReaderBase reader = conn.ExecuteReader(sql)) { while (reader.Read()) { string create = StringFromDb(reader[resultColumn]); if (ifNotExists && create != null && create.StartsWith(@"CREATE TABLE")) { create = @"CREATE TABLE IF NOT EXISTS" + create.Substring(@"CREATE TABLE".Length); } return create; } } return null; }
static private void ExportTableData(MySqlConnector conn, StreamWriter writer, BackupOptions options) { List<string> lstTables = GetObjectList(conn, DbObjectType.TABLE); foreach (string table in lstTables) { if (IsView(conn, table)) { continue; } Query query = options.GetTableDataQuery(table); using (DataReaderBase reader = (query == null ? conn.ExecuteReader(string.Format(@"SELECT * FROM {0}", table)) : query.ExecuteReader(conn))) { while (reader.Read()) { writer.Write(string.Format(@"INSERT INTO {0} (", table)); for (Int32 idx = 0, count = reader.GetColumnCount(); idx < count; idx++) { if (idx > 0) writer.Write(@","); writer.Write(conn.EncloseFieldName(reader.GetColumnName(idx))); } writer.Write(@") VALUES("); for (Int32 idx = 0, count = reader.GetColumnCount(); idx < count; idx++) { if (idx > 0) writer.Write(@","); writer.Write(conn.PrepareValue(reader[idx])); } writer.Write(string.Format(@") {0}{1}", DELIMITER, NEW_LINE)); } } } }
static private void ExportTriggers(MySqlConnector conn, StreamWriter writer) { using (DataReaderBase reader = conn.ExecuteReader(@"SHOW TRIGGERS")) { while (reader.Read()) { writer.WriteLine(@"CREATE TRIGGER {0} ", StringFromDb(reader[@"Trigger"])); writer.WriteLine(@"{0} {1} ON {2} ", StringFromDb(reader[@"Timing"]), StringFromDb(reader[@"Event"]), StringFromDb(reader[@"Table"])); writer.WriteLine(@"FOR EACH ROW "); writer.WriteLine(@"{0} {1}{2}", StringFromDb(reader[@"Statement"]), DELIMITER, NEW_LINE); } } }
static public List<string> GetObjectList(MySqlConnector conn, DbObjectType type) { string sql = null; switch (type) { case DbObjectType.TABLE: sql = @"SHOW TABLES"; break; default: sql = string.Format(@"SHOW {0} STATUS", type.ToString()); break; } List<string> lstResults = new List<string>(); using (DataReaderBase reader = conn.ExecuteReader(sql)) { while (reader.Read()) { switch (type) { case DbObjectType.TABLE: lstResults.Add(reader.GetStringOrEmpty(0)); break; default: lstResults.Add((string)reader[@"Name"]); break; } } } return lstResults; }