This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/db/sqlite.go
2021-01-04 01:55:41 -05:00

96 lines
2.5 KiB
Go

package db
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type MediaItem struct {
mediaID int
userID int
EXIFDate string
uploadDate string
latitude string
longitude string
mediaType int
relPath string
}
func init() {
// Initialize DB
}
func ItemsFromAlbum(userID int, albumID int) []MediaItem {
database, _ := sql.Open("sqlite3", "./db/imagini.db")
rows, _ := database.Query(`
SELECT
MediaItems.*
FROM
MediaAlbums
INNER JOIN MediaItems ON MediaAlbums.mediaID = MediaItems.mediaID
WHERE MediaAlbums.albumID = ? AND MediaItems.userID = ?`, albumID, userID)
var matchingMediaItems []MediaItem
var mi MediaItem
for rows.Next() {
err := rows.Scan(&mi.mediaID, &mi.userID, &mi.EXIFDate, &mi.uploadDate, &mi.latitude, &mi.longitude, &mi.mediaType, &mi.relPath)
if err != nil {
log.Fatal(err)
}
matchingMediaItems = append(matchingMediaItems, mi)
}
rows.Close()
return matchingMediaItems
}
func ItemsFromTags(userID int, tagID int) []MediaItem {
database, _ := sql.Open("sqlite3", "./db/imagini.db")
rows, _ := database.Query(`
SELECT
MediaItems.*
FROM
MediaTags
INNER JOIN MediaItems ON MediaTags.mediaID = MediaItems.mediaID
WHERE MediaTags.tagID = ? AND MediaItems.userID = ?`, tagID, userID)
var matchingMediaItems []MediaItem
var mi MediaItem
for rows.Next() {
err := rows.Scan(&mi.mediaID, &mi.userID, &mi.EXIFDate, &mi.uploadDate, &mi.latitude, &mi.longitude, &mi.mediaType, &mi.relPath)
if err != nil {
log.Fatal(err)
}
matchingMediaItems = append(matchingMediaItems, mi)
}
rows.Close()
return matchingMediaItems
}
func IndexMediaItems(newItems []MediaItem) {
database, _ := sql.Open("sqlite3", "./db/imagini.db")
statement, _ := database.Prepare(`
INSERT INTO MediaItems (
userID,
EXIFDate
uploadDate,
latitude,
longitude,
mediaType,
relPath
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
for _, s := range newItems {
statement.Exec(
&s.userID,
&s.EXIFDate,
&s.uploadDate,
&s.latitude,
&s.longitude,
&s.mediaType,
&s.relPath,
)
}
database.Close()
}