Joedb 10.2.1
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 ////////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////////
40 (
41 const std::string &command,
42 std::istream &parameters,
43 std::istream &in,
44 std::ostream &out
45 )
46 {
48 (
49 command,
50 parameters,
51 in,
52 out
53 );
54
55 if (status == Status::done)
56 return status;
57 else if (command == "help") ///////////////////////////////////////////////
58 {
59 out << R"RRR(Data manipulation
60~~~~~~~~~~~~~~~~~
61 insert_into <table_name> <record_id>
62 delete_from <table_name> <record_id>
63 insert_vector <table_name> <record_id> <size>
64 delete_vector <table_name> <record_id> <size>
65 update <table_name> <record_id> <field_name> <value>
66 update_vector <table_name> <record_id> <field_name> <N> <v_1> ... <v_N>
67
68)RRR";
69
70 return Status::ok;
71 }
72 else if (command == "insert_into") ///////////////////////////////////////
73 {
74 const Table_Id table_id = parse_table(parameters, readable);
75 Record_Id record_id = Record_Id::null;
76 parameters >> record_id;
77
78 if (record_id.is_null())
79 record_id = readable.get_size(table_id);
80
81 writable.insert_into(table_id, record_id);
82
83 if (parameters.good())
84 {
85 for (const auto &[fid, fname]: readable.get_fields(table_id))
86 {
87 update_value(parameters, table_id, record_id, fid);
88 if (parameters.fail())
89 throw Exception("failed parsing value");
90 }
91 }
92 }
93 else if (command == "delete_from") ////////////////////////////////////////
94 {
95 const Table_Id table_id = parse_table(parameters, readable);
96 Record_Id record_id = Record_Id::null;
97 parameters >> record_id;
98
99 writable.delete_from(table_id, record_id);
100 }
101 else if (command == "insert_vector") /////////////////////////////////////
102 {
103 const Table_Id table_id = parse_table(parameters, readable);
104 Record_Id record_id = Record_Id::null;
105 size_t size = 0;
106 parameters >> record_id >> size;
107 writable.insert_vector(table_id, record_id, size);
108 }
109 else if (command == "delete_vector") /////////////////////////////////////
110 {
111 const Table_Id table_id = parse_table(parameters, readable);
112 Record_Id record_id = Record_Id::null;
113 size_t size = 0;
114 parameters >> record_id >> size;
115 writable.delete_vector(table_id, record_id, size);
116 }
117 else if (command == "update") ////////////////////////////////////////////
118 {
119 const Table_Id table_id = parse_table(parameters, readable);
120 Record_Id record_id = Record_Id::null;
121 parameters >> record_id;
122 std::string field_name;
123 parameters >> field_name;
124 const Field_Id field_id = readable.find_field(table_id, field_name);
125 update_value(parameters, table_id, record_id, field_id);
126 }
127 else if (command == "update_vector") /////////////////////////////////////
128 {
129 const Table_Id table_id = parse_table(parameters, readable);
130 Record_Id record_id = Record_Id::null;
131 parameters >> record_id;
132 std::string field_name;
133 parameters >> field_name;
134 const Field_Id field_id = readable.find_field(table_id, field_name);
135 size_t size = 0;
136 parameters >> size;
137
138 if (max_record_id.is_not_null() && size >= size_t(index_t(max_record_id)))
139 throw Exception("vector is too big");
140 else
141 {
142 switch(readable.get_field_type(table_id, field_id).get_type_id())
143 {
145 throw Exception("bad field");
146 break;
147
148 #define TYPE_MACRO(type, return_type, type_id, R, W)\
149 case Type::Type_Id::type_id:\
150 {\
151 std::vector<type> v(size);\
152 for (size_t i = 0; i < size; i++)\
153 v[i] = joedb::read_##type_id(parameters);\
154 writable.update_vector_##type_id(table_id, record_id, field_id, size, &v[0]);\
155 }\
156 break;
157 #include "joedb/TYPE_MACRO.h"
158 }
159 }
160 }
161 else
162 return Status::not_found;
163
164 return Status::done;
165 }
166}
Status process_command(const std::string &command, std::istream &parameters, std::istream &in, std::ostream &out) override
Status process_command(const std::string &command, std::istream &parameters, std::istream &in, std::ostream &out) override
constexpr bool is_null() const
Definition index_types.h:41
static const Record_Id null
Definition index_types.h:44
ptrdiff_t index_t
Definition index_types.h:18