2019-01-08 00:27:24 +03:00
|
|
|
package db
|
|
|
|
|
2019-01-08 10:57:50 +03:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-01-09 10:44:14 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-08 10:57:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-08 18:33:49 +03:00
|
|
|
const migrateScript = `
|
2019-01-09 10:44:14 +03:00
|
|
|
alter table blocks add mtime timestamp default NULL;
|
|
|
|
update blocks set mtime = current_timestamp;
|
|
|
|
create index blocks_mtime on blocks(mtime);
|
|
|
|
|
|
|
|
CREATE TRIGGER update_blocks_mtime_insert after insert on blocks for each row
|
|
|
|
begin
|
|
|
|
update blocks set mtime = current_timestamp where pos = new.pos;
|
|
|
|
end;
|
|
|
|
|
|
|
|
CREATE TRIGGER update_blocks_mtime_update after update on blocks for each row
|
|
|
|
begin
|
|
|
|
update blocks set mtime = current_timestamp where pos = old.pos;
|
|
|
|
end;
|
2019-01-08 18:33:49 +03:00
|
|
|
`
|
|
|
|
|
2019-01-08 00:27:24 +03:00
|
|
|
type Sqlite3Accessor struct {
|
2019-01-08 10:57:50 +03:00
|
|
|
db *sql.DB
|
2019-01-09 10:44:14 +03:00
|
|
|
filename string
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Sqlite3Accessor) Migrate() error {
|
2019-01-09 10:44:14 +03:00
|
|
|
log := logrus.WithFields(logrus.Fields{"prefix": "db/sqlite.Migrate","filename":db.filename})
|
|
|
|
|
|
|
|
//RW connection
|
|
|
|
rwdb, err := sql.Open("sqlite3", db.filename + "?mode=rw")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rwdb.Close()
|
|
|
|
|
|
|
|
hasMtime := true
|
|
|
|
_, err = rwdb.Query("select max(mtime) from blocks")
|
|
|
|
if err != nil {
|
|
|
|
hasMtime = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasMtime {
|
|
|
|
log.Info("Migrating database")
|
|
|
|
_, err = rwdb.Exec(migrateScript)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 00:27:24 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-08 10:46:12 +03:00
|
|
|
func (db *Sqlite3Accessor) FindLatestBlocks(mintime int64, limit int) ([]Block, error) {
|
2019-01-08 10:57:50 +03:00
|
|
|
return make([]Block, 0), nil
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 10:46:12 +03:00
|
|
|
func (db *Sqlite3Accessor) FindBlocks(posx int, posz int, posystart int, posyend int) ([]Block, error) {
|
2019-01-08 10:57:50 +03:00
|
|
|
return make([]Block, 0), nil
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 10:46:12 +03:00
|
|
|
func (db *Sqlite3Accessor) CountBlocks(x1, x2, y1, y2, z1, z2 int) (int, error) {
|
2019-01-08 10:57:50 +03:00
|
|
|
return 0, nil
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 10:46:12 +03:00
|
|
|
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {
|
2019-01-08 10:57:50 +03:00
|
|
|
db, err := sql.Open("sqlite3", filename + "?mode=ro")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-09 10:44:14 +03:00
|
|
|
sq := &Sqlite3Accessor{db: db, filename: filename}
|
2019-01-08 10:57:50 +03:00
|
|
|
return sq, nil
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|