-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
120 lines (106 loc) · 3.12 KB
/
main.cpp
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
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include "btree/BTree.h"
#include "Lexer.h"
#include "filesystem/Filesystem.h"
#include "filesystem/BasicFilesystem.h"
#include "frsql.h"
#ifdef BUILD_TESTS
#include <gtest/gtest.h>
#include <chrono>
#endif
#include <ranges>
int main(int argc, char **argv)
{
#ifdef BUILD_TESTS
if(argc > 1 && strstr(argv[1], "test") != nullptr)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
#endif
std::unique_ptr<FilesystemBacking> backing;
if(argc > 1)
{
const std::string filepath(argv[1]);
backing = std::make_unique<DiskBacking>();
if(!backing->open(filepath, false))
{
if(!backing->open(filepath, true))
{
std::cout << "Failed to open/create database file '" << filepath << "'\n";
return EXIT_FAILURE;
}
else
{
std::cout << "Created new database file '" << filepath << "'\n";
}
BasicFilesystem::Format(backing);
}
else
{
std::cout << "Opened existing database file '" << filepath << "'\n";
}
}
else
{
std::cout << "No database filepath provided. Using an in-memory backing.\n";
backing = std::make_unique<MemoryBacking>();
BasicFilesystem::Format(backing);
}
Frsql frsql(std::make_unique<BasicFilesystem>(std::move(backing)));
// auto begin = std::chrono::steady_clock::now();
// for(size_t a = 0; a < 9000000; a++)
// {
// frsql.exec("SELECT 10 IN (5, 7, 8, 10, 11, 15, 17)");
// }
// auto end = std::chrono::steady_clock::now();
// std::cout << "Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "ms" << std::endl;
// return 0;
while(true)
{
std::string query;
std::cout << "Query: ";
std::getline(std::cin, query);
if(query.empty())
{
continue;
}
#ifdef BUILD_TESTS
if (query == "test")
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
#endif
if(query == "exit")
{
break;
}
try
{
frsql.exec(query, [&](const row_t& row) {
for (auto& r : row)
{
if (r.type == Variable::Type::INT)
{
std::cout << r.store.int64 << ", ";
}
else if (r.type == Variable::Type::STRING)
{
std::cout << std::string_view(r.store.str, r.store.len) << ", ";
}
}
if (!row.empty())
{
std::cout << "\n";
}
});
}
catch (const DatabaseError &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
}
return 0;
}