Krug.BaseEctoDAO behaviour (Krug v1.1.44) 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. (Required)
  • ets_keyshould be an atom thats identifier the ETS table for caching. (Required) This should be created in Ecto repository initialization, inside the init/2 function, call Krug.EtsUtil.new(:my_ets_key_atom_identifier)
defmodule MyApp.App.Repo do 
  
  use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.MyXQL
  ...
  alias Krug.EtsUtil
  
  def init(_type, config) do
    ...
    EtsUtil.new(:my_ets_key_atom_identifier)
    {:ok, config}
  end

end


defmodule MyApp.App.DAOService do

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

end

This mechanism also includes by default a in-memory query cache of last cache_objects_per_table (default to 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"], 
    cache_objects_per_table: 10,
    ets_key: :my_ets_key_atom_identifier

end

Link to this section Summary

Callbacks

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

Return the ets_key (:cache_objects_per_table) value to be used whit other database services to handle other cache storage functionalities, for example store processed list objects relative to a SQL search in place of store the brute database resultset.

Return the ets_key (:my_ets_key_atom_identifier) value to be used whit other database services to handle other cache storage functionalities, for example store processed list objects relative to a SQL search in place of store the brute database resultset.

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
Link to this callback

get_cache_objects_per_table()

View Source (since 1.0.3)

Specs

get_cache_objects_per_table() :: number()

Return the ets_key (:cache_objects_per_table) value to be used whit other database services to handle other cache storage functionalities, for example store processed list objects relative to a SQL search in place of store the brute database resultset.

BaseEctoDAOSqlCache.put_cache(ets_key,table_name,normalized_sql,params,resultset,cache_objects_per_table)
Link to this callback

get_ets_key()

View Source (since 1.0.2)

Specs

get_ets_key() :: atom()

Return the ets_key (:my_ets_key_atom_identifier) value to be used whit other database services to handle other cache storage functionalities, for example store processed list objects relative to a SQL search in place of store the brute database resultset.

Krug.EtsUtil.store_in_cache(ets_key,obj_key,obj_value)
BaseEctoDAOSqlCache.put_cache(ets_key,table_name,normalized_sql,params,resultset,cache_objects_per_table)

cache_result = Krug.EtsUtil.load_from_cache(ets_key,key)
cache_result = Krug.BaseEctoDAOSqlCache.load_from_cache(ets_key,table_name,normalized_sql,params)

Krug.EtsUtil.remove_from_cache(ets_key,obj_key)

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