Created
June 19, 2012 17:45
-
-
Save joevandyk/2955512 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create sequence current_user_id; | |
create function set_current_user_id(user_id integer) returns integer language sql as $f$ | |
select setval('current_user_id', $1::integer)::integer; | |
$f$; | |
create function current_user_id() returns integer language sql as $f$ | |
select currval('current_user_id')::integer; | |
$f$; | |
create table products(id integer primary key, price numeric); | |
insert into products values (1, 1.99), (2, 2.50); | |
create function price(product_id integer, user_id integer) returns numeric language sql as $f$ | |
select price + $2 from products where products.id = $1; | |
$f$; | |
create view product_prices as ( | |
select products.id, price, (select price(products.id, current_user_id())) as custom_price | |
from products); | |
select set_current_user_id(1); | |
select * from product_prices; | |
select set_current_user_id(4); | |
select * from product_prices; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment