summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-11-09 18:28:47 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-11-09 18:28:47 +0000
commitbbd49f15810737fb28e8fa933b264059735d49b2 (patch)
tree8a6ae27ab7be60f55ad0da7bbc42fe1a88a9d076
parentAdd exception messages with adhoc exception (diff)
downloadlibdbpp-bbd49f15810737fb28e8fa933b264059735d49b2.tar.bz2
libdbpp-bbd49f15810737fb28e8fa933b264059735d49b2.tar.xz
libdbpp-bbd49f15810737fb28e8fa933b264059735d49b2.zip
Improve exceptions in SQL parser to include line number
-rw-r--r--libdbpp/sqlParse.h9
-rw-r--r--libdbpp/sqlParse.ll14
-rw-r--r--libdbpp/unittests/testConnection.cpp7
3 files changed, 24 insertions, 6 deletions
diff --git a/libdbpp/sqlParse.h b/libdbpp/sqlParse.h
index dcdf073..3c8add0 100644
--- a/libdbpp/sqlParse.h
+++ b/libdbpp/sqlParse.h
@@ -3,6 +3,7 @@
#include <istream>
#include <string>
+#include <exception.h>
#include "connection.h"
#include <boost/filesystem/path.hpp>
#ifndef yyFlexLexer
@@ -11,6 +12,14 @@
#endif
namespace DB {
+class DLL_PUBLIC SqlParseException : public AdHoc::StdException {
+ public:
+ SqlParseException(const char *, unsigned int);
+ private:
+ std::string message() const throw() override;
+ const char * reason;
+ const unsigned int line;
+};
/// @cond
class SqlParse : public yyFlexLexer {
diff --git a/libdbpp/sqlParse.ll b/libdbpp/sqlParse.ll
index 253b91f..5ab7899 100644
--- a/libdbpp/sqlParse.ll
+++ b/libdbpp/sqlParse.ll
@@ -9,6 +9,7 @@
%{
#include <stdexcept>
+#include <buffer.h>
#include "sqlParse.h"
#pragma GCC diagnostic ignored "-Wsign-compare"
%}
@@ -52,7 +53,7 @@ scriptdir "$SCRIPTDIR"
}
<COMMENT><<EOF>> {
- throw std::runtime_error("Unterminated comment");
+ throw SqlParseException("Unterminated comment", yylineno);
}
{comment} {
@@ -97,7 +98,7 @@ scriptdir "$SCRIPTDIR"
}
<DOLLARQUOTE><<EOF>> {
- throw std::runtime_error("Unterminated dollar quoted string");
+ throw SqlParseException("Unterminated dollar quoted string", yylineno);
}
<QUOTE>{any} {
@@ -105,7 +106,7 @@ scriptdir "$SCRIPTDIR"
}
<QUOTE><<EOF>> {
- throw std::runtime_error("Unterminated quoted string");
+ throw SqlParseException("Unterminated quoted string", yylineno);
}
<STATEMENT>{term} {
@@ -124,6 +125,13 @@ scriptdir "$SCRIPTDIR"
%%
namespace DB {
+ SqlParseException::SqlParseException(const char * r, unsigned int l) : reason(r), line(l) { }
+
+ std::string
+ SqlParseException::message() const throw()
+ {
+ return stringf("Error parsing SQL script: %s at line %u", reason, line);
+ }
SqlParse::SqlParse(std::istream & f, const boost::filesystem::path & s, const Connection * c) :
yyFlexLexer(&f, NULL),
diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp
index ed727de..b9cabdf 100644
--- a/libdbpp/unittests/testConnection.cpp
+++ b/libdbpp/unittests/testConnection.cpp
@@ -7,6 +7,7 @@
#include <fstream>
#include <vector>
#include <error.h>
+#include <sqlParse.h>
// LCOV_EXCL_START
class MockDb : public DB::Connection {
@@ -98,19 +99,19 @@ BOOST_AUTO_TEST_CASE( parse2 )
BOOST_REQUIRE_THROW({
s.open((rootDir / "unterminatedComment.sql").string());
mock->executeScript(s, rootDir);
- }, std::runtime_error);
+ }, DB::SqlParseException);
s.close();
BOOST_REQUIRE_THROW({
s.open((rootDir / "unterminatedDollarQuote.sql").string());
mock->executeScript(s, rootDir);
- }, std::runtime_error);
+ }, DB::SqlParseException);
s.close();
BOOST_REQUIRE_THROW({
s.open((rootDir / "unterminatedString.sql").string());
mock->executeScript(s, rootDir);
- }, std::runtime_error);
+ }, DB::SqlParseException);
s.close();
}