我在 Postgres 工作,我需要将包含许多行和列的完整结果集发送到存储过程或函数中。这可能吗?如果是这样,我在哪里可以看到语法资源?
好的,这就是我在无法发送结果集的情况下设置它的方式,它迫使我打破比较逻辑并将其放在两个不同的位置,但是我的目标是将实际查找促销逻辑保持在一个地方,我在这里做了。这可能有一天会改变,比较逻辑不太可能改变,它很标准。
促销行项目逻辑
- 将在 INSERT 上为 promo_objects、promo_buy_objects 和 promo_get_objects 表设置触发器,在 promo 表上将有一个 UPDATE 触发器。- 外部参照的触发器将调用名为 set_best_product_promos 的存储过程,该过程将决定哪个促销最适合该对象,然后将保存到新表中:
promo_best_product_promos
promo_id、object_id、expiration_date
- 促销的触发器将调用 update_best_product_promos 并将发送 promo_id,如果 active = true 它将更新该促销的到期日期,否则它将删除该促销的所有条目
新表已添加到 promo.sql 脚本中,但是在编写函数之前无法添加触发器和函数。
脚本将在每晚午夜运行以删除已过期的条目。PSEUDO FOR 购物车代码(应用程序代码)就像我们现在展示的那样运行联合查询_object_promotions(这将获取该项目的所有可用促销)
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
编辑:所以我想我也可以将这个购物车逻辑转储为存储过程,然后为比较逻辑制作一个,并将其全部存储在存储过程中并且可移植和通用?
用于 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
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