我正在将旧的 oracle sql join 语法转换为 postgres 中兼容的 ANSI sql 代码。我正在使用 python 来为此目的创建实用程序。到目前为止,我的研究非常好,我发现使用sqlparser
python 库。
示例 sql 语法:
SELECT i.product_id
, d.language_id
, CASE WHEN d.language_id IS NOT NULL
THEN d.translated_name
ELSE TRANSLATE(i.product_name USING NCHAR_CS)
END AS product_name
, i.category_id
, CASE WHEN d.language_id IS NOT NULL
THEN d.translated_description
ELSE TRANSLATE(i.product_description USING NCHAR_CS)
END AS product_description
, i.weight_class
, i.warranty_period
, i.supplier_id
, i.product_status
, i.list_price
, i.min_price
, i.catalog_url
FROM product_information i
, product_descriptions d
WHERE d.product_id (+) = i.product_id
AND d.language_id (+) = sys_context('USERENV','LANG');
预期输出:
SELECT i.product_id
, d.language_id
, CASE WHEN d.language_id IS NOT NULL
THEN d.translated_name
ELSE TRANSLATE(i.product_name USING NCHAR_CS)
END AS product_name
, i.category_id
, CASE WHEN d.language_id IS NOT NULL
THEN d.translated_description
ELSE TRANSLATE(i.product_description USING NCHAR_CS)
END AS product_description
, i.weight_class
, i.warranty_period
, i.supplier_id
, i.product_status
, i.list_price
, i.min_price
, i.catalog_url
FROM product_information i right outer join product_descriptions d
on
d.product_id = i.product_id
and d.language_id = sys_context('USERENV','LANG');
到目前为止,我已经尝试过这样的。我有标记,但不确定如何在旧版 Oracle 连接语法的情况下交换位置。如果有人可以提供一些见解。我对 python 很陌生。
import sqlparse
query= '''
SELECT i.product_id
, d.language_id
, CASE WHEN d.language_id IS NOT NULL
THEN d.translated_name
ELSE TRANSLATE(i.product_name USING NCHAR_CS)
END AS product_name
, i.category_id
, CASE WHEN d.language_id IS NOT NULL
THEN d.translated_description
ELSE TRANSLATE(i.product_description USING NCHAR_CS)
END AS product_description
, i.weight_class
, i.warranty_period
, i.supplier_id
, i.product_status
, i.list_price
, i.min_price
, i.catalog_url
FROM product_information i
, product_descriptions d
WHERE d.product_id (+) = i.product_id
AND d.language_id (+) = sys_context('USERENV','LANG')'''
res=sqlparse.parse(query)
stmt = res[0]
print (stmt.tokens[-1])
输出我得到 where 子句连接
WHERE d.product_id (+) = i.product_id
AND d.language_id (+) = sys_context('USERENV','LANG');
我被困在这里,如何将其重写为
FROM product_information i right outer join product_descriptions d
on d.product_id = i.product_id
and d.language_id = sys_context('USERENV','LANG');