Пример #1
0
        void WriteTransformations(XmlTextWriter xmlWriter, EntityMap entity)
        {
            xmlWriter.WriteStartElement("properties");
            foreach (var trans in entity.Properties)
            {
                WriteTransformations(xmlWriter, trans);
            }

            foreach (var rel in entity.Relations)
            {
                if (rel.CollectionType == CollectionType.None)
                {
                    WriteTransformations(xmlWriter, rel, "reference");
                }
                else
                {
                    WriteList(xmlWriter, rel);
                }
            }
            xmlWriter.WriteEndElement();//relations
        }
        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;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        void MapPrimaryKey(EntityMap entMap)
        {
            var pks = entMap.Properties.Where(p => p.IsPrimaryKey).ToList();
            var pkr = entMap.Relations.Where(r => r.IsPrimaryKey).ToList();

            List <PrimaryKeyProperty> keys = new List <PrimaryKeyProperty>();

            foreach (var p in pks)
            {
                keys.Add(p);
                entMap.Properties.Remove(p.Name);
            }
            foreach (var p in pkr)
            {
                keys.Add(p);
                entMap.Relations.Remove(p.Name);
            }

            if (keys.Count > 0)
            {
                entMap.PrimaryKey = new PrimaryKey(keys.ToArray());
            }
        }
Пример #4
0
            void ElementEntityPrimaryKey(XmlReader reader, MapConfig config, EntityMap entMap)
            {
                bool hasReachedEndGracefully   = false;
                List <PrimaryKeyProperty> keys = new List <PrimaryKeyProperty>();

                while (reader.Read())
                {
                    if (reader.CanReadElement("key"))
                    {
                        PrimaryKeyProperty pk = new PrimaryKeyProperty();
                        var prop = Read_PropertyElement(reader, config, "key", pk);
                        if (prop != null)
                        {
                            pk.Key = prop;
                            IKeyGenerator idGenerator;
                            if (!string.IsNullOrWhiteSpace(pk.KeyGenerationStrategy) && config.PrimaryKeyGeneratorStore.TryGetValue(pk.KeyGenerationStrategy, out idGenerator))
                            {
                                pk.KeyGenerator = idGenerator;
                            }
                            keys.Add(pk);
                        }
                    }
                    else if (reader.HasReachedEndOfElement("primaryKey"))
                    {
                        hasReachedEndGracefully = true;
                        break;
                    }
                }

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

                entMap.AddKeyRange(keys);
            }
Пример #5
0
        public static ICollection <Property> GetPropertiesNotInComplexType(this ComplexType complexType, EntityMap entity)
        {
            var props = complexType.Properties.Except(entity);

            return(props.ToList());
        }
Пример #6
0
        /// <summary>
        /// Gets the CLR type as string.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public virtual string GetClrTypeAsString(Goliath.Data.Providers.SqlDialect dialect, EntityMap entity)
        {
            if (IsComplexType && (entity.Parent != null))
            {
                ComplexType complexT;
                if (entity.Parent.ComplexTypes.TryGetValue(ComplexTypeName, out complexT))
                {
                    return(complexT.FullName);
                }
                else
                {
                    return(ComplexTypeName);
                }
                //var complexT = entity.Parent.ComplexTypes[ComplexTypeName];
                //if (complexT != null)
                //{
                //    return complexT.FullName;
                //}
            }
            else
            {
                var clrType = dialect.GetClrType(DbType, IsNullable);
                return(ToPrintString(clrType));
            }

            throw new GoliathDataException(string.Format("Could not find CLR type for {0}.", PropertyName));
        }
