-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
167 lines (149 loc) · 4.52 KB
/
database.go
File metadata and controls
167 lines (149 loc) · 4.52 KB
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
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
// Post represents a WordPress post with title, publish date, update date, HTML content, and related taxonomies.
type Post struct {
ID int `db:"ID"`
Title string `db:"title"`
PublishedDate string `db:"published_date"`
UpdatedDate string `db:"updated_date"`
Content string `db:"content"`
URL string // Will be populated from WordPress API
Tags []string // Will be populated separately
Categories []string // Will be populated separately
IsFeatured bool // Default is false
FeaturedImage string // Will be populated from WordPress API
}
// ConnectDB establishes a connection to the MySQL database
func ConnectDB(host, port, user, password, dbName string) (*sqlx.DB, error) {
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
user, password, host, port, dbName,
)
return sqlx.Connect("mysql", dsn)
}
// FetchPosts retrieves all published posts from the database
func FetchPosts(db *sqlx.DB) ([]Post, error) {
query := `
SELECT
ID,
post_title AS title,
post_date AS published_date,
post_modified AS updated_date,
post_content AS content
FROM wp_posts
WHERE
post_type = 'post'
AND post_status = 'publish'
ORDER BY post_date DESC;
`
var posts []Post
if err := db.Select(&posts, query); err != nil {
return nil, fmt.Errorf("query execution error: %v", err)
}
return posts, nil
}
// FetchPostTags retrieves all tags for a post
func FetchPostTags(db *sqlx.DB, postID int) ([]string, error) {
var tags []string
query := `
SELECT t.name
FROM wp_terms t
INNER JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
INNER JOIN wp_term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id = ?
AND tt.taxonomy = 'post_tag';
`
if err := db.Select(&tags, query, postID); err != nil {
return nil, fmt.Errorf("error fetching tags for post %d: %v", postID, err)
}
return tags, nil
}
// FetchPostCategories retrieves all categories for a post
func FetchPostCategories(db *sqlx.DB, postID int) ([]string, error) {
var categories []string
query := `
SELECT t.name
FROM wp_terms t
INNER JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
INNER JOIN wp_term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id = ?
AND tt.taxonomy = 'category';
`
if err := db.Select(&categories, query, postID); err != nil {
return nil, fmt.Errorf("error fetching categories for post %d: %v", postID, err)
}
return categories, nil
}
// FetchFeaturedImage retrieves the featured image URL for a post
func FetchFeaturedImage(db *sqlx.DB, postID int) (string, error) {
var featuredImageID int
query := `
SELECT meta_value
FROM wp_postmeta
WHERE post_id = ?
AND meta_key = '_thumbnail_id';
`
if err := db.Get(&featuredImageID, query, postID); err != nil {
return "", fmt.Errorf("error fetching featured image ID for post %d: %v", postID, err)
}
if featuredImageID > 0 {
var imageURL string
query := `
SELECT guid
FROM wp_posts
WHERE ID = ?;
`
if err := db.Get(&imageURL, query, featuredImageID); err != nil {
return "", fmt.Errorf("error fetching featured image URL for post %d: %v", postID, err)
}
return imageURL, nil
}
return "", nil
}
// FetchPages retrieves all published pages from the WordPress database
func FetchPages(db *sqlx.DB) ([]Post, error) {
query := `
SELECT
ID,
post_title AS title,
post_date AS published_date,
post_modified AS updated_date,
post_content AS content
FROM wp_posts
WHERE
post_type = 'page'
AND post_status = 'publish'
ORDER BY post_date DESC;
`
var pages []Post
if err := db.Select(&pages, query); err != nil {
return nil, fmt.Errorf("failed to fetch pages: %v", err)
}
return pages, nil
}
// getImageURLsFromDB simply SELECTs the GUID column
func GetImageURLsFromDB(db *sqlx.DB, ids []int) ([]string, error) {
stmt, err := db.Prepare(`
SELECT guid
FROM wp_posts
WHERE ID = ?
AND post_type = 'attachment'
`)
if err != nil {
return nil, err
}
defer stmt.Close()
var urls []string
for _, id := range ids {
var url string
if err := stmt.QueryRow(id).Scan(&url); err != nil {
return nil, fmt.Errorf("id %d: %w", id, err)
}
urls = append(urls, url)
}
return urls, nil
}