44 static void sqlite_ext_pow(sqlite3_context *context,
int argc, sqlite3_value **argv) {
46 if( sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL ) {
47 sqlite3_result_null(context);
50 double r1 = sqlite3_value_double(argv[0]);
51 double r2 = sqlite3_value_double(argv[1]);
52 double val = pow(r1,r2);
54 sqlite3_result_double( context, val );
56 sqlite3_result_error( context, strerror(errno), errno );
67 static void sqlite_ext_log2(sqlite3_context *context,
int argc, sqlite3_value **argv) {
69 if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
70 sqlite3_result_null(context);
73 double r1 = sqlite3_value_double(argv[0]);
74 double val = log2(r1);
76 sqlite3_result_double(context, val);
78 sqlite3_result_error(context, strerror(errno), errno);
89 static void sqlite_ext_ceil(sqlite3_context *context,
int argc, sqlite3_value **argv) {
92 if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
93 sqlite3_result_null(context);
96 double r1 = sqlite3_value_double(argv[0]);
97 double val = ceil(r1);
99 sqlite3_result_double(context, val);
101 sqlite3_result_error(context, strerror(errno), errno);
115 if( sqlite3_value_type(argv[0]) == SQLITE_NULL ){
116 sqlite3_result_null(context);
120 r1 = sqlite3_value_double(argv[0]);
124 sqlite3_result_double(context, val);
126 sqlite3_result_error(context, strerror(errno), errno);
133 int r = sqlite3_open_v2( filename.c_str(), &
database_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX,
nullptr );
134 if ( r != SQLITE_OK ) {
137 int eTextRep = SQLITE_UTF8;
138 #if SQLITE_VERSION_NUMBER > 3008003
139 eTextRep |= SQLITE_DETERMINISTIC;
141 r = sqlite3_create_function_v2(
database_,
150 if ( r != SQLITE_OK ) {
153 r = sqlite3_create_function_v2(
database_,
162 if ( r != SQLITE_OK ) {
165 r = sqlite3_create_function_v2(
database_,
174 if ( r != SQLITE_OK ) {
177 r = sqlite3_create_function_v2(
database_,
186 if ( r != SQLITE_OK ) {
196 if ( r != SQLITE_OK ) std::cout <<
"sqlite3_close returns " << r << std::endl;
201 int r = sqlite3_db_config(
database_, SQLITE_DBCONFIG_ENABLE_FKEY, 1, &succes );
202 if ( r != SQLITE_OK ) {
209 int r = sqlite3_db_config(
database_, SQLITE_DBCONFIG_ENABLE_FKEY, 0, &succes );
210 if ( r != SQLITE_OK ) {
217 int r = sqlite3_db_config(
database_, SQLITE_DBCONFIG_ENABLE_TRIGGER, 1, &succes );
218 if ( r != SQLITE_OK ) {
224 std::stringstream ss;
232 std::stringstream ss;
233 ss <<
"BEGIN IMMEDIATE";
240 std::stringstream ss;
241 ss <<
"BEGIN EXCLUSIVE";
248 std::stringstream ss;
256 std::stringstream ss;
264 std::stringstream ss;
265 ss <<
"SAVEPOINT " << sp;
272 std::stringstream ss;
273 ss <<
"RELEASE " << sp;
280 std::stringstream ss;
281 ss <<
"ROLLBACK TO SAVEPOINT " << sp;
288 int64_t r = sqlite3_last_insert_rowid(
database_ );
298 int r = sqlite3_wal_checkpoint_v2(
database_, NULL, SQLITE_CHECKPOINT_FULL, &nLog, &nCkpt );
299 if ( r != SQLITE_OK ) {
307 int r = sqlite3_wal_checkpoint_v2(
database_, NULL, SQLITE_CHECKPOINT_PASSIVE, &nLog, &nCkpt );
308 if ( r != SQLITE_OK ) {
316 #if SQLITE_VERSION_NUMBER > 30080303
317 int r = sqlite3_wal_checkpoint_v2(
database_, NULL, SQLITE_CHECKPOINT_TRUNCATE, &nLog, &nCkpt );
319 int r = sqlite3_wal_checkpoint_v2(
database_, NULL, SQLITE_CHECKPOINT_RESTART, &nLog, &nCkpt );
321 if ( r != SQLITE_OK ) {
327 std::stringstream ss;
328 ss <<
"PRAGMA user_version=" << version;
337 query.
prepare(
"PRAGMA user_version" );
338 if ( query.
step() ) {
360 int r = sqlite3_prepare_v2(
database_, sql.c_str(), -1, &
stmt_, &err );
361 if ( r != SQLITE_OK ) {
362 std::stringstream ss;
363 ss << sqlite3_errmsg(
database_ ) <<
" at '" << err <<
"' sql='" << sql <<
"'";
370 auto r = sqlite3_clear_bindings(
stmt_ );
371 if ( r != SQLITE_OK ) {
375 auto r = sqlite3_reset(
stmt_ );
376 if ( r != SQLITE_OK ) {
382 int r = sqlite3_finalize(
stmt_ );
384 if ( r != SQLITE_OK ) {
390 int r = sqlite3_step(
stmt_ );
391 if ( r != SQLITE_DONE ) {
397 return sqlite3_step(
stmt_ );
404 int r = sqlite3_step(
stmt_ );
405 if ( r != SQLITE_DONE ) {
412 int r = sqlite3_bind_double(
stmt_, position, value );
413 if ( r != SQLITE_OK ) {
419 int r = sqlite3_bind_int(
stmt_, position, value );
420 if ( r != SQLITE_OK ) {
426 int r = sqlite3_bind_int64(
stmt_, position, value );
427 if ( r != SQLITE_OK ) {
432 void DML::bind(
int position,
const std::string &value ) {
433 int r = sqlite3_bind_text(
stmt_, position, value.c_str(),
static_cast<int>(value.length()), SQLITE_TRANSIENT );
434 if ( r != SQLITE_OK ) {
440 int r = sqlite3_bind_blob64(
stmt_, position, value.
getArray(), value.
getSize(), SQLITE_TRANSIENT );
441 if ( r != SQLITE_OK ) {
446 void DML::bind(
const std::string &name,
double value ) {
447 auto rc = sqlite3_bind_parameter_index(
stmt_, name.c_str() );
449 else bind( rc, value );
453 auto rc = sqlite3_bind_parameter_index(
stmt_, name.c_str() );
455 else bind( rc, value );
458 void DML::bind(
const std::string &name, int64_t value ) {
459 auto rc = sqlite3_bind_parameter_index(
stmt_, name.c_str() );
461 else bind( rc, value );
464 void DML::bind(
const std::string &name,
const std::string &value ) {
465 auto rc = sqlite3_bind_parameter_index(
stmt_, name.c_str() );
467 else bind( rc, value );
471 auto rc = sqlite3_bind_parameter_index(
stmt_, name.c_str() );
473 else bind( rc, value );
477 int r = sqlite3_step(
stmt_ );
478 if ( r != SQLITE_DONE && r != SQLITE_ROW ) {
481 return r == SQLITE_ROW;
485 return sqlite3_column_type(
stmt_, col ) == SQLITE_NULL;
489 return static_cast<DataType>( sqlite3_column_type(
stmt_, col ) );
493 return sqlite3_column_int(
stmt_, col );
497 return sqlite3_column_int64(
stmt_, col );
501 return sqlite3_column_double(
stmt_, col );
505 const char* c = (
const char*)sqlite3_column_text(
stmt_, col );
506 if ( c )
return c;
else return "";
511 int size = sqlite3_column_bytes(
stmt_, col );
513 bytes.
append( tmp_data, size );
517 return sqlite3_column_count(
stmt_ );