Joedb 10.4.3
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Data_Manipulation_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
7#include <vector>
8
9namespace joedb
10{
11 ////////////////////////////////////////////////////////////////////////////
12 void Data_Manipulation_Command_Processor::update_value
13 ////////////////////////////////////////////////////////////////////////////
14 (
15 std::istream &in,
16 Table_Id table_id,
17 Record_Id record_id,
18 Field_Id field_id
19 )
20 {
21 switch(readable.get_field_type(table_id, field_id).get_type_id())
22 {
24 throw Exception("bad field");
25
26 #define TYPE_MACRO(type, return_type, type_id, read_method, write_method)\
27 case Type::Type_Id::type_id:\
28 {\
29 const type value = joedb::read_##type_id(in);\
30 writable.update_##type_id(table_id, record_id, field_id, value);\
31 }\
32 break;
33 #include "joedb/TYPE_MACRO.h"
34 }
35 }
36
37 ////////////////////////////////////////////////////////////////////////////
38 void Data_Manipulation_Command_Processor::insert_into
39 ////////////////////////////////////////////////////////////////////////////
40 (
41 std::istream &parameters,
42 bool append
43 )
44 {
45 const Table_Id table_id = parse_table(parameters, readable);
46 Record_Id record_id = Record_Id::null;
47
48 if (!append)
49 parameters >> record_id;
50
51 if (record_id.is_null())
52 record_id = readable.get_size(table_id);
53
54 writable.insert_into(table_id, record_id);
55
56 if (parameters.good())
57 {
58 for (const auto &[fid, fname]: readable.get_fields(table_id))
59 {
60 update_value(parameters, table_id, record_id, fid);
61 if (parameters.fail())
62 throw Exception("failed parsing value");
63 }
64 }
65 }
66
67 ////////////////////////////////////////////////////////////////////////////
69 ////////////////////////////////////////////////////////////////////////////
70 (
71 const std::string_view command,
72 std::istream &parameters,
73 std::istream &in,
74 std::ostream &out
75 )
76 {
78 (
79 command,
80 parameters,
81 in,
82 out
83 );
84
85 if (status == Status::done)
86 return status;
87 else if (command == "help") ///////////////////////////////////////////////
88 {
89 out << R"RRR(Data manipulation
90~~~~~~~~~~~~~~~~~
91 append_into <table_name> [<field_value>*]
92 insert_into <table_name> <record_id> [<field_value>*]
93 delete_from <table_name> <record_id>
94 insert_vector <table_name> <record_id> <size>
95 delete_vector <table_name> <record_id> <size>
96 update <table_name> <record_id> <field_name> <value>
97 update_vector <table_name> <record_id> <field_name> <N> <v_1> ... <v_N>
98
99)RRR";
100
101 return Status::ok;
102 }
103 else if (command == "append_into") ///////////////////////////////////////
104 {
105 insert_into(parameters, true);
106 }
107 else if (command == "insert_into") ///////////////////////////////////////
108 {
109 insert_into(parameters, false);
110 }
111 else if (command == "delete_from") ////////////////////////////////////////
112 {
113 const Table_Id table_id = parse_table(parameters, readable);
114 Record_Id record_id = Record_Id::null;
115 parameters >> record_id;
116
117 writable.delete_from(table_id, record_id);
118 }
119 else if (command == "insert_vector") /////////////////////////////////////
120 {
121 const Table_Id table_id = parse_table(parameters, readable);
122 Record_Id record_id = Record_Id::null;
123 size_t size = 0;
124 parameters >> record_id >> size;
125 writable.insert_vector(table_id, record_id, size);
126 }
127 else if (command == "delete_vector") /////////////////////////////////////
128 {
129 const Table_Id table_id = parse_table(parameters, readable);
130 Record_Id record_id = Record_Id::null;
131 size_t size = 0;
132 parameters >> record_id >> size;
133 writable.delete_vector(table_id, record_id, size);
134 }
135 else if (command == "update") ////////////////////////////////////////////
136 {
137 const Table_Id table_id = parse_table(parameters, readable);
138 Record_Id record_id = Record_Id::null;
139 parameters >> record_id;
140 std::string field_name;
141 parameters >> field_name;
142 const Field_Id field_id = readable.find_field(table_id, field_name);
143 update_value(parameters, table_id, record_id, field_id);
144 }
145 else if (command == "update_vector") /////////////////////////////////////
146 {
147 const Table_Id table_id = parse_table(parameters, readable);
148 Record_Id record_id = Record_Id::null;
149 parameters >> record_id;
150 std::string field_name;
151 parameters >> field_name;
152 const Field_Id field_id = readable.find_field(table_id, field_name);
153 size_t size = 0;
154 parameters >> size;
155
156 if (max_record_id.is_not_null() && size >= size_t(index_t(max_record_id)))
157 throw Exception("vector is too big");
158 else
159 {
160 switch(readable.get_field_type(table_id, field_id).get_type_id())
161 {
163 throw Exception("bad field");
164 break;
165
166 #define TYPE_MACRO(type, return_type, type_id, R, W)\
167 case Type::Type_Id::type_id:\
168 {\
169 std::vector<type> v(size);\
170 for (size_t i = 0; i < size; i++)\
171 v[i] = joedb::read_##type_id(parameters);\
172 writable.update_vector_##type_id(table_id, record_id, field_id, size, &v[0]);\
173 }\
174 break;
175 #include "joedb/TYPE_MACRO.h"
176 }
177 }
178 }
179 else
180 return Status::not_found;
181
182 return Status::done;
183 }
184}
Status process_command(std::string_view command, std::istream &parameters, std::istream &in, std::ostream &out) override
Status process_command(std::string_view command, std::istream &parameters, std::istream &in, std::ostream &out) override
static const Record_Id null
Definition index_types.h:44
ptrdiff_t index_t
Definition index_types.h:18