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