Krug.BaseEctoDAO behaviour (Krug v0.4.14) View Source

Defines a behaviour for higher-level CRUD functionalities module to facilitate the raw queries usage with Ecto.

Utilization: Create a module that extends Krug.BaseEctoDAO.

  • MyApp.App.Repo should be a module that extends Ecto.Repo.
defmodule MyApp.App.DAOService do

  use Krug.BaseEctoDAO, repo: MyApp.App.Repo

end

This mechanism also includes by default a in-memory query cache of last 10 select results of each database table. This cache (for respective table) is empty each time that a entry is inserted/updated/deleted from the respective table. You can disable this mechanism for expensive tables (large text data or many columns), or tables that are very intensible/frequently writed/updated. For this use the nocache_tables atribute.

defmodule MyApp.App.DAOService do

  use Krug.BaseEctoDAO, repo: MyApp.App.Repo, nocache_tables: ["my_table_no_cache1","my_table_no_cache2"]

end

Link to this section Summary

Callbacks

Delete a row from a table. Return false only if fail in execution of SQL command.

Insert a new row on a table. Return false only if fail in execution of SQL command.

Load a resultset from a table. Return nil only if fail in execution of SQL command.

Load a resultset from a table. Return nil only if fail in execution of SQL command.

Update a row on a table. Return false only if fail in execution of SQL command.

Link to this section Callbacks

Specs

delete(sql :: String.t(), params :: Enum.t()) :: boolean()

Delete a row from a table. Return false only if fail in execution of SQL command.

Examples

iex > sql = "delete from person where name = ? and id = ?"
iex > MyApp.App.DAOService.delete(sql,["Person Name That Not Exist",1000000000000001])
true
iex > sql = "delete from person where columnThatNotExists = ?"
iex > MyApp.App.DAOService.delete(sql,["Johannes Backend 3"])
false

Specs

insert(sql :: String.t(), params :: Enum.t()) :: boolean()

Insert a new row on a table. Return false only if fail in execution of SQL command.

Examples

iex > sql = "insert into person(name,email,age,address) values (?,?,?,?)"
iex > MyApp.App.DAOService.insert(sql,["Johannes Backend 2"])
false
iex > sql = "insert into person(name,email,age,address) values (?,?,?,?)"
iex > MyApp.App.DAOService.insert(sql,["Johannes Backend 2","johannes@backend.com",54,"404 street"])
true

Specs

load(sql :: String.t()) :: map()

Load a resultset from a table. Return nil only if fail in execution of SQL command.

To see how handle returned resultset, search by Krug.ResultSetHandler in this documentation.

Examples

iex > sql = "select id, name, email, age, address from person limit 10"
iex > MyApp.App.DAOService.load(sql)
%MyXQL.Result{
  columns: ["id", "name", "email", "age", "address"],
  connection_id: 1515,
  last_insert_id: 0,
  num_rows: 1,
  num_warnings: 0,
  rows: [
    [1,"Johannes Backend","johannes@backend.com",54,"404 street"]
  ]
}
iex > sql = "select id, columnThatNonExists, email, age, address from person limit 10"
iex > MyApp.App.DAOService.load(sql)
nil

Specs

load(sql :: String.t(), params :: Enum.t()) :: map()

Load a resultset from a table. Return nil only if fail in execution of SQL command.

To see how handle returned resultset, search by Krug.ResultSetHandler in this documentation.

Examples

iex > sql = "select id, name, email, age, address from person where age < ?"
iex > MyApp.App.DAOService.load(sql,[20])
%MyXQL.Result{
  columns: ["id", "name", "email", "age", "address"],
  connection_id: 1515,
  last_insert_id: 0,
  num_rows: 0,
  num_warnings: 0,
  rows: nil
}
iex > sql = "select id, columnThatNotExists, email, age, address from person where age < ?"
iex > MyApp.App.DAOService.load(sql,[20])
nil

Specs

update(sql :: String.t(), params :: Enum.t()) :: boolean()

Update a row on a table. Return false only if fail in execution of SQL command.

Examples

iex > sql = "update person set name = ?, email = ? where id = ?"
iex > MyApp.App.DAOService.update(sql,["Johannes Backend 3","johannes@has.not.email",1])
true
iex > sql = "update person set columnThatNotExists = ?, email = ? where id = ?"
iex > MyApp.App.DAOService.update(sql,["Johannes Backend 3","johannes@has.not.email",1])
false