Merge pull request #136 from fekir/fix_stream

Ensure that operator<< correctly prints strings with embedded '\0'
This commit is contained in:
Sébastien Rombauts 2017-08-21 11:10:57 +02:00 committed by GitHub
commit 9c15cf7c98
2 changed files with 24 additions and 1 deletions

View File

@ -116,7 +116,7 @@ int Column::getBytes() const noexcept // nothrow
// Standard std::ostream inserter // Standard std::ostream inserter
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn) std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
{ {
aStream << aColumn.getText(); aStream.write(aColumn.getText(), aColumn.getBytes());
return aStream; return aStream;
} }

View File

@ -199,3 +199,26 @@ TEST(Column, getName) {
EXPECT_EQ("msg", oname1); EXPECT_EQ("msg", oname1);
#endif #endif
} }
TEST(Column, stream) {
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
EXPECT_EQ(0, db.exec("CREATE TABLE test (msg TEXT)"));
SQLite::Statement insert(db, "INSERT INTO test VALUES (?)");
// content to test
const char str_[] = "stringwith\0embedded";
std::string str(str_, sizeof(str_)-1);
insert.bind(1, str);
// Execute the one-step query to insert the row
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(1, db.getTotalChanges());
SQLite::Statement query(db, "SELECT * FROM test");
query.executeStep();
std::stringstream ss;
ss << query.getColumn(0);
std::string content = ss.str();
EXPECT_EQ(content, str);
}