Пример #7
0
        void WriteTransformations(XmlTextWriter xmlWriter, EntityMap entity, StatementMap statement)
        {
            string elementName;
            bool   writeOperation = false;

            switch (statement.OperationType)
            {
            case MappedStatementType.Insert:
                elementName = "insert";
                break;

            case MappedStatementType.Delete:
                elementName = "delete";
                break;

            case MappedStatementType.Query:
                elementName = "query";
                break;

            case MappedStatementType.Update:
                elementName = "update";
                break;

            default:
                elementName    = "statement";
                writeOperation = true;
                break;
            }

            string statName;
            string resultMap = statement.ResultMap;

            if (entity != null)
            {
                statName = statement.Name.Replace(entity.FullName + "_", string.Empty);
            }
            else
            {
                statName = statement.Name;
                //resultMap = statement.ResultMap;
            }

            xmlWriter.WriteStartElement(elementName);
            if (!string.IsNullOrWhiteSpace(statement.Name))
            {
                xmlWriter.WriteStartAttribute("name");
                xmlWriter.WriteString(statName);
                xmlWriter.WriteEndAttribute();
            }

            if (!string.IsNullOrWhiteSpace(resultMap))
            {
                xmlWriter.WriteStartAttribute("resultMap");
                xmlWriter.WriteString(resultMap);
                xmlWriter.WriteEndAttribute();
            }

            if (!string.IsNullOrWhiteSpace(statement.DbName))
            {
                xmlWriter.WriteStartAttribute("dbName");
                xmlWriter.WriteString(statement.DbName);
                xmlWriter.WriteEndAttribute();
            }

            if (writeOperation)
            {
                xmlWriter.WriteStartAttribute("operationType");
                xmlWriter.WriteString(statement.OperationType.ToString());
                xmlWriter.WriteEndAttribute();
            }

            bool writeBodyElement = false;

            if (statement.DbParametersMap.Count > 0)
            {
                writeBodyElement = true;
                xmlWriter.WriteStartElement("dbParameters");
                foreach (var dbParam in statement.DbParametersMap)
                {
                    xmlWriter.WriteStartElement("param");

                    xmlWriter.WriteStartAttribute("name");
                    xmlWriter.WriteString(dbParam.Key);
                    xmlWriter.WriteEndAttribute();
                    if (dbParam.Value.HasValue)
                    {
                        xmlWriter.WriteStartAttribute("dbType");
                        xmlWriter.WriteString(dbParam.Value.Value.ToString());
                        xmlWriter.WriteEndAttribute();
                    }
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();
            }

            if (statement.InputParametersMap.Count > 0)
            {
                writeBodyElement = true;
                xmlWriter.WriteStartElement("inputParameters");
                foreach (var input in statement.InputParametersMap)
                {
                    xmlWriter.WriteStartElement("input");

                    xmlWriter.WriteStartAttribute("name");
                    xmlWriter.WriteString(input.Key);
                    xmlWriter.WriteEndAttribute();

                    xmlWriter.WriteStartAttribute("type");
                    xmlWriter.WriteString(input.Value);
                    xmlWriter.WriteEndAttribute();

                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();
            }

            if (!string.IsNullOrWhiteSpace(statement.Body))
            {
                if (writeBodyElement)
                {
                    xmlWriter.WriteStartElement("body");
                }

                xmlWriter.WriteCData(statement.Body);

                if (writeBodyElement)
                {
                    xmlWriter.WriteEndElement();
                }
            }

            xmlWriter.WriteEndElement();
        }
Пример #8
0
 /// <summary>
 /// Gets the CLR type as string.
 /// </summary>
 /// <param name="dialect"></param>
 /// <param name="entity">The entity.</param>
 /// <returns></returns>
 public override string GetClrTypeAsString(Providers.SqlDialect dialect, EntityMap entity)
 {
     return(IsPrimaryKey ? base.GetClrTypeAsString(dialect, entity) : ReferenceEntityName);
 }
Пример #9
0
 public static string GetIndexKey(EntityMap tbl)
 {
     return($"{tbl.SchemaName}.{tbl.TableName}");
 }
Пример #10
0
 /// <summary>
 /// Builds the name of the procedure.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="type">The type.</param>
 /// <returns></returns>
 public static string BuildMappedStatementName(EntityMap map, MappedStatementType type)
 {
     return(string.Format("{0}_{1}", map.FullName, type));
 }
Пример #11
0
        /// <summary>
        /// Tries the get value.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="procType">Type of the proc.</param>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public bool TryGetValue(EntityMap map, MappedStatementType procType, out StatementMap val)
        {
            var procName = BuildMappedStatementName(map, procType);

            return(TryGetValue(procName, out val));
        }
Пример #12
0
 /// <summary>
 /// Adds the specified map.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="dbName">Name of the db.</param>
 /// <param name="type">The type.</param>
 /// <param name="body">The body.</param>
 public StatementMap Add(EntityMap map, string dbName, MappedStatementType type, string body)
 {
     return(Add(map, dbName, type, body, Platform.Id));
 }
Пример #13
0
            EntityMap Read_EntityElement(XmlReader reader, MapConfig config)
            {
                EntityMap entMap = new EntityMap();

                if (reader.CanReadElement("entity"))
                {
                    while (reader.MoveToNextAttribute())
                    {
                        string currentAttribute = reader.Name;
                        switch (currentAttribute)
                        {
                        case "name":
                            entMap.Name = reader.Value;
                            break;

                        case "extends":
                            entMap.Extends = reader.Value;
                            break;

                        case "assembly":
                            entMap.AssemblyName = reader.Value;
                            break;

                        case "entityNamespace":
                            entMap.Namespace = reader.Value;
                            break;

                        case "table":
                            entMap.TableName = reader.Value;
                            break;

                        case "schema":
                            entMap.SchemaName = reader.Value;
                            break;

                        case "alias":
                            entMap.TableAlias = reader.Value;
                            break;

                        case "order":
                            entMap.Order = ReadInteger(reader.Value);
                            break;

                        case "sort":
                            entMap.SortOrder = ReadInteger(reader.Value);
                            break;

                        case "trackable":
                            entMap.IsTrackable = ReadBool(reader.Value);
                            break;

                        case "linkTable":
                            bool isLinkTable;
                            if (bool.TryParse(reader.Value, out isLinkTable))
                            {
                                entMap.IsLinkTable = isLinkTable;
                            }
                            break;

                        default:
                            if (includeMetaData)
                            {
                                if (!string.IsNullOrWhiteSpace(currentAttribute) && currentAttribute.StartsWith("data_"))
                                {
                                    string metaKey = currentAttribute.Replace("data_", string.Empty);
                                    if (!entMap.MetaDataAttributes.ContainsKey(metaKey))
                                    {
                                        entMap.MetaDataAttributes.Add(metaKey, reader.Value);
                                    }
                                }
                            }
                            break;
                        }
                    }

                    while (reader.Read())
                    {
                        if (reader.HasReachedEndOfElement("entity"))
                        {
                            return(entMap);
                        }

                        //element primary key
                        else if (reader.CanReadElement("primaryKey"))
                        {
                            ElementEntityPrimaryKey(reader, config, entMap);
                        }

                        //element properties
                        else if (reader.CanReadElement("properties"))
                        {
                            ElementEntityProperties(reader, config, entMap);
                        }

                        //element relations
                        else if (reader.CanReadElement("relations"))
                        {
                            ElementEntityReadRelations(reader, config, entMap);
                        }

                        else if (reader.CanReadElement("statements"))
                        {
                            Read_StatementsElement(reader, config, entMap);
                        }
                    }

                    throw new MappingSerializationException(typeof(EntityMap), "missing a </entity> end tag");
                }
                return(null);
            }
Пример #14
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");
                }
            }
