View Source MnesiaAssistant.Query (Mishka developer tools v0.1.7)

Querying is one of the most significant aspects of working with the database; for this reason, you can access the aggregation functions of Mnesia in this section of the database.

Summary

Functions

Read delete/3 document.

By using this function, you will be able to delete a record from the table based on the key that you desire.

To use :mnesia.delete_object/3 in Elixir, you need to specify the table from which to delete (Tab), the complete record to be deleted (Rec), and the lock type (LockKind). This function is particularly useful when you need to delete a specific record and you know the entire tuple structure of that record.

Dirty equivalent of the function delete/2.

Dirty equivalent of the function delete_object/1.

Dirty equivalent of the function first/1.

Dirty equivalent of the function index_read/3.

Dirty equivalent of the function last/1.

There is a distinction between this method and the select function, which is that you are aware of the record that you wish to remove from the table. It is like: :mnesia.match_object({Person, :_, "Mishka", :_})

Dirty equivalent of the function next/2.

Dirty equivalent of the function prev/2.

The database can be accessed through the use of this function to retrieve a record. It is important to remind you that if you are seeking for a reliable assurance for the retrieval of data, you can utilise transaction.

Mnesia has no special counter records. However, records of the form {Tab, Key, Integer} can be used as (possibly disc-resident) counters when Tab is a set. This function updates a counter with a positive or negative number. However, counters can never become less than zero. There are two significant differences between this function and the action of first reading the record, performing the arithmetic, and then writing the record

Dirty equivalent of the function write/1.

Returns first record of the table concerned.

it is like Enum.reduce.

Works exactly like foldl/3 but iterates the table in the opposite order for the ordered_set table type. For all other table types, foldr/3 and foldl/3 are synonyms.

In a manner similar to the function mnesia:index_read/3, any index information can be used when trying to match records. This function takes a pattern that obeys the same rules as the function mnesia:match_object/3, except that this function requires the following conditions

