Erlang/OTP Forums

Author Message

<  Erlang  ~  Update "query" for erlang?

laserlars
Posted: Tue Nov 27, 2007 2:19 pm Reply with quote
Joined: 10 Sep 2007 Posts: 5
Hello

Someone who know what a update query for erlang would look like?

Or do I need to make a write on a existing key to update it? Apparently if i do, i need to give it all its fields again or they would be set to unassigned?
View user's profile Send private message
francesco
Posted: Fri Nov 30, 2007 6:37 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
I assume you are talking about overwriting entries in destructive tables such as mnesia, ets and dets? If so, then yes, you need to overwrite the existing record / tuple. It will work with the exception of tables of type bags and duplicate bags, where you first need to delete the element.

I hope the above helps. Let me know if you need an example.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
laserlars
Posted: Fri Nov 30, 2007 7:56 pm Reply with quote
Joined: 10 Sep 2007 Posts: 5
Well, I got a solution running, but I must say that it is quite ugly, now i have to retreive all the fields from the row and then make a write on the same key with all the fields again.

Code:

changeArea(UserId, Area) ->   
   [{GroupId,Name, Haircut, Shirt, Pants, Position, Level, Node, Socket, Vote}] = database:do(qlc:q([{X#active_connections.groupid, X#active_connections.name, X#active_connections.haircut, X#active_connections.shirt, X#active_connections.pants, X#active_connections.position, X#active_connections.level, X#active_connections.node, X#active_connections.socket, X#active_connections.vote} || X <- mnesia:table(active_connections),
            X#active_connections.userid =:= UserId])),
   
   Row = #active_connections{userid = UserId, groupid=GroupId, name=Name, haircut=Haircut, shirt=Shirt, pants=Pants, position=Position, node = Node, socket = Socket, level = Level,area = Area, vote=Vote},
   F = fun() ->
      mnesia:write(Row)
   end,
   mnesia:transaction(F).


I don't think you will get any girls with such ugly code Smile If you have a better suggestion please shoot!
View user's profile Send private message
seancharles
Posted: Fri Nov 30, 2007 9:40 pm Reply with quote
User Joined: 18 Jul 2007 Posts: 57
I am no mnesia expert but I think that this:
Code:

changeArea(UserId, Area) ->   
   [{GroupId,Name, Haircut, Shirt, Pants, Position, Level, Node, Socket, Vote}] = database:do(qlc:q([{X#active_connections.groupid, X#active_connections.name, X#active_connections.haircut, X#active_connections.shirt, X#active_connections.pants, X#active_connections.position, X#active_connections.level, X#active_connections.node, X#active_connections.socket, X#active_connections.vote} || X <- mnesia:table(active_connections),
            X#active_connections.userid =:= UserId])),
   
   Row = #active_connections{userid = UserId, groupid=GroupId, name=Name, haircut=Haircut, shirt=Shirt, pants=Pants, position=Position, node = Node, socket = Socket, level = Level,area = Area, vote=Vote},
   F = fun() ->
      mnesia:write(Row)
   end,
   mnesia:transaction(F).

could be replaced with this, these are the ways that I use Erlang / mnesia and it works for me.
Code:

changeArea(UserId, Area) ->   
   %% Assumes 1 match returned from the query
   Match = hd( database:do(
      qlc:q([ X || X <- mnesia:table(active_connections),
                   X#active_connections.userid =:= UserId]))),

   %% Modify just the area of the matching record
   Row = Match#active_connections{area = Area},

   %% Update the table with the modified record
   mnesia:transaction( fun() -> mnesia:write(Row) end ).

That's off the top of my now Erlang-battered head, without typing it in anywhere other than here but I am reasonably confident it's correct!

Hope that helped !?!?
Smile
->Sean.
View user's profile Send private message
laserlars
Posted: Tue Dec 04, 2007 4:01 pm Reply with quote
Joined: 10 Sep 2007 Posts: 5
Worked like a charm!

Thanks alot
View user's profile Send private message
seancharles
Posted: Tue Dec 04, 2007 4:54 pm Reply with quote
User Joined: 18 Jul 2007 Posts: 57
I am as pleased as you are!
Smile
->Sean
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum