示例#1
0
        private string GetIndicatorText(Table table, Column column)
        {
            List<string> indicators = new List<string>();

            if (column.InPrimaryKey)
            {
                indicators.Add("PK");
            }

            var fkIndex = table.IndexOfForeignKey(column);
            if (fkIndex >= 0)
            {
                indicators.Add("FK" + (fkIndex + 1).ToString());
            }

            return string.Join(", ", indicators);
        }
示例#2
0
        public static string Resolve(Column column, string typeString)
        {
            string result = typeString;
            if (column.ColumnType.MaxLength.HasValue)
            {
                result = result.Replace("%maxLength%", column.ColumnType.MaxLength.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
            }
            if (column.ColumnType.Precision.HasValue)
            {
                result = result.Replace("%precision%", column.ColumnType.Precision.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
            }
            if (column.ColumnType.Scale.HasValue)
            {
                result = result.Replace("%scale", column.ColumnType.Scale.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
            }

            return result;
        }
示例#3
0
 public void Add(Column column)
 {
     List.Add(column);
 }
示例#4
0
 private string GetColumnText(Table table, Column column)
 {
     return column.Name; // TODO: Improve
 }
示例#5
0
        private void ApplyParsedSchema(Parsing.DataSchema parsedSchema, IVerificationContext context)
        {
            const string PropertyMacroDefault = "(default)";

            // Column types
            foreach (Parsing.DataSchemaColumnType parsedColumnType in parsedSchema.ColumnTypes)
            {
                ColumnType columnType = new ColumnType();

                columnType.Name = parsedColumnType.name;
                columnType.Description = parsedColumnType.description;

                if (parsedColumnType.baseType != null)
                {
                    ColumnType baseType = this.ColumnTypes[parsedColumnType.baseType];
                    if (baseType == null)
                    {
                        context.Add(new VerificationMessage(Verification.Severity.Error,
                            string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ColumnTypeBaseTypeUnkown, parsedColumnType.name, parsedColumnType.baseType)));
                        continue;
                    }
                    columnType.BaseType = baseType;
                    Debug.Assert(columnType.BaseType != null);
                }

                // Set default values when no base type
                if (columnType.BaseType == null)
                {
                    columnType.CanBeNull = false;
                    columnType.IsDbGenerated = false;
                }

                if (parsedColumnType.canBeNullSpecified)
                {
                    columnType.CanBeNull = parsedColumnType.canBeNull;
                }

                if (parsedColumnType.isDbGeneratedSpecified)
                {
                    columnType.IsDbGenerated = parsedColumnType.isDbGenerated;
                }

                if (parsedColumnType.maxLengthSpecified)
                {
                    columnType.MaxLength = parsedColumnType.maxLength;
                }

                if (parsedColumnType.enumType != null)
                {
                    columnType.EnumTypeName = parsedColumnType.enumType;
                }

                if (parsedColumnType.precisionSpecified)
                {
                    columnType.Precision = parsedColumnType.precision;
                }

                if (parsedColumnType.scaleSpecified)
                {
                    columnType.Scale = parsedColumnType.scale;
                }

                if (parsedColumnType.Target != null)
                {
                    foreach (var parsedTarget in parsedColumnType.Target)
                    {
                        TargetSystem targetSystem = this.TargetSystems[parsedTarget.name];
                        if (targetSystem == null)
                        {
                            targetSystem = new TargetSystem { Name = parsedTarget.name };
                            this.TargetSystems.Add(targetSystem);
                        }

                        Target target = new Target
                        {
                            TargetSystem = targetSystem,
                            DataType = parsedTarget.dataType,
                            DataTypeWhenReferenced = parsedTarget.dataTypeWhenReferenced,
                            DotNetType = parsedTarget.dotNetType,
                            DotNetTypeNullable = parsedTarget.dotNetTypeNullable
                        };
                        if (parsedTarget.ExtendedProperties != null && parsedTarget.ExtendedProperties.Length > 0)
                        {
                            target.ExtendedProperties.Clear();
                            foreach (var property in parsedTarget.ExtendedProperties)
                            {
                                target.ExtendedProperties.Add(property.name, property.Value);
                            }
                        }

                        try
                        {
                            columnType.Targets.Add(target);
                        }
                        catch (ArgumentException)
                        {
                            context.Add(new VerificationMessage(Severity.Error,
                                string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ColumnTypeTargetDuplicates, target.TargetSystem.Name, columnType.Name)));
                        }
                    }
                }

                columnType.Verify(context);
                if (!this.ColumnTypes.Contains(columnType.Name))
                {
                    this.ColumnTypes.Add(columnType);
                }
                else
                {
                    context.Add(new VerificationMessage(Severity.Error,
                        string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ColumnTypeDuplicates, columnType.Name)));
                }

            }

            if (parsedSchema.Tables != null)
            {
                // Tables: First pass - references are skipped until second pass.
                foreach (var parsedTable in parsedSchema.Tables)
                {
                    Table table = new Table();
                    table.Name = parsedTable.name;

                    try
                    {
                        this.Tables.Add(table);
                    }
                    catch (ArgumentException)
                    {
                        context.Add(new VerificationTableMessage(Severity.Error, table.Name, Properties.Resources.SchemaTableExists));
                    }

                    table.Description = parsedTable.description;

                    // Columns
                    foreach (var parsedColumn in parsedTable.Columns)
                    {
                        Column column = new Column();
                        column.Name = parsedColumn.name;
                        column.Description = parsedColumn.description;

                        if (parsedColumn.canBeNullSpecified)
                        {
                            column.CanBeNull = parsedColumn.canBeNull;
                        }

                        if (parsedColumn.inPrimaryKeySpecified)
                        {
                            column.InPrimaryKey = parsedColumn.inPrimaryKey;
                        }

                        if (!string.IsNullOrWhiteSpace(parsedColumn.columnType))
                        {
                            ColumnType columnType = this.ColumnTypes[parsedColumn.columnType];
                            if (columnType != null)
                            {
                                column.ColumnType = columnType;
                            }
                            else
                            {
                                context.Add(new VerificationTableMessage(Severity.Error, table.Name, column.Name,
                                    string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ColumnTypeUnknown, parsedColumn.columnType)));
                            }
                        }

                        table.Columns.Add(column);
                    }

                    // Indices
                    if (parsedTable.Indices != null)
                    {
                        foreach (var parsedIndex in parsedTable.Indices)
                        {
                            Index index = new Index(table);
                            if (!string.IsNullOrWhiteSpace(parsedIndex.name))
                            {
                                index.Name = parsedIndex.name;
                            }
                            if (parsedIndex.uniqueSpecified)
                            {
                                index.IsUnique = parsedIndex.unique;
                            }

                            foreach (var parsedIndexColumn in parsedIndex.Column)
                            {
                                Column column = table.Columns[parsedIndexColumn.name];
                                if (column != null)
                                {
                                    index.Columns.Add(column);
                                }
                                else
                                {
                                    context.Add(new VerificationTableMessage(Severity.Error, table.Name,
                                        string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.IndexColumnUnknown, parsedIndexColumn.name)));
                                }
                            }
                            table.Indices.Add(index);
                        }
                    }
                }

                // Tables: Second pass - resolve references
                foreach (var parsedTable in parsedSchema.Tables)
                {
                    Table table = this.Tables[parsedTable.name];

                    foreach (var parsedColumn in parsedTable.Columns)
                    {
                        if (!string.IsNullOrWhiteSpace(parsedColumn.references))
                        {
                            Column column = table.Columns[parsedColumn.name];
                            string toTableName;
                            string toColumnName;
                            SplitReference(parsedColumn.references, out toTableName, out toColumnName);

                            Table toTable = this.Tables[toTableName];
                            if (toTable == null)
                            {
                                context.Add(new VerificationTableMessage(Severity.Error, table.Name, column.Name,
                                    string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ReferencedTableNotExists,
                                        parsedColumn.references)));
                                continue;
                            }
                            Column toColumn = toTable.Columns[toColumnName];
                            if (toColumn == null)
                            {
                                context.Add(new VerificationTableMessage(Severity.Error, table.Name, column.Name,
                                    string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ReferencedColumnNotExists, parsedColumn.references)));
                                continue;
                            }
                            ForeignKey foreignKey = new ForeignKey();
                            foreignKey.FromTable = table;
                            foreignKey.ToTable = toTable;
                            foreignKey.Columns.Add(new ForeignKey.ColumnPair { FromColumn = column, ToColumn = toColumn });
                            if (!string.IsNullOrEmpty(parsedColumn.referenceName))
                            {
                                foreignKey.Name = parsedColumn.referenceName;
                            }
                            table.ForeignKeys.Add(foreignKey);

                            column.ColumnType = this.GetReferenceToColumnType(toColumn.ColumnType);
                            column.CanBeNull = false;
                            if (parsedColumn.canBeNullSpecified)
                                column.CanBeNull = parsedColumn.canBeNull;

                            // Association properties
                            foreignKey.AssociationProperty = new AssociationProperty();
                            if (parsedColumn.AssociationCode != null)
                            {
                                if (parsedColumn.AssociationCode.Child != null)
                                {
                                    ParsedAssociationEndPointToNative(parsedColumn.AssociationCode.Child, foreignKey.AssociationProperty.Child);
                                }
                                else
                                {
                                    foreignKey.AssociationProperty.Child = null;
                                }
                                ParsedAssociationEndPointToNative(parsedColumn.AssociationCode.Parent, foreignKey.AssociationProperty.Parent);
                            }
                            else
                            {
                                // Check the quick-attributes for association properties
                                if (string.IsNullOrEmpty(parsedColumn.childProperty))
                                {
                                    foreignKey.AssociationProperty.Child = null;
                                }
                                else
                                {
                                    if (string.Compare(parsedColumn.childProperty, PropertyMacroDefault, StringComparison.Ordinal) != 0)
                                    {
                                        foreignKey.AssociationProperty.Child.Name = parsedColumn.childProperty;
                                    }
                                    else
                                    {
                                        // Using default
                                        foreignKey.AssociationProperty.Child.Name = null;
                                    }
                                }

                                if (string.IsNullOrEmpty(parsedColumn.parentProperty))
                                {
                                    foreignKey.AssociationProperty.Parent = null;
                                }
                                else
                                {
                                    if (string.Compare(parsedColumn.parentProperty, PropertyMacroDefault, StringComparison.Ordinal) != 0)
                                    {
                                        foreignKey.AssociationProperty.Parent.Name = parsedColumn.parentProperty;
                                    }
                                    else
                                    {
                                        // Using default
                                        foreignKey.AssociationProperty.Parent.Name = null;
                                    }
                                }
                            }
                        }
                    }   // end foreach (var parsedColumn in parsedTable.Columns)

                    // Foreign keys
                    if (parsedTable.ForeignKeys != null)
                    {
                        foreach (var parsedForeignKey in parsedTable.ForeignKeys)
                        {
                            ForeignKey foreignKey = new ForeignKey();
                            foreignKey.Name = parsedForeignKey.name;
                            foreignKey.FromTable = table;

                            Table toTable = this.Tables[parsedForeignKey.toTable];
                            if (toTable == null)
                            {
                                context.Add(new VerificationTableMessage(Severity.Error, table.Name,
                                    string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ForeignKeyTableNotExists, parsedForeignKey.toTable)));
                                continue;
                            }

                            foreignKey.ToTable = toTable;

                            foreach (var columnPair in parsedForeignKey.ColumnPairs)
                            {
                                Column fromColumn = table.Columns[columnPair.from];
                                if (fromColumn == null)
                                {
                                    context.Add(new VerificationTableMessage(Severity.Error, table.Name,
                                        string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ForeignKeyFromColumnNotExists, columnPair.from)));
                                    continue;
                                }

                                Column toColumn = toTable.Columns[columnPair.to];
                                if (toColumn == null)
                                {
                                    context.Add(new VerificationTableMessage(Severity.Error, table.Name,
                                        string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.ForeignKeyToColumnNotExists, columnPair.to)));
                                    continue;
                                }

                                foreignKey.Columns.Add(new ForeignKey.ColumnPair { FromColumn = fromColumn, ToColumn = toColumn });
                            }

                            table.ForeignKeys.Add(foreignKey);
                        }
                    }

                    // Table settings
                    if (parsedTable.Settings != null)
                    {
                        foreach (var parsedSetting in parsedTable.Settings)
                        {
                            if (!this.TargetSystems.Contains(parsedSetting.target))
                            {
                                context.Add(new VerificationTableMessage(Severity.Error, table.Name,
                                    string.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.TableSettingsTargetNotExists, parsedSetting.target)));
                                continue;
                            }
                            table.Settings.Add(parsedSetting.target, parsedSetting.property, parsedSetting.value);
                        }
                    }

                }
            }
        }