예제 #1
0
        /// <summary>
        /// 添加sqlmap
        /// </summary>
        /// <param name="info"></param>
        public void Add(SqlMapInfo info, bool refresh = false)
        {
            if (refresh == true)
            {
                var v = SqlMaps.FirstOrDefault(p => p.Path == info.Path);
                if (v != null)
                {
                    Statement tmp = null;
                    foreach (var st in v.Statements)
                    {
                        statements.TryRemove(st.FullSqlId, out tmp);
                    }

                    SqlMaps.Remove(v);
                }
            }
            if (SqlMaps.Count(p => p.Path == info.Path) < 1)
            {
                SqlMaps.Add(info);

                foreach (var st in info.Statements)
                {
                    statements[st.FullSqlId] = st;
                }
            }
        }
예제 #2
0
        public void AddOrSetSqlMapInfo(SqlMapInfo sm)
        {
            var v = SqlMaps.FirstOrDefault(p => p.Path == sm.Path && p.Scope == sm.Scope);

            if (v != null)
            {
                //Statement tmp = null;
                //foreach (var st in v.Statements)
                //{
                //    statements.TryRemove(st.FullSqlId, out tmp);
                //}

                SqlMaps.Remove(v);
            }

            SqlMaps.Add(sm);
        }
예제 #3
0
        public SqlMapInfo LoadSqlMap(IDatabase db, ConfigStream configStream)
        {
            using (configStream)
            {
                var sqlMap = new SqlMapInfo
                {
                    Path       = configStream.Path,
                    Statements = new List <Statement> {
                    },
                    Caches     = new List <SqlMapCache> {
                    }
                };
                XmlDocument xmlDoc = new XmlDocument();

                try
                {
                    //xmlDoc.LoadXml(configStream.Config);
                    var text = FileLoader.LoadText(configStream.Path, db);
                    xmlDoc.LoadXml(text);
                    // xmlDoc.Load(configStream.Path);

                    XmlNamespaceManager xmlNsM = new XmlNamespaceManager(xmlDoc.NameTable);
                    xmlNsM.AddNamespace("ns", "http://PureData.net/schemas/SqlMap.xsd");
                    sqlMap.Scope = xmlDoc.SelectSingleNode("//ns:SqlMap", xmlNsM)
                                   .Attributes["Scope"].Value;

                    //避免大小写 统一 20191115
                    sqlMap.Scope = SqlMapManager.Instance.FormatSqlMapNameCase(sqlMap.Scope);

                    #region Init Caches
                    var cacheNodes = xmlDoc.SelectNodes("//ns:Cache", xmlNsM);
                    foreach (XmlElement cacheNode in cacheNodes)
                    {
                        var cache = SqlMapCache.Load(cacheNode);
                        sqlMap.Caches.Add(cache);
                    }
                    #endregion

                    #region Init Statement
                    var statementNodes = xmlDoc.SelectNodes("//ns:Statement", xmlNsM);
                    foreach (XmlElement statementNode in statementNodes)
                    {
                        var statement = Statement.Load(statementNode, sqlMap);

                        sqlMap.Statements.Add(statement);
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    throw new PureDataException("SqlMapLoader", ex);
                }
                finally
                {
                    xmlDoc.RemoveAll();

                    xmlDoc = null;
                    GC.Collect();
                }

                return(sqlMap);
            }
        }
예제 #4
0
        public static Statement Load(XmlElement statementNode, SqlMapInfo sqlMapInfo)
        {
            var statement = new Statement
            {
                Id = SqlMapManager.Instance.FormatSqlMapNameCase(statementNode.Attributes["Id"].Value),

                SqlTags    = new List <ITag> {
                },
                SqlMapInfo = sqlMapInfo
            };

            string cacheId = statementNode.GetValueInXmlAttributes("Cache");

            if (!String.IsNullOrEmpty(cacheId))
            {
                var cache = sqlMapInfo.Caches.FirstOrDefault(m => m.Id == cacheId);
                if (cache == null)
                {
                    throw new Exception(string.Format("SqlMap.Statement.Id:{0} can not find Cache.Id:{1}", statement.Id, cacheId));
                }
                statement.Cache = cache;
            }

            var             tagNodes = statementNode.ChildNodes;
            string          prepend, property;
            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 Exception(string.Format("Statement.Include tag's RefId can not be self statement.id:{0}", include.RefId));
            //    }
            //    var refStatement = smartSqlMap.Statements.FirstOrDefault(m => m.Id == include.RefId);
            //    if (refStatement != null)
            //    {
            //        include.Ref = refStatement;
            //    }
            //    else
            //    {
            //        //修复跨sqlmap的引用refId
            //        var refStatement2 = SqlMapManager.Instance.Statements.FirstOrDefault(p => p.Value.Id == include.RefId).Value;
            //        if (refStatement2 != null)
            //        {
            //            include.Ref = refStatement2;
            //        }
            //        else
            //        {
            //            throw new Exception(string.Format("Statement.Include tag can not Include statement.id:{0}", include.RefId));
            //        }


            //    }
            //}
            #endregion

            #region Init Tag

            //foreach (XmlNode tagNode in tagNodes)
            //{
            //    prepend = "";
            //    property = "";
            //    if (tagNode.Attributes != null)
            //    {
            //        prepend = tagNode.GetValueInXmlAttributes("Prepend");
            //        property = tagNode.GetValueInXmlAttributes("Property");
            //    }

            //         switch (tagNode.Name)
            //    {
            //        case "Include":
            //            {
            //                var refId = tagNode.GetValueInXmlAttributes("RefId");

            //                var refStatement = smartSqlMap.Statements.FirstOrDefault(m => m.Id == refId);
            //                if (refStatement == null)
            //                {
            //                    throw new ArgumentException(string.Format("SqlMap.Statement.Load can not find statement.id:{0}", refId));
            //                }
            //                if (refId == statement.Id)
            //                {
            //                    throw new ArgumentException(string.Format("SqlMap.Statement.Load Include.RefId can not be self statement.id:{0}", refId));
            //                }
            //                statement.SqlTags.Add(new Include
            //                {
            //                    RefId = refId,
            //                    Ref = refStatement
            //                });
            //                break;
            //            }
            //        case "Switch":
            //            {
            //                var switchTag = new Switch
            //                {
            //                    Property = property,
            //                    Prepend = prepend,
            //                    Cases = new List<Switch.Case>()
            //                };
            //                var caseNodes = tagNode.ChildNodes;
            //                foreach (XmlNode caseNode in caseNodes)
            //                {
            //                    var caseCompareValue = caseNode.GetValueInXmlAttributes("CompareValue");
            //                    var caseBodyText = caseNode.InnerText.Replace("\n", "");
            //                    switchTag.Cases.Add(new Switch.Case
            //                    {
            //                        CompareValue = caseCompareValue,
            //                        BodyText = caseBodyText
            //                    });
            //                }
            //                statement.SqlTags.Add(switchTag);
            //                break;
            //            }
            //        default:
            //            {
            //                var tag = LoadTag(tagNode , null);
            //                if (tag != null) { statement.SqlTags.Add(tag); }
            //                break;
            //            };
            //    }
            //   }

            #endregion

            return(statement);
        }