加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

利用正则表达式类解析SQL语句,达到Worklist兼容各个RIS数据库的

发布时间:2020-12-13 19:47:40 所属栏目:百科 来源:网络整理
导读:在做RIS的项目中遇到一个问题, 因为Worklist要兼容各个RIS数据库, 因此设计了目前这个架构。 1.xml文件来配置RIS数据库的数据源, 2.xml文件来存储关于查询/更新数据库的SQL语句, 其中参数/值输入可用用{x}来代替。 如{0}代表第一个参数 3.xml来配置Workl
 在做RIS的项目中遇到一个问题, 因为Worklist要兼容各个RIS数据库, 因此设计了目前这个架构。  
 1.xml文件来配置RIS数据库的数据源,  
 2.xml文件来存储关于查询/更新数据库的SQL语句, 其中参数/值输入可用用{x}来代替。 如{0}代表第一个参数 
 3.xml来配置Worklist SCU的query的字段和数据库字段的映射, 可以用Tag ID或者Tag Name与数据库中的Column名来匹配。 
 4.工作流程: Worklist SCU发出Query, WorklistSCP收到后利用映射文件, 解析成要查询的column名字,根据xml中查询数据库的xml的模板,生成真正的Sql查询语句。调用数据库API执行Sql语句。  


  public class RISQueryHelper     {         public string BuildSelectQuery(string select)         {             if (String.IsNullOrEmpty(select))             {                 throw new ArgumentNullException("input select query is null.");             }               string query = select.Replace("@","").Replace("&apos;","'");               return query;         }           public string BuildUpdateQuery(string update,params object[] columnValues)         {             if (String.IsNullOrEmpty(update))             {                 throw new ArgumentNullException("input sql statement is null or empty.");             }               BuildTableRelationship(update);               string sql = update.Replace("@","'");               //Sql template mark the parameter by{x},x like: 1,2,3,4,5,...n             //For example: UPDATE @RequestedProcedure SET @Status='{0}' WHERE @Order_Refid IN (SELECT @Refid FROM @Orders WHERE @AccessionNo='{1}';             //So we can use String.Format to replace {x} by our real value.             {                 sql = String.Format(sql,columnValues);             }               return sql;         }           public void BuildTableRelationship(string update)         {             //{Get table & column name,we will use these data to construct relationship between tables and column for further.             string[] tables = null;             string[] cols = null;               if (!BuildUpdateQueryArgs(update,out tables,out cols))             {                 throw new Exception("Build sql statement failed. Input sql is : " + update);             }             //end,need to parse when statement has many tables.}         }           public bool BuildUpdateQueryArgs(string update,out string[] tableNames,out string[] columnNames)         {             if (String.IsNullOrEmpty(update))             {                 throw new ArgumentNullException("input select query is null.");             }               List<string> tables = new List<string>();             List<string> columns = new List<string>();             string sql = update;               BuildTableNames(ref sql,ref tables);             BuildColumnNames(ref sql,ref columns);               tableNames = tables.ToArray();             columnNames = columns.ToArray();               return true;         }           private void BuildTableNames(ref string sql,ref List<string> tables)         {             var statement = Regex.Matches(                 sql,@"bfromb.+?bwhereb|bupdateb.+?bsetb",RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);               foreach (Match t in statement)             {                 //delete table operation from sql statement.                 sql = sql.Replace(t.Value,"");                   //save table name                 tables.Add(Regex.Replace(t.Value,@"bupdateb|@|bsetb|s|bfromb|bwhereb","",RegexOptions.IgnoreCase));             }         }           private void BuildColumnNames(ref string sql,ref List<string> columns)         {             var statement = Regex.Matches(                 sql,@"@w+bs*=",RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);               foreach (Match t in statement)             {                 //delete column operation from sql statement.                 sql = sql.Replace(t.Value,"");                   //save column name                 columns.Add(Regex.Replace(t.Value,@"@|s|=",RegexOptions.IgnoreCase));             }         }     } 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读