Skip to content

Commit

Permalink
Merge pull request #56 from aminroosta/gcc611
Browse files Browse the repository at this point in the history
fix #55
  • Loading branch information
aminroosta committed Jul 5, 2016
2 parents 186d087 + 3079eaf commit 5910571
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
37 changes: 25 additions & 12 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ namespace sqlite {
template<typename T> friend database_binder& operator <<(database_binder& db, const std::unique_ptr<T>& val);
template<typename T> friend void get_col_from_db(database_binder& db, int inx, std::unique_ptr<T>& val);
template<typename T> friend T operator++(database_binder& db, int);
// Overload instead of specializing function templates (http://www.gotw.ca/publications/mill17.htm)
friend database_binder& operator<<(database_binder& db, const int& val);
friend void get_col_from_db(database_binder& db, int inx, int& val);
friend database_binder& operator <<(database_binder& db, const sqlite_int64& val);
friend void get_col_from_db(database_binder& db, int inx, sqlite3_int64& i);
friend database_binder& operator <<(database_binder& db, const float& val);
friend void get_col_from_db(database_binder& db, int inx, float& f);
friend database_binder& operator <<(database_binder& db, const double& val);
friend void get_col_from_db(database_binder& db, int inx, double& d);
friend void get_col_from_db(database_binder& db, int inx, std::string & s);
friend database_binder& operator <<(database_binder& db, const std::string& txt);
friend void get_col_from_db(database_binder& db, int inx, std::u16string & w);
friend database_binder& operator <<(database_binder& db, const std::u16string& txt);


#ifdef _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT
Expand Down Expand Up @@ -370,15 +383,15 @@ namespace sqlite {
};

// int
template<> inline database_binder& operator<<(database_binder& db, const int& val) {
inline database_binder& operator<<(database_binder& db, const int& val) {
int hresult;
if((hresult = sqlite3_bind_int(db._stmt.get(), db._inx, val)) != SQLITE_OK) {
exceptions::throw_sqlite_error(hresult);
}
++db._inx;
return db;
}
template<> inline void get_col_from_db(database_binder& db, int inx, int& val) {
inline void get_col_from_db(database_binder& db, int inx, int& val) {
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
val = 0;
} else {
Expand All @@ -387,7 +400,7 @@ namespace sqlite {
}

// sqlite_int64
template<> inline database_binder& operator <<(database_binder& db, const sqlite_int64& val) {
inline database_binder& operator <<(database_binder& db, const sqlite_int64& val) {
int hresult;
if((hresult = sqlite3_bind_int64(db._stmt.get(), db._inx, val)) != SQLITE_OK) {
exceptions::throw_sqlite_error(hresult);
Expand All @@ -396,7 +409,7 @@ namespace sqlite {
++db._inx;
return db;
}
template<> inline void get_col_from_db(database_binder& db, int inx, sqlite3_int64& i) {
inline void get_col_from_db(database_binder& db, int inx, sqlite3_int64& i) {
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
i = 0;
} else {
Expand All @@ -405,7 +418,7 @@ namespace sqlite {
}

// float
template<> inline database_binder& operator <<(database_binder& db, const float& val) {
inline database_binder& operator <<(database_binder& db, const float& val) {
int hresult;
if((hresult = sqlite3_bind_double(db._stmt.get(), db._inx, double(val))) != SQLITE_OK) {
exceptions::throw_sqlite_error(hresult);
Expand All @@ -414,7 +427,7 @@ namespace sqlite {
++db._inx;
return db;
}
template<> inline void get_col_from_db(database_binder& db, int inx, float& f) {
inline void get_col_from_db(database_binder& db, int inx, float& f) {
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
f = 0;
} else {
Expand All @@ -423,7 +436,7 @@ namespace sqlite {
}

// double
template<> inline database_binder& operator <<(database_binder& db, const double& val) {
inline database_binder& operator <<(database_binder& db, const double& val) {
int hresult;
if((hresult = sqlite3_bind_double(db._stmt.get(), db._inx, val)) != SQLITE_OK) {
exceptions::throw_sqlite_error(hresult);
Expand All @@ -432,7 +445,7 @@ namespace sqlite {
++db._inx;
return db;
}
template<> inline void get_col_from_db(database_binder& db, int inx, double& d) {
inline void get_col_from_db(database_binder& db, int inx, double& d) {
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
d = 0;
} else {
Expand Down Expand Up @@ -491,7 +504,7 @@ namespace sqlite {
}

// std::string
template<> inline void get_col_from_db(database_binder& db, int inx, std::string & s) {
inline void get_col_from_db(database_binder& db, int inx, std::string & s) {
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
s = std::string();
} else {
Expand All @@ -504,7 +517,7 @@ namespace sqlite {
template<std::size_t N> inline database_binder& operator <<(database_binder& db, const char(&STR)[N]) { return db << std::string(STR); }
template<std::size_t N> inline database_binder& operator <<(database_binder& db, const char16_t(&STR)[N]) { return db << std::u16string(STR); }

template<> inline database_binder& operator <<(database_binder& db, const std::string& txt) {
inline database_binder& operator <<(database_binder& db, const std::string& txt) {
int hresult;
if((hresult = sqlite3_bind_text(db._stmt.get(), db._inx, txt.data(), -1, SQLITE_TRANSIENT)) != SQLITE_OK) {
exceptions::throw_sqlite_error(hresult);
Expand All @@ -514,7 +527,7 @@ namespace sqlite {
return db;
}
// std::u16string
template<> inline void get_col_from_db(database_binder& db, int inx, std::u16string & w) {
inline void get_col_from_db(database_binder& db, int inx, std::u16string & w) {
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
w = std::u16string();
} else {
Expand All @@ -524,7 +537,7 @@ namespace sqlite {
}


template<> inline database_binder& operator <<(database_binder& db, const std::u16string& txt) {
inline database_binder& operator <<(database_binder& db, const std::u16string& txt) {
int hresult;
if((hresult = sqlite3_bind_text16(db._stmt.get(), db._inx, txt.data(), -1, SQLITE_TRANSIENT)) != SQLITE_OK) {
exceptions::throw_sqlite_error(hresult);
Expand Down
3 changes: 1 addition & 2 deletions tests/boost_optional.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ void insert(database& db, bool is_null) {
}

void select(database& db, bool should_be_null) {
db << "select id,val from test" >> [&](long long id, boost::optional<int> val) {
id = id;
db << "select id,val from test" >> [&](long long, boost::optional<int> val) {
if(should_be_null) {
if(val) exit(EXIT_FAILURE);
} else {
Expand Down
4 changes: 3 additions & 1 deletion tests/nullptr_uniqueptr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ int main() {
}

cout << "age:" << *age_p << " name:" << *name_p << " img:";
for(auto i : *img_p) cout << i << ","; cout << endl;
for(auto i : *img_p)
cout << i << ",";
cout << endl;
};

db << "select age,name,img from tbl where id = 2" >> [](unique_ptr<int> age_p, unique_ptr<string> name_p, unique_ptr<vector<int>> img_p) {
Expand Down
10 changes: 6 additions & 4 deletions tests/readme_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main() {
<< u"bob"
<< 83.25;

int age = 21;
int age = 22;
float weight = 68.5;
string name = "jack";
db << u"insert into user (age,name,weight) values (?,?,?);" // utf16 query string
Expand All @@ -43,9 +43,11 @@ int main() {
// slects from user table on a condition ( age > 18 ) and executes
// the lambda for each row returned .
db << "select age,name,weight from user where age > ? ;"
<< 18
>> [&](int age, string name, double weight) {
cout << age << ' ' << name << ' ' << weight << endl;
<< 21
>> [&](int _age, string _name, double _weight) {
if(_age != age || _name != name)
exit(EXIT_FAILURE);
cout << _age << ' ' << _name << ' ' << _weight << endl;
};

// selects the count(*) from user table
Expand Down

0 comments on commit 5910571

Please sign in to comment.