Created
June 19, 2012 17:06
-
-
Save joevandyk/2955316 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
begin; | |
-- Could make this a temporary table, right? | |
CREATE TABLE current_user_ids (id SERIAL PRIMARY KEY, user_id integer); | |
create function current_user_id() returns integer language sql as $f$ | |
select user_id from current_user_ids where id = currval('current_user_ids_id_seq'); | |
$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); | |
insert into current_user_ids (user_id) values (1); | |
select * from product_prices; | |
insert into current_user_ids (user_id) values (4); | |
select * from product_prices; | |
/* | |
INSERT 0 1 | |
id | price | custom_price | |
----+-------+-------------- | |
1 | 1.99 | 2.99 | |
2 | 2.50 | 3.50 | |
(2 rows) | |
INSERT 0 1 | |
id | price | custom_price | |
----+-------+-------------- | |
1 | 1.99 | 5.99 | |
2 | 2.50 | 6.50 | |
(2 rows) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment