Created
May 9, 2014 15:24
-
-
Save scrogson/3683c5a4e2813536323f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defrecord User, name: "", email: "" | |
defmodule Amnesia do | |
@compile {:parse_transform, :qlc} | |
def create_schema do | |
create_table User | |
end | |
def find(record, id) do | |
:mnesia.read(record, id) | |
end | |
def write(object) do | |
:mnesia.write(object) | |
end | |
def delete(_user=User[]), do: nil | |
def delete(object) do | |
:mnesia.delete_object(object) | |
end | |
def create_table(record) do | |
:mnesia.create_table(record, [{:attributes, Dict.keys(record.__record__(:fields))}]) | |
end | |
end | |
:mnesia.start | |
Amnesia.create_schema | |
{:atomic, :ok} = :mnesia.transaction fn -> | |
Amnesia.write(User.new(name: "John Doe", email: "[email protected]")) | |
end | |
{:atomic, user} = :mnesia.transaction fn -> | |
Amnesia.find(User, "[email protected]") | |
end | |
IO.inspect user | |
{:atomic, users} = :mnesia.transaction fn -> | |
query = :qlc.q(lc u inlist :mnesia.table(User), u.name == "John Doe", do: u) | |
:qlc.e(query) | |
end | |
IO.inspect users | |
# $ elixir mnesia.exs | |
# mnesia.exs:45: warning: qlc:q/1 called, but "qlc.hrl" not included | |
# [] | |
# ** (MatchError) no match of right hand side value: {:aborted, | |
# {:function_clause, [{:elixir_compiler_0, :"-__FILE__/1-lc$^0/1-0-", | |
# [qlc_handle: {:qlc_table, #Function<20.61328436/1 in :mnesia.table/2>, true, | |
# #Function<21.61328436/1 in :mnesia.table/2>, #Function<22.61328436/0 in | |
# :mnesia.table/2>, #Function<23.61328436/1 in :mnesia.table/2>, | |
# #Function<26.61328436/1 in :mnesia.table/2>, #Function<25.61328436/2 in | |
# :mnesia.table/2>, #Function<24.61328436/0 in :mnesia.table/2>, :"=:=", | |
# :undefined, :no_match_spec}], [file: 'mnesia.exs', line: 45]}, | |
# {:elixir_compiler_0, :"-__FILE__/1-fun-2-", 0, [file: 'mnesia.exs', line: | |
# 45]}, {:mnesia_tm, :apply_fun, 3, [file: 'mnesia_tm.erl', line: 833]}, | |
# {:mnesia_tm, :execute_transaction, 5, [file: 'mnesia_tm.erl', line: 813]}, | |
# {:elixir_compiler_0, :__FILE__, 1, [file: 'mnesia.exs', line: 44]}, | |
# {:elixir_compiler, :dispatch_loaded, 6, [file: 'src/elixir_compiler.erl', | |
# line: 84]}, {:elixir_lexical, :run, 2, [file: 'src/elixir_lexical.erl', | |
# line: 17]}, {:elixir_compiler, :quoted, 2, [file: | |
# 'src/elixir_compiler.erl', line: 26]}]}} | |
# mnesia.exs:44: (file) | |
# (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2 | |
# (elixir) lib/code.ex:303: Code.require_file/2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment