Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding sqlite::endr, and ability to give functions to database_binder #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ namespace sqlite {
}

}
void endr() {
execute();
reset();
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the operator, so I don't think we need a member function. If we keep this function, we may prefer a more descriptive name.

std::string sql() {
#if SQLITE_VERSION_NUMBER >= 3014000
Expand Down Expand Up @@ -286,6 +290,10 @@ namespace sqlite {
}
};

inline void endr(database_binder& db) {
db.endr();
}

namespace sql_function_binder {
template<
typename ContextType,
Expand Down Expand Up @@ -787,6 +795,24 @@ namespace sqlite {
val = i;
}

/// apply a member function
inline database_binder& operator <<(database_binder& db, void (database_binder::* pf)(void)) {
(db.*pf)();
return db;
}

/// apply a function pointer
inline database_binder& operator<< (database_binder& db, void (*fun)(database_binder*)) {
fun(&db);
return db;
}

/// apply a lambda
inline database_binder& operator<< (database_binder& db, std::function<void(database_binder&)>f) {
f(db);
return db;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use our function_traits instead of std::function here to avoid type-erasure?
What is the use-case for calling this with a lambda?

// std::optional support for NULL values
#ifdef MODERN_SQLITE_STD_OPTIONAL_SUPPORT
template <typename OptionalT> inline database_binder& operator <<(database_binder& db, const std::optional<OptionalT>& val) {
Expand Down Expand Up @@ -894,6 +920,7 @@ namespace sqlite {
// Some ppl are lazy so we have a operator for proper prep. statemant handling.
void inline operator++(database_binder& db, int) { db.execute(); db.reset(); }


// Convert the rValue binder to a reference and call first op<<, its needed for the call that creates the binder (be carefull of recursion here!)
template<typename T> database_binder& operator << (database_binder&& db, const T& val) { return db << val; }

Expand Down
5 changes: 5 additions & 0 deletions tests/prepared_statment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ int main() {

}

auto ppsc = db << "CREATE TABLE tnums(name VARCHAR(30) PRIMARY KEY ASC NOT NULL UNIQUE, num INT NOT NULL);";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement is never executed. Either don't save the prepared statement to execute it implicitly or call execute/endr/operator++.

auto ppsi = db << "INSERT INTO tnums VALUES (?,?);";
ppsi << "zero" << 0 << endr;
ppsi << [&](database_binder&dbi) { dbi << "one" << 1; } << endr;


} catch(sqlite_exception e) {
cout << "Unexpected error " << e.what() << endl;
Expand Down