Joedb 10.0.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 if (parameters.good())
83 {
84 for (const auto &[fid, fname]: readable.get_fields(table_id))
85 {
86 update_value(parameters, table_id, record_id, fid);
87 if (parameters.fail())
88 throw Exception("failed parsing value");
89 }
90 }
91 }
92 else if (command == "delete_from") ////////////////////////////////////////
93 {
94 const Table_Id table_id = parse_table(parameters, readable);
95 Record_Id record_id = Record_Id::null;
96 parameters >> record_id;
97
98 writable.delete_from(table_id, record_id);
99 }
100 else if (command == "insert_vector") /////////////////////////////////////
101 {
102 const Table_Id table_id = parse_table(parameters, readable);
103 Record_Id record_id = Record_Id::null;
104 size_t size = 0;
105 parameters >> record_id >> size;
106 writable.insert_vector(table_id, record_id, size);
107 }
108 else if (command == "delete_vector") /////////////////////////////////////
109 {
110 const Table_Id table_id = parse_table(parameters, readable);
111 Record_Id record_id = Record_Id::null;
112 size_t size = 0;
113 parameters >> record_id >> size;
114 writable.delete_vector(table_id, record_id, size);
115 }
116 else if (command == "update") ////////////////////////////////////////////
117 {
118 const Table_Id table_id = parse_table(parameters, readable);
119 Record_Id record_id = Record_Id::null;
120 parameters >> record_id;
121 std::string field_name;
122 parameters >> field_name;
123 const Field_Id field_id = readable.find_field(table_id, field_name);
124 update_value(parameters, table_id, record_id, field_id);
125 }
126 else if (command == "update_vector") /////////////////////////////////////
127 {
128 const Table_Id table_id = parse_table(parameters, readable);
129 Record_Id record_id = Record_Id::null;
130 parameters >> record_id;
131 std::string field_name;
132 parameters >> field_name;
133 const Field_Id field_id = readable.find_field(table_id, field_name);
134 size_t size = 0;
135 parameters >> size;
136
137 if (max_record_id.is_not_null() && size >= size_t(index_t(max_record_id)))
138 throw Exception("vector is too big");
139 else
140 {
141 switch(readable.get_field_type(table_id, field_id).get_type_id())
142 {
144 throw Exception("bad field");
145 break;
146
147 #define TYPE_MACRO(type, return_type, type_id, R, W)\
148 case Type::Type_Id::type_id:\
149 {\
150 std::vector<type> v(size);\
151 for (size_t i = 0; i < size; i++)\
152 v[i] = joedb::read_##type_id(parameters);\
153 writable.update_vector_##type_id(table_id, record_id, field_id, size, &v[0]);\
154 }\
155 break;
156 #include "joedb/TYPE_MACRO.h"
157 }
158 }
159 }
160 else
161 return Status::not_found;
162
163 return Status::done;
164 }
165}
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
Definition Blob.h:7