Skip to content

搞英语 → 看世界

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

Menu
  • 首页
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
  • Product Hunt
  • Visual Capitalist
  • Elon Musk
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

发表回复 取消回复

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

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Bob Nystrom (1)
  • Dan Wang (1)
  • Joel on Software (1)
  • John Resig (1)
  • Laurence Gellert's Blog (1)
  • Matt Might's blog (3)
  • News Letter (191)
  • Philip Walton (1)
  • Pivotal (1)
  • Sam Julien (1)
  • Scott Hanselman's Blog (2)
  • Tom's blog (1)
  • Wait But Why (1)
  • 英文媒体 (36,595)
    • Ars Technica (2,472)
    • Daily Infographic (285)
    • Engadget (5,712)
    • Enonomist (77)
    • FlowingData (254)
    • Hacker News (773)
    • Hacker News Daily (314)
    • Hacker Noon (125)
    • Harvard Health (150)
    • KK – Cool Tools (213)
    • KK – Recomendo (263)
    • Make Use Of (158)
    • NASA Astronomy Picture (267)
    • Product Hunt (7,487)
    • Psyche (220)
    • Quanta Magazine (187)
    • Science current issue (536)
    • Sidebar (1,029)
    • Singularity HUB (278)
    • TechCrunch (9,039)
    • The Practical Developer (99)
    • The Verge (6,276)
    • Visual Capitalist (381)
  • 英文推特 (17,682)
    • Bill Gates (342)
    • Brett Winton (1,333)
    • Cathie Wood (303)
    • Durov's Channel (25)
    • Elon Musk (5,422)
    • GeekWire (2,868)
    • Hunter Walk (55)
    • Mark Gurman (1,061)
    • Naval (699)
    • Parag Agrawal (52)
    • Ray Dalio (1,045)
    • Riccardo Mori (16)
    • Steph Smith (2,269)
    • Tim Cook (169)
    • Vitalik Buterin (2,023)
  • 英文独立博客 (3,800)
    • A learning a day (327)
    • A Smart Bear (2)
    • AddyOsmani.com (10)
    • Adwyat Krishna (29)
    • Ahmad Shadeed (2)
    • Alex Turek (3)
    • All Poetry (1)
    • All That is Solid (49)
    • André Staltz (2)
    • arxivblog (37)
    • Astral Codex Ten (15)
    • Atoms vs Bits (26)
    • AVC (40)
    • Basic Apple Guy (41)
    • Ben Thompson (13)
    • Benedict Evans (8)
    • Blog – storytelling with data (43)
    • Built For Mars (11)
    • Caleb Porzio (1)
    • Cameron Sun (2)
    • Christian Heilmann (39)
    • Christopher C (3)
    • Chun Tian (binghe) (1)
    • Codrops (16)
    • Cold Takes (16)
    • Dan Luu (1)
    • Daniel Lemire's blog (51)
    • David Amos (23)
    • David Perell (6)
    • David Walsh Blog (36)
    • Derek Sivers (28)
    • Desvl (14)
    • Devon's Site (5)
    • Digital Inspiration (26)
    • DKB Blog (4)
    • Douglas Vaghetti (12)
    • dropsafe (56)
    • DSHR (37)
    • Dunk (5)
    • DYNOMIGHT (38)
    • eagereyes (7)
    • Endless Metrics (135)
    • Entitled Opinions (8)
    • Exception Not Found (6)
    • Experimental History (21)
    • Farnam Street (6)
    • Fed Guy (10)
    • Felix Krause (3)
    • Florent Crivello (2)
    • Free Mind (7)
    • Full Stack Economics (40)
    • Funny JS (3)
    • Future A16Z (47)
    • Glassnode Insights (55)
    • Human Who Codes (4)
    • Infographics – Cool Infographics (10)
    • Information is Beautiful (11)
    • Irrational Exuberance (40)
    • Jacob Kaplan-Moss (13)
    • Jakob Greenfeld (44)
    • James Sinclair (3)
    • Jason Fried (17)
    • Jeff Kaufman (178)
    • John's internet house (31)
    • Johnny Rodgers (4)
    • Julia Evans (25)
    • Julian.com (2)
    • Kalzumeus (1)
    • Kevin Cox (10)
    • Kevin Norman (3)
    • KK – The Technium (50)
    • Krishna (7)
    • Lee Robinson (5)
    • Lines and Colors (51)
    • Lyn Alden – Investment Strategy (3)
    • Martin Fowler (28)
    • More To That (13)
    • Morgan Housel (76)
    • My Super Secret Diary (30)
    • Naval Blog (3)
    • Neckar's New Money (76)
    • Nick Whitaker (4)
    • Nicky's New Shtuff (1)
    • nutcroft (10)
    • Paul Graham (2)
    • Paul Graham: Essays (2)
    • Penguin Random House (66)
    • Phoenix's island (1)
    • Prof Galloway (35)
    • Python Weekly (30)
    • Rachel (33)
    • Real Life (34)
    • Sasha (63)
    • Science & technology (122)
    • Sébastien Dubois (6)
    • Secretum Secretorum (13)
    • Seth's Blog (146)
    • Shu Ding (3)
    • SignalFire (9)
    • Simon Willison's Weblog (167)
    • Simons Foundation (85)
    • SLIME MOLD TIME MOLD (23)
    • Slyar Home (8)
    • Spencer Greenberg (10)
    • Stay SaaSy (11)
    • Stephen Malina (4)
    • Stephen Wolfram Writings (2)
    • Strange Loop Canon (23)
    • Stratechery (10)
    • Tech Notes (11)
    • The Commonplace (29)
    • The Generalist (2)
    • The Intrinsic Perspective (30)
    • The Latest in Hearing Health | HeardThat (8)
    • The Mad Ned Memo (2)
    • The Rabbit Hole (37)
    • TLDR Newsletter (81)
    • Tomasz Tunguz (91)
    • Tony Kulesa (2)
    • Troy Hunt (58)
    • Tychlog (1)
    • Uncharted Territories (57)
    • Visualising Data (9)
    • Weichen Liu (20)
    • What's New (55)
    • Works in Progress (1)
    • Workspaces (32)
    • Writing (8)
    • Xe's Blog (35)
    • xkcd.com (117)
    • Yihui Xie (13)
    • Zoran Jambor (11)
©2023 搞英语 → 看世界 | Design: Newspaperly WordPress Theme