sqlite - SQLite/Room 中的“重复”功能
问题描述
我目前正在做我的一个副项目,我已经决定实施一个“记录/输入重复”系统。
所以基本上,在特定时间间隔内再次创建一行(但具有其他唯一 ID,因为每个费用都应该是唯一可编辑的)。例如:每月费用(例如租金)将是重复记录,如下所示:
费用
id, amount, date, repId, status
1, 800, 01.03.2021, 1, finished
2, 800, 01.04.2021, 1, finished
3, 800, 01.05.2021, 1, notpaidyet,
4, 800, 01.06.2021, 1, notpaidyet
5, 800, 01.07.2021, 1, notpaidyet (=currentExpense/nextExpense, is always in the future!)
重复
id, repeatmenttype, numOfCreatedRepeatments, maxRepeatments,currentExpenseId
1, MONTHLY, 5, unlimited, 5
我的想法是为此使用触发器,因为有很多情况我需要处理很多情况:
例如:
1)如果用户说他已经支付了让我们说 7 月 ( currentExpense
) =UPDATE,我想自动创建 8 月的费用,只要numOfCreatedRepeatments
< maxRepeatments
。
- 创建
numOfCreatedRepeatments
下一个时 自动更新。numOfCreatedRepeatments/currentExpense
3)如果currentExpense
日期更改为 6 月 2 日(出于某种原因)并且repeatmenttype
更改为 WEEKLY,则应创建新的费用记录直到当前日期 + 未来的额外费用(>currentDate),这将再次用作 currentExpense 所以该表将如下所示:
4, 800, 01.06.2021, 1, notpaidyet
5, 800, 02.06.2021, 1, notpaidyet
6, 800, 09.06.2021, 1, notpaidyet
7, 800, 16.06.2021, 1, notpaidyet
8, 800, 23.06.2021, 1, notpaidyet (=currentExpense, as 22.06 > 23.06!)
4)...
当 nextExpense 日期 = 当前日期时创建nextExpense
nect 记录,将使用 解决AlarmManager
,因此这将在代码中解决,因为 SQLite 不支持时间戳触发器。
我的问题是,最聪明的实现会是什么样子?
我的首选是使用触发器,但是我在某处读到触发器很慢,并且当 Expense 在其他表中用作外键时,我需要在外键表中创建额外的行。这会使触发器不可读并且不是很快,对吗?
另一方面,在代码中实现功能也会使代码有点不可读,并导致调用许多插入、更新等
解决方案
推荐阅读
- javascript - 未调用 XMLHttpRequest onload
- r - R 有任何更优雅的解决方案可以将列添加到多个数据框中
- python - 让子类从实例化的主类继承非默认的 __init__ 值
- python - Python/Caffe2:ImportError:没有名为 tools.setup_helpers.env 的模块
- system-verilog - 在 SystemVerilog 中调整动态数组的大小
- javascript - preventDefault() 和 stopImmediatePropagation 不起作用
- java - Eclipse Java 窗口在 Eclipse 中测试时打开,但在导出时不打开
- javascript - 基于相对于矩形的两个点创建 CSS 线性渐变
- firebase - 无需身份验证即可写入/更新一个特定字段的 Firestore 规则
- javascript - 如何使用 React.js 16 在服务器上呈现“禁用”的 HTML 属性?