main
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using Gorilla.Commons.Infrastructure.FileSystem;
6using gorilla.commons.utility;
7using momoney.database.transactions;
8using File = Gorilla.Commons.Infrastructure.FileSystem.File;
9
10namespace momoney.database
11{
12 public class ObjectDatabase : IDatabase, IDatabaseConfiguration
13 {
14 readonly IConnectionFactory factory;
15 File path;
16
17 public ObjectDatabase(IConnectionFactory factory)
18 {
19 this.factory = factory;
20 path = new ApplicationFile(Path.GetTempFileName());
21 }
22
23 public IEnumerable<T> fetch_all<T>() where T : Identifiable<Guid>
24 {
25 using (var connection = factory.open_connection_to(path_to_database()))
26 {
27 return connection.query<T>().ToList();
28 }
29 }
30
31 public void apply(DatabaseCommand command)
32 {
33 using (var connection = factory.open_connection_to(path_to_database()))
34 {
35 command.run(connection);
36 connection.commit();
37 }
38 }
39
40 public void open(File file)
41 {
42 path = new ApplicationFile(Path.GetTempFileName());
43 file.copy_to(path.path);
44 }
45
46 public void copy_to(string new_path)
47 {
48 path.copy_to(new_path);
49 }
50
51 public void close(string name)
52 {
53 path.delete();
54 path = new ApplicationFile(Path.GetTempFileName());
55 }
56
57 File path_to_database()
58 {
59 return path;
60 }
61 }
62}