summaryrefslogtreecommitdiff
path: root/product/database/ObjectDatabase.cs
blob: 9ee6c1b362a751be2a5b39affbaf607b0b4c2057 (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Gorilla.Commons.Infrastructure.FileSystem;
using gorilla.commons.utility;
using momoney.database.transactions;
using File = Gorilla.Commons.Infrastructure.FileSystem.File;

namespace momoney.database
{
    public class ObjectDatabase : IDatabase, IDatabaseConfiguration
    {
        readonly IConnectionFactory factory;
        File path;

        public ObjectDatabase(IConnectionFactory factory)
        {
            this.factory = factory;
            path = new ApplicationFile(Path.GetTempFileName());
        }

        public IEnumerable<T> fetch_all<T>() where T : Identifiable<Guid>
        {
            using (var connection = factory.open_connection_to(path_to_database()))
            {
                return connection.query<T>().ToList();
            }
        }

        public void apply(DatabaseCommand command)
        {
            using (var connection = factory.open_connection_to(path_to_database()))
            {
                command.run(connection);
                connection.commit();
            }
        }

        public void open(File file)
        {
            path = new ApplicationFile(Path.GetTempFileName());
            file.copy_to(path.path);
        }

        public void copy_to(string new_path)
        {
            path.copy_to(new_path);
        }

        public void close(string name)
        {
            path.delete();
            path = new ApplicationFile(Path.GetTempFileName());
        }

        File path_to_database()
        {
            return path;
        }
    }
}