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