From 87bdf9c413318eee6c95122199d11aa8c7ca7b0e Mon Sep 17 00:00:00 2001 From: siyamandayubi Date: Thu, 16 Mar 2017 20:04:52 +0100 Subject: [PATCH] Fix MySQL support (#7607) Fixes #7602 --- .../DefaultDataMigrationInterpreter.cs | 4 +- .../Interpreters/MySqlCommandInterpreter.cs | 61 ++++++++++++++----- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs b/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs index f3fcab8b8..b940ed089 100644 --- a/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs +++ b/src/Orchard/Data/Migration/Interpreters/DefaultDataMigrationInterpreter.cs @@ -405,7 +405,7 @@ namespace Orchard.Data.Migration.Interpreters { if ( value == null ) { return "null"; } - + TypeCode typeCode = Type.GetTypeCode(value.GetType()); switch (typeCode) { case TypeCode.Empty: @@ -428,7 +428,7 @@ namespace Orchard.Data.Migration.Interpreters { case TypeCode.Decimal: return Convert.ToString(value, CultureInfo.InvariantCulture); case TypeCode.DateTime: - return String.Concat("'", Convert.ToString(value, CultureInfo.InvariantCulture), "'"); + return String.Concat("'", ((DateTime)value).ToString("yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture), "'"); } return "null"; diff --git a/src/Orchard/Data/Migration/Interpreters/MySqlCommandInterpreter.cs b/src/Orchard/Data/Migration/Interpreters/MySqlCommandInterpreter.cs index 6ae15d78f..562fd46d7 100644 --- a/src/Orchard/Data/Migration/Interpreters/MySqlCommandInterpreter.cs +++ b/src/Orchard/Data/Migration/Interpreters/MySqlCommandInterpreter.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Linq; using System.Text; using NHibernate.Dialect; using Orchard.Data.Migration.Schema; @@ -8,12 +9,14 @@ using Orchard.Environment.Configuration; using Orchard.Localization; namespace Orchard.Data.Migration.Interpreters { - public class MySqlCommandInterpreter : ICommandInterpreter { + public class MySqlCommandInterpreter : ICommandInterpreter, ICommandInterpreter { private readonly Lazy _dialectLazy; private readonly ShellSettings _shellSettings; + private readonly ITransactionManager _transactionManager; private readonly DefaultDataMigrationInterpreter _dataMigrationInterpreter; - public MySqlCommandInterpreter(DefaultDataMigrationInterpreter dataMigrationInterpreter) { + public MySqlCommandInterpreter(DefaultDataMigrationInterpreter dataMigrationInterpreter, ITransactionManager transactionManager) { + _transactionManager = transactionManager; _dataMigrationInterpreter = dataMigrationInterpreter; T = NullLocalizer.Instance; } @@ -26,14 +29,16 @@ namespace Orchard.Data.Migration.Interpreters { public MySqlCommandInterpreter( ShellSettings shellSettings, - ISessionFactoryHolder sessionFactoryHolder) { - _shellSettings = shellSettings; - _dialectLazy = new Lazy(() => Dialect.GetDialect(sessionFactoryHolder.GetConfiguration().Properties)); + ISessionFactoryHolder sessionFactoryHolder, + ITransactionManager transactionManager) { + _shellSettings = shellSettings; + _transactionManager = transactionManager; + _dialectLazy = new Lazy(() => Dialect.GetDialect(sessionFactoryHolder.GetConfiguration().Properties)); } public string[] CreateStatements(AlterColumnCommand command) { var builder = new StringBuilder(); - + builder.AppendFormat("alter table {0} modify column {1} ", _dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)), _dialectLazy.Value.QuoteForColumnName(command.ColumnName)); @@ -42,8 +47,7 @@ namespace Orchard.Data.Migration.Interpreters { // type if (command.DbType != DbType.Object) { builder.Append(DefaultDataMigrationInterpreter.GetTypeName(_dialectLazy.Value, command.DbType, command.Length, command.Precision, command.Scale)); - } - else { + } 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")); } @@ -56,7 +60,7 @@ namespace Orchard.Data.Migration.Interpreters { _dialectLazy.Value.QuoteForTableName(PrefixTableName(command.TableName)), _dialectLazy.Value.QuoteForColumnName(command.ColumnName)); var initLength2 = builder2.Length; - + if (command.Default != null) { builder2.Append(" set default ").Append(_dataMigrationInterpreter.ConvertToSqlValue(command.Default)).Append(" "); } @@ -64,13 +68,11 @@ namespace Orchard.Data.Migration.Interpreters { // result var result = new List(); - if (builder.Length > initLength) - { + if (builder.Length > initLength) { result.Add(builder.ToString()); } - if (builder2.Length > initLength2) - { + if (builder2.Length > initLength2) { result.Add(builder2.ToString()); } @@ -82,5 +84,36 @@ namespace Orchard.Data.Migration.Interpreters { return tableName; return _shellSettings.DataTablePrefix + "_" + tableName; } + + public string[] CreateStatements(AddIndexCommand command) { + var session = _transactionManager.GetSession(); + + using (var sqlCommand = session.Connection.CreateCommand()) { + var columnNames = String.Join(", ", command.ColumnNames.Select(c => string.Format("'{0}'", c))); + var tableName = PrefixTableName(command.TableName); + // check whether the index contains big nvarchar columns or text fields + string sql = @"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = '{1}' AND COLUMN_NAME in ({0}) AND TABLE_SCHEMA = '{2}' AND + ((Data_type = 'varchar' and CHARACTER_MAXIMUM_LENGTH > 767) OR data_type= 'text');"; + + sql = string.Format(sql, columnNames, tableName, session.Connection.Database); + sqlCommand.CommandText = sql; + + var columnList = command.ColumnNames.ToList(); + using (var reader = sqlCommand.ExecuteReader()) { + // Provide prefix for string columns with length longer than 767 + while (reader.Read()) { + var columnName = reader.GetString(0); + columnList[columnList.IndexOf(columnName)] = string.Format("{0}(767)", columnName); + } + } + + return new[] {string.Format("create index {1} on {0} ({2}) ", + _dialectLazy.Value.QuoteForTableName(tableName), + _dialectLazy.Value.QuoteForTableName(PrefixTableName(command.IndexName)), + String.Join(", ", columnList))}; + + } + } } -} +} \ No newline at end of file