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

96 lines
2.5 KiB
Go
Raw Normal View History

2021-01-04 05:16:58 +00:00
package db
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type MediaItem struct {
2021-01-04 06:55:41 +00:00
mediaID int
userID int
EXIFDate string
uploadDate string
latitude string
longitude string
mediaType int
relPath string
2021-01-04 05:16:58 +00:00
}
func init() {
// Initialize DB
}
func ItemsFromAlbum(userID int, albumID int) []MediaItem {
2021-01-04 06:55:41 +00:00
database, _ := sql.Open("sqlite3", "./db/imagini.db")
rows, _ := database.Query(`
2021-01-04 05:16:58 +00:00
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 {
2021-01-04 06:55:41 +00:00
database, _ := sql.Open("sqlite3", "./db/imagini.db")
rows, _ := database.Query(`
2021-01-04 05:16:58 +00:00
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) {
2021-01-04 06:55:41 +00:00
database, _ := sql.Open("sqlite3", "./db/imagini.db")
2021-01-04 05:16:58 +00:00
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()
}