Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

用于包装逻辑和 SQL 查询的数据库函数

Posted on 2022-03-09

当你制作一个数据库支持的应用程序时,你有一些需要运行多个数据库查询的功能。

例如,要将资金从一个帐户转移到另一个帐户,您必须在帐户#1 中插入一个负数,在帐户#2 中插入一个正数。

通常你在你的主要代码中做:你的 JavaScript、Python、Ruby 或其他任何东西。

但是,如果未来的某些代码绕过了您关键的“业务逻辑”功能怎么办?新代码可以直接访问数据库,而无需通过现有函数。

或者,如果您需要用新语言重写一些代码怎么办?如果所有这些数据逻辑都保存在周围的代码中,您将有大量数据需要重写。

在我上一家公司,当我们将一些旧的 PHP 代码转换为 Ruby 时,我感受到了这种痛苦。我不得不重写很多逻辑。

事后看来,数据逻辑应该在数据库本身。

更新数据总是需要的简单逻辑(如货币转移示例)应保存在数据库函数中。然后你周围的代码——你的 JavaScript、Python、Ruby 或其他任何东西——可以调用这些数据库函数,如果你将语言更改为 Swift、Kotlin、Elixir 或其他任何语言,则永远不需要重写。

这是一个 PostgreSQL 示例,来自我之前的帖子:

首先,制作三个简单的表格:

  1. 有价格的物品。
  2. 带有数量的订单项。
  3. 带有总价的发票。

创建两个示例项目,一个 5 美元和一个 9 美元的项目。并创建发票#1 进行测试。

 create table items ( id serial primary key, price int not null check (price > 0) ); create table invoices ( id serial primary key, total int ); create table lineitems ( invoice_id int not null references invoices(id), item_id int not null references items(id), quantity int not null check (quantity > 0), primary key (invoice_id, item_id) ); -- example data: insert into items (price) values (5); insert into items (price) values (9); insert into invoices (total) values (0);

下载代码

如果有人想将商品添加到他们的购物车,您需要先查看它是否已经在他们的购物车中。如果它不在他们的购物车中,请将其插入。但是,如果该商品在他们的购物车中,您需要对其进行更新,以将新数量添加到其现有数量中。

因此,将所有这些逻辑包装在一个名为 cart_add 的简单函数中。

 create function cart_add(inv int, item int, quant int) returns void as $$ begin -- does this invoice + item combination already exist? perform 1 from lineitems where invoice_id = inv and item_id = item; if found then -- yes? add this quantity update lineitems set quantity = quantity + quant where invoice_id = inv and item_id = item; else -- no? insert insert into lineitems values (inv, item, quant); end if; end; $$ language plpgsql;

下载代码

有人更新他们的购物车,以更改 Lineitem 的数量。如果他们将数量更改为 2、5 甚至 1,没问题,只需更新数量即可。但是,如果他们将数量更改为 0 怎么办?您不希望数量为 0 的 Lineitem 挂在他们的购物车上。不,如果数量为 0 或以下,您想删除该 Lineitem。

因此,将所有这些逻辑包装在一个名为 cart_set 的简单函数中。

 -- update the quantity of an item in the cart create function cart_set(inv int, item int, quant int) returns void as $$ begin if quant > 0 then update lineitems set quantity = quant where invoice_id = inv and item_id = item; else -- quantity 0 or below? delete delete from lineitems where invoice_id = inv and item_id = item; end if; end; $$ language plpgsql;

下载代码

在那里,现在这个数据逻辑是它所属的地方:数据本身。

您的 JavaScript、Python、Ruby 或任何可以调用函数的东西,如下所示:

 select cart_add(1, 1, 3); select * from lineitems; select cart_add(1, 2, 4); select * from lineitems; select cart_set(1, 2, 1); select * from lineitems;

下载代码

想象一下,如果您对数据库中需要做的所有重要事情都这样做了?

然后任何语言的任何代码都可以调用这些函数,因为知道数据库本身将处理逻辑。将数据逻辑保持在应有的位置:使用数据。

在此处下载最终示例文件: /code/api01.sql 。

在以后的帖子中对此进行更多介绍。或者在那之前,请参阅我在 Github 上的示例。

来源: https://sive.rs/api01

发表回复 取消回复

要发表评论,您必须先登录。

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Lou Plummer
  • Luke Wroblewski
  • Matt Stoller
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme