2023-03-18 16:40:05 +03:00
|
|
|
package utils
|
2022-09-18 19:28:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Database() {
|
2023-06-05 18:54:14 +03:00
|
|
|
db, err := sql.Open("sqlite3", "Starlio.db")
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
2022-09-18 19:28:38 +03:00
|
|
|
}
|
|
|
|
|
2022-12-15 21:04:53 +03:00
|
|
|
if !TableExists(db, "settings") {
|
2022-09-18 19:28:38 +03:00
|
|
|
sqlTable := `
|
|
|
|
CREATE TABLE IF NOT EXISTS settings (
|
2022-09-25 11:04:32 +03:00
|
|
|
startup INTEGER DEFAULT 0,
|
2022-11-21 22:22:00 +03:00
|
|
|
wallpaper INTEGER DEFAULT 0,
|
2023-03-18 16:40:05 +03:00
|
|
|
save_logg INTEGER DEFAULT 1
|
2022-09-18 19:28:38 +03:00
|
|
|
);`
|
2022-09-25 11:04:32 +03:00
|
|
|
|
|
|
|
_, err = db.Exec(sqlTable)
|
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
2022-09-18 19:28:38 +03:00
|
|
|
}
|
|
|
|
|
2023-03-18 16:40:05 +03:00
|
|
|
stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper, save_logg) values(?,?,?)")
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
2022-09-18 19:28:38 +03:00
|
|
|
}
|
|
|
|
|
2023-03-18 16:40:05 +03:00
|
|
|
_, err = stmt.Exec(0, 0, 1)
|
2022-09-25 11:04:32 +03:00
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
2022-09-18 19:28:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 21:04:53 +03:00
|
|
|
|
|
|
|
if !TableExists(db, "favorite") {
|
|
|
|
sqlTable := `
|
|
|
|
CREATE TABLE IF NOT EXISTS favorite (
|
|
|
|
title TEXT,
|
|
|
|
explanation TEXT,
|
|
|
|
copyright TEXT,
|
|
|
|
date TEXT,
|
|
|
|
url TEXT,
|
|
|
|
hdurl TEXT,
|
|
|
|
media_type TEXT
|
|
|
|
);`
|
|
|
|
|
|
|
|
_, err = db.Exec(sqlTable)
|
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDatabase() *sql.DB {
|
2023-06-05 18:54:14 +03:00
|
|
|
db, err := sql.Open("sqlite3", "Starlio.db")
|
2022-12-15 21:04:53 +03:00
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TableExists(db *sql.DB, name string) bool {
|
|
|
|
var exists bool
|
|
|
|
err := db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name=?)", name).Scan(&exists)
|
|
|
|
if err != nil {
|
|
|
|
Logger(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists
|
2022-09-18 19:28:38 +03:00
|
|
|
}
|