/modules/mod_tkvstore/models/m_tkvstore.erl

https://code.google.com/p/zotonic/ · Erlang · 98 lines · 51 code · 18 blank · 29 comment · 0 complexity · a3ff8ba17abedc56df32d76ac6d88783 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2010 Marc Worrell
  3. %% Date: 2010-10-07
  4. %%
  5. %% @doc Simple store for key/value pairs
  6. %% Copyright 2010 Marc Worrell
  7. %%
  8. %% Licensed under the Apache License, Version 2.0 (the "License");
  9. %% you may not use this file except in compliance with the License.
  10. %% You may obtain a copy of the License at
  11. %%
  12. %% http://www.apache.org/licenses/LICENSE-2.0
  13. %%
  14. %% Unless required by applicable law or agreed to in writing, software
  15. %% distributed under the License is distributed on an "AS IS" BASIS,
  16. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. %% See the License for the specific language governing permissions and
  18. %% limitations under the License.
  19. -module(m_tkvstore).
  20. -author("Marc Worrell <marc@worrell.nl").
  21. -behaviour(gen_model).
  22. %% interface functions
  23. -export([
  24. m_find_value/3,
  25. m_to_list/2,
  26. m_value/2,
  27. get/3,
  28. put/4,
  29. delete/3,
  30. init/1
  31. ]).
  32. -include_lib("zotonic.hrl").
  33. %% @doc Fetch the value for the key from a model source
  34. %% @spec m_find_value(Key, Source, Context) -> term()
  35. m_find_value(Type, #m{value=undefined} = M, _Context) ->
  36. M#m{value=Type};
  37. m_find_value(Key, #m{value=Type}, Context) ->
  38. get(Type, Key, Context).
  39. %% @doc Transform a value to a list, used for template loops
  40. %% @spec m_to_list(Source, Context) -> list()
  41. m_to_list(_, _Context) ->
  42. [].
  43. %% @doc Transform a model value so that it can be formatted or piped through filters
  44. %% @spec m_value(Source, Context) -> term()
  45. m_value(#m{}, _Context) ->
  46. undefined.
  47. %% @doc Fetch a value from the store
  48. get(Type, Key, Context) ->
  49. z_db:q1("select props from tkvstore where type = $1 and key = $2", [Type, Key], Context).
  50. %% @doc Put a value into the store
  51. put(Type, Key, Data, Context) ->
  52. F = fun(Ctx) ->
  53. case z_db:q1("select count(*) from tkvstore where type = $1 and key = $2", [Type, Key], Ctx) of
  54. 0 ->
  55. z_db:q("insert into tkvstore (type, key, props) values ($1, $2, $3)", [Type, Key, Data], Ctx);
  56. 1 ->
  57. z_db:q("update tkvstore set props = $3 where type = $1 and key = $2", [Type, Key, Data], Ctx)
  58. end
  59. end,
  60. z_db:transaction(F, Context).
  61. %% @doc Delete a value from the store
  62. delete(Type, Key, Context) ->
  63. z_db:q("delete from tkvstore where type = $1 and key = $2", [Type, Key], Context),
  64. ok.
  65. %% @doc Ensure that the persistent table is present
  66. init(Context) ->
  67. case z_db:table_exists(tkvstore, Context) of
  68. true ->
  69. ok;
  70. false ->
  71. z_db:q("
  72. create table tkvstore (
  73. type character varying(32) not null,
  74. key character varying(64) not null,
  75. props bytea,
  76. constraint tkvstore_pkey primary key (type, key)
  77. )
  78. ", Context),
  79. z_db:flush(Context)
  80. end.