如何为用户/连接设置一些上下文变量 [英] How to set some context variable for a user/connection
本文介绍了如何为用户/连接设置一些上下文变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前使用 Firebird SQL 作为我的共享软件的数据库后端,也希望支持 PG 9.3+.
I currently use Firebird SQL as the db backend of my shareware, would like to support also PG 9.3+.
在 FB 中,我使用 set/get_context 来做到这一点:
In FB I use set/get_context to do this:
例如我会在用户登录后执行此操作:
e.g. I would do this after a user logs in:
select rdb$set_context('USER_SESSION', 'LANGUAGE_ID', %s) \
from rdb$database" % appobj.loggedInUser.language.id
在我的一些观点中,我会使用:
In some of my views I would then use:
... AND t.fk_language_id=rdb$get_context('USER_SESSION', 'LANGUAGE_ID')
我搜索了 PG 文档并进行了一些谷歌搜索,但还没有找到解决方案.
I searched through PG doc and did some googling but didn't find a solution yet.
希望得到任何提示.维尔纳
Would appreciate any tips. Werner
推荐答案
您可以在自定义架构中使用会话变量:
You can use a session variables in custom schema:
postgres=# set myvars.language_id = 10;
SET
postgres=# show myvars.language_id;
myvars.language_id
--------------------
10
(1 row)
或通过函数 (http://www.postgresql.org/docs/9.4/static/functions-admin.html):
postgres=# select set_config('myvars.language_id', '20', false);
set_config
------------
20
(1 row)
postgres=# select current_setting('myvars.language_id');
current_setting
-----------------
20
(1 row)
这篇关于如何为用户/连接设置一些上下文变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文