Initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
data
|
84
db.go
Normal file
84
db.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
Ready bool
|
||||||
|
path string
|
||||||
|
readConn *sql.DB
|
||||||
|
writeConn *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Open() error {
|
||||||
|
if db.path == "" {
|
||||||
|
return fmt.Errorf("database path not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(db.path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Printf("Database file does not exist at %s, creating", db.path)
|
||||||
|
file, err := os.Create(db.path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create database file: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Database created at %s", db.path)
|
||||||
|
file.Close()
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to open database file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
|
writeConn, err := sql.Open("sqlite3", db.path+"?_journal=WAL&_synchronous=NORMAL")
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("%++v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
writeConn.SetMaxOpenConns(1)
|
||||||
|
writeConn.SetConnMaxIdleTime(30 * time.Second)
|
||||||
|
writeConn.SetConnMaxLifetime(30 * time.Second)
|
||||||
|
db.writeConn = writeConn
|
||||||
|
|
||||||
|
readConn, err := sql.Open("sqlite3", db.path+"?mode=ro&_journal=WAL&_synchronous=NORMAL&_mode=ro")
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("%++v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
readConn.SetMaxOpenConns(4)
|
||||||
|
readConn.SetConnMaxIdleTime(30 * time.Second)
|
||||||
|
readConn.SetConnMaxLifetime(30 * time.Second)
|
||||||
|
db.readConn = readConn
|
||||||
|
|
||||||
|
db.Ready = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Init(ddl string) error {
|
||||||
|
if !db.Ready {
|
||||||
|
return fmt.Errorf("database not ready")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) Close() error {
|
||||||
|
err := db.writeConn.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.readConn.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
17
ddl.sql
Normal file
17
ddl.sql
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
create table guild (
|
||||||
|
id integer primary key,
|
||||||
|
name text
|
||||||
|
);
|
||||||
|
|
||||||
|
create table stinky (
|
||||||
|
id integer primary key,
|
||||||
|
name text,
|
||||||
|
guild integer references guild(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
create table note (
|
||||||
|
id integer primary key,
|
||||||
|
content text,
|
||||||
|
timestamp string,
|
||||||
|
stinky integer references stinky(id)
|
||||||
|
);
|
34
main.go
Normal file
34
main.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Error *log.Logger
|
||||||
|
var Warning *log.Logger
|
||||||
|
func init() {
|
||||||
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||||
|
logFile, err := os.Create("main.log")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error creating log file: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
logger := io.MultiWriter(os.Stdout, logFile)
|
||||||
|
log.SetOutput(logger)
|
||||||
|
|
||||||
|
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
|
||||||
|
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
|
||||||
|
log.Lmicroseconds|log.Lshortfile)
|
||||||
|
Warning = log.New(io.MultiWriter(logFile, os.Stdout),
|
||||||
|
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
|
||||||
|
log.Lmicroseconds|log.Lshortfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Println("Hello, World!")
|
||||||
|
Warning.Println("Hello, World!")
|
||||||
|
Error.Println("Hello, World!")
|
||||||
|
}
|
Reference in New Issue
Block a user