2023-08-23 02:06:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2024-07-03 06:49:33 +00:00
|
|
|
sn "github.com/ekzyis/snappy"
|
2023-08-23 02:06:37 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
db *sql.DB
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
2024-07-03 06:42:10 +00:00
|
|
|
db, err = sql.Open("sqlite3", "nitterbot.sqlite3")
|
2023-08-23 02:06:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
migrate(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrate(db *sql.DB) {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS comments (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
text TEXT NOT NULL,
|
|
|
|
parent_id INTEGER NOT NULL,
|
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error during migration: %w", err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ItemHasComment(parentId int) bool {
|
|
|
|
var count int
|
|
|
|
err := db.QueryRow(`SELECT COUNT(1) FROM comments WHERE parent_id = ?`, parentId).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error during item check: %w", err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return count > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveComment(comment *sn.Comment) {
|
|
|
|
_, err := db.Exec(`INSERT INTO comments(id, text, parent_id) VALUES (?, ?, ?)`, comment.Id, comment.Text, comment.ParentId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error during item insert: %w", err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|