Joedb 9.5.0
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: 9.5.0
8// joedbc compilation time: May 5 2025 19:51:43
9// Generation of this file: 2025-05-05 17:51:47 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());
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.skip_directly_to(int64_t(file_schema_size));
73 schema_journal.append_until(journal, detail::schema_string_size);
74
75 schema_journal.skip_directly_to(int64_t(file_schema_size));
76 upgrading_schema = true;
77 schema_journal.append_until(*this, detail::schema_string_size);
78 upgrading_schema = false;
79
80 journal.valid_data();
81 journal.soft_checkpoint();
82 }
83 }
84
85 ////////////////////////////////////////////////////////////////////////////
86 Writable_Database::Writable_Database
87 ////////////////////////////////////////////////////////////////////////////
88 (
90 bool perform_initialization
91 ):
92 journal(file)
93 {
94 journal.rewind();
95
96 if (perform_initialization)
97 initialize();
98 }
99
100 ////////////////////////////////////////////////////////////////////////////
101 Writable_Database::Writable_Database(joedb::Buffered_File &file):
102 ////////////////////////////////////////////////////////////////////////////
103 Writable_Database(file, true)
104 {
105 }
106
107 ////////////////////////////////////////////////////////////////////////////
108 void Writable_Database::check_single_row()
109 ////////////////////////////////////////////////////////////////////////////
110 {
111 {
112 const auto table = get_settings_table();
113 if (table.first() != the_settings() || table.last() != the_settings())
114 throw joedb::Exception("Single-row constraint failure for table settings");
115 }
116 }
117
118 ////////////////////////////////////////////////////////////////////////////
119 void Writable_Database::create_table(const std::string &name)
120 ////////////////////////////////////////////////////////////////////////////
121 {
123
125 {
126 if (current_table_id == Table_Id{1})
127 new_settings();
128 }
129 }
130
131 ////////////////////////////////////////////////////////////////////////////
132 void Writable_Database::add_field
133 ////////////////////////////////////////////////////////////////////////////
134 (
135 Table_Id table_id,
136 const std::string &name,
137 joedb::Type type
138 )
139 {
140 Database::add_field(table_id, name, type);
141
142 if (table_id == Table_Id{1})
143 {
144 const auto field_id = ++storage_of_settings.current_field_id;
145 if (upgrading_schema)
146 {
147 if (field_id == Field_Id{1})
148 {
149 for (const auto record: get_settings_table())
150 set_dark_mode(record, true);
151 }
152 if (field_id == Field_Id{2})
153 {
154 for (const auto record: get_settings_table())
155 set_host(record, "www.kayufu.com");
156 }
157 if (field_id == Field_Id{3})
158 {
159 for (const auto record: get_settings_table())
160 set_user(record, "joe");
161 }
162 }
163 }
164 }
165}
int64_t get_size() const override
Get the size of the file, or -1 if it is unknown.
Definition Memory_File.h:24
void append_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) final
void comment(const std::string &comment) final
void soft_checkpoint()
Definition Writable.h:21
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)
Automatically generated by joedbc.
Definition Client.h:19