PageRenderTime 39ms CodeModel.GetById 19ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

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