防止添加主键已存在于 mnesia 中的记录的最佳方法是什么?
假设我有一个简单的记录定义:
-record(data, {primary_key = '_', more_stuff = '_'}).
我想要一个简单的函数,将这些记录之一添加到 mnesia 数据库中。 但如果已经有一个具有相同主项的条目,我希望它失败 钥匙。
(在下面的示例中,假设我已经定义了
db_get_data(Key)->
Q = qlc:q([Datum
|| Datum = #data{primary_key = RecordKey}
<- mnesia:table(data),
RecordKey =:= Key]),
qlc:e(Q).
)
以下内容有效,但让我觉得有点丑陋...
add_data(D) when is_record(D, data)->
{atomic, Result} = mnesia:transaction(fun()->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> {error, bzzt_duplicate_primary_key}
end
end),
case Result of
{error, _} = Error -> throw(Error);
_ -> result
end.
这也有效,但也很丑陋:
add_data(D) when is_record(D, data)->
{atomic, Result} = mnesia:transaction(fun()->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> throw({error, bzzt_duplicate_primary_key})
end
end).
它与上面的不同之处在于上面的抛出
{error, bzzt_duplicate_primary_key},
而这个抛出
{error, {badmatch, {aborted, {throw,{error, bzzt_duplicate_primary_key}}}}}
那么:是否有一些约定来指示此类错误? 或者有没有一种内置的方法可以让 mnesia 为我抛出这个错误?
Suppose I've got a simple record definition:
-record(data, {primary_key = '_', more_stuff = '_'}).
I want a simple function that adds one of these records to a mnesia database. But I want it to fail if there's already an entry with the same primary
key.
(In the following examples, assume I've already defined
db_get_data(Key)->
Q = qlc:q([Datum
|| Datum = #data{primary_key = RecordKey}
<- mnesia:table(data),
RecordKey =:= Key]),
qlc:e(Q).
)
The following works, but strikes me as sort of ugly ...
add_data(D) when is_record(D, data)->
{atomic, Result} = mnesia:transaction(fun()->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> {error, bzzt_duplicate_primary_key}
end
end),
case Result of
{error, _} = Error -> throw(Error);
_ -> result
end.
This works too, but is also ugly:
add_data(D) when is_record(D, data)->
{atomic, Result} = mnesia:transaction(fun()->
case db_get_data(D#data.primary_key) of
[] -> db_add_data(D);
_ -> throw({error, bzzt_duplicate_primary_key})
end
end).
It differs from the above in that the above throws
{error, bzzt_duplicate_primary_key},
whereas this one throws
{error, {badmatch, {aborted, {throw,{error, bzzt_duplicate_primary_key}}}}}
So: is there some convention for indicating this sort of error? Or is there a built-in way that I can get mnesia to throw this error for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为它们都很好,如果你只是让你的代码更漂亮,比如:
或者
你会抛出错误还是返回错误? 我自己会返回一个错误。 我们将代码分成 mnesia 工作单元 - 一个具有一组函数的模块,这些函数执行不在事务中的基本 mnesia 活动,以及一个 api 模块,它将工作单元“组合”成 mnesia 事务,其功能与上面的非常相似。
I think both of them are fine, if you only make your code more pretty, like:
or
Do you throw errors or return errors? I would return an error myself. We split out code out into mnesia work units - a module with a set of functions that perform basic mnesia activities not in transactions, and an api module which 'composes' the work units into mnesia transactions with functions that look very similar to the one above.