aboutsummaryrefslogtreecommitdiff
path: root/src/PhotoStore.cs
blob: e0b292bddbbd7606768f0975eaa92532ebb05dde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Sqlite;

namespace iPhotoExtractor
{
    class PhotoStore
    {
        private readonly string _sqliteConnectionString;

        public PhotoStore(string pathToDbFile)
        {
            _sqliteConnectionString = $"Data Source={pathToDbFile};";
        }

        public List<Photo> GetAllPhotos()
        {
            var photos = new Dictionary<int, Photo>();

            var query =
@"select
    spi.primaryKey,
    spi.caption,
    se.name,
    sfi.relativePath
from SqPhotoInfo spi
left outer join SqEvent se on se.primaryKey = spi.event
left outer join SqFileImage sfim on sfim.photoKey = spi.primaryKey
left outer join SqFileInfo sfi on sfi.primaryKey = sfim.sqFileInfo
where lower(sfi.relativePath) like 'modified%' or lower(sfi.relativePath) like 'originals%'
";

            using (var connection = new SqliteConnection(_sqliteConnectionString))
            {
                connection.Open();

                using (var command = new SqliteCommand(query, connection))
                using (SqliteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var id = Convert.ToInt32(reader["primaryKey"]);
                        var photoPath = new PhotoPath(reader["relativePath"].ToString());

                        if (!photos.ContainsKey(id))
                        {
                            var caption = reader["caption"].ToString();
                            var albumName = reader["name"].ToString();
                            var photo = new Photo(id, caption, albumName);

                            photos.Add(id, photo);
                        }

                        photos[id].AddPath(photoPath);
                    }
                }
            }

            return photos.Values.ToList();
        }
    }
}