【问题标题】:C# Get base table name along with alias or correlation name from a queryC# 从查询中获取基表名称以及别名或相关名称
【发布时间】:2017-10-17 20:52:39
【问题描述】:

我正在尝试使用以下方法解析 SQL 查询:

SqlDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo);
DataTable schemaTable = reader.GetSchemaTable();

我得到了基表名,但我还需要找到表别名。

示例查询:

select AuthorId, a.Name as [AuthorName], c.Name as City, s.Name as [State] from Author a
inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId
inner join City c on c.CityId = zc.CityId
inner join [State] s on s.StateId = c.StateId

我检查了regex solution,但无法弄清楚如何提取“Author a”、“ZipCode cd”、“City c”、“[State] s”

【问题讨论】:

标签: c# sql alias correlation getschematable


【解决方案1】:

使用 Regex 类型语法必须考虑的定义。

元字符

元字符指定用户可以在相应位置输入的字符。

Character Descriptión
. Any single character.
[aeiou] Any single character from the specified character set.
[^aeiou] Any single character not from the specified character set.
[0-9a-fA-F] Any single character from the specified character range.
\w Any single alphanumeric character. The same as [a-zA-Z_0-9]
\W Any single non-alphanumeric character. The same as [^a-zA-Z_0-9]
\d Any single numeric character. The same as [0-9]
\D Any single non-numeric character. The same as [^0-9]
量词

量词跟在元字符后面,并指定该字符应重复多少次。下表列出了可用的限定符。

Quantifier Description Examples
* Zero or more matches. The same as {0,} [a-zA-Z]<em></em>, \w
+ One or more matches. The same as {1,} [a-zA-Z]+, \w+
? Zero or one matches. The same as {0,1} [a-zA-Z]?, \w?
{n} Exactly n matches. [0-9]{2}
{n,} At least n matches. [0-9]{2,}
{n,m} At least n, but not more than m matches. [0-9]{2,7}

可以提供帮助的解决方案。

1。使用 Regex 从 SQL 语句中提取表名

正则表达式

/(from|join|into)\s+([`]*\w*.*\w[`]|(\[)*\w*.*(\])|\w*\.*\w*)/g
2。使用 Regex 从 SQL 语句中提取具有别名的表名

正则表达式

(from|join|into)\s+([`]*\w*.*\w[`] *\w*|(\[)*\w*.*(\]) *\w*|\w*\.*\w* *\w*)
3。使用 Regex 从 SQL 语句中提取列名

正则表达式

/(\w*\.*\w+|`\w*.*\w`|(\[)\w*.*(\]))+(,|\s+,|\s+FROM|\s+from)/g

为 C# 生成的代码

public static class QueryExtension
    {
        public static List<string> GetTables(this string query)
        {
            List<string> tables = new List<string>();
            string pattern = @"(from|join|into)\s+([`]*\w*.*\w[`]|(\[)*\w*.*(\])|\w*\.*\w*)";            
            
            foreach (Match m in Regex.Matches(query, pattern))
            {                
                string name = m.Groups[2].Value;                                
                tables.Add(name);
            }

            return tables;
        }
        public static List<string> GetTablesWithAliases(this string query)
        {
            List<string> tables = new List<string>();
            string pattern = @"(from|join|into)\s+([`]*\w*.*\w[`] *\w*|(\[)*\w*.*(\]) *\w*|\w*\.*\w* *\w*)";

            foreach (Match m in Regex.Matches(query, pattern))
            {
                string name = m.Groups[2].Value;
                tables.Add(name);
            }

            return tables;
        }
        public static List<string> GetColumns(this string query)
        {
            List<string> columns = new List<string>();
            string pattern = @"(\w*\.*\w+|`\w*.*\w`|(\[)\w*.*(\]))+(,|\s+,|\s+FROM|\s+from)";

            foreach (Match m in Regex.Matches(query, pattern))
            {
                string name = m.Groups[1].Value;
                columns.Add(name);
            }

            return columns;
        }
        public static string Join(this IEnumerable<string> values, string separator) {
            return string.Join(separator, values);
        }
    }

测试字符串

-------------------------------------------------------
select AuthorId, a.Name as [AuthorName], c.Name as City, s.Name as [State] from Author a
inner join `dbo`.`otherTable` ot on ot.col1 = a.Name
inner join Zipcode zc on zc.ZipCodeId = a.ZipCodeId
inner join City c on c.CityId = zc.CityId
inner join [State] s on s.StateId = c.StateId
-------------------------------------------------------

输出

//-------GetTables------
Author
`dbo`.`otherTable`
Zipcode
City
[State]
//-------GetTablesWithAliases------
Author a
`dbo`.`otherTable` ot
Zipcode zc
City c
[State] s

更多 C# 代码示例(此处)
https://stackoverflow.com/a/68889908/16731336

参考文献

Extract table names from an SQL statement with Regex
Regular Expression Language - Quick Reference
Simplified Regular Expressions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 2011-06-27
    • 1970-01-01
    • 2023-03-13
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多