/// <summary>
        /// Resolve Database operation from <paramref name="expression"/><br/>
        /// <br/>
        /// i.e: AlterColumnExpression => AlterColumn, CreateTableExpression => CreateTable, AlterTableExpression => AlterTable
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static string GetDbOperationFromExpression(this IMigrationExpression expression)
        {
            var dbOperation = expression.GetType().Name;
            var index       = dbOperation.IndexOf("Expression", StringExtensions.CurrentIgnoreCaseStringComparison);

            if (index > -1)
            {
                dbOperation = dbOperation.Substring(0, index);
            }

            return(dbOperation);
        }
Beispiel #2
0
        protected string GenerateSql(IMigrationExpression expression)
        {
            string result = null;

            try
            {
                MethodInfo generatorMethod = Processor.Generator.GetType().GetMethod("Generate", new Type[] { expression.GetType() });
                if (generatorMethod == null)
                {
                    throw new ArgumentOutOfRangeException(String.Format("Can't find generator for {0}", expression.ToString()));
                }

                result = generatorMethod.Invoke(Processor.Generator, new object[] { expression }) as string;
            }
            catch (Exception e)
            {
                throw new ArgumentOutOfRangeException(String.Format("Can't find generator for {0}", expression.ToString()), e);
            }
            return(result);
        }
Beispiel #3
0
        public static string GetSqlEquivalent(this IMigrationExpression migrationExpression, IMigrationGenerator migrationGenerator)
        {
            if (migrationExpression is ExecuteSqlScriptExpression executeSqlScriptExpression)
            {
                return(executeSqlScriptExpression.SqlScript);
            }
            else if (migrationExpression is ExecuteSqlStatementExpression executeSqlStatementExpression)
            {
                return(executeSqlStatementExpression.SqlStatement);
            }
            else
            {
                if (migrationExpression is CreateForeignKeyExpression cfke)
                {
                    cfke.ForeignKey.ForeignColumns = cfke.ForeignKey.ForeignColumns.Select(c => c.ToSnakeCase()).ToList();
                    cfke.ForeignKey.PrimaryColumns = cfke.ForeignKey.PrimaryColumns.Select(c => c.ToSnakeCase()).ToList();
                    cfke.ForeignKey.ForeignTable   = cfke.ForeignKey.ForeignTable.ToSnakeCase();
                    cfke.ForeignKey.PrimaryTable   = cfke.ForeignKey.PrimaryTable.ToSnakeCase();
                    cfke.ForeignKey.Name           = cfke.ForeignKey.Name ??
                                                     $"FK_{cfke.ForeignKey.ForeignTable}_{cfke.ForeignKey.ForeignColumns.First()}_{cfke.ForeignKey.PrimaryTable}_{cfke.ForeignKey.PrimaryColumns.First()}";
                }
                else if (migrationExpression is CreateTableExpression cte)
                {
                    List <ColumnDefinition> columnDefinitions = cte.Columns.ToList();
                    columnDefinitions.ForEach(cd => cd.Name = cd.Name.ToSnakeCase());
                    cte.Columns = columnDefinitions;
                }
                else if (migrationExpression is CreateConstraintExpression cce && cce.Constraint.IsPrimaryKeyConstraint)
                {
                    cce.Constraint.Columns        = cce.Constraint.Columns.Select(c => c.ToSnakeCase()).ToList();
                    cce.Constraint.ConstraintName = $"PK_{cce.Constraint.TableName}";
                }
                MethodInfo processMethod = migrationGenerator.GetType().GetMethod("Generate", new[] { migrationExpression.GetType() });
                string     sql           = (string)processMethod.Invoke(migrationGenerator, new[] { migrationExpression });

                return(sql);
            }
        }