Line data Source code
1 : // Copyright (c) 2019 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdatabase
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 :
21 : /** \file
22 : * \brief Cell file implementation.
23 : *
24 : * When handling a row, it has a set of cells. The set may change between
25 : * calls. To the minimum, though, a row should at least have one cell.
26 : */
27 :
28 : // self
29 : //
30 : #include "snapdatabase/database/cell.h"
31 :
32 :
33 : // last include
34 : //
35 : #include <snapdev/poison.h>
36 :
37 :
38 :
39 : namespace snapdatabase
40 : {
41 :
42 :
43 :
44 :
45 0 : cell::cell(schema_column::pointer_t c)
46 0 : : f_schema_column(c)
47 : {
48 0 : }
49 :
50 :
51 0 : schema_column::pointer_t cell::schema() const
52 : {
53 0 : return f_schema_column;
54 : }
55 :
56 :
57 0 : bool cell::is_void() const
58 : {
59 0 : return f_schema_column->type() == struct_type_t::STRUCT_TYPE_VOID;
60 : }
61 :
62 :
63 0 : void cell::set_void()
64 : {
65 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_VOID)
66 : {
67 : throw type_mismatch(
68 : "expected void type, received "
69 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
70 : }
71 0 : }
72 :
73 :
74 0 : int8_t cell::get_int8() const
75 : {
76 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT8)
77 : {
78 : throw type_mismatch(
79 : "expected int8 type, received "
80 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
81 : }
82 :
83 0 : return f_integer.f_value[0];
84 : }
85 :
86 :
87 0 : void cell::set_int8(int8_t value)
88 : {
89 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT8)
90 : {
91 : throw type_mismatch(
92 : "expected int8 type, received "
93 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
94 : }
95 :
96 0 : f_integer.f_value[0] = value;
97 0 : f_integer.f_value[1] = value < 0 ? -1 : 0;
98 0 : f_integer.f_value[2] = f_integer.f_value[1];
99 0 : f_integer.f_value[3] = f_integer.f_value[1];
100 0 : f_integer.f_value[4] = f_integer.f_value[1];
101 0 : f_integer.f_value[5] = f_integer.f_value[1];
102 0 : f_integer.f_value[6] = f_integer.f_value[1];
103 0 : f_integer.f_value[7] = f_integer.f_value[1];
104 0 : }
105 :
106 :
107 0 : uint8_t cell::get_uint8() const
108 : {
109 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS8
110 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT8)
111 : {
112 : throw type_mismatch(
113 : "expected bits8 or uint8 type, received "
114 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
115 : }
116 :
117 0 : return f_integer.f_value[0];
118 : }
119 :
120 :
121 0 : void cell::set_uint8(uint8_t value)
122 : {
123 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS8
124 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT8)
125 : {
126 : throw type_mismatch(
127 : "expected bits8 or uint8 type, received "
128 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
129 : }
130 :
131 0 : f_integer.f_value[0] = value;
132 0 : f_integer.f_value[1] = 0;
133 0 : f_integer.f_value[2] = 0;
134 0 : f_integer.f_value[3] = 0;
135 0 : f_integer.f_value[4] = 0;
136 0 : f_integer.f_value[5] = 0;
137 0 : f_integer.f_value[6] = 0;
138 0 : f_integer.f_value[7] = 0;
139 0 : }
140 :
141 :
142 0 : int16_t cell::get_int16() const
143 : {
144 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT16)
145 : {
146 : throw type_mismatch(
147 : "expected int16 type, received "
148 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
149 : }
150 :
151 0 : return f_integer.f_value[0];
152 : }
153 :
154 :
155 0 : void cell::set_int16(int16_t value)
156 : {
157 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT16)
158 : {
159 : throw type_mismatch(
160 : "expected int16 type, received "
161 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
162 : }
163 :
164 0 : f_integer.f_value[0] = value;
165 0 : f_integer.f_value[1] = static_cast<int64_t>(value) < 0 ? -1 : 0;
166 0 : f_integer.f_value[2] = f_integer.f_value[1];
167 0 : f_integer.f_value[3] = f_integer.f_value[1];
168 0 : f_integer.f_value[4] = f_integer.f_value[1];
169 0 : f_integer.f_value[5] = f_integer.f_value[1];
170 0 : f_integer.f_value[6] = f_integer.f_value[1];
171 0 : f_integer.f_value[7] = f_integer.f_value[1];
172 0 : }
173 :
174 :
175 0 : uint16_t cell::get_uint16() const
176 : {
177 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS16
178 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT16)
179 : {
180 : throw type_mismatch(
181 : "expected uint16 type, received "
182 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
183 : }
184 :
185 0 : return f_integer.f_value[0];
186 : }
187 :
188 :
189 0 : void cell::set_uint16(uint16_t value)
190 : {
191 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS16
192 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT16)
193 : {
194 : throw type_mismatch(
195 : "expected uint16 type, received "
196 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
197 : }
198 :
199 0 : f_integer.f_value[0] = value;
200 0 : f_integer.f_value[1] = 0;
201 0 : f_integer.f_value[2] = 0;
202 0 : f_integer.f_value[3] = 0;
203 0 : f_integer.f_value[4] = 0;
204 0 : f_integer.f_value[5] = 0;
205 0 : f_integer.f_value[6] = 0;
206 0 : f_integer.f_value[7] = 0;
207 0 : }
208 :
209 :
210 0 : int32_t cell::get_int32() const
211 : {
212 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT32)
213 : {
214 : throw type_mismatch(
215 : "expected int32 type, received "
216 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
217 : }
218 :
219 0 : return f_integer.f_value[0];
220 : }
221 :
222 :
223 0 : void cell::set_int32(int32_t value)
224 : {
225 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT32)
226 : {
227 : throw type_mismatch(
228 : "expected int32 type, received "
229 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
230 : }
231 :
232 0 : f_integer.f_value[0] = value;
233 0 : f_integer.f_value[1] = value < 0 ? -1 : 0;
234 0 : f_integer.f_value[2] = f_integer.f_value[1];
235 0 : f_integer.f_value[3] = f_integer.f_value[1];
236 0 : f_integer.f_value[4] = f_integer.f_value[1];
237 0 : f_integer.f_value[5] = f_integer.f_value[1];
238 0 : f_integer.f_value[6] = f_integer.f_value[1];
239 0 : f_integer.f_value[7] = f_integer.f_value[1];
240 0 : }
241 :
242 :
243 0 : uint32_t cell::get_uint32() const
244 : {
245 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS32
246 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT32)
247 : {
248 : throw type_mismatch(
249 : "expected bits32 or uint32 type, received "
250 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
251 : }
252 :
253 0 : return f_integer.f_value[0];
254 : }
255 :
256 :
257 0 : void cell::set_uint32(uint32_t value)
258 : {
259 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS32
260 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT32)
261 : {
262 : throw type_mismatch(
263 : "expected bits32 or uint32 type, received "
264 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
265 : }
266 :
267 0 : f_integer.f_value[0] = value;
268 0 : f_integer.f_value[1] = 0;
269 0 : f_integer.f_value[2] = 0;
270 0 : f_integer.f_value[3] = 0;
271 0 : f_integer.f_value[4] = 0;
272 0 : f_integer.f_value[5] = 0;
273 0 : f_integer.f_value[6] = 0;
274 0 : f_integer.f_value[7] = 0;
275 0 : }
276 :
277 :
278 0 : int64_t cell::get_int64() const
279 : {
280 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT64)
281 : {
282 : throw type_mismatch(
283 : "expected int64 type, received "
284 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
285 : }
286 :
287 0 : return f_integer.f_value[0];
288 : }
289 :
290 :
291 0 : void cell::set_int64(int64_t value)
292 : {
293 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT64)
294 : {
295 : throw type_mismatch(
296 : "expected int64 type, received "
297 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
298 : }
299 :
300 0 : f_integer.f_value[0] = value;
301 0 : f_integer.f_value[1] = static_cast<int64_t>(value) < 0 ? -1 : 0;
302 0 : f_integer.f_value[2] = f_integer.f_value[1];
303 0 : f_integer.f_value[3] = f_integer.f_value[1];
304 0 : f_integer.f_value[4] = f_integer.f_value[1];
305 0 : f_integer.f_value[5] = f_integer.f_value[1];
306 0 : f_integer.f_value[6] = f_integer.f_value[1];
307 0 : f_integer.f_value[7] = f_integer.f_value[1];
308 0 : }
309 :
310 :
311 0 : uint64_t cell::get_uint64() const
312 : {
313 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS64
314 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT64
315 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_TIME
316 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_MSTIME
317 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_USTIME
318 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_REFERENCE
319 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_OID)
320 : {
321 : throw type_mismatch(
322 : "expected int64 type, received "
323 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
324 : }
325 :
326 0 : return f_integer.f_value[0];
327 : }
328 :
329 :
330 0 : void cell::set_uint64(uint64_t value)
331 : {
332 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_BITS64
333 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT64
334 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_TIME
335 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_MSTIME
336 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_USTIME
337 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_REFERENCE
338 0 : && f_schema_column->type() != struct_type_t::STRUCT_TYPE_OID)
339 : {
340 : throw type_mismatch(
341 : "expected int64 type, received "
342 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
343 : }
344 :
345 0 : f_integer.f_value[0] = value;
346 0 : f_integer.f_value[1] = 0;
347 0 : f_integer.f_value[2] = 0;
348 0 : f_integer.f_value[3] = 0;
349 0 : f_integer.f_value[4] = 0;
350 0 : f_integer.f_value[5] = 0;
351 0 : f_integer.f_value[6] = 0;
352 0 : f_integer.f_value[7] = 0;
353 0 : }
354 :
355 :
356 0 : int512_t cell::get_int128() const
357 : {
358 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT128)
359 : {
360 : throw type_mismatch(
361 : "expected int128 type, received "
362 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
363 : }
364 :
365 0 : return f_integer;
366 : }
367 :
368 :
369 0 : void cell::set_int128(int512_t value)
370 : {
371 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT128)
372 : {
373 : throw type_mismatch(
374 : "expected int128 type, received "
375 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
376 : }
377 :
378 0 : f_integer = value;
379 0 : }
380 :
381 :
382 0 : uint512_t cell::get_uint128() const
383 : {
384 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT128)
385 : {
386 : throw type_mismatch(
387 : "expected uint128 type, received "
388 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
389 : }
390 :
391 0 : return f_integer;
392 : }
393 :
394 :
395 0 : void cell::set_uint128(uint512_t value)
396 : {
397 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT128)
398 : {
399 : throw type_mismatch(
400 : "expected uint128 type, received "
401 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
402 : }
403 :
404 0 : f_integer = value;
405 0 : }
406 :
407 :
408 0 : int512_t cell::get_int256() const
409 : {
410 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT256)
411 : {
412 : throw type_mismatch(
413 : "expected int256 type, received "
414 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
415 : }
416 :
417 0 : return f_integer;
418 : }
419 :
420 :
421 0 : void cell::set_int256(int512_t value)
422 : {
423 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT256)
424 : {
425 : throw type_mismatch(
426 : "expected int256 type, received "
427 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
428 : }
429 :
430 0 : f_integer = value;
431 0 : }
432 :
433 :
434 0 : uint512_t cell::get_uint256() const
435 : {
436 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT256)
437 : {
438 : throw type_mismatch(
439 : "expected uint256 type, received "
440 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
441 : }
442 :
443 0 : return f_integer;
444 : }
445 :
446 :
447 0 : void cell::set_uint256(uint512_t value)
448 : {
449 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT256)
450 : {
451 : throw type_mismatch(
452 : "expected uint256 type, received "
453 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
454 : }
455 :
456 0 : f_integer = value;
457 0 : }
458 :
459 :
460 0 : int512_t cell::get_int512() const
461 : {
462 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT512)
463 : {
464 : throw type_mismatch(
465 : "expected int512 type, received "
466 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
467 : }
468 :
469 0 : return f_integer;
470 : }
471 :
472 :
473 0 : void cell::set_int512(int512_t value)
474 : {
475 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_INT512)
476 : {
477 : throw type_mismatch(
478 : "expected int512 type, received "
479 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
480 : }
481 :
482 0 : f_integer = value;
483 0 : }
484 :
485 :
486 0 : uint512_t cell::get_uint512() const
487 : {
488 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT512)
489 : {
490 : throw type_mismatch(
491 : "expected uint512 type, received "
492 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
493 : }
494 :
495 0 : return f_integer;
496 : }
497 :
498 :
499 0 : void cell::set_uint512(uint512_t value)
500 : {
501 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_UINT512)
502 : {
503 : throw type_mismatch(
504 : "expected uint512 type, received "
505 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
506 : }
507 :
508 0 : f_integer = value;
509 0 : }
510 :
511 :
512 0 : float cell::get_float32() const
513 : {
514 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_FLOAT32)
515 : {
516 : throw type_mismatch(
517 : "expected float32 type, received "
518 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
519 : }
520 :
521 0 : return f_float_value;
522 : }
523 :
524 :
525 0 : void cell::set_float32(float value)
526 : {
527 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_FLOAT32)
528 : {
529 : throw type_mismatch(
530 : "expected float32 type, received "
531 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
532 : }
533 :
534 0 : f_float_value = value;
535 0 : }
536 :
537 :
538 0 : double cell::get_float64() const
539 : {
540 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_FLOAT64)
541 : {
542 : throw type_mismatch(
543 : "expected float64 type, received "
544 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
545 : }
546 :
547 0 : return f_float_value;
548 : }
549 :
550 :
551 0 : void cell::set_float64(double value)
552 : {
553 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_FLOAT64)
554 : {
555 : throw type_mismatch(
556 : "expected float64 type, received "
557 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
558 : }
559 :
560 0 : f_float_value = value;
561 0 : }
562 :
563 :
564 0 : long double cell::get_float128() const
565 : {
566 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_FLOAT128)
567 : {
568 : throw type_mismatch(
569 : "expected float128 type, received "
570 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
571 : }
572 :
573 0 : return f_float_value;
574 : }
575 :
576 :
577 0 : void cell::set_float128(long double value)
578 : {
579 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_FLOAT128)
580 : {
581 : throw type_mismatch(
582 : "expected float128 type, received "
583 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
584 : }
585 :
586 0 : f_float_value = value;
587 0 : }
588 :
589 :
590 0 : version_t cell::get_version() const
591 : {
592 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_VERSION)
593 : {
594 : throw type_mismatch(
595 : "expected version type, received "
596 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
597 : }
598 :
599 0 : return version_t(f_integer.f_value[0]);
600 : }
601 :
602 :
603 0 : void cell::set_version(version_t value)
604 : {
605 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_VERSION)
606 : {
607 : throw type_mismatch(
608 : "expected version type, received "
609 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
610 : }
611 :
612 0 : f_integer.f_value[0] = value.to_binary();
613 0 : f_integer.f_value[1] = 0;
614 0 : f_integer.f_value[2] = 0;
615 0 : f_integer.f_value[3] = 0;
616 0 : f_integer.f_value[4] = 0;
617 0 : f_integer.f_value[5] = 0;
618 0 : f_integer.f_value[6] = 0;
619 0 : f_integer.f_value[7] = 0;
620 0 : }
621 :
622 :
623 0 : std::string cell::get_string() const
624 : {
625 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_P8STRING
626 0 : || f_schema_column->type() != struct_type_t::STRUCT_TYPE_P16STRING
627 0 : || f_schema_column->type() != struct_type_t::STRUCT_TYPE_P32STRING)
628 : {
629 : throw type_mismatch(
630 : "expected cstring, p8/16/32string type, received "
631 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
632 : }
633 :
634 0 : return f_string;
635 : }
636 :
637 :
638 0 : void cell::set_string(std::string const & value)
639 : {
640 0 : if(f_schema_column->type() != struct_type_t::STRUCT_TYPE_P8STRING
641 0 : || f_schema_column->type() != struct_type_t::STRUCT_TYPE_P16STRING
642 0 : || f_schema_column->type() != struct_type_t::STRUCT_TYPE_P32STRING)
643 : {
644 : throw type_mismatch(
645 : "expected cstring, p8/16/32string type, received "
646 0 : + std::to_string(static_cast<int>(f_schema_column->type())));
647 : }
648 :
649 0 : f_string = value;
650 0 : }
651 :
652 :
653 :
654 :
655 :
656 : } // namespace snapdatabase
657 : // vim: ts=4 sw=4 et
|