Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Readable_Writable_Command_Processor.cpp
Go to the documentation of this file.
2#include "joedb/ui/type_io.h"
3#include "joedb/Readable.h"
4#include "joedb/Writable.h"
6
7namespace joedb
8{
9 ////////////////////////////////////////////////////////////////////////////
10 Type Readable_Writable_Command_Processor::parse_type
11 ////////////////////////////////////////////////////////////////////////////
12 (
13 std::istream &in,
14 std::ostream &out
15 ) const
16 {
17 std::string type_name;
18 in >> type_name;
19
20 if (type_name == "references")
21 {
22 std::string table_name;
23 in >> table_name;
24 const Table_Id table_id = readable.find_table(table_name);
25 if (table_id != Table_Id(0))
26 return Type::reference(table_id);
27 }
28
29 #define TYPE_MACRO(type, return_type, type_id, read, write)\
30 if (type_name == #type_id)\
31 return Type::type_id();
32 #define TYPE_MACRO_NO_REFERENCE
33 #include "joedb/TYPE_MACRO.h"
34
35 throw Exception("unknown type");
36 }
37
38 ////////////////////////////////////////////////////////////////////////////
39 Command_Processor::Status Readable_Writable_Command_Processor::process_command
40 ////////////////////////////////////////////////////////////////////////////
41 (
42 const std::string &command,
43 std::istream &parameters,
44 std::istream &in,
45 std::ostream &out
46 )
47 {
49 (
50 command,
51 parameters,
52 in,
53 out
54 );
55
56 if (status == Status::done)
57 return status;
58 else if (command == "help") ///////////////////////////////////////////////
59 {
60 out << R"RRR(Data definition
61~~~~~~~~~~~~~~~
62 create_table <table_name>
63 drop_table <table_name>
64 rename_table <old_table_name> <new_table_name>
65 add_field <table_name> <field_name> <type> [default <value>]
66 drop_field <table_name> <field_name>
67 rename_field <table_name> <old_field_name> <new_field_name>
68 custom <custom_name>
69
70 <type> may be:
71 string,
72 blob,
73 int8, int16, int32, int64,
74 float32, float64,
75 boolean,
76 references <table_name>
77
78)RRR";
79
80 return Status::ok;
81 }
82 else if (command == "create_table") ///////////////////////////////////////
83 {
84 std::string table_name;
85 parameters >> table_name;
86 writable.create_table(table_name);
87 }
88 else if (command == "drop_table") /////////////////////////////////////////
89 {
90 const Table_Id table_id = parse_table(parameters, readable);
91 writable.drop_table(table_id);
92 }
93 else if (command == "rename_table") ///////////////////////////////////////
94 {
95 const Table_Id table_id = parse_table(parameters, readable);
96 std::string new_name;
97 parameters >> new_name;
98 writable.rename_table(table_id, new_name);
99 }
100 else if (command == "add_field") //////////////////////////////////////////
101 {
102 const Table_Id table_id = parse_table(parameters, readable);
103 std::string field_name;
104 parameters >> field_name;
105 const Type type = parse_type(parameters, out);
106 if (type.get_type_id() != Type::Type_Id::null)
107 {
108 writable.add_field(table_id, field_name, type);
109
110 std::string next_word;
111 parameters >> next_word;
112
113 if (next_word == "=")
114 {
115 const Field_Id field_id =
116 readable.get_fields(table_id).rbegin()->first;
117
118 const Record_Id last_record_id =
119 readable.get_last_record_id(table_id);\
120
121 switch(type.get_type_id())
122 {
123 case Type::Type_Id::null: break;
124 #define TYPE_MACRO(type, return_type, type_id, read_method, write_method)\
125 case Type::Type_Id::type_id:\
126 {\
127 const type value = joedb::read_##type_id(parameters);\
128 for (Record_Id record_id = Record_Id(1); record_id <= last_record_id; ++record_id)\
129 if (readable.is_used(table_id, record_id))\
130 writable.update_##type_id(table_id, record_id, field_id, value);\
131 }\
132 break;
133 #include "joedb/TYPE_MACRO.h"
134 }
135 }
136 }
137 }
138 else if (command == "drop_field") ////////////////////////////////////////
139 {
140 const Table_Id table_id = parse_table(parameters, readable);
141 std::string field_name;
142 parameters >> field_name;
143 const Field_Id field_id = readable.find_field(table_id, field_name);
144 writable.drop_field(table_id, field_id);
145 }
146 else if (command == "rename_field") //////////////////////////////////////
147 {
148 const Table_Id table_id = parse_table(parameters, readable);
149 std::string field_name;
150 parameters >> field_name;
151 const Field_Id field_id = readable.find_field(table_id, field_name);
152 std::string new_field_name;
153 parameters >> new_field_name;
154 writable.rename_field(table_id, field_id, new_field_name);
155 }
156 else if (command == "custom") ////////////////////////////////////////////
157 {
158 std::string name;
159 parameters >> name;
160 writable.custom(name);
161 }
162 else
163 return Status::not_found;
164
165 return Status::done;
166 }
167}
Status process_command(const std::string &command, std::istream &parameters, std::istream &in, std::ostream &out) override
static Type reference(Table_Id table_id)
Definition Type.h:52
Definition Blob.h:7