Tutorial: Mnesia

5 Querying

We've going to be looking at how to query Mnesia tables in this section. Utility functions have been defined for your convenience in the LFE Mnesia tutorial repo, so let's bring those in now:

> (slurp '"src/querying.lfe")
#(ok querying)
>

5.1 Listing All the Data

We've already seen how to list all the data in a Mnesia database. From the previous section:

(mnesia:transaction
  (lambda ()
    (mnesia:foldl (lambda (x _) (lfe_io:format '"~p~n" (list x)))
                  0
                  'department)))

As mentioned, this has been converted to a generally useful function of this form:

(defun show-data (table-name)
  (mnesia:transaction
    (lambda ()
      (mnesia:foldl
        (lambda (x _) (lfe_io:format '"~p~n" (list x)))
        0
        table-name))))

It now lives in the querying module of the Mnesia tutorial. We've slurped that module, so we can just call it outright:

> (show-data 'employee)
#(employee 104659 "Tornkvist Torbjorn" 2 M 99514 #(222 22))
#(employee 117716 "Fedoriw Anna" 1 F 99143 #(221 31))
#(employee 115018 "Mattsson Hakan" 3 M 99251 #(203 348))
#(employee 104531 "Nilsson Hans" 3 M 99495 #(222 26))
#(employee 114872 "Dacker Bjarne" 3 M 99415 #(221 35))
#(employee 104732 "Wikstrom Claes" 2 M 99586 #(221 15))
#(employee 107912 "Carlsson Tuula" 2 F 94556 #(242 56))
#(employee 104465 "Johnson Torbjorn" 1 M 99184 #(242 38))
#(atomic ok)
>

5.2 Updates with Locks, Reads, and Writes

5.3 Select

5.4 QLC