Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
joedb_logdump.cpp
Go to the documentation of this file.
2#include "joedb/Multiplexer.h"
11
12#include <iostream>
13#include <memory>
14#include <cstring>
15#include <optional>
16
17namespace joedb
18{
19 /////////////////////////////////////////////////////////////////////////////
20 static void dump
21 /////////////////////////////////////////////////////////////////////////////
22 (
23 Readonly_Journal &journal,
24 Writable &writable,
25 bool print_checkpoint
26 )
27 {
28 if (print_checkpoint)
29 journal.replay_with_checkpoint_comments(writable);
30 else
31 journal.replay_log(writable);
32 }
33
34 /////////////////////////////////////////////////////////////////////////////
35 static int logdump_main(int argc, char **argv)
36 /////////////////////////////////////////////////////////////////////////////
37 {
38 if (argc <= 1)
39 {
40 std::cerr << "usage: " << argv[0];
41 std::cerr << " [--sql] [--sqlite] [--raw] [--header] [--schema-only] [--ignore-errors] [--load] [--print-checkpoint] [--blob] <file.joedb>\n";
42 return 1;
43 }
44 else
45 {
46 int arg_index = 1;
47
48 #define OPTION(b, s)\
49 bool b = false;\
50 if (arg_index < argc && std::strcmp(argv[arg_index], s) == 0)\
51 {\
52 b = true;\
53 arg_index++;\
54 }
55
56 OPTION(sql, "--sql");
57 OPTION(sqlite, "--sqlite");
58 OPTION(raw, "--raw");
59 OPTION(header, "--header");
60 OPTION(schema_only, "--schema-only");
61 OPTION(ignore_errors, "--ignore-errors");
62 OPTION(load, "--load");
63 OPTION(print_checkpoint, "--print-checkpoint");
64 OPTION(blob, "--blob");
65
66 if (arg_index != argc - 1)
67 return logdump_main(1, argv);
68
69 File file(argv[arg_index], Open_Mode::read_existing);
70
71 if (header)
72 {
73 dump_header(std::cout, file);
74 std::cout << '\n';
75 about_joedb(std::cout);
76 }
77 else
78 {
79 std::optional<Readonly_Journal> journal;
80
81 try
82 {
83 journal.emplace
84 (
85 file,
86 ignore_errors ?
88 Readonly_Journal::Check::all
89 );
90 }
91 catch (const Exception &e)
92 {
93 if (!ignore_errors)
94 {
95 std::cout << "Error opening journal file: " << e.what() << '\n';
96 std::cout << "run with the --ignore-errors flag to skip this check.\n";
97 }
98 return 1;
99 }
100
101 std::unique_ptr<Writable> writable;
102
103 const Buffered_File *blob_reader = blob ? &file : nullptr;
104
105 if (sql)
106 writable.reset(new SQL_Dump_Writable(std::cout, blob_reader));
107 else if (sqlite)
108 writable.reset(new SQL_Dump_Writable(std::cout, blob_reader, false));
109 else if (raw)
110 writable.reset(new Raw_Dump_Writable(std::cout));
111 else
112 writable.reset(new Interpreter_Dump_Writable(std::cout, blob));
113
114 if (schema_only)
115 {
116 Selective_Writable selective_writable
117 (
118 *writable,
120 );
121 journal->replay_log(selective_writable);
122 }
123 else if (load)
124 {
125 Database db;
126 Multiplexer multiplexer{db, *writable};
127 dump(*journal, multiplexer, print_checkpoint);
128 }
129 else
130 dump(*journal, *writable, print_checkpoint);
131 }
132 }
133
134 return 0;
135 }
136}
137
138/////////////////////////////////////////////////////////////////////////////
139int main(int argc, char **argv)
140/////////////////////////////////////////////////////////////////////////////
141{
142 return joedb::main_exception_catcher(joedb::logdump_main, argc, argv);
143}
int main()
void dump_header(std::ostream &out, Buffered_File &file)
void about_joedb(std::ostream &out)
@ read_existing
fails if does not exist
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.
void dump(const Readable &db, Writable &writable, bool schema_only)
Definition dump.cpp:12
#define OPTION(b, s)
Definition Blob.h:7
JOEDB_FILE File
Definition File.h:25