Skip to content

Commit 3915197

Browse files
committed
Merge master
2 parents ed3676a + eb7c1ed commit 3915197

File tree

2 files changed

+24
-49
lines changed

2 files changed

+24
-49
lines changed

app/model/setting.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,15 @@ func SetNavigators(labels, urls []string) error {
4747
}
4848

4949
func GetSetting(k string) (*Setting, error) {
50-
row := db.QueryRow(`SELECT id, key, value, type, created_at, created_by from settings where key = ?`, k)
50+
row := db.QueryRow(stmtGetSettingByKey, k)
5151
s := new(Setting)
5252
err := scanSetting(row, s)
5353
return s, err
5454
}
5555

5656
func GetSettingValue(k string) string {
57-
row := db.QueryRow(`SELECT id, key, value, type, created_at, created_by from settings where key = ?`, k)
58-
s := new(Setting)
59-
scanSetting(row, s)
57+
// TODO: error handling
58+
s, _ := GetSetting(k)
6059
return s.Value
6160
}
6261

@@ -76,7 +75,7 @@ func GetCustomSettings() []*Setting {
7675

7776
func GetSettings(t string) []*Setting {
7877
settings := make([]*Setting, 0)
79-
rows, err := db.Query(`SELECT id, key, value, type, created_at, created_by from settings where type = ?`, t)
78+
rows, err := db.Query(stmtGetSettingsByType, t)
8079
if err != nil {
8180
return settings
8281
}
@@ -101,7 +100,7 @@ func (s *Setting) Save() error {
101100
writeDB.Rollback()
102101
return err
103102
}
104-
_, err = db.Exec(`INSERT OR REPLACE INTO settings (id, key, value, type, created_at, created_by) VALUES ((SELECT id FROM settings WHERE key = ?), ?, ?, ?, ?, ?)`, s.Key, s.Key, s.Value, s.Type, s.CreatedAt, s.CreatedBy)
103+
_, err = db.Exec(stmtUpdateSetting, s.Key, s.Key, s.Value, s.Type, s.CreatedAt, s.CreatedBy)
105104
if err != nil {
106105
writeDB.Rollback()
107106
return err

app/model/sql_stmt.go

+19-43
Original file line numberDiff line numberDiff line change
@@ -160,33 +160,25 @@ messages (
160160

161161
// Posts
162162
var postCountSelector = SQL.Select(`count(*)`).From(`posts`)
163-
var stmtGetPublishedPostsCount = postCountSelector.Copy().Where(`status = "published"`).SQL()
164-
var stmtGetAllPostsCount = postCountSelector.Copy().SQL()
165-
var stmtGetPostsCountByUser = postCountSelector.Copy().Where(`author_id = ?`).SQL()
166-
var stmtGetPostsCountByTag = postCountSelector.Copy().From(`posts, posts_tags`).Where(`posts_tags.post_id = posts.id`, `posts_tags.tag_id = ?`, `status = 'published'`).SQL()
167-
168163
var postSelector = SQL.Select(`id, title, slug, markdown, html, featured, page, allow_comment, comment_num, status, image, author_id, created_at, created_by, updated_at, updated_by, published_at, published_by`).From(`posts`)
169-
var stmtGetPublishedPostList = postSelector.Copy().Where(`status = "published"`).OrderBy(`published_at DESC`).Limit(`?`).Offset(`?`).SQL()
170-
var stmtGetAllPostList = postSelector.Copy().OrderBy(`published_at DESC`).Limit(`?`).Offset(`?`).SQL()
171-
var stmtGetPostsByUser = postSelector.Copy().Where(`status = 'published'`, `author_id = ?`).OrderBy(`published_at DESC`).Limit(`?`).Offset(`?`).SQL()
164+
var postsTagsSelector = SQL.Select(`posts.id, posts.title, posts.slug, posts.markdown, posts.html, posts.featured, posts.page, posts.allow_comment, posts.comment_num, posts.status, posts.image, posts.author_id, posts.created_at, posts.created_by, posts.updated_at, posts.updated_by, posts.published_at, posts.published_by`).From(`posts, posts_tags`)
172165

166+
var stmtGetPostsCountByTag = postCountSelector.Copy().From(`posts, posts_tags`).Where(`posts_tags.post_id = posts.id`, `posts_tags.tag_id = ?`, `status = 'published'`).SQL()
173167
var stmtGetPostById = postSelector.Copy().Where(`id = ?`).SQL()
174168
var stmtGetPostBySlug = postSelector.Copy().Where(`slug = ?`).SQL()
175-
176-
var postsTagsSelector = SQL.Select(`posts.id, posts.title, posts.slug, posts.markdown, posts.html, posts.featured, posts.page, posts.allow_comment, posts.comment_num, posts.status, posts.image, posts.author_id, posts.created_at, posts.created_by, posts.updated_at, posts.updated_by, posts.published_at, posts.published_by`).From(`posts, posts_tags`)
177169
var stmtGetPostsByTag = postsTagsSelector.Copy().Where(`status = 'published'`, `posts_tags.post_id = posts.id`, `posts_tags.tag_id = ?`).OrderBy(`published_at DESC`).Limit(`?`).Offset(`?`).SQL()
178170
var stmtGetAllPostsByTag = postsTagsSelector.Copy().Where(`posts_tags.post_id = posts.id`, `posts_tags.tag_id = ?`).OrderBy(`published_at DESC`).SQL()
179171

180-
var pageCountSelector = SQL.Select(`count(*)`).From(`posts`).Where(`page = 1`)
181-
var stmtGetPublishedPagesCount = pageCountSelector.Copy().Where(`status = "published"`).SQL()
182-
var stmtGetAllPagesCount = pageCountSelector.Copy().SQL()
183-
var stmtGetPagesCountByUser = pageCountSelector.Copy().Where(`author_id = ?`).SQL()
184-
var stmtGetPagesCountByTag = pageCountSelector.Copy().Where(`posts_tags.post_id = posts.id`, `posts_tags.tag_id = ?`, `status = 'published'`).SQL()
172+
const stmtInsertPost = `INSERT INTO posts (id, title, slug, markdown, html, featured, page, allow_comment, status, image, author_id, created_at, created_by, updated_at, updated_by, published_at, published_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
173+
const stmtUpdatePost = `UPDATE posts SET title = ?, slug = ?, markdown = ?, html = ?, featured = ?, page = ?, allow_comment = ?, status = ?, image = ?, updated_at = ?, updated_by = ? WHERE id = ?`
174+
const stmtUpdatePostPublished = `UPDATE posts SET title = ?, slug = ?, markdown = ?, html = ?, featured = ?, page = ?, allow_comment = ?, status = ?, image = ?, updated_at = ?, updated_by = ?, published_at = ?, published_by = ? WHERE id = ?`
175+
const stmtDeletePostById = `DELETE FROM posts WHERE id = ?`
185176

186-
var pageSelector = SQL.Select(`id, title, slug, markdown, html, featured, page, status, image, author_id, created_at, created_by, updated_at, updated_by, published_at, published_by`).From(`posts`).Where(`page = 1`)
177+
//PostTags
178+
const stmtDeletePostTagsByPostId = `DELETE FROM posts_tags WHERE post_id = ?`
179+
const stmtInsertPostTag = `INSERT INTO posts_tags (id, post_id, tag_id) VALUES (?, ?, ?)`
187180

188181
// Comments
189-
190182
var commentCountSelector = SQL.Select(`count(*)`).From(`comments`)
191183
var stmtGetAllCommentCount = commentCountSelector.SQL()
192184
var commentSelector = SQL.Select(`id, post_id, author, author_email, author_url, created_at, content, approved, agent, parent, user_id`).From(`comments`)
@@ -204,8 +196,12 @@ const stmtGetUserBySlug = `SELECT id, name, slug, email, image, cover, bio, webs
204196
const stmtGetUserByName = `SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE name = ?`
205197
const stmtGetUserByEmail = `SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE email = ?`
206198
const stmtGetHashedPasswordByEmail = `SELECT password FROM users WHERE email = ?`
207-
const stmtGetUsersCount = `SELECT count(*) FROM users`
208199
const stmtGetUsersCountByEmail = `SELECT count(*) FROM users where email = ?`
200+
const stmtInsertUser = `INSERT INTO users (id, name, slug, password, email, image, cover, created_at, created_by, updated_at, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
201+
const stmtUpdateUser = `UPDATE users SET name = ?, slug = ?, email = ?, image = ?, cover = ?, bio = ?, website = ?, location = ?, updated_at = ?, updated_by = ? WHERE id = ?`
202+
203+
// RoleUser
204+
const stmtInsertRoleUser = `INSERT INTO roles_users (id, role_id, user_id) VALUES (?, ?, ?)`
209205

210206
// Tokens
211207
const stmtGetTokenByValue = `SELECT value, user_id, created_at, expired_at FROM tokens WHERE value = ?`
@@ -216,37 +212,17 @@ const stmtGetAllTags = `SELECT id, name, slug FROM tags`
216212
const stmtGetTags = `SELECT tag_id FROM posts_tags WHERE post_id = ?`
217213
const stmtGetTagById = `SELECT id, name, slug FROM tags WHERE id = ?`
218214
const stmtGetTagBySlug = `SELECT id, name, slug, hidden FROM tags WHERE slug = ?`
219-
220-
// Settings
221-
const stmtGetBlog = `SELECT value FROM settings WHERE key = ?`
222-
const stmtGetPostCreationDateById = `SELECT created_at FROM posts WHERE id = ?`
223-
224-
const stmtInsertPost = `INSERT INTO posts (id, title, slug, markdown, html, featured, page, allow_comment, status, image, author_id, created_at, created_by, updated_at, updated_by, published_at, published_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
225-
const stmtInsertUser = `INSERT INTO users (id, name, slug, password, email, image, cover, created_at, created_by, updated_at, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
226-
const stmtInsertRoleUser = `INSERT INTO roles_users (id, role_id, user_id) VALUES (?, ?, ?)`
227215
const stmtInsertTag = `INSERT INTO tags (id, name, slug, created_at, created_by, updated_at, updated_by, hidden) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
228-
const stmtInsertPostTag = `INSERT INTO posts_tags (id, post_id, tag_id) VALUES (?, ?, ?)`
229-
const stmtInsertSetting = `INSERT INTO settings (id, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
230-
231-
const stmtUpdatePost = `UPDATE posts SET title = ?, slug = ?, markdown = ?, html = ?, featured = ?, page = ?, allow_comment = ?, status = ?, image = ?, updated_at = ?, updated_by = ? WHERE id = ?`
232-
const stmtUpdatePostPublished = `UPDATE posts SET title = ?, slug = ?, markdown = ?, html = ?, featured = ?, page = ?, allow_comment = ?, status = ?, image = ?, updated_at = ?, updated_by = ?, published_at = ?, published_by = ? WHERE id = ?`
233-
const stmtUpdateSettings = `UPDATE settings SET value = ?, updated_at = ?, updated_by = ? WHERE key = ?`
234-
const stmtUpdateUser = `UPDATE users SET name = ?, slug = ?, email = ?, image = ?, cover = ?, bio = ?, website = ?, location = ?, updated_at = ?, updated_by = ? WHERE id = ?`
235-
const stmtUpdateLastLogin = `UPDATE users SET last_login = ? WHERE id = ?`
236-
const stmtUpdateUserPassword = `UPDATE users SET password = ?, updated_at = ?, updated_by = ? WHERE id = ?`
237216
const stmtUpdateTag = `UPDATE tags SET name = ?, slug =?, updated_at = ?, updated_by = ?, hidden = ? WHERE id = ?`
238-
239-
const stmtDeletePostTagsByPostId = `DELETE FROM posts_tags WHERE post_id = ?`
240-
const stmtDeletePostById = `DELETE FROM posts WHERE id = ?`
241217
const stmtDeleteOldTags = `DELETE FROM tags WHERE id IN (SELECT id FROM tags EXCEPT SELECT tag_id FROM posts_tags)`
242218

243-
// Messages
244-
var messageCountSelector = SQL.Select(`count(*)`).From(`messages`)
245-
var sttmGetUnreadMessageCount = messageCountSelector.Copy().Where(`is_read = 0`).From(`messages`).SQL()
219+
// Settings
220+
const stmtGetSettingByKey = `SELECT id, key, value, type, created_at, created_by from settings where key = ?`
221+
const stmtGetSettingsByType = `SELECT id, key, value, type, created_at, created_by from settings where type = ?`
222+
const stmtUpdateSetting = `INSERT OR REPLACE INTO settings (id, key, value, type, created_at, created_by) VALUES ((SELECT id FROM settings WHERE key = ?), ?, ?, ?, ?, ?)`
246223

224+
// Messages
247225
var messageSelector = SQL.Select(`id, type, data, is_read, created_at`).From(`messages`)
248-
var stmtGetAllMessages = messageSelector.Copy().OrderBy(`created_at DESC`).SQL()
249226
var stmtGetUnreadMessages = messageSelector.Copy().Where(`is_read = 0`).OrderBy(`created_at DESC`).Limit(`?`).Offset(`?`).SQL()
250-
var stmtGetAllMessagesByPage = messageSelector.Copy().OrderBy(`created_at DESC`).Limit(`?`).Offset(`?`).SQL()
251227

252228
const stmtInsertMessage = `INSERT INTO messages (id, type, data, is_read, created_at) VALUES (?, ?, ?, ?, ?)`

0 commit comments

Comments
 (0)