Ejemplo n.º 1
0
        public static IProjectSchemaElement GetElementForXElement(Project project, ElementType elementType, XElement element)
        {
            string elementName = GetNameForElement(elementType, element);

            if (elementType == ElementType.Table)
            {
                return(project.FindTableMapping(element.Attribute("ConnectionName").Value, element.Attribute("SchemaName").Value, elementName));
            }
            else if (elementType == ElementType.ForeignKey)
            {
                return(project.FindForeignKeyMapping(element.Attribute("ConnectionName").Value, elementName));
            }
            else
            {
                string       parentElementName = GetNameForParentElement(elementType, element);
                string       connectionName    = ((XAttribute)element.XPathEvaluate("ancestor::TableMapping/@ConnectionName")).Value;
                string       schemaName        = ((XAttribute)element.XPathEvaluate("ancestor::TableMapping/@SchemaName")).Value;
                TableMapping tableMapping      = project.FindTableMapping(connectionName, schemaName, parentElementName);

                if (elementType == ElementType.Column)
                {
                    return(tableMapping.FindColumnMapping(elementName));
                }
                else if (elementType == ElementType.UniqueIndex)
                {
                    return(tableMapping.FindUniqueIndexMapping(elementName));
                }
                else if (elementType == ElementType.API)
                {
                    return(tableMapping.FindAPI(elementName));
                }
            }

            throw new ApplicationException("Unhandled TemplateOutputDefinitionElementType of \"{0}.\"".FormatString(elementType));
        }
Ejemplo n.º 2
0
        private void RefreshColumns(Project project, Connection connection, ref List <string> errors)
        {
            DataTable    dataTable = GetColumns(project, connection);
            TableMapping currentTableOrViewMapping = null;
            var          duplicates = FindDuplicates(dataTable, QueryConstants.TableOrView.SchemaName, QueryConstants.TableOrView.Name, QueryConstants.Column.Name);

            if (duplicates.Any())
            {
                errors.Add(FormatDuplicates(duplicates));
                return;
            }

            foreach (DataRow dr in dataTable.Rows)
            {
                string          forParent;
                string          name;
                string          schemaName;
                string          tableName;
                decimal         sequence;
                string          databaseDataType;
                decimal         length;
                string          defaultValue;
                bool            nullable;
                bool            primaryKey;
                DataTypeMapping dataTypeMapping;
                ColumnMapping   cm;
                bool            nullableInDatabase;
                string          dataType = null;

                ExtractColumnInfo(dr, out forParent, out name, out schemaName, out tableName, out sequence, out databaseDataType, out length, out defaultValue, out nullable, out primaryKey);
                dataTypeMapping    = project.FindDataTypeMapping(databaseDataType);
                nullableInDatabase = nullable;

                if (currentTableOrViewMapping == null || !currentTableOrViewMapping.TableName.Equals(tableName))
                {
                    if (QueryConstants.Column.ForTable.Equals(forParent))
                    {
                        currentTableOrViewMapping = project.FindTableMapping(connection.Name, schemaName, tableName);
                    }
                    else if (QueryConstants.Column.ForView.Equals(forParent))
                    {
                        currentTableOrViewMapping = project.FindViewMapping(connection.Name, schemaName, tableName);
                    }
                    else
                    {
                        throw new ApplicationException("Unknown 'for' value: {0}".FormatString(forParent));
                    }
                }

                cm = currentTableOrViewMapping.FindColumnMapping(name);

                if (dataTypeMapping != null)
                {
                    dataType = dataTypeMapping.ApplicationDataType;
                    nullable = (nullable && (dataTypeMapping.Nullable || (cm != null && cm.EnumerationMapping != null)));
                }

                if (cm == null)
                {
                    cm = new ColumnMapping(currentTableOrViewMapping, name, sequence, dataType, databaseDataType, length, defaultValue, nullable, nullableInDatabase, primaryKey, null, null);
                    currentTableOrViewMapping.ColumnMappings.Add(cm);
                }
                else
                {
                    bool isCustomDataType = (!cm.DataType.IsNullOrEmpty() && !project.DataTypeMappings.Any(o => string.Equals(o.ApplicationDataType, cm.DataType, StringComparison.InvariantCultureIgnoreCase)));

                    cm.Sequence           = sequence;
                    cm.DatabaseDataType   = databaseDataType;
                    cm.Length             = length;
                    cm.DefaultValue       = defaultValue;
                    cm.Nullable           = nullable;
                    cm.NullableInDatabase = nullableInDatabase;
                    cm.PrimaryKey         = primaryKey;

                    if (!isCustomDataType)
                    {
                        cm.DataType = dataType;
                    }
                }
            }

            RemoveNonExistantColumns(project, connection, dataTable);
        }