diff --git a/internal/app/appContext.go b/internal/app/appContext.go index 60a8bce..76b16a5 100644 --- a/internal/app/appContext.go +++ b/internal/app/appContext.go @@ -20,7 +20,7 @@ func (c *appContext) UseMetadataStore(connectionString string) error { logrus.Infof("Using SQL Lite data store with '%s'", connectionString) c.dataStore = localDataStore{} - err := c.dataStore.Open(connectionString) + err := c.dataStore.Initialize(connectionString) return err } diff --git a/internal/app/datastore.go b/internal/app/datastore.go index d4ab57c..45eaff6 100644 --- a/internal/app/datastore.go +++ b/internal/app/datastore.go @@ -1,12 +1,16 @@ package app import ( + "database/sql" "errors" + _ "github.com/mattn/go-sqlite3" + "log" "time" ) type ImageMetaData struct { ImageId int + PiwigoId int RelativeImagePath string Filename string Md5Sum string @@ -27,21 +31,104 @@ type localDataStore struct { connectionString string } -func (d *localDataStore) Open(connectionString string) error { +func (d *localDataStore) Initialize(connectionString string) error { if connectionString == "" { return errors.New("connection string could not be empty.") } d.connectionString = connectionString - //TODO: open and test connection - return nil + db, err := d.openDatabase() + if err != nil { + return err + } + defer db.Close() + + err = d.createTablesIfNeeded(db) + + return err } 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 } 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 }