views:

117

answers:

2

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 trigger 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
+1  A: 

Take a look at cursors.

Milen A. Radev
So I have to use FETCH because the query that I am doing w/ the cursor needs to be called from more than one place. It say FETCH will get the next result in the documentation, so would youselect from cursor functionloop fetch cursor into variableend loop?
KacieHouser
+1  A: 

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?

Unreason
I need to find the best promotion on a product. So a product may have many promotions tied to it. I need to do this from two different repositories, therefore, I decided to make SQl functions to handle it. In one repo, I just need to know what the best promotion is for the product. When I am in the cart I need to know what the best promotion is for a line item, however some of the promotions returned may not apply to that item in the cart (buy 4 get them 20% off if only 1 is in the cart, then I can't apply that promotion)
KacieHouser
So it is necessary to filter the promotions in the cart context before I figure out which is the best of those promotions. Therefore I can't just query all of the promotions inside that function and loop all in the same function. I have to know whats in the cart.
KacieHouser
So I need to query the promotions for that item in one function, in the cart case I will do some logic in my code and send those edited results to another function to determine which is the best promotion.
KacieHouser
Ok, but why you could not put all the logic in one function? You can pass a product (as row or as id) to a function, open a cursor on the cart, open another cursor on the promotions, see which match and find the best one. (side note, if you think it might be doable in pure sql and have time to describe your structures you might get a good review on os of possible solutions)
Unreason
Well in one instance this will be called, it doesn't matter what in the cart, in fact it needs to know truly what the best promotion is. It is for the product listing on the front of the site and all product pages. I can add what I have...
KacieHouser
OK I posted what I have
KacieHouser
Can you note what would be parameters to the functions? And since you went all the way into full details, can you step back and make it a bit cleaner from the big picture - what do tables store and how does the process go (the relationships are not so clear). However comment on your bold edit - if I get you right that was my point all along - that you don't need to pass the whole tables around anyway, just passing some ids. alternatively you can pass cursors - see http://developer.postgresql.org/pgdocs/postgres/plpgsql-cursors.html
Unreason
Yeah I think I know what I need to do now, thank you
KacieHouser