从SQL查询中获取参数名称

后端是PostgreSQL服务器9.1。

我正在尝试构建AdHoc XML报告。 报告文件将包含SQL查询,所有这些查询都必须以SELECT语句开头。 SQL查询将包含参数。 根据相关列的数据类型,这些参数将相应地呈现给用户以提供值。

一个重要的SQL查询:

SELECT * FROM customers WHERE ( customers.customer_code=@customer_code AND customers.location=@location AND customers.type= ( SELECT type from types WHERE types.code=@type_code AND types.is_active = @type_is_active ) AND customers.account_open_date BETWEEN @start_date AND @end_date ) OR customers.flagged = @flagged; 

我想从查询字符串中获取列名和参数的列表,并将它们放入字符串数组中并稍后处理。

我能够使用以下正则表达式仅匹配参数:

 @(?)(?\w+) 

预期比赛:

 customers.customer_code=@customer_code customers.location=@location types.code=@type_code types.is_active = @type_is_active customers.account_open_date BETWEEN @start_date AND @end_date customers.flagged = @flagged 

如何立即匹配“@Parameter”,“=”和“BETWEEN”?

我知道这有点晚了,但对于未来的研究来说:

我认为这个正则表达式符合您的目的:

 (\w+\.\w+(?:\s?\=\s?\@\w+|\sBETWEEN\s\@\w+\sAND\s\@\w+)) 

在这里检查这个Regex101小提琴 ,仔细阅读每个部分的说明。

基本上,它首先查找customer.xxx_yyy列,然后查找= @variableBETWEEN @variable1 AND @variable2

被捕组:

 MATCH 1 1. [37-75] `customers.customer_code=@customer_code` MATCH 2 1. [80-108] `customers.location=@location` MATCH 3 1. [184-205] `types.code=@type_code` MATCH 4 1. [218-251] `types.is_active = @type_is_active` MATCH 5 1. [266-327] `customers.account_open_date BETWEEN @start_date AND @end_date` MATCH 6 1. [333-361] `customers.flagged = @flagged`