Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: Erlang  |  size: 1.31 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -module(db_list).
  2.  
  3. -include("../include/backend.hrl").
  4.  
  5. -export([empty/0, insert/2, db_to_list/1, db_size/1]).
  6. -export([lookup/2, lookup_all/3]).
  7. -export([update/2, close/1]).
  8.  
  9. empty() ->
  10.     [].
  11.    
  12. insert(Account, []) ->
  13.     [Account];
  14. insert(Account, DBRef) ->
  15.     Lookup = lookup(Account#account.no, DBRef),
  16.     case Lookup of
  17.         {error, _Msg} -> DBRef ++ [Account];
  18.         _Else -> {error, exists}
  19.     end.
  20.    
  21. db_to_list(DBRef) ->
  22.     DBRef.
  23.    
  24. db_size(DBRef) ->
  25.     length(DBRef).
  26.    
  27. lookup(AccountNumber, DBRef) ->
  28.     F = fun(X) ->
  29.         case X#account.no of
  30.             AccountNumber -> true;
  31.             _Else -> false
  32.         end
  33.     end,
  34.     DBRef2 = lists:filter(F, DBRef),
  35.     case DBRef2 of
  36.         [] -> {error, instance};
  37.         [Account] -> Account
  38.     end.
  39.    
  40. lookup_all(AccountField, Key, DBRef) ->
  41.     F = fun(X) ->
  42.         Value = element(AccountField, X),
  43.         case Value of
  44.             Key -> true;
  45.             _Else -> false
  46.         end
  47.     end,
  48.     lists:filter(F, DBRef).
  49.    
  50. update(Account, DBRef) ->
  51.     F = fun(X) ->
  52.         AccountNumber = Account#account.no,
  53.         case X#account.no of
  54.             AccountNumber -> Account;
  55.             _Else -> X
  56.         end
  57.     end,
  58.     lists:map(F, DBRef).
  59.    
  60. close(_DBRef) ->
  61.     ok.