Joedb 9.1.4
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/doc/source/tutorial/build/joedbc
7// Version: 9.1.4
8// joedbc compilation time: Apr 15 2025 14:46:29
9// Generation of this file: 2025-04-15 12:46:31 GMT
10//
11/////////////////////////////////////////////////////////////////////////////
12#include "Writable_Database.h"
13#include "joedb/Writable.h"
15
16#include <ctime>
17
18namespace settings
19{
20 ////////////////////////////////////////////////////////////////////////////
21 void Writable_Database::write_comment(const std::string &comment)
22 ////////////////////////////////////////////////////////////////////////////
23 {
24 journal.comment(comment);
25 }
26
27 ////////////////////////////////////////////////////////////////////////////
29 ////////////////////////////////////////////////////////////////////////////
30 {
31 journal.timestamp(std::time(nullptr));
32 }
33
34 ////////////////////////////////////////////////////////////////////////////
35 void Writable_Database::write_timestamp(int64_t timestamp)
36 ////////////////////////////////////////////////////////////////////////////
37 {
38 journal.timestamp(timestamp);
39 }
40
41 ////////////////////////////////////////////////////////////////////////////
43 ////////////////////////////////////////////////////////////////////////////
44 {
45 journal.valid_data();
46 }
47
48 ////////////////////////////////////////////////////////////////////////////
49 void Writable_Database::play_journal()
50 ////////////////////////////////////////////////////////////////////////////
51 {
52 max_record_id = size_t(journal.get_checkpoint_position());
53 ready_to_write = false;
54 journal.play_until_checkpoint(*this);
55 ready_to_write = true;
56 max_record_id = 0;
57 }
58
59 ////////////////////////////////////////////////////////////////////////////
60 void Writable_Database::auto_upgrade()
61 ////////////////////////////////////////////////////////////////////////////
62 {
63 const size_t file_schema_size = size_t(schema_file.get_size());
64
65 if (file_schema_size < detail::schema_string_size)
66 {
67 journal.comment("Automatic schema upgrade");
68
69 joedb::Readonly_Memory_File schema_file(detail::schema_string, detail::schema_string_size);
71
72 schema_journal.set_position(int64_t(file_schema_size));
73 schema_journal.play_until(journal, detail::schema_string_size);
74
75 schema_journal.set_position(int64_t(file_schema_size));
76 upgrading_schema = true;
77 schema_journal.play_until(*this, detail::schema_string_size);
78 upgrading_schema = false;
79
80 journal.valid_data();
81 }
82 }
83
84 ////////////////////////////////////////////////////////////////////////////
85 Writable_Database::Writable_Database
86 ////////////////////////////////////////////////////////////////////////////
87 (
89 bool perform_initialization,
91 joedb::Commit_Level commit_level
92 ):
93 journal(file, check, commit_level)
94 {
95 journal.rewind();
96
97 if (perform_initialization)
98 initialize();
99 }
100
101 ////////////////////////////////////////////////////////////////////////////
102 Writable_Database::Writable_Database
103 ////////////////////////////////////////////////////////////////////////////
104 (
107 joedb::Commit_Level commit_level
108 ):
109 Writable_Database(file, true, check, commit_level)
110 {
111 }
112
113 ////////////////////////////////////////////////////////////////////////////
114 void Writable_Database::check_single_row()
115 ////////////////////////////////////////////////////////////////////////////
116 {
117 {
118 const auto table = get_settings_table();
119 if (table.first() != the_settings() || table.last() != the_settings())
120 throw joedb::Exception("Single-row constraint failure for table settings");
121 }
122 }
123
124 ////////////////////////////////////////////////////////////////////////////
125 void Writable_Database::create_table(const std::string &name)
126 ////////////////////////////////////////////////////////////////////////////
127 {
129
131 {
132 if (current_table_id == Table_Id{1})
133 new_settings();
134 }
135 }
136
137 ////////////////////////////////////////////////////////////////////////////
138 void Writable_Database::add_field
139 ////////////////////////////////////////////////////////////////////////////
140 (
141 Table_Id table_id,
142 const std::string &name,
143 joedb::Type type
144 )
145 {
146 Database::add_field(table_id, name, type);
147
148 if (table_id == Table_Id{1})
149 {
150 const auto field_id = ++storage_of_settings.current_field_id;
151 if (upgrading_schema)
152 {
153 if (field_id == Field_Id{1})
154 {
155 for (const auto record: get_settings_table())
156 set_dark_mode(record, true);
157 }
158 if (field_id == Field_Id{2})
159 {
160 for (const auto record: get_settings_table())
161 set_host(record, "www.kayufu.com");
162 }
163 if (field_id == Field_Id{3})
164 {
165 for (const auto record: get_settings_table())
166 set_user(record, "joe");
167 }
168 }
169 }
170 }
171}
int64_t get_size() const override
Definition Memory_File.h:24
void play_until(Writable &writable, int64_t end)
void set_position(int64_t position)
int64_t get_checkpoint_position() const
void play_until_checkpoint(Writable &writable)
void timestamp(int64_t timestamp) final
void comment(const std::string &comment) final
size_t max_record_id
Definition Database.h:88
static constexpr id_of_settings the_settings()
Definition Database.h:515
void create_table(const std::string &name) override
Definition Database.h:410
joedb::Writable_Journal schema_journal
Definition Database.h:382
void add_field(Table_Id table_id, const std::string &name, joedb::Type type) override
Definition Database.h:434
void timestamp(int64_t timestamp) override
Definition Database.h:377
joedb::Memory_File schema_file
Definition Database.h:381
Table_Id current_table_id
Definition Database.h:89
void comment(const std::string &comment) override
Definition Database.h:376
container_of_settings get_settings_table() const
Definition Database.h:582
A writable Database constructed from a writable joedb::Buffered_File.
void write_comment(const std::string &comment)
Commit_Level
Definition Writable.h:13
Automatically generated by joedbc.
Definition Client.h:19