public Statement Load(XElement statementNode, SmartSqlMap smartSqlMap) { var statement = Create(statementNode); statement.SmartSqlMap = smartSqlMap; var type = statementNode.GetAttribute("ResultType"); if (string.IsNullOrWhiteSpace(type) == false) { statement.ResultType = Type.GetType(type); } string cacheId = statementNode.GetAttribute("Cache"); #region Attribute For Cache & ResultMap & ParameterMap 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}"); } string resultMapId = statementNode.GetAttribute("ResultMap"); if (!String.IsNullOrEmpty(resultMapId)) { var resultMap = smartSqlMap.ResultMaps.FirstOrDefault(r => r.Id == resultMapId); statement.ResultMap = resultMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find ResultMap.Id:{resultMapId}"); } string parameterMapId = statementNode.GetAttribute("ParameterMap"); if (!String.IsNullOrEmpty(parameterMapId)) { var parameterMap = smartSqlMap.ParameterMaps.FirstOrDefault(r => r.Id == parameterMapId); statement.ParameterMap = parameterMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find ParameterMap.Id:{parameterMapId}"); } #endregion var tagNodes = statementNode.Nodes(); IList <Include> includes = new List <Include>(); foreach (XNode 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); }
private void LoadStatementInSqlMap(SmartSqlMap sqlMap, XmlNodeList statementNodes) { foreach (XmlElement statementNode in statementNodes) { var statement = _statementFactory.Load(statementNode, sqlMap); sqlMap.Statements.Add(statement.FullSqlId, statement); } }
private void LoadStatementInSqlMap(SmartSqlMap sqlMap, IEnumerable <XElement> statementNodes) { foreach (XElement statementNode in statementNodes) { var statement = _statementFactory.Load(statementNode, sqlMap); sqlMap.Statements.Add(statement); } }
public 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; #region Attribute For Cache & ResultMap & ParameterMap 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}"); } string resultMapId = statementNode.Attributes["ResultMap"]?.Value; if (!String.IsNullOrEmpty(resultMapId)) { var resultMap = smartSqlMap.ResultMaps.FirstOrDefault(r => r.Id == resultMapId); statement.ResultMap = resultMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find ResultMap.Id:{resultMapId}"); } string parameterMapId = statementNode.Attributes["ParameterMap"]?.Value; if (!String.IsNullOrEmpty(parameterMapId)) { var parameterMap = smartSqlMap.ParameterMaps.FirstOrDefault(r => r.Id == parameterMapId); statement.ParameterMap = parameterMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find ParameterMap.Id:{parameterMapId}"); } #endregion 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); }
private void InitSqlMapStatementMap(SmartSqlMap sqlmap) { foreach (var statementKV in sqlmap.Statements) { var statement = statementKV.Value; if (MappedStatement.ContainsKey(statement.FullSqlId)) { throw new SmartSqlException($"SmartSqlMapConfig Load MappedStatements: StatementId:{statement.FullSqlId} already exists!"); } MappedStatement.Add(statement.FullSqlId, statement); } }
private void InitSqlMapStatementMap(SmartSqlMap sqlmap) { foreach (var statement in sqlmap.Statements) { if (MappedStatement.ContainsKey(statement.FullSqlId)) { MappedStatement.Remove(statement.FullSqlId); if (_logger.IsEnabled(LogLevel.Warning)) { _logger.LogWarning($"SmartSqlMapConfig Load MappedStatements: StatementId:{statement.FullSqlId} already exists!"); } } MappedStatement.Add(statement.FullSqlId, statement); } }
public async Task <SmartSqlMap> LoadSmartSqlMapAsync(string path, SmartSqlMapConfig smartSqlMapConfig) { var sqlMap = new SmartSqlMap { SmartSqlMapConfig = smartSqlMapConfig, Path = path, Statements = new List <Statement> { }, Caches = new List <SqlMap.Cache> { } }; var configResult = await ZooClient.getDataAsync(path, new SmartSqlMapWatcher(smartSqlMapConfig, this)); using (MemoryStream configStream = new MemoryStream(configResult.Data)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configStream); XmlNamespaceManager xmlNsM = new XmlNamespaceManager(xmlDoc.NameTable); xmlNsM.AddNamespace("ns", "http://SmartSql.net/schemas/SmartSqlMap.xsd"); sqlMap.Scope = xmlDoc.SelectSingleNode("//ns:SmartSqlMap", xmlNsM) .Attributes["Scope"].Value; #region Init Caches var cacheNodes = xmlDoc.SelectNodes("//ns:Cache", xmlNsM); foreach (XmlElement cacheNode in cacheNodes) { var cache = SqlMap.Cache.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 return(sqlMap); } }
private SmartSqlMap LoadSmartSqlMap(string path, SmartSqlMapConfig smartSqlMapConfig) { var sqlMap = new SmartSqlMap { SmartSqlMapConfig = smartSqlMapConfig, Path = path, Statements = new List <Statement> { }, Caches = new List <SqlMap.Cache> { } }; using (var configStream = FileLoader.Load(path)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configStream); XmlNamespaceManager xmlNsM = new XmlNamespaceManager(xmlDoc.NameTable); xmlNsM.AddNamespace("ns", "http://SmartSql.net/schemas/SmartSqlMap.xsd"); sqlMap.Scope = xmlDoc.SelectSingleNode("//ns:SmartSqlMap", xmlNsM) .Attributes["Scope"].Value; #region Init Caches var cacheNodes = xmlDoc.SelectNodes("//ns:Cache", xmlNsM); foreach (XmlElement cacheNode in cacheNodes) { var cache = SqlMap.Cache.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 return(sqlMap); } }
public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream) { using (configStream.Stream) { var sqlMap = new SmartSqlMap { SqlMapConfig = SqlMapConfig, Path = configStream.Path, Statements = new Dictionary <String, Statement> { }, Caches = new Dictionary <String, Configuration.Cache> { }, ResultMaps = new Dictionary <String, ResultMap> { }, MultipleResultMaps = new Dictionary <String, MultipleResultMap> { }, ParameterMaps = new Dictionary <String, ParameterMap> { } }; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configStream.Stream); XmlNamespaceManager xmlNsM = new XmlNamespaceManager(xmlDoc.NameTable); xmlNsM.AddNamespace("ns", "http://SmartSql.net/schemas/SmartSqlMap.xsd"); sqlMap.Scope = xmlDoc.SelectSingleNode("//ns:SmartSqlMap", xmlNsM) .Attributes["Scope"].Value; #region Init Caches var cacheNodes = xmlDoc.SelectNodes("//ns:Caches/ns:Cache", xmlNsM); foreach (XmlElement cacheNode in cacheNodes) { var cache = CacheFactory.Load(cacheNode, sqlMap); sqlMap.Caches.Add(cache.Id, cache); } #endregion #region Init ResultMaps var resultMapsNodes = xmlDoc.SelectNodes("//ns:ResultMaps/ns:ResultMap", xmlNsM); foreach (XmlElement xmlNode in resultMapsNodes) { var resultMap = MapFactory.LoadResultMap(xmlNode, sqlMap, xmlNsM); sqlMap.ResultMaps.Add(resultMap.Id, resultMap); } #endregion #region Init MultipleResultMaps var multipleResultMapsNode = xmlDoc.SelectNodes("//ns:MultipleResultMaps/ns:MultipleResultMap", xmlNsM); foreach (XmlElement xmlNode in multipleResultMapsNode) { var multipleResultMap = MapFactory.LoadMultipleResultMap(xmlNode, sqlMap); sqlMap.MultipleResultMaps.Add(multipleResultMap.Id, multipleResultMap); } #endregion #region Init ParameterMaps var parameterMaps = xmlDoc.SelectNodes("//ns:ParameterMaps/ns:ParameterMap", xmlNsM); foreach (XmlElement xmlNode in parameterMaps) { var parameterMap = MapFactory.LoadParameterMap(xmlNode, sqlMap); sqlMap.ParameterMaps.Add(parameterMap.Id, parameterMap); } #endregion #region Init Statement var statementNodes = xmlDoc.SelectNodes("//ns:Statements/ns:Statement", xmlNsM); LoadStatementInSqlMap(sqlMap, statementNodes); #endregion return(sqlMap); } }
public Statement Load(XmlElement statementNode, SmartSqlMap smartSqlMap) { var statement = new Statement { Id = statementNode.Attributes["Id"].Value, SqlTags = new List <ITag> { }, ReadDb = statementNode.Attributes["ReadDb"]?.Value, SmartSqlMap = smartSqlMap, CacheId = statementNode.Attributes["Cache"]?.Value, ResultMapId = statementNode.Attributes["ResultMap"]?.Value, ParameterMapId = statementNode.Attributes["ParameterMap"]?.Value, MultipleResultMapId = statementNode.Attributes["MultipleResultMap"]?.Value, IncludeDependencies = new List <Include>() }; if (!String.IsNullOrEmpty(statement.ReadDb)) { var readDb = smartSqlMap.SqlMapConfig.Database.ReadDataSources?.FirstOrDefault(m => m.Name == statement.ReadDb); if (readDb == null) { throw new SmartSqlException($"Statement.Id:{statement.FullSqlId} can not find ReadDb:{statement.ReadDb}!"); } } #region Init CacheId & ResultMapId & ParameterMapId & MultipleResultMapId if (statement.CacheId?.IndexOf('.') < 0) { statement.CacheId = $"{smartSqlMap.Scope}.{statement.CacheId}"; } if (statement.ResultMapId?.IndexOf('.') < 0) { statement.ResultMapId = $"{smartSqlMap.Scope}.{statement.ResultMapId}"; } if (statement.ParameterMapId?.IndexOf('.') < 0) { statement.ParameterMapId = $"{smartSqlMap.Scope}.{statement.ParameterMapId}"; } if (statement.MultipleResultMapId?.IndexOf('.') < 0) { statement.MultipleResultMapId = $"{smartSqlMap.Scope}.{statement.MultipleResultMapId}"; } #endregion #region Init CommandType & SourceChoice & Transaction var cmdTypeStr = statementNode.Attributes["CommandType"]?.Value; var sourceChoiceStr = statementNode.Attributes["SourceChoice"]?.Value; var transactionStr = statementNode.Attributes["Transaction"]?.Value; if (Enum.TryParse <CommandType>(cmdTypeStr, out CommandType cmdType)) { statement.CommandType = cmdType; } if (Enum.TryParse <DataSourceChoice>(cmdTypeStr, out DataSourceChoice sourceChoice)) { statement.SourceChoice = sourceChoice; } if (Enum.TryParse <IsolationLevel>(transactionStr, out IsolationLevel isolationLevel)) { statement.Transaction = isolationLevel; } #endregion var tagNodes = statementNode.ChildNodes; foreach (XmlNode tagNode in tagNodes) { var tag = LoadTag(tagNode, statement); if (tag != null) { statement.SqlTags.Add(tag); } } return(statement); }
public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream) { try { using (configStream.Stream) { var sqlMap = new SmartSqlMap { SqlMapConfig = SqlMapConfig, Path = configStream.Path, Statements = new List <Statement> { }, Caches = new List <Configuration.Cache> { }, ResultMaps = new List <ResultMap> { }, ParameterMaps = new List <ParameterMap> { } }; XDocument xmlDoc = XDocument.Load(configStream.Stream, LoadOptions.SetLineInfo); var nametable = xmlDoc.Root.CreateReader().NameTable; XmlNamespaceManager xmlNsM = new XmlNamespaceManager(nametable); xmlNsM.AddNamespace("ns", "http://SmartSql.net/schemas/SmartSqlMap.xsd"); sqlMap.Scope = xmlDoc.Root.XPathSelectElement("//ns:SmartSqlMap", xmlNsM) .Attribute("Scope").Value; #region Init Caches var cacheNodes = xmlDoc.XPathSelectElements("//ns:Cache", xmlNsM); foreach (XElement cacheNode in cacheNodes) { var cache = CacheFactory.Load(cacheNode); sqlMap.Caches.Add(cache); } #endregion #region Init ResultMaps var resultMapsNodes = xmlDoc.XPathSelectElements("//ns:ResultMap", xmlNsM); foreach (XElement xmlNode in resultMapsNodes) { var resultMap = MapFactory.LoadResultMap(xmlNode, SqlMapConfig); sqlMap.ResultMaps.Add(resultMap); } #endregion #region Init ParameterMaps var parameterMaps = xmlDoc.XPathSelectElements("//ns:ParameterMap", xmlNsM); foreach (XElement xmlNode in parameterMaps) { var parameterMap = MapFactory.LoadParameterMap(xmlNode, SqlMapConfig); sqlMap.ParameterMaps.Add(parameterMap); } #endregion #region Init Statement var statementNodes = xmlDoc.XPathSelectElements("//ns:Statement", xmlNsM); LoadStatementInSqlMap(sqlMap, statementNodes); var insertNodes = xmlDoc.XPathSelectElements("//ns:Insert", xmlNsM); LoadStatementInSqlMap(sqlMap, insertNodes); var updateNodes = xmlDoc.XPathSelectElements("//ns:Update", xmlNsM); LoadStatementInSqlMap(sqlMap, updateNodes); var deleteNodes = xmlDoc.XPathSelectElements("//ns:Delete", xmlNsM); LoadStatementInSqlMap(sqlMap, deleteNodes); var selectNodes = xmlDoc.XPathSelectElements("//ns:Select", xmlNsM); LoadStatementInSqlMap(sqlMap, selectNodes); #endregion return(sqlMap); } } catch (Exception ex) { throw new Exception(configStream.Path, ex); } }
public Statement Load(XmlElement statementNode, SmartSqlMap smartSqlMap) { var statement = new Statement { Id = statementNode.Attributes["Id"].Value, SqlTags = new List <ITag> { }, SmartSqlMap = smartSqlMap }; var cmdTypeStr = statementNode.Attributes["CommandType"]?.Value; var sourceChoiceStr = statementNode.Attributes["SourceChoice"]?.Value; if (Enum.TryParse <CommandType>(cmdTypeStr, out CommandType cmdType)) { statement.CommandType = cmdType; } if (Enum.TryParse <DataSourceChoice>(cmdTypeStr, out DataSourceChoice sourceChoice)) { statement.SourceChoice = sourceChoice; } string cacheId = statementNode.Attributes["Cache"]?.Value; #region Attribute For Cache & ResultMap & ParameterMap & MultipleResultMap 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}"); } string resultMapId = statementNode.Attributes["ResultMap"]?.Value; if (!String.IsNullOrEmpty(resultMapId)) { var resultMap = smartSqlMap.ResultMaps.FirstOrDefault(r => r.Id == resultMapId); statement.ResultMap = resultMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find ResultMap.Id:{resultMapId}"); } string parameterMapId = statementNode.Attributes["ParameterMap"]?.Value; if (!String.IsNullOrEmpty(parameterMapId)) { var parameterMap = smartSqlMap.ParameterMaps.FirstOrDefault(r => r.Id == parameterMapId); statement.ParameterMap = parameterMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find ParameterMap.Id:{parameterMapId}"); } string multipleResultMapId = statementNode.Attributes["MultipleResultMap"]?.Value; if (!String.IsNullOrEmpty(multipleResultMapId)) { var multipleResultMap = smartSqlMap.MultipleResultMaps.FirstOrDefault(r => r.Id == multipleResultMapId); statement.MultipleResultMap = multipleResultMap ?? throw new SmartSqlException($"Statement.Id:{statement.Id} can not find MultipleResultMap.Id:{multipleResultMapId}"); } #endregion StringBuilder fullSqlTextBuilder = new StringBuilder(); var tagNodes = statementNode.ChildNodes; IList <Include> includes = new List <Include>(); foreach (XmlNode tagNode in tagNodes) { var tag = LoadTag(tagNode, includes, fullSqlTextBuilder); 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 var fullSqlText = fullSqlTextBuilder.ToString(); statement.SqlCommandType = _sqlCommandAnalyzer.Analyse(fullSqlText); return(statement); }
public static Cache Load(XmlElement cacheNode, SmartSqlMap sqlMap) { var cache = new Cache { Id = cacheNode.Attributes["Id"].Value, Type = cacheNode.Attributes["Type"].Value, Parameters = new Dictionary <String, Object>(), FlushOnExecutes = new List <FlushOnExecute>() }; if (cache.Id.IndexOf('.') < 0) { cache.Id = $"{sqlMap.Scope}.{cache.Id}"; } foreach (XmlNode childNode in cacheNode.ChildNodes) { switch (childNode.Name) { case "Parameter": { string key = childNode.Attributes["Key"]?.Value; string val = childNode.Attributes["Value"]?.Value; if (!String.IsNullOrEmpty(key)) { cache.Parameters.Add(key, val); } break; } case "FlushInterval": { string hoursStr = childNode.Attributes["Hours"]?.Value; string minutesStr = childNode.Attributes["Minutes"]?.Value; string secondsStr = childNode.Attributes["Seconds"]?.Value; int.TryParse(hoursStr, out int hours); int.TryParse(minutesStr, out int minutes); int.TryParse(secondsStr, out int seconds); cache.FlushInterval = new FlushInterval { Hours = hours, Minutes = minutes, Seconds = seconds }; cache.Parameters.Add("FlushInterval", cache.FlushInterval); break; } case "FlushOnExecute": { string statementId = childNode.Attributes["Statement"]?.Value; if (!String.IsNullOrEmpty(statementId)) { if (statementId.IndexOf('.') < 0) { statementId = $"{sqlMap.Scope}.{statementId}"; } cache.FlushOnExecutes.Add(new FlushOnExecute { Statement = statementId }); } break; } } } cache.Parameters.Add("Cache.Id", cache.Id); cache.Provider = CreateCacheProvider(cache); return(cache); }
public SmartSqlMap LoadSmartSqlMap(ConfigStream configStream) { using (configStream.Stream) { var sqlMap = new SmartSqlMap { SqlMapConfig = SqlMapConfig, Path = configStream.Path, Statements = new List <Statement> { }, Caches = new List <Configuration.Cache> { }, ResultMaps = new List <ResultMap> { }, ParameterMaps = new List <ParameterMap> { } }; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configStream.Stream); XmlNamespaceManager xmlNsM = new XmlNamespaceManager(xmlDoc.NameTable); xmlNsM.AddNamespace("ns", "http://SmartSql.net/schemas/SmartSqlMap.xsd"); sqlMap.Scope = xmlDoc.SelectSingleNode("//ns:SmartSqlMap", xmlNsM) .Attributes["Scope"].Value; #region Init Caches var cacheNodes = xmlDoc.SelectNodes("//ns:Cache", xmlNsM); foreach (XmlElement cacheNode in cacheNodes) { var cache = CacheFactory.Load(cacheNode); sqlMap.Caches.Add(cache); } #endregion #region Init ResultMaps var resultMapsNodes = xmlDoc.SelectNodes("//ns:ResultMap", xmlNsM); foreach (XmlElement xmlNode in resultMapsNodes) { var resultMap = MapFactory.LoadResultMap(xmlNode, SqlMapConfig, xmlNsM); sqlMap.ResultMaps.Add(resultMap); } #endregion #region Init ParameterMaps var parameterMaps = xmlDoc.SelectNodes("//ns:ParameterMap", xmlNsM); foreach (XmlElement xmlNode in parameterMaps) { var parameterMap = MapFactory.LoadParameterMap(xmlNode, SqlMapConfig); sqlMap.ParameterMaps.Add(parameterMap); } #endregion #region Init Statement var statementNodes = xmlDoc.SelectNodes("//ns:Statement", xmlNsM); LoadStatementInSqlMap(sqlMap, statementNodes); var insertNodes = xmlDoc.SelectNodes("//ns:Insert", xmlNsM); LoadStatementInSqlMap(sqlMap, insertNodes); var updateNodes = xmlDoc.SelectNodes("//ns:Update", xmlNsM); LoadStatementInSqlMap(sqlMap, updateNodes); var deleteNodes = xmlDoc.SelectNodes("//ns:Delete", xmlNsM); LoadStatementInSqlMap(sqlMap, deleteNodes); var selectNodes = xmlDoc.SelectNodes("//ns:Select", xmlNsM); LoadStatementInSqlMap(sqlMap, selectNodes); #endregion return(sqlMap); } }