Assume that there is an index on position Pos for a certain record type. This function can be used to read the records without knowing the actual key for the record. For example, with an index in position 1 of table person, the call mnesia:index_read(person, 36, #person.age) returns a list of all persons with age 36. Pos can also be an attribute name (atom), but if the notation mnesia:index_read(person, 36, age) is used, the field position is searched for in runtime, for each call.

When this function is executed inside a transaction-context, it returns true, otherwise false.

Returns last record of the table concerned.

There is a distinction between this method and the select function, which is that you are aware of the record that you wish to remove from the table. It is like: :mnesia.match_object({Person, :_, "Mishka", :_})

Returns next record of the table concerned from your selected :id as the key.

Returns prev record of the table concerned from your selected :id as the key.

The database can be accessed through the use of this function to retrieve a record. It is important to remind you that if you are seeking for a reliable assurance for the retrieval of data, you can utilise transaction.

It is like MnesiaAssistant.Query.delete(table, key, :sticky_write). For more information read delete/3 or mnesia:delete(Tab, Key, sticky_write).

It is like MnesiaAssistant.Query.delete_object(table, record, :sticky_write) where Tab is element(1, Record). For more information read delete_object/3 or mnesia:delete_object(Tab, Record, sticky_write).

It is like MnesiaAssistant.Query.write(Person, updated_person, :sticky_write) or mnesia:write(Tab, Record, sticky_write). For more information read write/3.

Read select/5 document.

Read select/5 document.

The select/5 function allows you to specify a custom query using any operator or function in the Elixir language (or Erlang for that matter).

Calls the Fun in a context that is not protected by a transaction. The Mnesia function calls performed in the Fun are mapped to the corresponding dirty functions. It is performed in almost the same context as mnesia:async_dirty/1,2. The difference is that the operations are performed synchronously. The caller waits for the updates to be performed on all active replicas before the Fun returns. For details, see mnesia:activity/4 and the User's Guide.

It is like read/3 with :write LockKind.

Read write/3 document.

The following function will help you update a record of a table based on a specific key.

Functions

Read delete/3 document.

Link to this function

delete(table, key, lock_type)

View Source

By using this function, you will be able to delete a record from the table based on the key that you desire.

Note: The LockKind ([:write, :sticky_write]) argument is used to control the locking behavior during the delete operation, which can influence data consistency and concurrency control for the operation.

Example:

  MnesiaAssistant.Query.delete(Person, 1, :write)
  # OR
  MnesiaAssistant.Query.delete(Person, 1)

To use :mnesia.delete_object/3 in Elixir, you need to specify the table from which to delete (Tab), the complete record to be deleted (Rec), and the lock type (LockKind). This function is particularly useful when you need to delete a specific record and you know the entire tuple structure of that record.

Example:

  record_to_delete = {Person, 2, "John Doe", 30}

  MnesiaAssistant.Query.delete_object(record_to_delete)
  # OR
  MnesiaAssistant.Query.delete_object(Person, record_to_delete, :write)
Link to this function

delete_object(table, record, lock_type)

View Source

Read delete_object/1 document.

Read dirty_delete/1 document.

Link to this function

dirty_delete(table, key)

View Source

Dirty equivalent of the function delete/2.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_delete(Person, 20)
  # OR
  MnesiaAssistant.Query.dirty_delete({Person, 20})
Link to this function

dirty_delete_object(record)

View Source

Dirty equivalent of the function delete_object/1.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  Query.dirty_delete_object({Person, :_, "Mishka", :_})
Link to this function

dirty_delete_object(table, record)

View Source

Read dirty_delete_object/1 document.

Dirty equivalent of the function first/1.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_first(Person)
Link to this function

dirty_index_match_object(pattern, index_attr)

View Source

Dirty equivalent of the function index_match_object/2.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_index_match_object({Person, :_, "Mishka", :_}, 30)
  MnesiaAssistant.Query.dirty_index_match_object(Person, {Person, :_, "Mishka", :_}, 30)
Link to this function

dirty_index_match_object(table, pattern, index_attr)

View Source

Read dirty_index_match_object/2 document.

Link to this function

dirty_index_read(table, key, attr)

View Source

Dirty equivalent of the function index_read/3.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_index_read(Person, 20, :age)

Attr = index_attr()

Dirty equivalent of the function last/1.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_last(Person)
Link to this function

dirty_match_object(pattern)

View Source

Read dirty_match_object/2 document.

Link to this function

dirty_match_object(table, pattern)

View Source

There is a distinction between this method and the select function, which is that you are aware of the record that you wish to remove from the table. It is like: :mnesia.match_object({Person, :_, "Mishka", :_})

### Example:

    alias MnesiaAssistant.Query

    Query.dirty_match_object(Person, [:_, "Mishka", :_])
    # OR
    Query.dirty_match_object(Person, {Person, :_, "Mishka", :_})
    # OR
    Query.dirty_match_object({Person, :_, "Mishka", :_})

> Note: In the context of mnesia, the dirty keyword indicates that it > encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Dirty equivalent of the function next/2.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_next(Person, 20)

Dirty equivalent of the function prev/2.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_prev(Person, 20)

The database can be accessed through the use of this function to retrieve a record. It is important to remind you that if you are seeking for a reliable assurance for the retrieval of data, you can utilise transaction.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  alias MnesiaAssistant.Query

  Query.dirty_read(Person, 20)
Link to this function

dirty_select(table, spec)

View Source

Read dirty_select/4 document.

Link to this function

dirty_select(table, match_fields, conds, result_type \\ [:"$$"])

View Source

Dirty equivalent of the function select/5.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_select(Person, [{{Person, :"$1", :"$2", :"$3"}, [{:>, :"$1", 3}], [:"$$"]}])
  # OR
  MnesiaAssistant.Query.dirty_select(
    Person,
    [:"$1", :"$2", :"$3"],
    [{:>, :"$1", 3}],
    [:"$$"]
  )
Link to this function

dirty_update_counter(arg, incr)

View Source

Mnesia has no special counter records. However, records of the form {Tab, Key, Integer} can be used as (possibly disc-resident) counters when Tab is a set. This function updates a counter with a positive or negative number. However, counters can never become less than zero. There are two significant differences between this function and the action of first reading the record, performing the arithmetic, and then writing the record:

  1. It is much more efficient.
  2. mnesia:dirty_update_counter/3 is performed as an atomic operation although it is not protected by a transaction.

If two processes perform mnesia:dirty_update_counter/3 simultaneously, both updates take effect without the risk of losing one of the updates. The new value NewVal of the counter is returned.

If Key does not exist, a new record is created with value Incr if it is larger than 0, otherwise it is set to 0.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.dirty_update_counter(:counters, :visits, 1)
  # OR+
  MnesiaAssistant.Query.dirty_update_counter({:counters, :visits}, 1)
Link to this function

dirty_update_counter(table, key, incr)

View Source

Read dirty_update_counter/2 document.

Read dirty_write/2 document.

Link to this function

dirty_write(table, record)

View Source

Dirty equivalent of the function write/1.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  updated_person = {Person, id, name, new_age}

  MnesiaAssistant.Query.dirty_write(Person, updated_person)
  # OR
  MnesiaAssistant.Query.dirty_write(updated_person)

Returns first record of the table concerned.

Example:

  MnesiaAssistant.Query.first(Person)
Link to this function

foldl(table, initial_acc, foldl_fun)

View Source

it is like Enum.reduce.

Example:

  initial_acc = 0

  fun = fn {:people, _id, _name, age}, acc ->
    acc + age
  end

  MnesiaAssistant.Query.foldl(Person, initial_acc, fun)
  # OR
  MnesiaAssistant.Query.foldl(Person, initial_acc, fun, :write)
  # OR
  MnesiaAssistant.Transaction.transaction(fn ->
    MnesiaAssistant.Query.foldl(Person, initial_acc, fun)
  end)
Link to this function

foldl(table, initial_acc, foldl_fun, lock_type)

View Source

Read foldl/3 document.

Link to this function

foldr(table, initial_acc, foldr_fun)

View Source

Works exactly like foldl/3 but iterates the table in the opposite order for the ordered_set table type. For all other table types, foldr/3 and foldl/3 are synonyms.

Read foldl/3 document.

Link to this function

foldr(table, initial_acc, foldr_fun, lock_type)

View Source

Read foldr/3 document.

Link to this function

index_match_object(pattern, index_attr)

View Source

In a manner similar to the function mnesia:index_read/3, any index information can be used when trying to match records. This function takes a pattern that obeys the same rules as the function mnesia:match_object/3, except that this function requires the following conditions:

  • The table Tab must have an index on position Pos.
  • The element in position Pos in Pattern must be bound. Pos is an integer (#record.Field) or an attribute name.

The two index search functions described here are automatically started when searching tables with qlc list comprehensions and also when using the low-level mnesia:[dirty_]match_object functions.

The semantics of this function is context-sensitive. For details, see mnesia:activity/4. In transaction-context, it acquires a lock of type LockKind on the entire table or on a single record. Currently, the lock type read is supported.

Example:

  MnesiaAssistant.Query.index_match_object({Person, :_, "Mishka", :_}, 30)
  MnesiaAssistant.Query.index_match_object(Person, {Person, :_, "Mishka", :_}, 30, :write)
Link to this function

index_match_object(table, pattern, index_attr, lock_type)

View Source
Link to this function

index_read(table, key, attr)

View Source

Assume that there is an index on position Pos for a certain record type. This function can be used to read the records without knowing the actual key for the record. For example, with an index in position 1 of table person, the call mnesia:index_read(person, 36, #person.age) returns a list of all persons with age 36. Pos can also be an attribute name (atom), but if the notation mnesia:index_read(person, 36, age) is used, the field position is searched for in runtime, for each call.

Example:

  MnesiaAssistant.Query.index_read(Person, 20, :age)

When this function is executed inside a transaction-context, it returns true, otherwise false.

Returns last record of the table concerned.

Example:

  MnesiaAssistant.Query.last(Person)

Read match_object/2 document.

Link to this function

match_object(table, pattern)

View Source

There is a distinction between this method and the select function, which is that you are aware of the record that you wish to remove from the table. It is like: :mnesia.match_object({Person, :_, "Mishka", :_})

Example:

  alias MnesiaAssistant.Query

  Query.match_object(Person, [:_, "Mishka", :_])
  # OR
  Query.match_object(Person, [:_, "Mishka", :_], :write)
  # OR
  Query.match_object(Person, {Person, :_, "Mishka", :_}, :write)
  # OR
  Query.match_object({Person, :_, "Mishka", :_})
Link to this function

match_object(table, pattern, lock_type)

View Source

Read match_object/2 document.

Returns next record of the table concerned from your selected :id as the key.

Example:

  MnesiaAssistant.Query.next(Person, 1)

Returns prev record of the table concerned from your selected :id as the key.

Example:

  MnesiaAssistant.Query.prev(Person, 1)

The database can be accessed through the use of this function to retrieve a record. It is important to remind you that if you are seeking for a reliable assurance for the retrieval of data, you can utilise transaction.

Example:

  MnesiaAssistant.Transaction.transaction(fn ->
    MnesiaAssistant.Query.read({Person, 20})
  end)
  # OR
  MnesiaAssistant.Query.read(Person, 20)
  # OR use one of @table_lock_types [:read, :write, :sticky_write]
  MnesiaAssistant.Query.read(Person, 20, lock_type)

Reads all records from table Tab with key Key. This function has the same semantics regardless of the location of Tab. If the table is of type bag, the function mnesia:read(Tab, Key) can return an arbitrarily long list. If the table is of type set, the list is either of length 1, or [].

Note: The LockKind ([:read, :write, :sticky_write]) argument is used to control the locking behavior during the read operation, which can influence data consistency and concurrency control for the operation.

Link to this function

read(table, key, lock_type)

View Source

It is like MnesiaAssistant.Query.delete(table, key, :sticky_write). For more information read delete/3 or mnesia:delete(Tab, Key, sticky_write).

Example:

  MnesiaAssistant.Query.s_delete(Person, 20)

It is like MnesiaAssistant.Query.delete_object(table, record, :sticky_write) where Tab is element(1, Record). For more information read delete_object/3 or mnesia:delete_object(Tab, Record, sticky_write).

Example:

  MnesiaAssistant.Query.s_delete_object({Person, 2, "John Doe", 30})

It is like MnesiaAssistant.Query.write(Person, updated_person, :sticky_write) or mnesia:write(Tab, Record, sticky_write). For more information read write/3.

Example:

  updated_person = {Person, id, name, new_age}

  MnesiaAssistant.Query.s_write(updated_person)

Read select/5 document.

Read select/5 document.

Link to this function

select(table, spec, lock_type)

View Source

Read select/5 document.

Link to this function

select(table, spec, limit, lock_type)

View Source

Read select/5 document.

Link to this function

select(table, match_fields, conds, opts, atom)

View Source

The select/5 function allows you to specify a custom query using any operator or function in the Elixir language (or Erlang for that matter).

Example:

  alias MnesiaAssistant.Query

  Query.select(Person, [{{Person, :"$1", :"$2", :"$3"}, [{:>, :"$1", 3}], [:"$$"]}])
  # OR you can use this helper `select/5`
  Query.select(
    Person,
    [:"$1", :"$2", :"$3"],
    [{:>, :"$1", 3}],
    :write,
    [result_type: [:"$$"], lock_type: :write]
  )
Link to this function

sync_dirty(sync_dirty_fn)

View Source

Calls the Fun in a context that is not protected by a transaction. The Mnesia function calls performed in the Fun are mapped to the corresponding dirty functions. It is performed in almost the same context as mnesia:async_dirty/1,2. The difference is that the operations are performed synchronously. The caller waits for the updates to be performed on all active replicas before the Fun returns. For details, see mnesia:activity/4 and the User's Guide.

Note: In the context of mnesia, the dirty keyword indicates that it encompasses the maximum speed, but it does not guarantee that it will execute correctly.

Example:

  MnesiaAssistant.Query.sync_dirty(fn -> something end)
  # OR
  MnesiaAssistant.Query.sync_dirty(fn -> something end, args)
Link to this function

sync_dirty(sync_dirty_fn, args)

View Source

Read sync_dirty/1 document.

It is like read/3 with :write LockKind.

Example:

  MnesiaAssistant.Query.wread(Person, 20)

Read write/3 document.

Link to this function

write(table, record, lock_type \\ :write)

View Source

The following function will help you update a record of a table based on a specific key.

Note: The LockKind ([:write, :sticky_write]) argument is used to control the locking behavior during the write operation, which can influence data consistency and concurrency control for the operation.

Example:

  updated_person = {Person, id, name, new_age}

  MnesiaAssistant.Query.write(Person, updated_person)
  # OR
  MnesiaAssistant.Query.write(Person, updated_person, :write)
  # OR
  MnesiaAssistant.Query.write(updated_person)