Exemplo n.º 1
0
        public static Statement Load(XmlElement statementNode, SmartSqlMap smartSqlMap)
        {
            var statement = new Statement
            {
                Id = statementNode.Attributes["Id"].Value,

                SqlTags     = new List <ITag> {
                },
                SmartSqlMap = smartSqlMap
            };
            string cacheId = statementNode.Attributes["Cache"]?.Value;

            if (!String.IsNullOrEmpty(cacheId))
            {
                var cache = smartSqlMap.Caches.FirstOrDefault(m => m.Id == cacheId);
                statement.Cache = cache ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find Cache.Id:{cacheId}");
            }
            var             tagNodes = statementNode.ChildNodes;
            IList <Include> includes = new List <Include>();

            foreach (XmlNode tagNode in tagNodes)
            {
                var tag = LoadTag(tagNode, includes);
                if (tag != null)
                {
                    statement.SqlTags.Add(tag);
                }
            }

            #region Init Include
            foreach (var include in includes)
            {
                if (include.RefId == statement.Id)
                {
                    throw new SmartSqlException($"Statement.Load Include.RefId can not be self statement.id:{include.RefId}");
                }
                var refStatement = smartSqlMap.Statements.FirstOrDefault(m => m.Id == include.RefId);

                include.Ref = refStatement ?? throw new SmartSqlException($"Statement.Load can not find statement.id:{include.RefId}");
            }
            #endregion
            return(statement);
        }
Exemplo n.º 2
0
        public static Statement Load(XmlElement statementNode, SmartSqlMap smartSqlMap)
        {
            var statement = new Statement
            {
                Id = statementNode.Attributes["Id"].Value,

                SqlTags     = new List <ITag> {
                },
                SmartSqlMap = smartSqlMap
            };
            string cacheId = statementNode.Attributes["Cache"]?.Value;

            if (!String.IsNullOrEmpty(cacheId))
            {
                var cache = smartSqlMap.Caches.FirstOrDefault(m => m.Id == cacheId);
                statement.Cache = cache ?? throw new SmartSqlException($"SmartSql.Statement.Id:{statement.Id} can not find Cache.Id:{cacheId}");
            }
            var tagNodes = statementNode.ChildNodes;

            foreach (XmlNode tagNode in tagNodes)
            {
                var prepend  = tagNode.Attributes?["Prepend"]?.Value;
                var property = tagNode.Attributes?["Property"]?.Value;

                #region Init Tag
                switch (tagNode.Name)
                {
                case "Include":
                {
                    var refId        = tagNode.Attributes?["RefId"]?.Value;
                    var refStatement = smartSqlMap.Statements.FirstOrDefault(m => m.Id == refId);
                    if (refStatement == null)
                    {
                        throw new SmartSqlException($"SmartSql.Statement.Load can not find statement.id:{refId}");
                    }
                    if (refId == statement.Id)
                    {
                        throw new SmartSqlException($"SmartSql.Statement.Load Include.RefId can not be self statement.id:{refId}");
                    }
                    statement.SqlTags.Add(new Include
                        {
                            RefId = refId,
                            Ref   = refStatement
                        });
                    break;
                }

                default:
                {
                    var tag = LoadTag(tagNode);
                    if (tag != null)
                    {
                        statement.SqlTags.Add(tag);
                    }
                    break;
                };
                }
                #endregion
            }
            return(statement);
        }