2022-10-19 03:25:43 +04:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2023-02-05 16:28:55 +04:00
|
|
|
"time"
|
2022-10-19 03:25:43 +04:00
|
|
|
|
2023-03-11 22:21:33 +04:00
|
|
|
"git.arav.su/Arav/justguestbook/guestbook"
|
2022-10-19 03:25:43 +04:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-01-12 03:27:45 +04:00
|
|
|
"github.com/pkg/errors"
|
2022-10-19 03:25:43 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed queries/schema.sql
|
2023-01-12 03:27:45 +04:00
|
|
|
querySchema string
|
2022-10-19 03:25:43 +04:00
|
|
|
|
|
|
|
//go:embed queries/entryGetAll.sql
|
2023-01-12 03:27:45 +04:00
|
|
|
queryGetAll string
|
2022-10-21 04:25:15 +04:00
|
|
|
//go:embed queries/entryCount.sql
|
|
|
|
queryCount string
|
2022-10-19 03:25:43 +04:00
|
|
|
|
|
|
|
//go:embed queries/entryNew.sql
|
|
|
|
queryNewEntry string
|
2022-10-21 04:25:15 +04:00
|
|
|
//go:embed queries/entryUpdate.sql
|
|
|
|
queryUpdateEntry string
|
2022-10-19 03:25:43 +04:00
|
|
|
//go:embed queries/entryDelete.sql
|
|
|
|
queryDeleteEntry string
|
|
|
|
//go:embed queries/replyNew.sql
|
|
|
|
queryNewReply string
|
2022-10-21 04:25:15 +04:00
|
|
|
//go:embed queries/replyUpdate.sql
|
|
|
|
queryUpdateReply string
|
2022-10-19 03:25:43 +04:00
|
|
|
//go:embed queries/replyDelete.sql
|
|
|
|
queryDeleteReply string
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-12 03:27:45 +04:00
|
|
|
stmtGetAll *sql.Stmt
|
2022-10-21 04:25:15 +04:00
|
|
|
stmtCount *sql.Stmt
|
2022-10-19 03:25:43 +04:00
|
|
|
stmtNewEntry *sql.Stmt
|
2022-10-21 04:25:15 +04:00
|
|
|
stmtUpdateEntry *sql.Stmt
|
2022-10-19 03:25:43 +04:00
|
|
|
stmtDeleteEntry *sql.Stmt
|
|
|
|
stmtNewReply *sql.Stmt
|
2022-10-21 04:25:15 +04:00
|
|
|
stmtUpdateReply *sql.Stmt
|
2022-10-19 03:25:43 +04:00
|
|
|
stmtDeleteReply *sql.Stmt
|
|
|
|
)
|
|
|
|
|
|
|
|
func initDBStatements(db *sql.DB) error {
|
2023-01-12 03:27:45 +04:00
|
|
|
db.Exec("PRAGMA foreign_keys = ON;")
|
|
|
|
|
|
|
|
_, err := db.Exec(querySchema)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to init schema")
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
stmtGetAll, err = db.Prepare(queryGetAll)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryGetAll")
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2022-10-21 04:25:15 +04:00
|
|
|
stmtCount, err = db.Prepare(queryCount)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryCount")
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
stmtNewEntry, err = db.Prepare(queryNewEntry)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryNewEntry")
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2022-10-21 04:25:15 +04:00
|
|
|
stmtUpdateEntry, err = db.Prepare(queryUpdateEntry)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryUpdateEntry")
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
stmtDeleteEntry, err = db.Prepare(queryDeleteEntry)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryDeleteEntry")
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
stmtNewReply, err = db.Prepare(queryNewReply)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryNewReply")
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2022-10-21 04:25:15 +04:00
|
|
|
stmtUpdateReply, err = db.Prepare(queryUpdateReply)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryUpdateReply")
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
stmtDeleteReply, err = db.Prepare(queryDeleteReply)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return errors.Wrap(err, "failed to prepare queryDeleteReply")
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SQLiteDatabase struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(filePath string) (*SQLiteDatabase, error) {
|
|
|
|
db, err := sql.Open("sqlite3", dsn(filePath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDBStatements(db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SQLiteDatabase{db: db}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SQLiteDatabase) Entries(page, pageSize int64) (entries []*guestbook.Entry, err error) {
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
rows, err := tx.Stmt(stmtGetAll).Query(pageSize, (page-1)*pageSize)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var entry guestbook.Entry
|
2023-02-05 20:03:36 +04:00
|
|
|
var entry_created string
|
2022-10-19 03:25:43 +04:00
|
|
|
var reply_created sql.NullString
|
|
|
|
var reply_message sql.NullString
|
|
|
|
if err = rows.Scan(
|
2023-02-05 20:03:36 +04:00
|
|
|
&entry.ID, &entry_created, &entry.Name,
|
2023-01-12 03:27:45 +04:00
|
|
|
&entry.Message, &entry.Website,
|
2022-10-19 03:25:43 +04:00
|
|
|
&reply_created, &reply_message); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
entry.Created, err = time.Parse(guestbook.DateFormat, entry_created)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
if reply_message.Valid /* reply_created is also valid if reply is */ {
|
2023-02-05 16:28:55 +04:00
|
|
|
date, err := time.Parse(guestbook.DateFormat, reply_created.String)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
entry.Reply = &guestbook.Reply{
|
2023-01-12 03:27:45 +04:00
|
|
|
ID: entry.ID,
|
2023-02-05 16:28:55 +04:00
|
|
|
Created: date,
|
2022-10-19 03:25:43 +04:00
|
|
|
Message: reply_message.String}
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = append(entries, &entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
// Count returns how much entries are in an `entry` table.
|
|
|
|
func (d *SQLiteDatabase) Count() (count int64, err error) {
|
2022-10-21 04:25:15 +04:00
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
err = tx.Stmt(stmtCount).QueryRow().Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
tx.Commit()
|
|
|
|
|
2022-10-21 04:25:15 +04:00
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
// NewEntry inserts a passed Entry struct and fills its ID field if successful.
|
2022-10-19 03:25:43 +04:00
|
|
|
func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error {
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
r, err := tx.Stmt(stmtNewEntry).Exec(entry.Created.Format(guestbook.DateFormat), entry.Name, entry.Message,
|
2022-10-19 03:25:43 +04:00
|
|
|
entry.Website, entry.HideWebsite)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
entry.ID, err = r.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
// UpdateEntry
|
2023-02-05 20:03:36 +04:00
|
|
|
func (d *SQLiteDatabase) UpdateEntry(entry *guestbook.Entry) error {
|
2022-10-21 04:25:15 +04:00
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
2023-02-05 20:03:36 +04:00
|
|
|
return err
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
row := tx.Stmt(stmtUpdateEntry).QueryRow(entry.Name, entry.Message, entry.Website, entry.HideWebsite, entry.ID)
|
2023-02-05 20:03:36 +04:00
|
|
|
if row.Err() != nil {
|
|
|
|
return row.Err()
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
2023-01-12 03:27:45 +04:00
|
|
|
tx.Commit()
|
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
return nil
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
func (d *SQLiteDatabase) DeleteEntry(entryID int64) (int64, error) {
|
2022-10-21 04:25:15 +04:00
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return -1, err
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
res, err := tx.Stmt(stmtDeleteEntry).Exec(entryID)
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return -1, err
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
c, err := res.RowsAffected()
|
2022-10-21 04:25:15 +04:00
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return -1, err
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
return c, nil
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) error {
|
2022-10-19 03:25:43 +04:00
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return err
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
_, err = tx.Stmt(stmtNewReply).Exec(reply.ID, reply.Created.Format(guestbook.DateFormat), reply.Message)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
2023-01-12 03:27:45 +04:00
|
|
|
return err
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
return nil
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
func (d *SQLiteDatabase) UpdateReply(reply *guestbook.Reply) error {
|
2022-10-21 04:25:15 +04:00
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
2023-02-05 20:03:36 +04:00
|
|
|
return err
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
row := tx.Stmt(stmtUpdateReply).QueryRow(
|
|
|
|
reply.Created.Format(guestbook.DateFormat), reply.Message, reply.ID)
|
|
|
|
if row.Err() != nil {
|
|
|
|
return row.Err()
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
tx.Commit()
|
2022-10-21 04:25:15 +04:00
|
|
|
|
2023-02-05 20:03:36 +04:00
|
|
|
return nil
|
2022-10-21 04:25:15 +04:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
func (d *SQLiteDatabase) DeleteReply(entryID int64) error {
|
2022-10-21 04:25:15 +04:00
|
|
|
tx, err := d.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
res, err := tx.Stmt(stmtDeleteReply).Exec(entryID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:27:45 +04:00
|
|
|
tx.Commit()
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SQLiteDatabase) Close() error {
|
2023-01-12 03:27:45 +04:00
|
|
|
stmtCount.Close()
|
|
|
|
stmtDeleteEntry.Close()
|
|
|
|
stmtDeleteReply.Close()
|
|
|
|
stmtGetAll.Close()
|
|
|
|
stmtNewEntry.Close()
|
|
|
|
stmtNewReply.Close()
|
|
|
|
stmtUpdateEntry.Close()
|
|
|
|
stmtUpdateReply.Close()
|
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
return d.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dsn(filePath string) string {
|
|
|
|
return fmt.Sprintf("file:%s?_journal=WAL&_mutex=full", filePath)
|
|
|
|
}
|