~evanj/cms

ref: f4362a4175b569eb2b18199a45525b0022b41478 cms/internal/s/db/hook.go -rw-r--r-- 4.0 KiB
f4362a41Evan M Jones Fix(db/content.go): Quickfix for broken content search (accidently 4 months ago
                                                                                
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package db

import (
	"database/sql"
	"strconv"

	"git.sr.ht/~evanj/cms/internal/m/hook"
	"git.sr.ht/~evanj/cms/internal/m/space"
)

const (
	insert = `
		INSERT INTO cms_hooks (SPACE_ID, URL) 
		VALUES (?, ?)
	`

	query = `
		SELECT ID, URL, SPACE_ID
		FROM cms_hooks
		WHERE ID = ?
	`

	delete = `
		DELETE FROM cms_hooks
		WHERE ID = ?
	`
)

type Hook struct {
	id, url, spaceID string
}

func (db *DB) HookNew(space space.Space, url string) (hook.Hook, error) {
	t, err := db.Begin()
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	res, err := t.Exec(insert, space.ID(), url)
	if err != nil {
		return nil, err
	}

	id, err := res.LastInsertId()
	if err != nil {
		return nil, err
	}

	var hook Hook
	if err := t.QueryRow(query, id).Scan(&hook.id, &hook.url, &hook.spaceID); err != nil {
		return nil, err
	}

	return &hook, t.Commit()
}

func (db *DB) hookGet(t *sql.Tx, space space.Space, id string) (hook.Hook, error) {
	var hook Hook
	if err := t.QueryRow(query, id).Scan(&hook.id, &hook.url, &hook.spaceID); err != nil {
		return nil, err
	}

	return &hook, nil
}

func (db *DB) HookGet(space space.Space, id string) (hook.Hook, error) {
	t, err := db.Begin()
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	h, err := db.hookGet(t, space, id)
	if err != nil {
		return nil, err
	}

	return h, t.Commit()
}

func (db *DB) HookDelete(s space.Space, h hook.Hook) error {
	t, err := db.Begin()
	if err != nil {
		return err
	}
	defer t.Rollback()

	if _, err := t.Exec(delete, h.ID()); err != nil {
		return err
	}

	return t.Commit()
}

func (db *DB) hooksPerSpace(t *sql.Tx, space space.Space, before int) (hook.HookList, error) {
	var (
		r       []hook.Hook
		id      int
		hasMore bool
	)

	before = beformat(before)

	q := `
		SELECT ID FROM cms_hooks
		WHERE SPACE_ID = ? AND ID < ?
		ORDER BY ID DESC LIMIT ?
	 `

	rows, err := db.Query(q, space.ID(), before, perPage+1)
	if err != nil {
		return nil, err
	}

	for i := 0; rows.Next(); i++ {
		if i == perPage {
			hasMore = true
			break
		}

		if err := rows.Scan(&id); err != nil {
			return nil, err
		}

		ct, err := db.hookGet(t, space, strconv.Itoa(id))
		if err != nil {
			return nil, err
		}

		r = append(r, ct)
	}

	return newHookList(r, hasMore, id), nil
}

func (db *DB) HooksPerSpace(space space.Space, before int) (hook.HookList, error) {
	t, err := db.Begin()
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	list, err := db.hooksPerSpace(t, space, before)
	if err != nil {
		db.log.Println(err)
		return nil, err
	}

	return list, t.Commit()
}

// Interface impl.

func (h *Hook) ID() string  { return h.id }
func (h *Hook) URL() string { return h.url }

// HOOK ITER STRUCT / INTERFACE

type hookIter struct {
	db    *DB
	t     *sql.Tx
	space space.Space

	// For pump
	list hook.HookList
	err  error
}

func (db *DB) HookIter(t *sql.Tx, space space.Space) *hookIter {
	iter := &hookIter{db, t, space, newHookList(nil, false, 0), nil}
	iter.pump()
	return iter
}

func (iter *hookIter) pump() {
	list, err := iter.db.hooksPerSpace(iter.t, iter.space, iter.list.Before())
	iter.list = list
	iter.err = err
}

func (iter *hookIter) Next() bool {
	if iter.err != nil {
		return true // Error is picked up with Scan call.
	}
	return len(iter.list.List()) > 0
}

func (iter *hookIter) Scan() (hook.Hook, error) {
	var (
		first hook.Hook
		err   error
	)

	if iter.err != nil {
		return first, iter.err
	}

	list := iter.list.List()
	first, rest := list[0], list[1:]

	iter.list = newHookList(rest, iter.list.More(), iter.list.Before())
	if err != nil {
		return nil, err
	}

	if len(iter.list.List()) < 1 {
		iter.pump()
	}

	return first, nil
}

// HOOK LIST STRUCT / INTERFACE

type HookList struct {
	HookList       []hook.Hook
	HookListMore   bool
	HookListBefore int
}

func newHookList(list []hook.Hook, hasMore bool, last int) *HookList {
	return &HookList{list, hasMore, last}
}

func (hl *HookList) List() []hook.Hook { return hl.HookList }
func (hl *HookList) More() bool        { return hl.HookListMore }
func (hl *HookList) Before() int       { return hl.HookListBefore }