Joedb 10.0.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Writable_Database.cpp
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//
3// This code was automatically generated by the joedb compiler
4// https://www.joedb.org/
5//
6// Path to compiler: /home/rcoulom/repos/joedb/compcmake/gcc_debug/joedbc
7// Version: 10.0.1
8// joedbc compilation time: Jun 13 2025 15:26:57
9// Generation of this file: 2025-06-13 13:27:16 GMT
10//
11/////////////////////////////////////////////////////////////////////////////
12#include "Writable_Database.h"
14
15#include <ctime>
16
17namespace settings
18{
19 ////////////////////////////////////////////////////////////////////////////
20 void Writable_Database::write_comment(const std::string &comment)
21 ////////////////////////////////////////////////////////////////////////////
22 {
23 journal.comment(comment);
24 }
25
26 ////////////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////////////
29 {
30 journal.timestamp(std::time(nullptr));
31 }
32
33 ////////////////////////////////////////////////////////////////////////////
34 void Writable_Database::write_timestamp(int64_t timestamp)
35 ////////////////////////////////////////////////////////////////////////////
36 {
37 journal.timestamp(timestamp);
38 }
39
40 ////////////////////////////////////////////////////////////////////////////
42 ////////////////////////////////////////////////////////////////////////////
43 {
44 journal.valid_data();
45 }
46
47 ////////////////////////////////////////////////////////////////////////////
48 void Writable_Database::play_journal()
49 ////////////////////////////////////////////////////////////////////////////
50 {
51 max_record_id = size_t(journal.get_checkpoint());
52 ready_to_write = false;
53 journal.play_until_checkpoint(*this);
54 ready_to_write = true;
55 max_record_id = 0;
56 }
57
58 ////////////////////////////////////////////////////////////////////////////
59 void Writable_Database::auto_upgrade()
60 ////////////////////////////////////////////////////////////////////////////
61 {
62 const size_t file_schema_size = size_t(schema_file.get_size());
63
64 if (file_schema_size < detail::schema_string_size)
65 {
66 journal.comment("Automatic schema upgrade");
67
68 joedb::Readonly_Memory_File schema_file(detail::schema_string, detail::schema_string_size);
70
71 schema_journal.skip_directly_to(int64_t(file_schema_size));
72 schema_journal.raw_play_until(journal, detail::schema_string_size);
73
74 schema_journal.skip_directly_to(int64_t(file_schema_size));
75 upgrading_schema = true;
76 schema_journal.raw_play_until(*this, detail::schema_string_size);
77 upgrading_schema = false;
78
79 journal.valid_data();
80 journal.soft_checkpoint();
81 }
82 }
83
84 ////////////////////////////////////////////////////////////////////////////
85 void Writable_Database::initialize()
86 ////////////////////////////////////////////////////////////////////////////
87 {
88 play_journal();
90 auto_upgrade();
91 check_single_row();
92 }
93
94 ////////////////////////////////////////////////////////////////////////////
95 Writable_Database::Writable_Database
96 ////////////////////////////////////////////////////////////////////////////
97 (
99 joedb::Recovery recovery,
100 bool perform_initialization
101 ):
102 journal(joedb::Journal_Construction_Lock(file, recovery))
103 {
104 journal.rewind();
105
106 if (perform_initialization)
107 initialize();
108 }
109
110 ////////////////////////////////////////////////////////////////////////////
111 Writable_Database::Writable_Database(joedb::Buffered_File &file):
112 ////////////////////////////////////////////////////////////////////////////
113 Writable_Database(file, joedb::Recovery::none, true)
114 {
115 }
116
117 ////////////////////////////////////////////////////////////////////////////
118 Writable_Database::Writable_Database
119 ////////////////////////////////////////////////////////////////////////////
120 (
122 joedb::Recovery recovery
123 ):
124 Writable_Database(file, recovery, true)
125 {
126 }
127
128 ////////////////////////////////////////////////////////////////////////////
129 void Writable_Database::check_single_row()
130 ////////////////////////////////////////////////////////////////////////////
131 {
132 {
133 const auto table = get_settings_table();
134 if (table.first() != the_settings() || table.last() != the_settings())
135 throw joedb::Exception("Single-row constraint failure for table settings");
136 }
137 }
138
139 ////////////////////////////////////////////////////////////////////////////
140 void Writable_Database::create_table(const std::string &name)
141 ////////////////////////////////////////////////////////////////////////////
142 {
144
146 {
147 if (current_table_id == Table_Id{1})
148 new_settings();
149 }
150 }
151
152 ////////////////////////////////////////////////////////////////////////////
153 void Writable_Database::add_field
154 ////////////////////////////////////////////////////////////////////////////
155 (
156 Table_Id table_id,
157 const std::string &name,
158 joedb::Type type
159 )
160 {
161 Database_Writable::add_field(table_id, name, type);
162
163 if (table_id == Table_Id{1})
164 {
165 const auto field_id = ++storage_of_settings.current_field_id;
166 if (upgrading_schema)
167 {
168 if (field_id == Field_Id{1})
169 {
170 for (const auto record: get_settings_table())
171 set_dark_mode(record, true);
172 }
173 if (field_id == Field_Id{2})
174 {
175 for (const auto record: get_settings_table())
176 set_host(record, "www.kayufu.com");
177 }
178 if (field_id == Field_Id{3})
179 {
180 for (const auto record: get_settings_table())
181 set_user(record, "joe");
182 }
183 }
184 }
185 }
186}
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
Definition Memory_File.h:24
void raw_play_until(Writable &writable, int64_t end)
void play_until_checkpoint(Writable &writable)
int64_t get_checkpoint() const
void skip_directly_to(int64_t position)
void timestamp(int64_t timestamp) override
void comment(const std::string &comment) override
void comment(const std::string &comment) override
void add_field(Table_Id table_id, const std::string &name, joedb::Type type) override
joedb::Writable_Journal schema_journal
void timestamp(int64_t timestamp) override
void create_table(const std::string &name) override
static constexpr id_of_settings the_settings()
Definition Database.h:201
container_of_settings get_settings_table() const
Definition Database.h:268
A Database that contains a joedb::Writable_Journal and keeps them in sync.
void write_comment(const std::string &comment)
Definition Blob.h:7
Automatically generated by joedbc.
Definition Client.h:19