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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*
This file is part of libdjinterop.
libdjinterop is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libdjinterop is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with libdjinterop. If not, see <http://www.gnu.org/licenses/>.
*/
#include "el_storage.hpp"
#include <djinterop/database.hpp>
#include <djinterop/exceptions.hpp>
#include "../util.hpp"
#include "schema/schema.hpp"
namespace djinterop::enginelibrary
{
namespace
{
sqlite::database make_attached_db(const std::string& directory, bool must_exist)
{
if (!dir_exists(directory))
{
if (must_exist)
{
throw database_not_found{directory};
}
else
{
// Note: only creates leaf directory, not entire tree.
create_dir(directory);
}
}
sqlite::database db{":memory:"};
db << "ATTACH ? as 'music'" << (directory + "/m.db");
db << "ATTACH ? as 'perfdata'" << (directory + "/p.db");
return db;
}
semantic_version get_version(sqlite::database& db)
{
// Check that the `Information` table has been created.
std::string sql =
"SELECT SUM(rows) FROM ("
" SELECT COUNT(*) AS rows "
" FROM music.sqlite_master "
" WHERE name = 'Information' "
" UNION ALL "
" SELECT COUNT(*) AS rows "
" FROM perfdata.sqlite_master "
" WHERE name = 'Information' "
")";
int32_t table_count;
db << sql >> table_count;
if (table_count != 2)
{
throw database_inconsistency{
"Did not find an `Information` table for both the music and "
"performance databases"};
}
semantic_version music_version;
semantic_version perfdata_version;
db << "SELECT schemaVersionMajor, schemaVersionMinor, "
"schemaVersionPatch FROM music.Information" >>
std::tie(music_version.maj, music_version.min, music_version.pat);
db << "SELECT schemaVersionMajor, schemaVersionMinor, "
"schemaVersionPatch FROM music.Information" >>
std::tie(
perfdata_version.maj, perfdata_version.min, perfdata_version.pat);
if (music_version != perfdata_version)
{
throw database_inconsistency{
"The stated schema versions do not match between the music and "
"performance data databases!"};
}
return music_version;
}
} // anonymous namespace
el_storage::el_storage(const std::string& directory) :
directory{directory}, db{make_attached_db(directory, true)},
version{get_version(db)},
schema_creator_validator{schema::make_schema_creator_validator(version)}
{
}
el_storage::el_storage(const std::string& directory, semantic_version version) :
directory{directory}, db{make_attached_db(directory, false)},
version{version}, schema_creator_validator{
schema::make_schema_creator_validator(version)}
{
// Create the desired schema on the new database.
schema_creator_validator->create(db);
}
} // namespace djinterop::enginelibrary
|