Пример #15
0
        public static ICollection <Property> GetPropertiesInCommonWithComplexType(this ComplexType complexType, EntityMap entity)
        {
            var props = complexType.Properties.Intersect(entity);

            return(props.ToList());
        }
Пример #16
0
        //static bool ComplexTypeContainsProperty(string propertyName,
        /// <summary>
        /// Determines whether this instance can print the specified property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="entity">The entity.</param>
        /// <returns>
        ///     <c>true</c> if this instance can print the specified property; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanPrint(this Property property, EntityMap entity)
        {
            if (entity == null)
            {
                return(true);
            }

            if (string.IsNullOrWhiteSpace(entity.Extends))
            {
                return(true);
            }

            var baseModel = entity.BaseModel;

            if (baseModel == null)
            {
                return(true);
            }

            if (property.MetaDataAttributes.TryGetValue("printable", out string val))
            {
                if (val.ToUpper() == "FALSE")
                {
                    return(false);
                }
            }

            try
            {
                if (baseModel is ComplexType)
                {
                    var complexType = baseModel as ComplexType;

                    if (complexType.Properties.Contains(property.Name))
                    {
                        return(false);
                    }
                }
                else if (baseModel is EntityMap)
                {
                    var ent = baseModel as EntityMap;

                    if (ent.IsSubClass)
                    {
                        var supEnt = ent.Parent.GetEntityMap(ent.Extends);
                        return(CanPrint(property, supEnt));
                    }
                    else if ((ent.BaseModel != null) && (ent.BaseModel is ComplexType))
                    {
                        var supComplex = ent.BaseModel as ComplexType;
                        if (supComplex.Properties.Contains(property.Name))
                        {
                            return(false);
                        }
                    }

                    if (ent.Properties.Contains(property.Name))
                    {
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Debug, string.Format("CanPrint:{0}.{1}\n\n{2}", entity.Name, property.PropertyName, ex.ToString()));
            }
            return(true);
        }
Пример #17
0
        /// <summary>
        /// Adds the specified map.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="type">The type.</param>
        /// <param name="body">The body.</param>
        public StatementMap Add(EntityMap map, MappedStatementType type, string body)
        {
            string procName = BuildMappedStatementName(map, type);

            return(Add(procName, procName, type, body, Platform.Id));
        }