This commit is contained in:
NatureFreshMilk 2019-01-14 12:48:46 +01:00
parent abfb61a12e
commit 03598d4960
3 changed files with 49 additions and 4 deletions

View File

@ -13,6 +13,6 @@ type Tile struct {
type DBAccessor interface {
Migrate() error
GetTile(pos coords.TileCoords) (*Tile, error)
GetTile(layerId int, pos coords.TileCoords) (*Tile, error)
SetTile(pos coords.TileCoords, tile *Tile) error
}

View File

@ -12,11 +12,11 @@ const migrateScript = `
create table if not exists tiles(
data blob,
mtime bigint,
layer int,
layerid int,
x int,
y int,
zoom int,
primary key(x,y,zoom,layer)
primary key(x,y,zoom,layerid)
);
`
@ -39,7 +39,43 @@ func (db *Sqlite3Accessor) Migrate() error {
return nil
}
func (db *Sqlite3Accessor) GetTile(pos coords.TileCoords) (*Tile, error) {
const getTileQuery = `
select data,mtime from tiles t
where t.layerid = ?
and t.x = ?
and t.y = ?
and t.zoom = ?
`
func (db *Sqlite3Accessor) GetTile(layerId int, pos coords.TileCoords) (*Tile, error) {
rows, err := db.db.Query(getTileQuery, layerId, pos.X, pos.Y, pos.Zoom)
if err != nil {
return nil, err
}
if rows.Next() {
var data []byte
var mtime int64
err = rows.Scan(&data, &mtime)
if err != nil {
return nil, err
}
if data == nil {
return nil, nil
}
mb := Tile{
Pos: pos,
LayerId: layerId,
Data: data,
Mtime: mtime,
}
return &mb, nil
}
return nil, nil
}

View File

@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"testing"
"mapserver/coords"
)
func TestMigrate(t *testing.T) {
@ -22,4 +23,12 @@ func TestMigrate(t *testing.T) {
if err != nil {
panic(err)
}
pos := coords.NewTileCoords(0,0,13)
_, err = db.GetTile(0, pos)
if err != nil {
panic(err)
}
}