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


    在做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("'", "'");
     
                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("@", "").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,
                    @"from.+?where|update.+?set",
                    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, @"update|@|set|s|from|where", "", RegexOptions.IgnoreCase));
                }
            }
     
            private void BuildColumnNames(ref string sql, ref List<string> columns)
            {
                var statement = Regex.Matches(
                    sql,
                    @"@w+s*=",
                    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));
                }
            }
        }
    
  • 相关阅读:
    dotnet core gbk 编码错误解决方案
    测试工程师有福啦!一键生成api文档及测试功能
    Asp.net MVC + AngularJS 统一权限管理系统(一)
    移动开发在路上-- IOS移动开发 五 网络请求封装
    移动开发在路上-- IOS移动开发系列 网络交互四(2)
    移动开发在路上-- IOS移动开发系列 网络交互四(1)
    GZIP怎么运用在.NET MVC 简单实现
    顺时针打印矩阵
    numpy教程 pandas教程 Python数据科学计算简介(莫烦 视频链接+代码笔记)
    4. Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/muzizongheng/p/3170840.html
Copyright © 2020-2023  润新知