forked from xstreck1/TREMPPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3ppext.cpp
More file actions
195 lines (157 loc) · 5.24 KB
/
sqlite3ppext.cpp
File metadata and controls
195 lines (157 loc) · 5.24 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
// sqlite3ppext.cpp
//
// The MIT License
//
// Copyright (c) 2012 Wongoo Lee (iwongu at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "sqlite3ppext.hpp"
namespace sqlite3pp
{
namespace ext
{
namespace
{
void function_impl(sqlite3_context* ctx, int nargs, sqlite3_value** values)
{
function::function_handler* f = static_cast<function::function_handler*>(sqlite3_user_data(ctx));
context c(ctx, nargs, values);
(*f)(c);
}
void step_impl(sqlite3_context* ctx, int nargs, sqlite3_value** values)
{
std::pair<function::pfunction_base, function::pfunction_base>* p =
static_cast<std::pair<function::pfunction_base, function::pfunction_base>*>(sqlite3_user_data(ctx));
context c(ctx, nargs, values);
((function::function_handler&)*(*p).first)(c);
}
void finalize_impl(sqlite3_context* ctx)
{
std::pair<function::pfunction_base, function::pfunction_base>* p =
static_cast<std::pair<function::pfunction_base, function::pfunction_base>*>(sqlite3_user_data(ctx));
context c(ctx);
((function::function_handler&)*(*p).second)(c);
}
} // namespace
context::context(sqlite3_context* ctx, int nargs, sqlite3_value** values)
: ctx_(ctx), nargs_(nargs), values_(values)
{
}
int context::args_count() const
{
return nargs_;
}
int context::args_bytes(int idx) const
{
return sqlite3_value_bytes(values_[idx]);
}
int context::args_type(int idx) const
{
return sqlite3_value_type(values_[idx]);
}
int context::get(int idx, int) const
{
return sqlite3_value_int(values_[idx]);
}
double context::get(int idx, double) const
{
return sqlite3_value_double(values_[idx]);
}
long long int context::get(int idx, long long int) const
{
return sqlite3_value_int64(values_[idx]);
}
char const* context::get(int idx, char const*) const
{
return reinterpret_cast<char const*>(sqlite3_value_text(values_[idx]));
}
std::string context::get(int idx, std::string) const
{
return get(idx, (char const*)0);
}
void const* context::get(int idx, void const*) const
{
return sqlite3_value_blob(values_[idx]);
}
void context::result(int value)
{
sqlite3_result_int(ctx_, value);
}
void context::result(double value)
{
sqlite3_result_double(ctx_, value);
}
void context::result(long long int value)
{
sqlite3_result_int64(ctx_, value);
}
void context::result(std::string const& value)
{
result(value.c_str(), false);
}
void context::result(char const* value, bool fstatic)
{
sqlite3_result_text(ctx_, value, strlen(value), fstatic ? SQLITE_STATIC : SQLITE_TRANSIENT);
}
void context::result(void const* value, int n, bool fstatic)
{
sqlite3_result_blob(ctx_, value, n, fstatic ? SQLITE_STATIC : SQLITE_TRANSIENT);
}
void context::result()
{
sqlite3_result_null(ctx_);
}
void context::result(std::nullptr_t)
{
sqlite3_result_null(ctx_);
}
void context::result_copy(int idx)
{
sqlite3_result_value(ctx_, values_[idx]);
}
void context::result_error(char const* msg)
{
sqlite3_result_error(ctx_, msg, strlen(msg));
}
void* context::aggregate_data(int size)
{
return sqlite3_aggregate_context(ctx_, size);
}
int context::aggregate_count()
{
return sqlite3_aggregate_count(ctx_);
}
function::function(database& db) : db_(db.db_)
{
}
int function::create(char const* name, function_handler h, int nargs)
{
fh_[name] = pfunction_base(new function_handler(h));
return sqlite3_create_function(db_, name, nargs, SQLITE_UTF8, fh_[name].get(), function_impl, 0, 0);
}
aggregate::aggregate(database& db) : db_(db.db_)
{
}
int aggregate::create(char const* name, function_handler s, function_handler f, int nargs)
{
ah_[name] = std::make_pair(pfunction_base(new function_handler(s)), pfunction_base(new function_handler(f)));
return sqlite3_create_function(db_, name, nargs, SQLITE_UTF8, &ah_[name], 0, step_impl, finalize_impl);
}
} // namespace ext
} // namespace sqlite3pp