From dc18a54ce75ae59f7d414361b1c9781a27c14b20 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 24 Dec 2015 17:30:41 +0000 Subject: Check stream state before execute --- libdbpp/connection.cpp | 4 ++++ libdbpp/unittests/testConnection.cpp | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libdbpp/connection.cpp b/libdbpp/connection.cpp index 24d19f9..333becb 100644 --- a/libdbpp/connection.cpp +++ b/libdbpp/connection.cpp @@ -1,5 +1,6 @@ #include "connection.h" #include "modifycommand.h" +#include "error.h" #include #include #include @@ -19,6 +20,9 @@ DB::Connection::execute(const std::string & sql) const void DB::Connection::executeScript(std::istream & f, const boost::filesystem::path & s) const { + if (!f.good()) { + throw SqlParseException("Script stream not in good state.", 0); + } DB::SqlParse p(f, s, this); while (p.yylex()) ; } diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp index 86ca2a8..a40c5cc 100644 --- a/libdbpp/unittests/testConnection.cpp +++ b/libdbpp/unittests/testConnection.cpp @@ -66,11 +66,18 @@ BOOST_AUTO_TEST_CASE( resolve ) BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("otherdb", "doesn't matter"), AdHoc::LoadLibraryException); } +BOOST_AUTO_TEST_CASE( parseBad ) +{ + auto mock = DB::ConnectionFactory::createNew("MockDb", "doesn't matter"); + std::fstream s("/bad"); + BOOST_REQUIRE_THROW(mock->executeScript(s, rootDir), DB::SqlParseException); + delete mock; +} + BOOST_AUTO_TEST_CASE( parse ) { auto mock = DB::ConnectionFactory::createNew("MockDb", "doesn't matter"); std::fstream s((rootDir / "parseTest.sql").string()); - BOOST_REQUIRE(s.good()); mock->executeScript(s, rootDir); MockDb * mockdb = dynamic_cast(mock); BOOST_REQUIRE(mockdb); @@ -84,7 +91,7 @@ BOOST_AUTO_TEST_CASE( parse2 ) auto mock = DB::ConnectionPtr(DB::ConnectionFactory::createNew("MockDb", "doesn't matter")); auto mockdb = boost::dynamic_pointer_cast(mock); BOOST_REQUIRE(mockdb); - std::fstream s; + std::ifstream s; s.open((rootDir / "dollarQuote.sql").string()); mock->executeScript(s, rootDir); -- cgit v1.2.3