This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharg_values.h
145 lines (112 loc) · 3.9 KB
/
arg_values.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include <sqlite3.h>
#include "row_extractor.h"
namespace sqlite_orm {
struct arg_value {
arg_value() : arg_value(nullptr) {}
arg_value(sqlite3_value* value_) : value(value_) {}
template<class T>
T get() const {
return row_extractor<T>().extract(this->value);
}
bool is_null() const {
auto type = sqlite3_value_type(this->value);
return type == SQLITE_NULL;
}
bool is_text() const {
auto type = sqlite3_value_type(this->value);
return type == SQLITE_TEXT;
}
bool is_integer() const {
auto type = sqlite3_value_type(this->value);
return type == SQLITE_INTEGER;
}
bool is_float() const {
auto type = sqlite3_value_type(this->value);
return type == SQLITE_FLOAT;
}
bool is_blob() const {
auto type = sqlite3_value_type(this->value);
return type == SQLITE_BLOB;
}
bool empty() const {
return this->value == nullptr;
}
private:
sqlite3_value* value = nullptr;
};
struct arg_values {
struct iterator {
iterator(const arg_values& container_, int index_) :
container(container_), index(index_),
currentValue(index_ < int(container_.size()) ? container_[index_] : arg_value()) {}
iterator& operator++() {
++this->index;
if(this->index < int(this->container.size())) {
this->currentValue = this->container[this->index];
} else {
this->currentValue = {};
}
return *this;
}
iterator operator++(int) {
auto res = *this;
++this->index;
if(this->index < int(this->container.size())) {
this->currentValue = this->container[this->index];
} else {
this->currentValue = {};
}
return res;
}
arg_value operator*() const {
if(this->index < int(this->container.size()) && this->index >= 0) {
return this->currentValue;
} else {
throw std::system_error{orm_error_code::index_is_out_of_bounds};
}
}
arg_value* operator->() const {
return &this->currentValue;
}
bool operator==(const iterator& other) const {
return &other.container == &this->container && other.index == this->index;
}
bool operator!=(const iterator& other) const {
return !(*this == other);
}
private:
const arg_values& container;
int index = 0;
mutable arg_value currentValue;
};
arg_values() : arg_values(0, nullptr) {}
arg_values(int argsCount_, sqlite3_value** values_) : argsCount(argsCount_), values(values_) {}
size_t size() const {
return this->argsCount;
}
bool empty() const {
return 0 == this->argsCount;
}
arg_value operator[](int index) const {
if(index < this->argsCount && index >= 0) {
sqlite3_value* value = this->values[index];
return {value};
} else {
throw std::system_error{orm_error_code::index_is_out_of_bounds};
}
}
arg_value at(int index) const {
return this->operator[](index);
}
iterator begin() const {
return {*this, 0};
}
iterator end() const {
return {*this, this->argsCount};
}
private:
int argsCount = 0;
sqlite3_value** values = nullptr;
};
}