I am working in Postgres and I need to send in a full result set with many rows and column into a stored procedure or a function. Is this possible? If so, where can I see resources for syntax?
OK this is how I have it set up without being able to send in a result set, it forces me to break out comparison logic and put it in two different spots, however my goal is to keep the actual finding the promotion logic in one place, which I have done here. This may change one day, the comparison logic is less likely to change, it is pretty standard.
Promotion Line Item Logic
-There will be triggers set on INSERT for the promo_objects, promo_buy_objects, and promo_get_objects tables, there will be an UPDATE trigger on the promo table. -The trigge开发者_JS百科r for the xrefs will call a stored procedure called set_best_product_promos that will decide which promotion is best for that object and it will then save to a new table:
promo_best_product_promos
promo_id, object_id, expiration_date
-The trigger for promo will call update_best_product_promos and will send in the promo_id and if active = true it will update the expiration date for that promo else it will delete all entries for that promo
The new table has been added to the promo.sql script, however the triggers and function can not be added until the function is written.
A script will run at midnight every night to delete the entries that have expired. PSEUDO FOR cart code (application code) Run the union query just as we are now shown_object_promotions (this gets all available promotions for the item)
Loop through results
if buy_quantity > 0
IF the quantity of the buy item in the cart is greater than or = the buy_quantity (I think c.active_items is the items in the cart)
IF get_quantity > 0
If the get item is in the cart AND it is the item sent into this function (I think c.active_items is the items in the cart)
run the get_best_product_promos function
run comparison logic
else
run the get_best_product_promos function
run comparison logic
EDIT: So I guess I could dump this cart logic as a stored procedure as well, and then make one for the comparison logic, and boom its all in stored procedures and portable and generic?
PSEUDO FOR set_best_product_promos:
-You will send in the object_id and promo_id
-You will declare all of your variables
-Go ahead an query the end date of the promo
-You will then query the promo_best_product_promos table to see if an entry exists for this product
IF exists:
RUN YOUR UNION QUERY accept this time you will have to explicitly say all the fields you want and what variables to select them into
Then loop through your query
LOOP
run get_best_product_promos
run comparison logic
END LOOP
Now take those variables you set in the crazy logic and update promo_best_product_promos
ELSE:
insert the object_id, promo_id, and end date (expiration_date) into the promo_best_product_promos table
PSEUDO FOR get_best_product_promos:
If no buy and no get quantities
If discount type = percent
calculate value of the promotion for this item to compare later
calculate the new price for the product and update the estimated unit price
If discount type = dollar
calculate value of the promotion for this item to compare later
calculate the new price for the product and update the estimated unit price
If discount type = price
calculate value of the promotion for this item to compare later
calculate the new price for the product and update the estimated unit price
If discount amount = Free
do nothing
pass
If buy quantity but no get quantity
If discount type = percent
calculate value of the promotion for this item to compare later
If discount type = dollar
calculate value of the promotion for this item to compare later
If discount type = price
calculate value of the promotion for this item to compare later
If discount amount = Free
do nothing
pass
Else (assumes there is both buy and get)
IF the quantity of the buy item in the cart is >= the buy_quantity (I think c.active_items is the items in the cart)
If discount type = percent
calculate value of the promotion for this item to compare later
If discount type = dollar
calculate value of the promotion for this item to compare later
If discount type = price
calculate value of the promotion for this item to compare later
If discount amount = Free
#Use a different var here like in select_cart_promotion - they will always get this promotion
calculate the value of the promotion for these items
do something here to ensure the get product is in the cart
Take a look at cursors.
Postgres user defined functions can be written in many languages
On the formats of input and output parameters for PL/pgSQL you can check the documentation here
Are you sure that you need to pass this to a function? I believe you could structure your functions to avoid this, functions can return tables and get get tables inside of them. If this table of yours is a query/table/view then you can use SQL inside the function to get to it (passing only parameters of other data type); if this table is the result of another function you can call the function to get to the table. What's your scenario?
精彩评论