public void Build(IVerificationContext context, IScriptDocumentGeneratorFactory docGenFactory) { var dataSchema = CompileSchema(context); if (dataSchema == null) { return; } dataSchema.Verify(context); if (context.Entries.Count != 0) { return; } context.Add(new VerificationMessage(Severity.Info, Properties.Resources.DataSchemaValidated)); foreach (var transformation in Transformations) { var targetSystem = dataSchema.TargetSystems[transformation.TargetSystemName]; if (targetSystem == null) { context.Add(new VerificationMessage(Severity.Error, string.Format(CultureInfo.CurrentCulture, Properties.Resources.TargetSystemNameInTransformationNotFound, transformation.TargetSystemName, transformation.Name))); continue; } context.Add(new VerificationMessage(Severity.Info, string.Format(CultureInfo.CurrentCulture, Properties.Resources.RunningTransformation, transformation.Name))); var transformationContext = new TransformationContext { DataSchema = dataSchema, Name = transformation.Name, TargetSystem = targetSystem, TransformationElement = transformation.Element, DocGenFactory = docGenFactory, VerificationContext = context }; transformation.Method.Transform(transformationContext); } }
public void Verify(IVerificationContext context, string tableName) { if (string.IsNullOrWhiteSpace(Name)) { context.Add(new VerificationTableMessage(Severity.Error, tableName, Properties.Resources.ColumnNameEmpty)); } }
public DataSchema CompileSchema(IVerificationContext context) { if (context == null) { throw new ArgumentNullException("context"); } DataSchema dataSchema = new DataSchema(); dataSchema.Name = Name; foreach (var filePath in InputFiles) { var inputFilePath = Path.GetFullPath(filePath); if (!File.Exists(inputFilePath)) { context.Add(new VerificationMessage(Severity.Error, string.Format(CultureInfo.CurrentCulture, Properties.Resources.InputFileNotFound, inputFilePath))); return null; } using (StreamReader reader = new StreamReader(inputFilePath)) { dataSchema.LoadDataSchemaFile(reader, context); } } if (!string.IsNullOrEmpty(Options.PrimaryKeyFormatString)) { dataSchema.NameFormats.PrimaryKeyFormatString = Options.PrimaryKeyFormatString; } if (!string.IsNullOrEmpty(Options.ForeignKeyFormatString)) { dataSchema.NameFormats.ForeignKeyFormatString = Options.ForeignKeyFormatString; } if (!string.IsNullOrEmpty(Options.UniqueIndexFormatString)) { dataSchema.NameFormats.UniqueIndexFormatString = Options.UniqueIndexFormatString; } if (!string.IsNullOrEmpty(Options.IndexFormatString)) { dataSchema.NameFormats.IndexFormatString = Options.IndexFormatString; } return dataSchema; }
private bool VerifyString(IVerificationContext context, string s, string errorText) { bool result = !string.IsNullOrWhiteSpace(s); if (!result) { context.Add(new VerificationMessage(Severity.Error, errorText)); } return result; }
public void LoadFrom(string projectFilePath, IVerificationContext context, ISchemaTransformationFactory schemaTransformationFactory) { string baseDirectory = Path.GetDirectoryName(projectFilePath); using (StreamReader reader = new StreamReader(projectFilePath)) { var serializer = new XmlSerializer(typeof(Parsing.Project)); var parsedProject = (Parsing.Project)serializer.Deserialize(reader); foreach (var parsedInputFile in parsedProject.ProjectSettings.InputFiles) { if (VerifyString(context, parsedInputFile.path, "Project cannot have an input file with an empty path.")) { InputFiles.Add(ExpandFilePath(parsedInputFile.path, baseDirectory)); } } Options.PrimaryKeyFormatString = parsedProject.ProjectSettings.Options.PrimaryKeyFormatString; Options.ForeignKeyFormatString = parsedProject.ProjectSettings.Options.ForeignKeyFormatString; Options.UniqueIndexFormatString = parsedProject.ProjectSettings.Options.UniqueIndexFormatString; Options.IndexFormatString = parsedProject.ProjectSettings.Options.IndexFormatString; foreach (var parsedTransformation in parsedProject.Transformations) { VerifyString(context, parsedTransformation.name, "Transformation cannot have an empty name."); VerifyString(context, parsedTransformation.method, "Transformation cannot have an empty method."); VerifyString(context, parsedTransformation.target, "Transformation cannot have an empty method."); if (context.HasErrors) { continue; } var transformation = new Transformation(); transformation.Name = parsedTransformation.name; try { transformation.Method = schemaTransformationFactory.Create(parsedTransformation.method); } catch (ArgumentException ex) { context.Add(new VerificationMessage(Severity.Error, ex.Message)); continue; } transformation.TargetSystemName = parsedTransformation.target; transformation.Element = new XElement(XName.Get("Transformation", TargetNamespace), new XAttribute("name", parsedTransformation.name), new XAttribute("method", parsedTransformation.method), new XAttribute("target", parsedTransformation.target), parsedTransformation.Any.Select(e => XElement.Parse(e.OuterXml)) ); Transformations.Add(transformation); } } Name = Path.GetFileNameWithoutExtension(projectFilePath); }
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); } } } } }
//private static XmlSchemaSet GetSchema() //{ // Assembly assembly = Assembly.GetExecutingAssembly(); // using (var reader = new StreamReader(assembly.GetManifestResourceStream(schemaResourceName))) // { // XmlSchemaSet schemas = new XmlSchemaSet(); // schemas.Add(targetNamespace, XmlReader.Create(reader)); // } //} public void Verify(IVerificationContext context) { if (string.IsNullOrWhiteSpace(Name)) { context.Add(new VerificationMessage(Severity.Error, Properties.Resources.DataSchemaNameEmpty)); } foreach (var target in this.TargetSystems) { target.Verify(context); } foreach (var columnType in this.ColumnTypes) { columnType.Verify(context); } foreach (var table in this.Tables) { table.Verify(context); } }