[英]Finding Functions within SQL
在類似於以下代碼的內容中,我只想提取具有多個參數的函數,並希望忽略具有 2 個參數的合並。 請幫助我使用 REGEX 已經為此工作了幾天,並認為這可能不是我能夠解決的問題。 我相信答案是一個組,並使用子正則表達式或類似的東西解析該組。
AND NOT COALESCE(UPPER(FUNCTION_TO_FIND(B.PARAM, B.TEST_PARAM1, B.TEST_PARAM2, B.TEST_PARAM3,'Routine Type', ATT_TO_DATE())),'NO_VALUE') IN (UPPER('Routine Appointment Letter'))
AND NOT UPPER(DBO.FUNCTION_TO_FIND( B.PARAM , B.TEST_PARAM1 , B.TEST_PARAM2, B.TEST_PARAM3,'Routine Type', ATT_TO_DATE())) IN (UPPER('Routine Appointment Letter'))
AND NOT COALESCE(1, 3) = 2
我希望找到
DBO.FUNCTION_TO_FIND( B.PARAM , B.TEST_PARAM1 , B.TEST_PARAM2, B.TEST_PARAM3,'Routine Type', ATT_TO_DATE())
和
FUNCTION_TO_FIND(B.PARAM, B.TEST_PARAM1, B.TEST_PARAM2, B.TEST_PARAM3,'Routine Type', ATT_TO_DATE())
請注意,這些函數的層數並不總是相同,但它們都會有 2 個以上的參數。
我嘗試了以下平衡括號函數的不同版本和編輯,但無法正確計算參數以捕獲整個函數。
(\\((?>[^()]|(?1))*\\))
編輯,下面的澄清
EDIT1 :請注意,在查找函數時,我將無法訪問將運行 SQL 的服務器,這必須完全脫機完成。
EDIT2 :進一步考慮這個問題,我認為這是一個需要解決的問題,而不是一個正則表達式,而是使用另一種工具即時創建正則表達式。
[[:alnum:]][^)( \\r\\n]+?\\s*?\\(
(\\((?>[^()]|(?1))*\\))
我會嘗試一些具有上述效果的東西,然后帶着答案回來。 同時,如果有人有不同的想法,請隨時貢獻。
好吧,我給它一個嘗試,並Microsoft.SqlServer.Management.SqlParser.Parser
從Microsoft.SqlServer.SqlManagementObjects
NuGet包可能是要走的路。
var sql = @"SELECT .... ";
var result = Parser.Parse(sql);
var batch = result.Script.Batches.First();
var select = batch.Children.Cast<SqlSelectStatement>().First();
var selectSpec = select.SelectSpecification;
因此, selectSpec.Xml
包含一個表示SELECT
語句的分層 XML 文檔。
SqlCodeObject
提供了一個實現訪問者模式的Accept()
方法。 編寫訪問者並評估您感興趣的類型的表達式: SqlNullScalarExpression
、 SqlUserDefinedScalarFunctionCallExpression
,也許更多。
請注意,解析器無法識別沒有dbo.
用戶定義函數dbo.
架構:
<SqlNullScalarExpression Location="((3,9),(3,137))">
<!--COALESCE(UPPER(FUNCTION_TO_FIND(B.PARAM, B.TEST_PARAM1, B.TEST_PARAM2, B.TEST_PARAM3,'Routine Type', ATT_TO_DATE())),'NO_VALUE')-->
</SqlNullScalarExpression>
相對
<SqlUserDefinedScalarFunctionCallExpression Location="((4,15),(4,122))" ObjectIdentifier="DBO.FUNCTION_TO_FIND">
<!--DBO.FUNCTION_TO_FIND( B.PARAM , B.TEST_PARAM1 , B.TEST_PARAM2, B.TEST_PARAM3,'Routine Type', ATT_TO_DATE())-->
<SqlObjectIdentifier Location="((4,15),(4,35))" SchemaName="DBO" ObjectName="FUNCTION_TO_FIND">
<!--DBO.FUNCTION_TO_FIND-->
<SqlIdentifier Location="((4,15),(4,18))" Value="DBO">
<!--DBO-->
</SqlIdentifier>
<SqlIdentifier Location="((4,19),(4,35))" Value="FUNCTION_TO_FIND">
<!--FUNCTION_TO_FIND-->
</SqlIdentifier>
</SqlObjectIdentifier>
...
<SqlBuiltinScalarFunctionCallExpression Location="((4,108),(4,121))" FunctionName="ATT_TO_DATE" IsStar="False">
<!--ATT_TO_DATE()-->
</SqlBuiltinScalarFunctionCallExpression>
</SqlUserDefinedScalarFunctionCallExpression>
我在 c# 中使用了以下與正則表達式混合來完成這個任務,是的,它非常粗糙,可以使用一些改進,但它確實有效。 隨意改進答案,但我將無法為下面的代碼提供持續支持。
Console.WriteLine("=====================================================");
int numberOfIterations = 0;
do
{// Variables
string searchResult; // Start of function call
string functionName; // Function Name
// funcFonud will be char length of function ( "IN (" length is 4)
// used to trim the substring for the next itteration.
int funcFoundNameLen = 0;
int funcFoundAt = 0;
int matchedParenEnd = 0; // End of Matched Paren
// Find the start of a function call.
string findFuncReg = "[a-zA-Z][^)( \\r\\n\\\"]+?\\s*?\\("; // fixes [[:alnum:]] issue
if (query.Length > 0)
{
Match match = Regex.Match(query, findFuncReg);
Console.WriteLine("Trying to match: "+query);
if (match.Success && match.Index >= 0) // Function call found
{
try
{
Console.WriteLine("Match was found: " + match.Index.ToString() + match.Value);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
funcFoundNameLen = match.Length;
funcFoundAt = match.Index;
searchResult = query.Substring(funcFoundAt);
functionName = searchResult.Substring(0, searchResult.IndexOf('(')).Trim();
writeFunc(functionName);
}
else
{
Console.WriteLine("Function Start NOT found");
return;
}
string subString = query.Substring(match.Index);
int openParen = 0;
// Following finds the matched paren
for (int i = 0; i < subString.Length; i++)
{
if (subString[i] == ')' && openParen == 1)
{
matchedParenEnd = i + 1; // Location of the end Paren
break;
}
// Following handles opening and closing of paren so that we
// can find the matched paren.
else
{
if (subString[i] == '(')
{
openParen = openParen + 1;
}
if (subString[i] == ')')
{
openParen = openParen - 1;
}
}
}
// Output function call.
string subCall = subString.Substring(funcFoundNameLen, matchedParenEnd- funcFoundNameLen);
Console.WriteLine("Sub Call: " + subCall);
// Set up for recursive call.
// string querySub = query.Substring(funcFoundAt);
// Console.WriteLine("querySub: " + querySub);
matchedParen(subCall);
// Substring based on the location that the function call was
// made to the end of the function call
Console.WriteLine("Remove: " + (funcFoundAt + matchedParenEnd).ToString() + " letters");
query = query.Substring(funcFoundAt + matchedParenEnd);
numberOfIterations++;
}
} while (query.Length > 0
// && numberOfIterations < 1
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.