Repository navigation
Expand file tree
/
Copy pathsimple_error.cpp
More file actions
206 lines (195 loc) · 7.22 KB
/
Copy pathsimple_error.cpp
File metadata and controls
206 lines (195 loc) · 7.22 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <teaql/provider_sqlite/simple_error.hpp>
namespace teaql::detail {
namespace {
template <class T>
void number(String& output, T value) {
char buffer[32];
auto converted = std::to_chars(buffer, buffer + sizeof(buffer), value);
if (converted.ec != std::errc{}) {
throw std::length_error("SQLite error number conversion failed");
}
output.append(buffer, converted.ptr);
}
std::string_view type_name(SqliteValueType value) {
constexpr std::string_view names[]{"Null", "Integer", "Real", "Text", "Blob"};
auto index = static_cast<std::size_t>(value);
if (index >= std::size(names)) {
throw std::invalid_argument("Unknown SQLite value type");
}
return names[index];
}
}
SqliteSimpleError SqliteSimpleError::unit(Memory r, Kind kind) {
switch (kind) {
case Kind::SqliteSingleThreadedMode:
case Kind::QueryReturnedNoRows:
case Kind::QueryReturnedMoreThanOneRow:
case Kind::InvalidQuery:
case Kind::UnwindingPanic:
case Kind::GetAuxWrongType:
return {r, kind};
default:
throw std::invalid_argument("SQLite error variant requires a payload");
}
}
SqliteSimpleError
SqliteSimpleError::integral_range(Memory r, std::size_t index, std::int64_t value) {
SqliteSimpleError error(r, Kind::IntegralValueOutOfRange);
error.index_ = index;
error.value_ = value;
return error;
}
SqliteSimpleError SqliteSimpleError::named(Memory r, Kind kind, std::string_view name) {
if (kind != Kind::InvalidParameterName && kind != Kind::InvalidColumnName) {
throw std::invalid_argument("SQLite error variant does not accept a name");
}
SqliteSimpleError error(r, kind);
error.name_ = name;
return error;
}
SqliteSimpleError SqliteSimpleError::indexed(Memory r, Kind kind, std::size_t index) {
if (kind != Kind::StatementChangedRows && kind != Kind::InvalidDatabaseIndex) {
throw std::invalid_argument("SQLite error variant does not accept a scalar index");
}
SqliteSimpleError error(r, kind);
error.index_ = index;
return error;
}
SqliteSimpleError
SqliteSimpleError::function_type(Memory r, std::size_t index, SqliteValueType type) {
type_name(type);
SqliteSimpleError error(r, Kind::InvalidFunctionParameterType);
error.index_ = index;
error.type_ = type;
return error;
}
SqliteSimpleError::SqliteSimpleError(std::allocator_arg_t,
Allocator a,
const SqliteSimpleError& other)
: kind_(other.kind_), name_(other.name_, a.resource()), index_(other.index_),
value_(other.value_), type_(other.type_) {}
String SqliteSimpleError::to_string(Memory r) const {
switch (kind_) {
case Kind::SqliteSingleThreadedMode:
return String("SQLite was compiled or configured for single-threaded use only", r);
case Kind::QueryReturnedNoRows:
return String("Query returned no rows", r);
case Kind::QueryReturnedMoreThanOneRow:
return String("Query returned more than one row", r);
case Kind::InvalidQuery:
return String("Query is not read-only", r);
case Kind::UnwindingPanic:
return String("unwinding panic", r);
case Kind::GetAuxWrongType:
return String("get_aux called with wrong type", r);
default:
break;
}
String text(r);
switch (kind_) {
case Kind::IntegralValueOutOfRange:
text = "Integer ";
number(text, value_);
text += " out of range";
if (index_ != SIZE_MAX) {
text += " at index ";
number(text, index_);
}
break;
case Kind::InvalidParameterName:
text = "Invalid parameter name: ";
text += name_;
break;
case Kind::InvalidColumnName:
text = "Invalid column name: ";
text += name_;
break;
case Kind::StatementChangedRows:
text = "Query changed ";
number(text, index_);
text += " rows";
break;
case Kind::InvalidFunctionParameterType:
text = "Invalid function parameter type ";
text += type_name(type_);
text += " at index ";
number(text, index_);
break;
case Kind::InvalidDatabaseIndex:
text = "Invalid database index: ";
number(text, index_);
break;
default:
throw std::invalid_argument("Unknown SQLite simple error variant");
}
return text;
}
bool operator==(const SqliteSimpleError& left, const SqliteSimpleError& right) {
if (left.kind_ != right.kind_) {
return false;
}
switch (left.kind_) {
case SqliteSimpleError::Kind::IntegralValueOutOfRange:
return left.index_ == right.index_ && left.value_ == right.value_;
case SqliteSimpleError::Kind::InvalidParameterName:
case SqliteSimpleError::Kind::InvalidColumnName:
return left.name_ == right.name_;
case SqliteSimpleError::Kind::StatementChangedRows:
case SqliteSimpleError::Kind::InvalidDatabaseIndex:
return left.index_ == right.index_;
case SqliteSimpleError::Kind::InvalidFunctionParameterType:
return left.index_ == right.index_ && left.type_ == right.type_;
default:
return true;
}
}
void write_debug(DebugWriter& writer, const SqliteSimpleError& error) {
constexpr std::string_view names[]{"SqliteSingleThreadedMode",
"IntegralValueOutOfRange",
"InvalidParameterName",
"QueryReturnedNoRows",
"QueryReturnedMoreThanOneRow",
"InvalidColumnName",
"StatementChangedRows",
"InvalidFunctionParameterType",
"InvalidQuery",
"UnwindingPanic",
"GetAuxWrongType",
"InvalidDatabaseIndex"};
auto index = static_cast<std::size_t>(error.kind());
if (index >= std::size(names)) {
throw std::invalid_argument("Unknown SQLite simple error diagnostic");
}
switch (error.kind()) {
case SqliteSimpleError::Kind::IntegralValueOutOfRange:
writer.tuple(names[index], [&](auto& out) {
out.integer(error.index());
out.raw(out.options().pretty ? ",\n" : ", ");
out.integer(error.value());
});
break;
case SqliteSimpleError::Kind::InvalidParameterName:
case SqliteSimpleError::Kind::InvalidColumnName:
writer.tuple(names[index], [&](auto& out) {
out.text(error.name());
});
break;
case SqliteSimpleError::Kind::StatementChangedRows:
case SqliteSimpleError::Kind::InvalidDatabaseIndex:
writer.tuple(names[index], [&](auto& out) {
out.integer(error.index());
});
break;
case SqliteSimpleError::Kind::InvalidFunctionParameterType:
writer.tuple(names[index], [&](auto& out) {
out.integer(error.index());
out.raw(out.options().pretty ? ",\n" : ", ");
write_debug(out, error.value_type());
});
break;
default:
writer.raw(names[index]);
break;
}
}
}