From 556f1c98528c4523c4095b438b9436ade5e0d84b Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Wed, 4 Jul 2018 13:16:52 -0700 Subject: wip, simplifications --- models/db.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'models') diff --git a/models/db.go b/models/db.go index 31d8bdd..d5bc7dc 100644 --- a/models/db.go +++ b/models/db.go @@ -5,8 +5,8 @@ package models import ( "adammathes.com/neko/config" + "adammathes.com/neko/vlog" "database/sql" - _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" "log" ) @@ -16,7 +16,9 @@ var DB *sql.DB func InitDB() { var err error // DB, err = sql.Open("mysql", dataSourceName) - DB, err = sql.Open(config.Config.DBDriver, config.Config.DBServer) + vlog.Printf("using sqlite3 db file %s\n", config.Config.DBFile) + + DB, err = sql.Open("sqlite3", config.Config.DBFile) if err != nil { log.Panic(err) } @@ -24,4 +26,53 @@ func InitDB() { if err = DB.Ping(); err != nil { log.Panic(err) } + + schema := ` +CREATE TABLE IF NOT EXISTS feed ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + url varchar(100) NOT NULL UNIQUE, + web_url varchar(255) NOT NULL DEFAULT '', + title varchar(255) NOT NULL DEFAULT '', + last_updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + category varchar(255) NOT NULL DEFAULT 'uncategorized' +); +CREATE INDEX feed_url ON feed (url); +CREATE INDEX feed_category ON feed (category); + +CREATE TABLE IF NOT EXISTS item ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + feed_id int(11) NOT NULL, + title text NOT NULL DEFAULT '', + url varchar(255) NOT NULL UNIQUE, + description text NOT NULL DEFAULT '', + publish_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + read_state tinyint(1) NOT NULL DEFAULT '0', + starred tinyint(1) NOT NULL DEFAULT '0', + full_content text NOT NULL DEFAULT '', + header_image text NOT NULL DEFAULT '', + CONSTRAINT item_ibfk_1 FOREIGN KEY (feed_id) REFERENCES feed(id) ON DELETE CASCADE +); +CREATE INDEX item_url ON item (url); +CREATE INDEX item_publish_date ON item (publish_date); +CREATE INDEX item_feed_id ON item (feed_id); +CREATE INDEX item_read_state ON item (read_state); + +CREATE VIRTUAL TABLE fts_item using fts4(content="item", title, url, description); +CREATE TRIGGER item_bu BEFORE UPDATE ON item BEGIN + DELETE FROM fts_item WHERE docid=old.rowid; +END; +CREATE TRIGGER item_bd BEFORE DELETE ON item BEGIN + DELETE FROM fts_item WHERE docid=old.rowid; +END; +CREATE TRIGGER item_au AFTER UPDATE ON item BEGIN + INSERT INTO fts_item(docid, title, url, description) VALUES(new.rowid, new.title, new.url, new.description); +END; +CREATE TRIGGER item_ai AFTER INSERT ON item BEGIN + INSERT INTO fts_item(docid, title, url, description) VALUES(new.rowid, new.title, new.url, new.description); +END; + + INSERT INTO fts_item(fts_item) VALUES('rebuild'); +` + + _, _ = DB.Exec(schema) } -- cgit v1.2.3