mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#19532: Implementing custom AlterColumnCommand query for MySql
Work Item: 19532 --HG-- branch : 1.x
This commit is contained in:
@@ -4,7 +4,6 @@ using System.Data;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NHibernate;
|
||||
using NHibernate.Dialect;
|
||||
using NHibernate.SqlTypes;
|
||||
using Orchard.ContentManagement.Records;
|
||||
@@ -75,7 +74,7 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
Visit(builder, createColumn);
|
||||
}
|
||||
|
||||
var primaryKeys = command.TableCommands.OfType<CreateColumnCommand>().Where(ccc => ccc.IsPrimaryKey).Select(ccc => ccc.ColumnName);
|
||||
var primaryKeys = command.TableCommands.OfType<CreateColumnCommand>().Where(ccc => ccc.IsPrimaryKey).Select(ccc => ccc.ColumnName).ToArray();
|
||||
if (primaryKeys.Any()) {
|
||||
if (appendComma) {
|
||||
builder.Append(", ");
|
||||
@@ -191,7 +190,7 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
|
||||
// type
|
||||
if (command.DbType != DbType.Object) {
|
||||
builder.Append(GetTypeName(command.DbType, command.Length, command.Precision, command.Scale));
|
||||
builder.Append(GetTypeName(_dialect, command.DbType, command.Length, command.Precision, command.Scale));
|
||||
}
|
||||
else {
|
||||
if(command.Length > 0 || command.Precision > 0 || command.Scale > 0) {
|
||||
@@ -278,23 +277,12 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
RunPendingStatements();
|
||||
}
|
||||
|
||||
private string GetTypeName(DbType dbType, int? length, byte precision, byte scale) {
|
||||
|
||||
// NHibernate has a bug in MsSqlCeDialect, as it's declaring the decimal type as this:
|
||||
// NUMERIC(19, $1), where $1 is the Length parameter, and it's wrong. It should be
|
||||
// NUMERIC(19, $s) in order to use the Scale parameter, as it's done for SQL Server dialects
|
||||
// https://nhibernate.jira.com/browse/NH-2979
|
||||
if (_dialect is NHibernate.Dialect.MsSqlCeDialect
|
||||
&& dbType == DbType.Decimal
|
||||
&& scale != 0) {
|
||||
return _dialect.GetTypeName(new SqlType(dbType), scale, precision, scale);
|
||||
}
|
||||
|
||||
public static string GetTypeName(Dialect dialect, DbType dbType, int? length, byte precision, byte scale) {
|
||||
return precision > 0
|
||||
? _dialect.GetTypeName(new SqlType(dbType, precision, scale))
|
||||
? dialect.GetTypeName(new SqlType(dbType, precision, scale))
|
||||
: length.HasValue
|
||||
? _dialect.GetTypeName(new SqlType(dbType, length.Value))
|
||||
: _dialect.GetTypeName(new SqlType(dbType));
|
||||
? dialect.GetTypeName(new SqlType(dbType, length.Value))
|
||||
: dialect.GetTypeName(new SqlType(dbType));
|
||||
}
|
||||
|
||||
private void Visit(StringBuilder builder, CreateColumnCommand command) {
|
||||
@@ -306,7 +294,7 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
builder.Append(_dialect.QuoteForColumnName(command.ColumnName)).Append(Space);
|
||||
|
||||
if (!command.IsIdentity || _dialect.HasDataTypeInIdentityColumn) {
|
||||
builder.Append(GetTypeName(command.DbType, command.Length, command.Precision, command.Scale));
|
||||
builder.Append(GetTypeName(_dialect, command.DbType, command.Length, command.Precision, command.Scale));
|
||||
}
|
||||
|
||||
// append identity if handled
|
||||
@@ -368,7 +356,7 @@ namespace Orchard.Data.Migration.Interpreters {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string ConvertToSqlValue(object value) {
|
||||
public static string ConvertToSqlValue(object value) {
|
||||
if ( value == null ) {
|
||||
return "null";
|
||||
}
|
||||
|
@@ -0,0 +1,64 @@
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using NHibernate.Dialect;
|
||||
using Orchard.Data.Migration.Schema;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Localization;
|
||||
|
||||
namespace Orchard.Data.Migration.Interpreters {
|
||||
public class MySqlCommandInterpreter : ICommandInterpreter<AlterColumnCommand> {
|
||||
private readonly Dialect _dialect;
|
||||
private readonly ShellSettings _shellSettings;
|
||||
|
||||
public MySqlCommandInterpreter() {
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public string DataProvider {
|
||||
get { return "MySql"; }
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public MySqlCommandInterpreter(
|
||||
ShellSettings shellSettings,
|
||||
ISessionFactoryHolder sessionFactoryHolder) {
|
||||
_shellSettings = shellSettings;
|
||||
var configuration = sessionFactoryHolder.GetConfiguration();
|
||||
_dialect = Dialect.GetDialect(configuration.Properties);
|
||||
}
|
||||
|
||||
public string[] CreateStatements(AlterColumnCommand command) {
|
||||
var builder = new StringBuilder();
|
||||
|
||||
builder.AppendFormat("alter table {0} modify column {1} ",
|
||||
_dialect.QuoteForTableName(PrefixTableName(command.TableName)),
|
||||
_dialect.QuoteForColumnName(command.ColumnName));
|
||||
|
||||
// type
|
||||
if (command.DbType != DbType.Object) {
|
||||
builder.Append(DefaultDataMigrationInterpreter.GetTypeName(_dialect, command.DbType, command.Length, command.Precision, command.Scale));
|
||||
}
|
||||
else {
|
||||
if (command.Length > 0 || command.Precision > 0 || command.Scale > 0) {
|
||||
throw new OrchardException(T("Error while executing data migration: you need to specify the field's type in order to change its properties"));
|
||||
}
|
||||
}
|
||||
|
||||
// [default value]
|
||||
if (command.Default != null) {
|
||||
builder.Append(" set default ").Append(DefaultDataMigrationInterpreter.ConvertToSqlValue(command.Default)).Append(" ");
|
||||
}
|
||||
|
||||
return new [] {
|
||||
builder.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
private string PrefixTableName(string tableName) {
|
||||
if (string.IsNullOrEmpty(_shellSettings.DataTablePrefix))
|
||||
return tableName;
|
||||
return _shellSettings.DataTablePrefix + "_" + tableName;
|
||||
}
|
||||
}
|
||||
}
|
@@ -173,6 +173,7 @@
|
||||
<Compile Include="Data\AbstractSessionInterceptor.cs" />
|
||||
<Compile Include="Data\Bags\SArray.cs" />
|
||||
<Compile Include="Data\FetchRequest.cs" />
|
||||
<Compile Include="Data\Migration\Interpreters\MySqlCommandInterpreter.cs" />
|
||||
<Compile Include="DisplayManagement\Arguments.cs" />
|
||||
<Compile Include="Data\Bags\SConvert.cs" />
|
||||
<Compile Include="Data\Bags\Serialization\IBagSerializer.cs" />
|
||||
|
Reference in New Issue
Block a user