blob: 2aa9d89eb7dcabaecb3c0df592cbeda3861a4d81 (
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
|
module Guff
class TagModel < Model
SQL = TemplateCache.new({
add_tags: "
INSERT INTO tags(name) VALUES(%{tags})
",
get_tags: "
SELECT tag_id,
name
FROM tags
WHERE name IN (%{tags})
",
} of Symbol => String)
def initialize(models : Models)
super(models, SQL)
end
def add_tags(tags : Array(String))
# get ids of existing tags
ids = get_ids(tags)
query(:add_tags, nil, {
"tags": tags.reject { |tag|
ids[tag]?
}.map { |tag|
"'" + @db.quote(tag) + "'"
}.join(','),
})
end
private def get_ids(
tags = [] of String : Array(String)
) : Hash(String, Int32)
r = {} of String => Int32
all(:get_tags, nil, {
"tags": tags.map { |tag|
"'" + @db.quote(tag) + "'"
}.join(','),
}) do |row|
r[row["name"] as String] = (row["tag_id"] as String).to_i
end
# return result
r
end
end
end
|