-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] Renaming columns #106
Comments
I have tried a quick modification to produce the result as you described. Here is the sample project: This project uses a modified MySqlBackup.NET. The following are the changes: File: /MySqlBackup/MySqlBackup.cs In this method: private string Export_GetInsertStatementHeader(RowsDataExportMode rowsExportMode, string tableName, MySqlDataReader rdr) replace the following line: if (_database.Tables[tableName].Columns[_colname].IsGeneratedColumn)
continue; with: if (_database.Tables[tableName].Columns.Contains(_colname))
{
if (_database.Tables[tableName].Columns[_colname].IsGeneratedColumn)
continue;
} In this method: private string Export_GetValueString(MySqlDataReader rdr, MySqlTable table) replace the following line if (table.Columns[columnName].IsGeneratedColumn)
continue; with: if (table.Columns.Contains(columnName))
{
if (table.Columns[columnName].IsGeneratedColumn)
continue;
} in this method: private string Export_GetValueString(MySqlDataReader rdr, MySqlTable table) replace the following line: var col = table.Columns[columnName]; with: MySqlColumn col = null;
if (table.Columns.Contains(columnName))
{
col = table.Columns[columnName];
} |
There is one issue, if you replace the column name by an alias name, then the for example: This original table structure:
will no longer fit the generate
But I assume that the destination database that you are going to import to, will have the column name |
In this method: private string Export_GetValueString(MySqlDataReader rdr, MySqlTable table) The
will be passed into another method: QueryExpress.ConvertToSqlFormat(ob, true, true, col, ExportInfo.BlobExportMode) The These non-compatible Date or Time values will be converted into else if (ob is MySqlDateTime mdt)
{
if (mdt.IsValidDateTime)
{
DateTime dtime = mdt.GetDateTime();
if (wrapStringWithSingleQuote)
sb.AppendFormat("'");
if (col.MySqlDataType == "datetime")
sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));
else if (col.MySqlDataType == "date")
sb.AppendFormat(dtime.ToString("yyyy-MM-dd", _dateFormatInfo));
else if (col.MySqlDataType == "time")
sb.AppendFormat("{0}:{1}:{2}", mdt.Hour, mdt.Minute, mdt.Second);
else
sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));
if (col.TimeFractionLength > 0)
{
sb.Append(".");
sb.Append(((MySqlDateTime)ob).Microsecond.ToString().PadLeft(col.TimeFractionLength, '0'));
}
if (wrapStringWithSingleQuote)
sb.AppendFormat("'");
}
else
{
if (wrapStringWithSingleQuote)
sb.AppendFormat("'");
if (col.MySqlDataType == "datetime")
sb.AppendFormat("0000-00-00 00:00:00");
else if (col.MySqlDataType == "date")
sb.AppendFormat("0000-00-00");
else if (col.MySqlDataType == "time")
sb.AppendFormat("00:00:00");
else
sb.AppendFormat("0000-00-00 00:00:00");
if (col.TimeFractionLength > 0)
{
sb.Append(".".PadRight(col.TimeFractionLength, '0'));
}
if (wrapStringWithSingleQuote)
sb.AppendFormat("'");
}
} but since you are using alias column name, the MySqlBackup.NET library unable to guess the alias column name belongs to which column in MySQL table. In this case (in the occurance of if you do have these incompatible datetime values issues), another error (exception) will occur, where the However, there is another workaround to avoid this, which is by appending a connection string option:
example:
or using the ConnectionStringBuilder: MySqlConnectionStringBuilder consb = new MySqlConnectionStringBuilder()
{
Server = "localhost",
UserID = "root",
Password = "1234",
Database = "test1",
ConvertZeroDateTime = true
};
using (MySqlConnection conn = new MySqlConnection(consb.ToString()))
{
} By specifying This will by pass the need to refer to |
That's indeed the case.
Imo it should be handled by the library. Will those changes come with a release? |
It requires user input to match the It can be something like mb.ExportInfo.MatchAliasColumns(table_name, ori_column_name, alias_column_name); I can add it into the next release. The next release date is unscheduled at the moment. You may use the modified version of MySqlBackup.NET attached in previous reply (above). |
Alright, thank you. Feel free to close the issue at your discretion. |
I want to use TablesToBeExportedDic with a query that contains an alias, e.i. something like this "select
ID
as 'ID_ALIAS` from table;", and the script should result "insert into table (ID_ALIAS, ..."I am currently post-processing the script, but I think it's a good feature. Is there an existing workaround for this issue? If the feature solves a valid user case, I don't mind working on its implementation.
The text was updated successfully, but these errors were encountered: