started adding sqlite code

This commit is contained in:
Philipp Häfelfinger 2019-03-15 00:35:49 +01:00
parent 54e59a3f98
commit 9ca31bcb2b
2 changed files with 92 additions and 5 deletions

View File

@ -20,7 +20,7 @@ func (c *appContext) UseMetadataStore(connectionString string) error {
logrus.Infof("Using SQL Lite data store with '%s'", connectionString) logrus.Infof("Using SQL Lite data store with '%s'", connectionString)
c.dataStore = localDataStore{} c.dataStore = localDataStore{}
err := c.dataStore.Open(connectionString) err := c.dataStore.Initialize(connectionString)
return err return err
} }

View File

@ -1,12 +1,16 @@
package app package app
import ( import (
"database/sql"
"errors" "errors"
_ "github.com/mattn/go-sqlite3"
"log"
"time" "time"
) )
type ImageMetaData struct { type ImageMetaData struct {
ImageId int ImageId int
PiwigoId int
RelativeImagePath string RelativeImagePath string
Filename string Filename string
Md5Sum string Md5Sum string
@ -27,21 +31,104 @@ type localDataStore struct {
connectionString string connectionString string
} }
func (d *localDataStore) Open(connectionString string) error { func (d *localDataStore) Initialize(connectionString string) error {
if connectionString == "" { if connectionString == "" {
return errors.New("connection string could not be empty.") return errors.New("connection string could not be empty.")
} }
d.connectionString = connectionString d.connectionString = connectionString
//TODO: open and test connection db, err := d.openDatabase()
return nil if err != nil {
return err
}
defer db.Close()
err = d.createTablesIfNeeded(db)
return err
} }
func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, error) { func (d *localDataStore) GetImageMetadata(relativePath string) (ImageMetaData, error) {
db, err := d.openDatabase()
if err != nil {
return ImageMetaData{}, err
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
return ImageMetaData{}, err
}
//TODO: select entry by path
//stmt, err := tx.Prepare("select * from image WHERE relativePath = '?'")
//if err != nil {
// log.Fatal(err)
//}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
return ImageMetaData{}, nil return ImageMetaData{}, nil
} }
func (d *localDataStore) SaveImageMetadata(m ImageMetaData) error { func (d *localDataStore) SaveImageMetadata(m ImageMetaData) error {
return nil db, err := d.openDatabase()
if err != nil {
return err
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
return err
}
if m.ImageId <= 0 {
err = d.insertImageMetaData(tx, m)
if err != nil {
return err
}
} else {
// TODO: update existing entry
}
err = tx.Commit()
return err
}
func (d *localDataStore) insertImageMetaData(tx *sql.Tx, m ImageMetaData) error {
stmt, err := tx.Prepare("INSERT INTO image (piwigoId, relativePath, fileName, md5sum, lastChanged, categoryPath, categoryId) VALUES (?,?,?,?,?,?,?)")
if err != nil {
return err
}
_, err = stmt.Exec(m.PiwigoId, m.RelativeImagePath, m.Filename, m.Md5Sum, m.LastChange, m.CategoryPath, m.CategoryId)
return err
}
func (d *localDataStore) openDatabase() (*sql.DB, error) {
db, err := sql.Open("sqlite3", d.connectionString)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
return db, err
}
func (d *localDataStore) createTablesIfNeeded(db *sql.DB) error {
_, err := db.Exec("CREATE TABLE IF NOT EXISTS image (" +
"imageId INTEGER PRIMARY KEY AUTOINCREMENT," +
"piwigoId INTEGER NULL," +
"relativePath NVARCHAR(1000) NOT NULL," +
"fileName NVARCHAR(255) NOT NULL," +
"md5sum NVARCHAR(50) NOT NULL," +
"lastChanged DATETIME NOT NULL," +
"categoryPath NVARCHAR(1000) NOT NULL," +
"categoryId INTEGER NULL" +
");")
return err
} }