public void CreateUserTableColumn(
            Dictionary <string, UserTable> userTables,
            IDataRecord reader)
        {
            var schemaName = Convert.ToString(reader[SchemaNameOrdinal]);
            var tableName  = Convert.ToString(reader[TableNameOrdinal]);

            //var commonDataType = DataTypes.GetCommonDataType(dataType);

            var userTableNamespaceBuilder = new StringBuilder(schemaName.Length + tableName.Length + 1);

            userTableNamespaceBuilder.Append(schemaName).Append(Constants.Dot).Append(tableName);

            var userTableNamespace = userTableNamespaceBuilder.ToString();

            if (!userTables.ContainsKey(userTableNamespace))
            {
                return;
            }

            var userTable = userTables[userTableNamespace];

            if (userTable == null)
            {
                return;
            }

            var userTableColumn = new UserTableColumn
            {
                UserTable     = userTable,
                ObjectName    = Convert.ToString(reader[ObjectNameOrdinal]),
                ColumnOrdinal = Convert.ToInt32(reader[ColumnOrdinalOrdinal]), // Note: naming scheme conflict, this is correct
                DataType      = Convert.ToString(reader[DataTypeOrdinal]),
                //CommonDataType = commonDataType,
                MaxLength        = Convert.ToInt64(reader[MaxLengthOrdinal]),
                Precision        = Convert.ToInt32(reader[PrecisionOrdinal]),
                Scale            = Convert.ToInt32(reader[ScaleOrdinal]),
                Collation        = Convert.ToString(reader[CollationOrdinal]),
                HasDefault       = Convert.ToBoolean(reader[HasDefaultOrdinal]),
                HasForeignKey    = Convert.ToBoolean(reader[HasForeignKeyOrdinal]),
                HasXmlCollection = Convert.ToBoolean(reader[HasXmlCollectionOrdinal]),
                IsUserDefined    = Convert.ToBoolean(reader[IsUserDefinedOrdinal]),
                IsAssemblyType   = Convert.ToBoolean(reader[IsAssemblyTypeOrdinal]),
                IsNullable       = Convert.ToBoolean(reader[IsNullableOrdinal]),
                IsAnsiPadded     = Convert.ToBoolean(reader[IsAnsiPaddedOrdinal]),
                IsRowGuidColumn  = Convert.ToBoolean(reader[IsRowGuidColumnOrdinal]),
                IsIdentity       = Convert.ToBoolean(reader[IsIdentityOrdinal]),
                IsComputed       = Convert.ToBoolean(reader[IsComputedOrdinal]),
                IsFileStream     = Convert.ToBoolean(reader[IsFileStreamOrdinal]),
                IsXmlDocument    = Convert.ToBoolean(reader[IsXmlDocumentOrdinal])
            };

            userTable.UserTableColumns.Add(userTableColumn);
        }
