Added additional exception class.

Handles the case of user defined exception in pl/pgsql to it can be reported
back to the calling application.
This commit is contained in:
Mike Neilson
2022-03-10 20:11:30 -08:00
committed by Roland Bock
parent b965b2a1df
commit 6477f09125
3 changed files with 50 additions and 1 deletions

View File

@@ -24,7 +24,10 @@
*/
#include <sqlpp11/exception.h>
#include <sqlpp11/custom_query.h>
#include <sqlpp11/postgresql/postgresql.h>
#include <sqlpp11/verbatim.h>
#include <sqlpp11/alias_provider.h>
#include "assertThrow.h"
@@ -32,6 +35,8 @@
#include "TabFoo.h"
#include "make_test_connection.h"
SQLPP_ALIAS_PROVIDER(returnVal)
namespace sql = sqlpp::postgresql;
int Exceptions(int, char*[])
{
@@ -57,12 +62,29 @@ int Exceptions(int, char*[])
c_timepoint timestamp with time zone DEFAULT now(),
c_day date
))");
db.execute(R"(create or replace function cause_error() returns int as $$
begin
raise exception 'User error' USING ERRCODE='ZX123';
end;
$$ language plpgsql
)");
assert_throw(db(insert_into(foo).set(foo.beta = std::numeric_limits<int16_t>::max() + 1)), sql::data_exception);
assert_throw(db(insert_into(foo).set(foo.gamma = "123456")), sql::check_violation);
db(insert_into(foo).set(foo.beta = 5));
assert_throw(db(insert_into(foo).set(foo.beta = 5)), sql::integrity_constraint_violation);
assert_throw(db.last_insert_id("tabfoo", "no_such_column"), sqlpp::postgresql::undefined_table);
assert_throw(db.last_insert_id("tabfoo", "no_such_column"), sqlpp::postgresql::undefined_table);
try
{
db.execute("select cause_error();");
}
catch( const sql::sql_user_error& e)
{
std::cout << e.code();
assert( e.code() == "ZX123");
}
}
catch (const sql::failure& e)
{