diff --git a/tiledb/logger.go b/tiledb/logger.go new file mode 100644 index 0000000..1f24934 --- /dev/null +++ b/tiledb/logger.go @@ -0,0 +1,11 @@ +package tiledb + +import ( + "github.com/sirupsen/logrus" +) + +var log *logrus.Entry + +func init() { + log = logrus.WithFields(logrus.Fields{"prefix": "tiledb"}) +} diff --git a/tiledb/sqlite.go b/tiledb/sqlite.go new file mode 100644 index 0000000..ab6b3da --- /dev/null +++ b/tiledb/sqlite.go @@ -0,0 +1,58 @@ +package tiledb + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "github.com/sirupsen/logrus" + "mapserver/coords" + "time" +) + +const migrateScript = ` +create table if not exists tiles( + data blob, + mtime bigint, + layer int, + x int, + y int, + zoom int, + primary key(x,y,zoom,layer) +); +` + +type Sqlite3Accessor struct { + db *sql.DB + filename string +} + +func (db *Sqlite3Accessor) Migrate() error { + log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database") + start := time.Now() + _, err := db.db.Exec(migrateScript) + if err != nil { + return err + } + t := time.Now() + elapsed := t.Sub(start) + log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed") + + return nil +} + +func (db *Sqlite3Accessor) GetTile(pos coords.TileCoords) (*Tile, error) { + return nil, nil +} + +func (db *Sqlite3Accessor) SetTile(pos coords.TileCoords, tile *Tile) error { + return nil +} + +func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) { + db, err := sql.Open("sqlite3", filename) + if err != nil { + return nil, err + } + + sq := &Sqlite3Accessor{db: db, filename: filename} + return sq, nil +} diff --git a/tiledb/sqlite_test.go b/tiledb/sqlite_test.go new file mode 100644 index 0000000..b60ef5d --- /dev/null +++ b/tiledb/sqlite_test.go @@ -0,0 +1,25 @@ +package tiledb + +import ( + "io/ioutil" + "os" + "testing" +) + +func TestMigrate(t *testing.T) { + tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite") + if err != nil { + panic(err) + } + defer os.Remove(tmpfile.Name()) + + db, err := NewSqliteAccessor(tmpfile.Name()) + if err != nil { + panic(err) + } + + err = db.Migrate() + if err != nil { + panic(err) + } +}