From b5d3e934b9c6774c73f0625ab5426bd24826d7b0 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Thu, 17 Jan 2019 08:01:48 +0100 Subject: [PATCH] tiledb insert --- tiledb/sqlite.go | 7 ++++--- tiledb/sqlite_test.go | 34 +++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/tiledb/sqlite.go b/tiledb/sqlite.go index d9e6c73..f5bbdd4 100644 --- a/tiledb/sqlite.go +++ b/tiledb/sqlite.go @@ -53,6 +53,8 @@ func (db *Sqlite3Accessor) GetTile(layerId int, pos coords.TileCoords) (*Tile, e return nil, err } + defer rows.Close() + if rows.Next() { var data []byte var mtime int64 @@ -80,13 +82,12 @@ func (db *Sqlite3Accessor) GetTile(layerId int, pos coords.TileCoords) (*Tile, e } const setTileQuery = ` -insert into tiles(x,y,zoom,layerid,data,mtime) +insert or replace into tiles(x,y,zoom,layerid,data,mtime) values(?, ?, ?, ?, ?, ?) -on conflict replace ` func (db *Sqlite3Accessor) SetTile(tile *Tile) error { - _, err := db.db.Query(setTileQuery, tile.Pos.X, tile.Pos.Y, tile.Pos.Zoom, tile.LayerId, tile.Data, tile.Mtime) + _, err := db.db.Exec(setTileQuery, tile.Pos.X, tile.Pos.Y, tile.Pos.Zoom, tile.LayerId, tile.Data, tile.Mtime) return err } diff --git a/tiledb/sqlite_test.go b/tiledb/sqlite_test.go index fea1aef..3eaebc4 100644 --- a/tiledb/sqlite_test.go +++ b/tiledb/sqlite_test.go @@ -8,7 +8,7 @@ import ( ) func TestMigrate(t *testing.T) { - tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite") + tmpfile, err := ioutil.TempFile("", "TileDBTest.*.sqlite") if err != nil { panic(err) } @@ -25,14 +25,38 @@ func TestMigrate(t *testing.T) { } pos := coords.NewTileCoords(0, 0, 13) - _, err = db.GetTile(0, pos) + tile, err := db.GetTile(0, pos) if err != nil { panic(err) } - data := []byte{} - tile := Tile{LayerId: 0, Pos: pos, Data: data} - err = db.SetTile(&tile) + if tile != nil { + t.Fatal("non-empty tile found") + } + + data := []byte{0x01, 0x02} + tile2 := Tile{LayerId: 0, Pos: pos, Data: data} + err = db.SetTile(&tile2) + + if err != nil { + panic(err) + } + + tile3, err := db.GetTile(0, pos) + + if err != nil { + panic(err) + } + + if tile3 == nil { + t.Fatal("no data returned") + } + + if len(tile2.Data) != len(tile3.Data) { + t.Fatal("inserted data does not match") + } + + err = db.SetTile(&tile2) if err != nil { panic(err)