为 mnesia 查询编写查询列表推导式

发布于 2024-08-24 20:43:44 字数 1893 浏览 10 评论 0原文

我正在尝试使用 erlang+mnesia 构建一个小型测试应用程序。

我有一个从 #user 记录构建的用户表,如下定义:

-record(user_details, {name, password}).
-record(user, {id, details}).

然后我插入具有该功能的用户:

add_sample_data() ->
    Mat = #user{
      details = #user_details{
    name = "mat", password = "mat"
       }
     }, 
    user:insert_user(Mat),

查询 [U#user.details || U <- mnesia:table(user)] 返回一个非空列表。 现在,我正在尝试构建一个查询,如果没有与details.name匹配的记录,则该查询将返回一个包含零记录的列表。如果有,则返回匹配的记录。

这是我使用的方法(这个有效):

user_exists() ->
  Fun = fun() ->
    Query = qlc:q([
      U#user.details || 
      U <- mnesia:table(user)
    ]),
    qlc:e(Query)
  end,
  case mnesia:transaction(Fun) of
    {atomic, []} -> false;
    {atomic, [_User]} -> true
  end.

我从 本教程。使用 mne_fun_query({sport, Sport}) 方法(幻灯片 19)中的 mnesia:select 解决了类似的问题,但现在我想使用 qlc 来解决。

我尝试了各种组合,但没有成功(通常在编译时失败......)。

我对 erlang 真的很陌生,如果你能知道哪个查询应该有效并解释一下,那将不胜感激!

垫。

编辑

这是一个不起作用的版本,但也许可以更好地解释我的问题

user_exists() ->
  Fun = fun() ->
    Query = qlc:q([
      U#user.details || 
      U <- mnesia:table(user), 
      U#user.details.name == "mat"     <<<<< This is the line with the problem
    ]),
    qlc:e(Query)
  end,
  case mnesia:transaction(Fun) of
    {atomic, []} -> false;
    {atomic, [_User]} -> true
  end.

和我遇到的错误:

mathieu@evangeneer:~/projects/nopair$ make
重新编译:src/resources/user_resource
src/resources/user_resource.erl:22: 之前的语法错误:'.'
src/resources/user_resource.erl:6: 函数 user_exists/2 未定义 make:
*** [erl] 错误 1

I'm trying to build a small testing app with erlang+mnesia.

I have a user table build from the #user record, as defined here:

-record(user_details, {name, password}).
-record(user, {id, details}).

then I insert a user with that function:

add_sample_data() ->
    Mat = #user{
      details = #user_details{
    name = "mat", password = "mat"
       }
     }, 
    user:insert_user(Mat),

the query [U#user.details || U <- mnesia:table(user)] return a non empty list.
now I'm trying to build a query which would return a list containing zero record if there is no record with details.name matching Name or the matching record if there is one.

here is the method I use (this one works):

user_exists() ->
  Fun = fun() ->
    Query = qlc:q([
      U#user.details || 
      U <- mnesia:table(user)
    ]),
    qlc:e(Query)
  end,
  case mnesia:transaction(Fun) of
    {atomic, []} -> false;
    {atomic, [_User]} -> true
  end.

I copied some stuff from this tutorial. A similar problem is solved with mnesia:select in the mne_fun_query({sport, Sport}) method (slide 19) but now I'd like to do it with qlc.

I tried various combinations but without any success (often failed at compilation time..).

I'm really new to erlang, if you can tell which query should works and explains it a little, that would be greatly appreciated!

mat.

edit

here is one version which does not work but maybe explain my problem better

user_exists() ->
  Fun = fun() ->
    Query = qlc:q([
      U#user.details || 
      U <- mnesia:table(user), 
      U#user.details.name == "mat"     <<<<< This is the line with the problem
    ]),
    qlc:e(Query)
  end,
  case mnesia:transaction(Fun) of
    {atomic, []} -> false;
    {atomic, [_User]} -> true
  end.

and the error I have:

mathieu@evangeneer:~/projects/nopair$ make
Recompile: src/resources/user_resource
src/resources/user_resource.erl:22: syntax error before: '.'
src/resources/user_resource.erl:6: function user_exists/2 undefined make:
*** [erl] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糯酥胸 2024-08-31 20:43:44

在问题行上:

U#user.details.name == "mat"

您正在尝试访问 user_details 记录,但未命名它。尝试...

(U#user.details)#user_details.name == "mat"

根据我的经验,编译器无法自行确定#user.details 是#user_details。

On the problem line:

U#user.details.name == "mat"

You are attempting to access the user_details record but not naming it. Try...

(U#user.details)#user_details.name == "mat"

From my experience, the compiler doesn't figure out on its own that #user.details is a #user_details.

寄离 2024-08-31 20:43:44

我认为 QLC 代码无法编译的最常见原因是,如果您遗漏了需要包含在包含 qlc 查询的模块中的头文件。尝试将以下内容添加到您的模块中,看看是否可以解决问题:

-include_lib("stdlib/include/qlc.hrl").

I think the most common reason QLC code wouldn't compile is if you've left out the header file you're required to include in modules containing qlc queries. Try adding the following to your module and see if it fixes the problem:

-include_lib("stdlib/include/qlc.hrl").
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文