public void Process(IDictionary <string, EntityMap> entities, StatementStore mappedStatementStore, IDictionary <string, string> entityRenames)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }
            if (entityRenames == null)
            {
                throw new ArgumentNullException("entityRenames");
            }

            foreach (var ent in entities.Values)
            {
                foreach (var prop in ent)
                {
                    string keyName = $"{ent.Name}.{prop.PropertyName}";
                    string renameVal;
                    if (entityRenames.TryGetValue(keyName, out renameVal))
                    {
                        var oldName = prop.PropertyName;
                        prop.PropertyName = renameVal;
                        logger.Log(LogLevel.Debug, $"Renamed {ent.Name}.{oldName} to {renameVal}");

                        var rel = prop as Relation;
                        if (rel != null)
                        {
                            EntityMap relMap = entities.Values.FirstOrDefault(c => rel.ReferenceEntityName.Equals(c.FullName));
                            if (relMap != null)
                            {
                                var bCols = relMap.Relations.Where(c => oldName.Equals(c.ReferenceProperty));
                                foreach (var relation in bCols)
                                {
                                    relation.ReferenceProperty = renameVal;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MapConfig" /> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="generators">The generators.</param>
        public MapConfig(ProjectSettings settings, params IKeyGenerator[] generators)
        {
            EntityConfigs = new EntityCollection();
            ComplexTypes  = new ComplexTypeCollection();

            MappedStatements = new StatementStore(settings.Platform);
            Settings         = settings;

            PrimaryKeyGeneratorStore = new KeyGeneratorStore
            {
                new Generators.GuidCombGenerator(),
                new Generators.AutoIncrementGenerator()
            };

            if (generators == null)
            {
                return;
            }
            foreach (var keyGenerator in generators)
            {
                PrimaryKeyGeneratorStore.Add(keyGenerator);
            }
        }
Exemplo n.º 3
0
 public void Process(IDictionary <string, EntityMap> entities,
                     StatementStore mappedStatementStore,
                     IDictionary <string, string> entityRenames)
 {
     ProcessTableNames(entities, entityRenames);
 }
Exemplo n.º 4
0
        public virtual void Process(IDictionary <string, EntityMap> entityList, StatementStore mappedStatementStore, IDictionary <string, string> entityRenames)
        {
            foreach (var ent in entityList.Values)
            {
                try
                {
                    //find link tables
                    if (settings.SupportManyToMany && ent.IsLinkTable)
                    {
                        var aRel = ent.PrimaryKey.Keys[0].Key as Relation;
                        var bRel = ent.PrimaryKey.Keys[1].Key as Relation;

                        if ((aRel == null) || (bRel == null))
                        {
                            continue;
                        }

                        if (entityList.TryGetValue(NamePostProcessor.GetTableKeyName(aRel), out EntityMap aEnt))
                        {
                            if (entityList.TryGetValue(NamePostProcessor.GetTableKeyName(bRel), out EntityMap bEnt))
                            {
                                var aRepPropName = bEnt.Name.Pluralize();
                                if (aEnt.Relations.Contains(aRepPropName))
                                {
                                    aRepPropName = $"{bEnt.Name.Pluralize()}On{ent.Name}_{aRel.ColumnName.Pascalize()}";
                                }

                                var keyName = $"{aEnt.Name}.{aRepPropName}";
                                if (entityRenames.ContainsKey(keyName))
                                {
                                    aRepPropName = entityRenames[keyName];
                                }

                                keyName = $"{aEnt.Name}.{bEnt.Name.Pluralize()}On{ent.Name}_{aRel.ColumnName.Pascalize()}";
                                if (entityRenames.ContainsKey(keyName))
                                {
                                    aRepPropName = entityRenames[keyName];
                                }

                                var aRelationProp = new Relation()
                                {
                                    IsComplexType       = true,
                                    LazyLoad            = true,
                                    ColumnName          = aRel.ReferenceColumn ?? string.Empty,
                                    ReferenceColumn     = bRel.ReferenceColumn ?? string.Empty,
                                    ReferenceProperty   = bRel.ReferenceProperty ?? string.Empty,
                                    CollectionType      = CollectionType.List,
                                    MapTableName        = ent.TableName,
                                    MapColumn           = aRel.ColumnName ?? string.Empty,
                                    MapPropertyName     = aRel.ReferenceColumn ?? string.Empty,
                                    MapReferenceColumn  = bRel.ColumnName ?? string.Empty,
                                    PropertyName        = aRepPropName,
                                    ReferenceEntityName = bEnt.FullName,
                                    ReferenceTable      = bEnt.TableName,
                                    RelationType        = RelationshipType.ManyToMany,
                                    Inverse             = true,
                                };
                                aEnt.Relations.Add(aRelationProp);

                                string aColCamel = aRel.ColumnName.Camelize();
                                string bColCamel = bRel.ColumnName.Camelize();

                                logger.Log(LogLevel.Debug, $"Processing map table {ent.TableName} statements -> {aColCamel}, {bColCamel}");
                                //build mapped statement for ease of adding and removing association
                                var aInsertStatement = new StatementMap
                                {
                                    Name            = $"{aEnt.FullName}_{aColCamel}_associate_{aEnt.Name}_with_{bEnt.Name}",
                                    OperationType   = MappedStatementType.Insert,
                                    Body            = $"INSERT INTO {ent.TableName} ({aRel.ColumnName}, {bRel.ColumnName}) VALUES({StatementMap.BuildPropTag(aColCamel, aRel.ReferenceProperty)}, {StatementMap.BuildPropTag(bColCamel, bRel.ReferenceProperty)});",
                                    DependsOnEntity = aEnt.FullName
                                };
                                aInsertStatement.InputParametersMap.Add(aColCamel, aEnt.FullName);
                                aInsertStatement.InputParametersMap.Add(bColCamel, bEnt.FullName);

                                var aDeleteStatement = new StatementMap
                                {
                                    Name            = $"{aEnt.FullName}_{aColCamel}_disassociate_{aEnt.Name}_with_{bEnt.Name}",
                                    OperationType   = MappedStatementType.Delete,
                                    Body            = string.Format("DELETE FROM {0} WHERE {1} = {3} AND {2} = {4};", ent.TableName, aRel.ColumnName, bRel.ColumnName, StatementMap.BuildPropTag(aColCamel, aRel.ReferenceProperty), StatementMap.BuildPropTag(bColCamel, bRel.ReferenceProperty)),
                                    DependsOnEntity = aEnt.FullName
                                };
                                aDeleteStatement.InputParametersMap.Add(aColCamel, aEnt.FullName);
                                aDeleteStatement.InputParametersMap.Add(bColCamel, bEnt.FullName);

                                mappedStatementStore.Add(aInsertStatement);
                                mappedStatementStore.Add(aDeleteStatement);

                                var bRepPropName = aEnt.Name.Pluralize();
                                if (bEnt.Relations.Contains(aRepPropName))
                                {
                                    bRepPropName = $"{aEnt.Name.Pluralize()}On{ent.Name}_{bRel.ColumnName.Pascalize()}";
                                }

                                keyName = $"{bEnt.Name}.{bRepPropName}";
                                if (entityRenames.ContainsKey(keyName))
                                {
                                    bRepPropName = entityRenames[keyName];
                                }

                                keyName = $"{bEnt.Name}.{aEnt.Name.Pluralize()}On{ent.Name}_{bRel.ColumnName.Pascalize()}";
                                if (entityRenames.ContainsKey(keyName))
                                {
                                    bRepPropName = entityRenames[keyName];
                                }


                                bEnt.Relations.Add(new Relation()
                                {
                                    IsComplexType          = true,
                                    ColumnName             = bRel.ReferenceColumn ?? string.Empty,
                                    ReferenceColumn        = aRel.ReferenceColumn ?? string.Empty,
                                    ReferenceProperty      = aRel.ReferenceProperty ?? string.Empty,
                                    MapTableName           = ent.TableName,
                                    MapColumn              = bRel.ColumnName ?? string.Empty,
                                    MapPropertyName        = bRel.ReferenceColumn ?? string.Empty,
                                    MapReferenceColumn     = aRel.ColumnName ?? string.Empty,
                                    CollectionType         = CollectionType.List,
                                    LazyLoad               = true,
                                    PropertyName           = bRepPropName,
                                    ReferenceEntityName    = aEnt.FullName,
                                    ReferenceTable         = aEnt.TableName,
                                    ManyToManyPropertyName = aRelationProp.PropertyName,
                                    RelationType           = RelationshipType.ManyToMany,
                                });
                                aRelationProp.ManyToManyPropertyName = bRepPropName;

                                var bInsertStatement = new StatementMap
                                {
                                    Name          = $"{bEnt.FullName}_{bColCamel}_associate_{bEnt.Name}_with_{aEnt.Name}",
                                    OperationType = MappedStatementType.Insert,
                                    Body          =
                                        $"INSERT INTO {ent.TableName} ({aRel.ColumnName}, {bRel.ColumnName}) VALUES({StatementMap.BuildPropTag(aColCamel, aRel.ReferenceProperty)}, {StatementMap.BuildPropTag(bColCamel, bRel.ReferenceProperty)});",
                                    DependsOnEntity = bEnt.FullName
                                };
                                bInsertStatement.InputParametersMap.Add(aColCamel, aEnt.FullName);
                                bInsertStatement.InputParametersMap.Add(bColCamel, bEnt.FullName);

                                var bDeleteStatement = new StatementMap
                                {
                                    Name            = $"{bEnt.FullName}_{bColCamel}_dissaciate_{bEnt.Name}_with_{aEnt.Name}",
                                    OperationType   = MappedStatementType.Delete,
                                    Body            = string.Format("DELETE FROM {0} WHERE {1} = {3} AND {2} = {4};", ent.TableName, aRel.ColumnName, bRel.ColumnName, StatementMap.BuildPropTag(aColCamel, aRel.ReferenceProperty), StatementMap.BuildPropTag(bColCamel, bRel.ReferenceProperty)),
                                    DependsOnEntity = bEnt.FullName
                                };

                                bDeleteStatement.InputParametersMap.Add(aColCamel, aEnt.FullName);
                                bDeleteStatement.InputParametersMap.Add(bColCamel, bEnt.FullName);

                                mappedStatementStore.Add(bInsertStatement);
                                mappedStatementStore.Add(bDeleteStatement);
                            }
                        }
                    }
                    else
                    {
                        if ((ent.PrimaryKey != null) && (ent.PrimaryKey.Keys.Count == 1) && settings.SupportTableInheritance)
                        {
                            var key = ent.PrimaryKey.Keys[0];
                            if ((key != null) && (key.Key is Relation))
                            {
                                var pk = (Relation)key.Key;
                                ent.Extends = pk.ReferenceEntityName;
                                logger.Log(LogLevel.Debug, $"Processing  {ent.Name} extends -> {ent.Extends}.");
                            }
                        }
                        else if (!settings.SupportManyToMany && (ent.PrimaryKey != null) && (ent.PrimaryKey.Keys.Count > 1))
                        {
                            for (var i = 0; i < ent.PrimaryKey.Keys.Count; i++)
                            {
                                var k = ent.PrimaryKey.Keys[i].Key as Relation;
                                if (k != null && k.RelationType == RelationshipType.ManyToOne)
                                {
                                    if (entityList.TryGetValue(NamePostProcessor.GetTableKeyName(k), out EntityMap other))
                                    {
                                        logger.Log(LogLevel.Debug, $"Processing One-To-Many ent:{ent.Name} other:{other.Name}.");

                                        var aRepPropName = ent.Name.Pluralize();
                                        if (other.Relations.Contains(aRepPropName))
                                        {
                                            aRepPropName = $"{ent.Name.Pluralize()}On{k.ColumnName.Pascalize()}";
                                        }

                                        var keyName = $"{other.Name}.{aRepPropName}";
                                        if (entityRenames.ContainsKey(keyName))
                                        {
                                            aRepPropName = entityRenames[keyName];
                                        }

                                        other.Relations.Add(new Relation()
                                        {
                                            IsComplexType       = true,
                                            LazyLoad            = true,
                                            ColumnName          = k.ReferenceColumn,
                                            ReferenceColumn     = k.ColumnName,
                                            ReferenceProperty   = k.PropertyName,
                                            PropertyName        = aRepPropName,
                                            ReferenceTable      = ent.TableName,
                                            RelationType        = RelationshipType.OneToMany,
                                            ReferenceEntityName = ent.FullName,
                                            CollectionType      = CollectionType.List,
                                        });
                                    }
                                }
                            }
                        }

                        for (int i = 0; i < ent.Relations.Count; i++)
                        {
                            var reference = ent.Relations[i];
                            if (reference.RelationType != RelationshipType.ManyToOne)
                            {
                                continue;
                            }

                            if (entityList.TryGetValue(NamePostProcessor.GetTableKeyName(reference), out EntityMap other))
                            {
                                logger.Log(LogLevel.Debug, $"Processing One-To-Many ent:{ent.Name} other:{other.Name}.");
                                var aRepPropName            = ent.Name.Pluralize();
                                var aNameByRelationPropName = $"{ent.Name.Pluralize()}On{reference.PropertyName.Pascalize()}";
                                if (other.Relations.Contains(aRepPropName))
                                {
                                    aRepPropName = aNameByRelationPropName;
                                }

                                var keyName = $"{other.Name}.{aRepPropName}";
                                if (entityRenames.ContainsKey(keyName))
                                {
                                    aRepPropName = entityRenames[keyName];
                                }

                                keyName = $"{other.Name}.{aNameByRelationPropName}";
                                if (!aRepPropName.Equals(aNameByRelationPropName) && entityRenames.ContainsKey(keyName))
                                {
                                    aRepPropName = entityRenames[keyName];
                                }

                                other.Relations.Add(new Relation()
                                {
                                    IsComplexType       = true,
                                    LazyLoad            = true,
                                    ColumnName          = reference.ReferenceColumn,
                                    ReferenceColumn     = reference.ColumnName,
                                    ReferenceProperty   = reference.PropertyName,
                                    PropertyName        = aRepPropName,
                                    ReferenceTable      = ent.TableName,
                                    RelationType        = RelationshipType.OneToMany,
                                    ReferenceEntityName = ent.FullName,
                                    CollectionType      = CollectionType.List,
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("Processing exception", ex);
                }
            }
        }
Exemplo n.º 5
0
            void Read_StatementsElement(XmlReader reader, MapConfig config, EntityMap entMap)
            {
                bool hasReachedEndGracefully = false;
                bool dependsOnEntity         = entMap != null;

                while (reader.Read())
                {
                    StatementMap statement   = null;
                    string       elementName = reader.Name;

                    if (reader.CanReadElement("query") || reader.CanReadElement("insert") || reader.CanReadElement("statement") || reader.CanReadElement("update") || reader.CanReadElement("delete"))
                    {
                        statement = Read_StatementElement(reader, config, elementName);

                        if (string.IsNullOrEmpty(statement.Name))
                        {
                            if (dependsOnEntity)
                            {
                                statement.Name = StatementStore.BuildMappedStatementName(entMap, statement.OperationType);
                                switch (statement.OperationType)
                                {
                                case MappedStatementType.Insert:
                                    entMap.UseMappedInsert = true;
                                    break;

                                case MappedStatementType.Delete:
                                    entMap.UseMappedDelete = true;
                                    break;

                                case MappedStatementType.Update:
                                    entMap.UseMappedUpdate = true;
                                    break;
                                }
                            }
                            else
                            {
                                throw new MappingSerializationException(typeof(StatementMap), "A statement in goliath.data/statements does not have name defined. A name cannot be infered.");
                            }
                        }

                        if (dependsOnEntity)
                        {
                            statement.DependsOnEntity = entMap.FullName;

                            if (string.IsNullOrEmpty(statement.ResultMap))
                            {
                                switch (statement.OperationType)
                                {
                                case MappedStatementType.Update:
                                case MappedStatementType.ExecuteNonQuery:
                                case MappedStatementType.Insert:
                                case MappedStatementType.Delete:
                                    statement.ResultMap = typeof(Int32).ToString();
                                    break;

                                case MappedStatementType.Query:
                                    statement.ResultMap = entMap.FullName;
                                    break;

                                case MappedStatementType.Undefined:
                                    break;

                                default:
                                    break;
                                }
                            }

                            if ((statement.InputParametersMap.Count == 0) && ((statement.OperationType == MappedStatementType.Insert) || (statement.OperationType == MappedStatementType.Update)))
                            {
                                //statement.InputParametersMap.Add("a", entMap.FullName);
                            }
                        }

                        if (string.IsNullOrWhiteSpace(statement.CanRunOn))
                        {
                            //if can run on is empty we expect the platform to be on the main file.
                            if (string.IsNullOrWhiteSpace(config.Settings.Platform))
                            {
                                throw new MappingSerializationException(typeof(StatementMap), string.Format("Statement {0} missing canRunOn. Please specify which platform to run or specify one config rdbms.", statement.Name));
                            }

                            statement.CanRunOn = config.Settings.Platform;
                        }

                        reader.MoveToElement();

                        if (!reader.IsEmptyElement)
                        {
                            while (reader.Read())
                            {
                                if (reader.HasReachedEndOfElement(elementName))
                                {
                                    break;
                                }

                                if ((reader.NodeType == XmlNodeType.Text) || (reader.NodeType == XmlNodeType.CDATA))
                                {
                                    statement.Body = reader.Value;
                                }

                                else if (reader.CanReadElement("dbParameters"))
                                {
                                    while (reader.Read())
                                    {
                                        if (reader.HasReachedEndOfElement("dbParameters"))
                                        {
                                            break;
                                        }

                                        if (reader.CanReadElement("param"))
                                        {
                                            Read_StatementParams(reader, statement);
                                        }
                                    }
                                }
                                else if (reader.CanReadElement("inputParameters"))
                                {
                                    while (reader.Read())
                                    {
                                        if (reader.HasReachedEndOfElement("inputParameters"))
                                        {
                                            break;
                                        }

                                        if (reader.CanReadElement("input"))
                                        {
                                            Read_StatementInputParam(reader, statement);
                                        }
                                    }
                                }
                                else if (reader.CanReadElement("body"))
                                {
                                    while (reader.Read())
                                    {
                                        if (reader.HasReachedEndOfElement("body"))
                                        {
                                            break;
                                        }

                                        if ((reader.NodeType == XmlNodeType.Text) || (reader.NodeType == XmlNodeType.CDATA))
                                        {
                                            statement.Body = reader.Value;
                                        }
                                    }
                                }
                            }
                        }

                        if (statement.OperationType == MappedStatementType.Undefined)
                        {
                            throw new MappingSerializationException(typeof(StatementMap), string.Format("Statement {0} must have have an operationType", statement.Name));
                        }


                        config.UnprocessedStatements.Add(statement);
                    }

                    else if (reader.HasReachedEndOfElement("statements"))
                    {
                        hasReachedEndGracefully = true;
                        break;
                    }
                }

                if (!hasReachedEndGracefully)
                {
                    throw new MappingSerializationException(typeof(StatementMap), "missing a </statements> end tag");
                }
            }