aboutsummaryrefslogtreecommitdiff
path: root/src/guff/template-database.cr
blob: bfe832124f66a475f4b2397c8f21b30c8554e878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Guff
  class TemplateDatabase
    def initialize(@db : Database, @templates : TemplateCache)
    end

    def one(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?
    )
      @db.one(template(key, tmpl_args), args)
    end

    def row(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?,
    )
      @db.row(template(key, tmpl_args), args)
    end

    def all(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?,
      &block    : Hash(String, ::SQLite3::Value) -> \
    )
      @db.all(template(key, tmpl_args), args, &block)
    end

    def query(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?
    )
      @db.query(template(key, tmpl_args), args)
    end

    def template(
      key   : Symbol,
      args  : Hash(String, String)?
    )
      @templates[key].run(args)
    end

    def last_insert_row_id
      @db.last_insert_row_id
    end

    def quote(s) : String
      @db.quote(s)
    end

    def transaction(&block)
      @db.transaction(&block)
    end
  end
end