SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
ColumnSchema.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #include "swcdb/core/Exception.h"
9 
10 
11 namespace SWC { namespace client { namespace SQL {
12 
13 
14 
15 ColumnSchema::ColumnSchema(const std::string& a_sql,
16  DB::Schema::Ptr& a_schema,
17  std::string& a_message)
18  : Reader(a_sql, a_message), schema(a_schema) {
19 }
20 
22 
23  while(remain && !err) {
24 
25  if(found_space())
26  continue;
27 
28  if(found_token("add", 3) ||
29  found_token("create", 6)) {
30  return parse((*func = Func::CREATE), true);
31  }
32 
33  if(found_token("modify", 6) ||
34  found_token("update", 6) ||
35  found_token("change", 6)) {
36  return parse((*func = Func::MODIFY), true);
37  }
38 
39  if(found_token("delete", 6) ||
40  found_token("remove", 6)) {
41  return parse((*func = Func::REMOVE), true);
42  }
43 
44  bool token_cmd = false;
45  expect_token("CREATE|MODIFY|REMOVE", 20, token_cmd);
46  break;
47  }
48  return err;
49 }
50 
51 int ColumnSchema::parse(ColumnSchema::Func func, bool token_cmd) {
52  bool token_typ = false;
53  bool bracket = false;
54 
55  while(remain && !err) {
56 
57  if(found_space())
58  continue;
59 
60  if(!token_cmd && (
61  (func == Func::CREATE && (found_token("add", 3) ||
62  found_token("create", 6))) ||
63  (func == Func::MODIFY && (found_token("modify", 6) ||
64  found_token("update", 6) ||
65  found_token("change", 6))) ||
66  (func == Func::REMOVE && (found_token("delete", 6) ||
67  found_token("remove", 6)))
68  )) {
69  token_cmd = true;
70  continue;
71  }
72  if(!token_cmd) {
73  expect_token("CREATE|MODIFY|REMOVE", 20, token_cmd);
74  break;
75  }
76  if(!token_typ && (found_token("column", 6) || found_token("schema", 6))) {
77  token_typ = true;
78  continue;
79  }
80  if(!token_typ) {
81  expect_token("column", 7, token_typ);
82  break;
83  }
84 
85  if(!bracket) {
86  expect_token("(", 1 , bracket);
87  continue;
88  }
89 
90  read_schema_options(func);
91  if(err)
92  break;
93 
94  expect_token(")", 1 , bracket);
95  break;
96  }
97  return err;
98 }
99 
101 
102  bool any = true;
103  bool was_set = false;
104 
106 
107  const char* stop = ", )";
108  std::string buff;
109 
110  while(any && remain && !err) {
111  if(found_space())
112  continue;
113 
114  if((any = found_token("cid", 3))) {
115  expect_eq();
116  if(err)
117  return;
118  read_uint64_t(schema->cid, was_set = false);
119  continue;
120  }
121 
122  if((any = found_token("name", 4))) {
123  expect_eq();
124  if(err)
125  return;
126  schema->col_name.clear();
127  read(schema->col_name, stop);
128  continue;
129  }
130 
131  if((any = found_token("tags", 4))) {
132  seek_space();
133  expect_eq();
134  seek_space();
135  if(err)
136  return;
137  seek_space();
138  bool chk;
139  expect_token("[", 1, chk);
140  if(err)
141  return;
142  while(remain && !err) {
143  if(found_space() || found_char(','))
144  continue;
145  read(buff, ",]");
146  if(!buff.empty())
147  schema->tags.emplace_back(std::move(buff));
148  if((chk = found_char(']')))
149  break;
150  }
151  if(!chk)
152  expect_token("]", 1, chk);
153  continue;
154  }
155 
156  if((any = found_token("type", 4))) {
157  expect_eq();
158  if(err)
159  return;
160  read(buff, stop);
161  if((schema->col_type = DB::Types::column_type_from(buff))
163  error_msg(Error::SQL_PARSE_ERROR, " unknown column type");
164  return;
165  }
166  buff.clear();
167  continue;
168  }
169  if((any = found_token("seq", 3))) {
170  expect_eq();
171  if(err)
172  return;
173  read(buff, stop);
174  if((schema->col_seq = DB::Types::range_seq_from(buff))
176  error_msg(Error::SQL_PARSE_ERROR, " unknown range seq type");
177  return;
178  }
179  buff.clear();
180  continue;
181  }
182 
183  if((any = found_token("revision", 8))) {
184  expect_eq();
185  if(err)
186  return;
187  read_int64_t(schema->revision, was_set);
188  continue;
189  }
190 
191  if((any = found_token("cell_versions", 13))) {
192  expect_eq();
193  if(err)
194  return;
195  read_uint32_t(schema->cell_versions, was_set);
196  continue;
197  }
198 
199  if((any = found_token("cell_ttl", 8))) {
200  expect_eq();
201  if(err)
202  return;
203  read_uint32_t(schema->cell_ttl, was_set);
204  continue;
205  }
206 
207  if((any = found_token("blk_encoding", 12))) {
208  expect_eq();
209  if(err)
210  return;
211  read(buff, stop);
212  if((schema->blk_encoding = Core::Encoder::encoding_from(buff))
214  error_msg(Error::SQL_PARSE_ERROR, " unknown blk_encoding");
215  return;
216  }
217  buff.clear();
218  continue;
219  }
220 
221  if((any = found_token("blk_size", 8))) {
222  expect_eq();
223  if(err)
224  return;
225  read_uint32_t(schema->blk_size, was_set);
226  continue;
227  }
228 
229  if((any = found_token("blk_cells", 9))) {
230  expect_eq();
231  if(err)
232  return;
233  read_uint32_t(schema->blk_cells, was_set);
234  continue;
235  }
236 
237  if((any = found_token("cs_replication", 14))) {
238  expect_eq();
239  if(err)
240  return;
241  read_uint8_t(schema->cs_replication, was_set);
242  continue;
243  }
244 
245  if((any = found_token("cs_size", 7))) {
246  expect_eq();
247  if(err)
248  return;
249  read_uint32_t(schema->cs_size, was_set);
250  continue;
251  }
252 
253  if((any = found_token("cs_max", 6))) {
254  expect_eq();
255  if(err)
256  return;
257  read_uint8_t(schema->cs_max, was_set);
258  continue;
259  }
260 
261  if((any = found_token("compact", 7))) {
262  expect_eq();
263  if(err)
264  return;
265  read_uint8_t(schema->compact_percent, was_set);
266  continue;
267  }
268 
269  if((any = found_token("log_rollout", 11))) {
270  expect_eq();
271  if(err)
272  return;
273  read_uint8_t(schema->log_rollout_ratio, was_set);
274  continue;
275  }
276 
277  if((any = found_token("log_compact", 11))) {
278  expect_eq();
279  if(err)
280  return;
281  read_uint8_t(schema->log_compact_cointervaling, was_set);
282  continue;
283  }
284 
285  if((any = found_token("log_preload", 11))) {
286  expect_eq();
287  if(err)
288  return;
289  read_uint8_t(schema->log_fragment_preload, was_set);
290  continue;
291  }
292 
293  break;
294  }
295 
296  if(schema->col_name.empty()) {
297  error_msg(
299  "create|delete|modify action require column name"
300  );
301  } else if((func == Func::MODIFY) &&
302  !schema->col_name.empty() &&
303  schema->cid == DB::Schema::NO_CID ) {
304  error_msg(
306  "modify action require column cid"
307  );
308  return;
309  }
310 }
311 
312 
313 }}} // SWC::client:SQL namespace
314 
SWC::Error::SQL_PARSE_ERROR
@ SQL_PARSE_ERROR
Definition: Error.h:122
SWC::DB::Types::column_type_from
Column SWC_PURE_FUNC column_type_from(const std::string &typ) noexcept
Definition: Column.cc:59
SWC::client::SQL::Reader::expect_eq
void expect_eq()
Definition: Reader.cc:82
SWC::DB::SchemaPrimitives::NO_CID
static constexpr const cid_t NO_CID
Definition: Schema.h:25
SWC::client::SQL::Reader::expect_token
void expect_token(const char *token, uint8_t token_len, bool &found)
Definition: Reader.cc:122
SWC::DB::Schema::Ptr
std::shared_ptr< Schema > Ptr
Definition: Schema.h:185
SWC::DB::Types::range_seq_from
KeySeq SWC_PURE_FUNC range_seq_from(const std::string &typ) noexcept
Definition: KeySeq.cc:48
SWC::client::SQL::Reader::seek_space
void seek_space()
Definition: Reader.cc:78
SWC::client::SQL::Reader::read_uint64_t
void read_uint64_t(uint64_t &value, bool &was_set, const char *stop=nullptr)
Definition: Reader.cc:274
SWC::client::SQL::Reader::remain
uint32_t remain
Definition: Reader.h:124
SWC::client::SQL::ColumnSchema::read_schema_options
void read_schema_options(Func func)
Definition: ColumnSchema.cc:100
SWC::Core::Encoder::encoding_from
Type SWC_PURE_FUNC encoding_from(const std::string &typ) noexcept
Definition: Encoder.cc:50
SWC::client::SQL::Reader
Definition: Reader.h:28
SWC::Error::COLUMN_SCHEMA_NAME_EMPTY
@ COLUMN_SCHEMA_NAME_EMPTY
Definition: Error.h:107
SWC::client::SQL::Reader::read_int64_t
void read_int64_t(int64_t &value, bool &was_set, const char *stop=nullptr)
Definition: Reader.cc:259
SWC::client::SQL::Reader::found_char
bool found_char(const char c)
Definition: Reader.cc:28
SWC::client::SQL::Reader::found_space
bool found_space()
Definition: Reader.cc:37
Exception.h
SWC::DB::Cell::Serial::Value::UNKNOWN
@ UNKNOWN
Definition: CellValueSerialField.h:34
SWC::client::SQL::ColumnSchema::ColumnSchema
ColumnSchema(const std::string &sql, DB::Schema::Ptr &schema, std::string &message)
Definition: ColumnSchema.cc:15
SWC::client::SQL::Reader::read
void read(std::string &buf, const char *stop=nullptr, bool keep_escape=false)
Definition: Reader.cc:185
SWC::client::SQL::Reader::read_uint8_t
void read_uint8_t(uint8_t &value, bool &was_set)
Definition: Reader.cc:223
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::DB::Schema::make
static SWC_CAN_INLINE Ptr make()
Definition: Schema.h:189
SWC::client::SQL::Reader::found_token
bool found_token(const char *token, uint8_t token_len)
Definition: Reader.cc:58
SWC::Comm::Protocol::Mngr::Params::ColumnMng::Function
Function
Definition: ColumnMng.h:21
SWC::client::SQL::ColumnSchema::parse
int parse(Func *func)
Definition: ColumnSchema.cc:21
SWC::client::SQL::Reader::err
int err
Definition: Reader.h:125
ColumnSchema.h
SWC::client::SQL::Reader::error_msg
void error_msg(int error, const std::string &msg)
Definition: Reader.cc:841
SWC::client::SQL::ColumnSchema::schema
DB::Schema::Ptr & schema
Definition: ColumnSchema.h:35
SWC::client::SQL::Reader::read_uint32_t
void read_uint32_t(uint32_t &value, bool &was_set, const char *stop=nullptr)
Definition: Reader.cc:250