Exemple #2
0
        private string GetFieldTypeString(UserTableColumn column)
        {
            var type = "";

            switch (column.DATA_TYPE)
            {
            case "BFILE":
            case "BLOB":
            case "LONG RAW":
            case "RAW":
                type = "byte[]";
                break;

            case "CHAR":
            case "CLOB":
            case "LONG":
            case "NCHAR":
            case "NCLOB":
            case "NVARCHAR2":
            case "ROWID":
            case "VARCHAR2":
                type = "string";
                break;

            case "DATE":
            case "TIMESTAMP":
                type = "DateTime";
                break;

            case "FLOAT":
            case "INTEGER":
            case "NUMBER":
                type = "decimal";
                break;

            case "INTERVAL YEAR TO  MONTH":
                type = "int";
                break;

            case "INTERVAL DAY TO  SECOND":
                type = "TimeSpan";
                break;

            default:
                break;
            }

            if (column.NULLABLE == "Y" && type != "string")
            {
                type += "?";
            }
            return(type);
        }
        public override string AlterUserTableColumn(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn)
        {
            var builder = new StringBuilder();

            builder.Append("ALTER TABLE ");
            builder.Append(CreateIdentifier(userTableColumn.UserTable));
            builder.Append(" ALTER COLUMN ");
            builder.Append(CreateUserTableColumn(sourceDataContext, targetDataContext, userTableColumn, true).Trim());
            return(builder.ToString());
        }
        public override string DropUserTableColumn(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn)
        {
            var builder = new StringBuilder();

            builder.Append("ALTER TABLE ");
            builder.Append(CreateIdentifier(userTableColumn.UserTable));
            builder.Append(" DROP COLUMN `");
            builder.Append(userTableColumn.ObjectName);
            builder.Append("`");
            return(builder.ToString());
        }
        // creates user-table column
        public override string CreateUserTableColumn(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn, bool firstColumn = false)
        {
            var builder    = new StringBuilder();
            var definition = DataTypes.ConvertDataTypeDefinition(sourceDataContext, targetDataContext, userTableColumn);

            if (string.IsNullOrEmpty(definition))
            {
                return(null);
            }

            builder.Append(firstColumn ? "    " : "  , ");
            builder.Append(definition);
            return(builder.ToString());
        }
        /// <summary>
        /// Compares the Character Set between source and target user-table columns and even
        /// matches against differing data contexts.
        /// </summary>
        /// <param name="sourceDataContext">The source DataContext.</param>
        /// <param name="targetDataContext">The target DataContext.</param>
        /// <param name="sourceUserTableColumn">The source user-table column to get a character set from.</param>
        /// <param name="targetUserTableColumn">The target user-table column to get a character set from.</param>
        /// <returns>
        ///     True - The character sets matched between differing datasources.
        ///     False - The character sets could not be matched up.
        /// </returns>
        public static bool CompareCharacterSet(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn sourceUserTableColumn, UserTableColumn targetUserTableColumn)
        {
            // TODO: adjust to use sourceUserTableColumn.CharacterSet once added.
            if (string.IsNullOrEmpty(sourceUserTableColumn.Collation) && string.IsNullOrEmpty(targetUserTableColumn.Collation))
            {
                return(true);
            }

            if (string.IsNullOrEmpty(sourceUserTableColumn.Collation))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(targetUserTableColumn.Collation))
            {
                return(false);
            }

            if (sourceDataContext.ContextType == targetDataContext.ContextType)
            {
                if (StringComparer.OrdinalIgnoreCase.Compare(
                        sourceUserTableColumn.Collation, targetUserTableColumn.Collation) != 0)
                {
                    return(false);
                }
            }

            switch (sourceDataContext.ContextType)
            {
            case DataContextType.MySql:
                if (!MySqlCharacterSets.Contains(sourceUserTableColumn.Collation))
                {
                    return(true);
                }

                switch (targetDataContext.ContextType)
                {
                case DataContextType.SqlServer:
                    string collation;
                    return(!MySqlToSqlServerLookup.TryGetValue(sourceUserTableColumn.Collation, out collation)
                                ? true
                                : StringComparer.OrdinalIgnoreCase.Compare(
                               collation, targetUserTableColumn.Collation) == 0);

                default:
                    return(true);
                }

            case DataContextType.SqlServer:
                if (!SqlServerCharacterSets.Contains(sourceUserTableColumn.Collation))
                {
                    return(true);
                }

                switch (targetDataContext.ContextType)
                {
                case DataContextType.MySql:
                    string collation;
                    return(!SqlServerToMySqlLookup.TryGetValue(sourceUserTableColumn.Collation, out collation)
                                ? true
                                : StringComparer.OrdinalIgnoreCase.Compare(
                               collation, targetUserTableColumn.Collation) == 0);

                default:
                    return(true);
                }

            default:
                return(true);
            }
        }
        /// <summary>
        /// Matches Character Set against differing data contexts and returns a string containing
        /// the collation for the target data context or string.Empty if it was not found.
        /// </summary>
        /// <param name="sourceDataContext">The source DataContext.</param>
        /// <param name="targetDataContext">The target Datacontext.</param>
        /// <param name="userTableColumn">The user-table column to pull Character Set from.</param>
        /// <returns>
        ///     The target Character Set to use for the given user-table column or string.Empty if one was not found.
        /// </returns>
        public static string ConvertCharacterSet(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn)
        {
            // TODO: adjust to use sourceUserTableColumn.CharacterSet once added.
            if (string.IsNullOrEmpty(userTableColumn.Collation))
            {
                return(string.Empty);
            }

            if (sourceDataContext.ContextType == targetDataContext.ContextType)
            {
                return(userTableColumn.Collation);
            }

            switch (sourceDataContext.ContextType)
            {
            case DataContextType.MySql:
                if (!MySqlCharacterSets.Contains(userTableColumn.Collation))
                {
                    return(string.Empty);
                }

                switch (targetDataContext.ContextType)
                {
                case DataContextType.SqlServer:
                    string collation;
                    return(!MySqlToSqlServerLookup.TryGetValue(userTableColumn.Collation, out collation)
                                ? string.Empty
                                : collation);

                default:
                    return(string.Empty);
                }

            case DataContextType.SqlServer:
                if (!SqlServerCharacterSets.Contains(userTableColumn.Collation))
                {
                    return(string.Empty);
                }

                switch (targetDataContext.ContextType)
                {
                case DataContextType.MySql:
                    string collation;
                    return(!SqlServerToMySqlLookup.TryGetValue(userTableColumn.Collation, out collation)
                                ? string.Empty
                                : collation);

                default:
                    return(string.Empty);
                }

            default:
                return(string.Empty);
            }
        }
        protected override void SetIsoState(DataContext sourceDataContext, DataContext targetDataContext, DataSyncAction syncAction, StringCollection stringCollection, string delimiter)
        {
            switch (syncAction.Type)
            {
            case DataSyncOperationType.CreateAggregateFunction:
            case DataSyncOperationType.CreateInlineTableValuedFunction:
            case DataSyncOperationType.CreateScalarFunction:
            case DataSyncOperationType.CreateStoredProcedure:
            case DataSyncOperationType.CreateTableValuedFunction:
            case DataSyncOperationType.CreateTrigger:
            case DataSyncOperationType.CreateView:
            case DataSyncOperationType.AlterAggregateFunction:
            case DataSyncOperationType.AlterInlineTableValuedFunction:
            case DataSyncOperationType.AlterScalarFunction:
            case DataSyncOperationType.AlterStoredProcedure:
            case DataSyncOperationType.AlterTableValuedFunction:
            case DataSyncOperationType.AlterTrigger:
            case DataSyncOperationType.AlterView:

                IModule module = null;
                if (syncAction.DataObject is IModule)
                {
                    module = (IModule)syncAction.DataObject;
                }

                if (module != null)
                {
                    if (CurrentCatalog == null)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, module.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (string.Compare(module.Catalog.ObjectName, CurrentCatalog.ObjectName, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, module.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (module.UsesAnsiNulls && !CurrentAnsiNulls)
                    {
                        SetAnsiNullsOn(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                    else if (!module.UsesAnsiNulls && CurrentAnsiNulls)
                    {
                        SetAnsiNullsOff(stringCollection);
                        stringCollection.Add(delimiter);
                    }

                    if (module.UsesQuotedIdentifier && !CurrentQuotedIdentifier)
                    {
                        SetQuotedIdentifierOn(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                    else if (!module.UsesQuotedIdentifier && CurrentQuotedIdentifier)
                    {
                        SetQuotedIdentifierOff(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                }

                if (!CurrentAnsiPadding)
                {
                    SetAnsiPaddingOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                if (!CurrentAnsiWarnings)
                {
                    SetAnsiWarningsOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                break;

            case DataSyncOperationType.CreateUserTable:
                UserTable userTable = null;
                if (syncAction.DataObject is UserTable)
                {
                    userTable = (UserTable)syncAction.DataObject;
                }

                if (userTable != null)
                {
                    if (CurrentCatalog == null)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, userTable.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (string.Compare(userTable.Catalog.ObjectName, CurrentCatalog.ObjectName, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, userTable.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (userTable.UsesAnsiNulls && !CurrentAnsiNulls)
                    {
                        SetAnsiNullsOn(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                    else if (!userTable.UsesAnsiNulls && CurrentAnsiNulls)
                    {
                        SetAnsiNullsOff(stringCollection);
                        stringCollection.Add(delimiter);
                    }

                    var createWithAnsiPadding = true;
                    foreach (var userTableColumn in userTable.UserTableColumns)
                    {
                        if (!userTableColumn.IsAnsiPadded && IsDataTypeAnsiPaddable(userTableColumn.DataType))
                        {
                            createWithAnsiPadding = false;
                        }
                    }

                    if (createWithAnsiPadding && !CurrentAnsiPadding)
                    {
                        SetAnsiPaddingOn(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                    else if (!createWithAnsiPadding && CurrentAnsiPadding)
                    {
                        SetAnsiPaddingOff(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                }

                if (!CurrentQuotedIdentifier)
                {
                    SetQuotedIdentifierOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                if (!CurrentAnsiWarnings)
                {
                    SetAnsiWarningsOn(stringCollection);
                    stringCollection.Add(delimiter);
                }
                break;

            case DataSyncOperationType.AddUserTableColumn:
            case DataSyncOperationType.AlterUserTableColumn:
                if (!CurrentAnsiNulls)
                {
                    SetAnsiNullsOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                if (!CurrentQuotedIdentifier)
                {
                    SetQuotedIdentifierOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                UserTableColumn alteredUserTableColumn = null;
                if (syncAction.DataObject is UserTableColumn)
                {
                    alteredUserTableColumn = (UserTableColumn)syncAction.DataObject;
                }

                if (alteredUserTableColumn != null)
                {
                    if (CurrentCatalog == null)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, alteredUserTableColumn.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (string.Compare(alteredUserTableColumn.Catalog.ObjectName, CurrentCatalog.ObjectName, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, alteredUserTableColumn.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (alteredUserTableColumn.IsAnsiPadded && !CurrentAnsiPadding)
                    {
                        SetAnsiPaddingOn(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                    else if (!alteredUserTableColumn.IsAnsiPadded && CurrentAnsiPadding)
                    {
                        SetAnsiPaddingOff(stringCollection);
                        stringCollection.Add(delimiter);
                    }
                }

                if (CurrentAnsiWarnings)
                {
                    SetAnsiWarningsOff(stringCollection);
                    stringCollection.Add(delimiter);
                }

                break;

            default:
                ICatalog catalogBasedDataObject = null;
                if (syncAction.DataObject is ICatalog)
                {
                    catalogBasedDataObject = (ICatalog)syncAction.DataObject;
                }

                if (catalogBasedDataObject != null)
                {
                    if (CurrentCatalog == null)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, catalogBasedDataObject.Catalog);
                        stringCollection.Add(delimiter);
                    }

                    if (string.Compare(catalogBasedDataObject.Catalog.ObjectName, CurrentCatalog.ObjectName, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        ChangeCatalog(sourceDataContext, targetDataContext, stringCollection, catalogBasedDataObject.Catalog);
                        stringCollection.Add(delimiter);
                    }
                }

                if (!CurrentAnsiNulls)
                {
                    SetAnsiNullsOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                if (!CurrentQuotedIdentifier)
                {
                    SetQuotedIdentifierOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                if (!CurrentAnsiPadding)
                {
                    SetAnsiPaddingOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                if (!CurrentAnsiWarnings)
                {
                    SetAnsiWarningsOn(stringCollection);
                    stringCollection.Add(delimiter);
                }

                break;
            }
        }
        public static DataSyncAction AddUserTableColumn(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn)
        {
            var builder = new StringBuilder();

            builder.Append(StartThe)
            .Append(userTableColumn.Description)
            .Append(Space)
            .Append(userTableColumn.Namespace)
            .Append(EndCreated);
            Func <string> script = () => ContextScriptFactory.AddUserTableColumn(sourceDataContext, targetDataContext, userTableColumn);

            if (string.IsNullOrEmpty(script.Invoke()))
            {
                return(null);
            }
            return(new DataSyncAction(
                       userTableColumn
                       , userTableColumn.Namespace
                       , builder.ToString()
                       , DataSyncOperationType.AddUserTableColumn
                       , script
                       ));
        }
Exemple #10
0
 public abstract string AddUserTableColumn(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn);
Exemple #11
0
 public abstract string CreateUserTableColumn(DataContext sourceDataContext, DataContext targetDataContext, UserTableColumn userTableColumn, bool firstColumn = false);