问题描述
我有以下工作代码:
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQuery = """
NOTIFY updateObject;
"""
plan = plpy.prepare(updateQuery)
plpy.execute(plan)
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
我现在想将字符串传递给我的sql代码:
CREATE EXTENSION IF NOT EXISTS plpython3u;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpython3u';
CREATE OR REPLACE FUNCTION test_notif_trigger_function() RETURNS trigger AS
$$
updateQuery = """
NOTIFY updateObject,$1;
"""
plan = plpy.prepare(updateQuery,["text"])
plpy.execute(plan,["test"])
$$ LANGUAGE plpython3u;
DROP TRIGGER IF EXISTS test_notif_trigger ON columnTest;
CREATE TRIGGER test_notif_trigger
AFTER UPDATE OF state
ON columnTest
FOR EACH ROW EXECUTE PROCEDURE test_notif_trigger_function();
UPDATE columnTest C
SET state=8
WHERE C.id=1;
但这给了我这个错误:
ERROR: spiexceptions.SyntaxError: syntax error at or near "$1"
LINE 3: NOTIFY updateObject,$1;
我不明白我的错误:我尝试了一个简单的SELECT并成功了。但是我不能在NOTIFY上使用$ 1。您有任何想法或方法可以通过plpython通知自定义字符串吗? (我的代码应该传递具有更新值的强制转换字典,因此我确实确实需要首先使用plpython然后使用NOTIFY)
解决方法
我终于找到了使用schema = [{"name": "fb_table_1","dimension": "department","metrics": ["headcount"]},{"name": "fb_table_2","dimension": "area","metrics": ["sale"]},{"name": "fb_table_3","dimension": "product","metrics": ["quantity","revenue"]}]
def get_required_string(table,metric):
template = f'''
select date,'{table["dimension"]}',{table['dimension']},'{metric}',{metric} from {table['name']}
'''
return template
sql_2 = " union ".join([get_required_string(table,metric) for table in schema for metric in table["metrics"]])
print(sql_2)
字符串方法的解决方法。
format