From 4acabe57939ff37bad0a8aabc5164b6d5add449b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 18 Feb 2011 17:56:53 +0200 Subject: Factor out MySQL error to exception translation into separate function --- odb/mysql/error.cxx | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 odb/mysql/error.cxx (limited to 'odb/mysql/error.cxx') diff --git a/odb/mysql/error.cxx b/odb/mysql/error.cxx new file mode 100644 index 0000000..460816a --- /dev/null +++ b/odb/mysql/error.cxx @@ -0,0 +1,60 @@ +// file : odb/mysql/errors.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::bad_alloc + +#include +#include +#include + +using namespace std; + +namespace odb +{ + namespace mysql + { + void + translate_error (connection&, + unsigned int e, + const std::string& sqlstate, + const std::string& message) + { + switch (e) + { + case CR_OUT_OF_MEMORY: + { + throw bad_alloc (); + } + case ER_LOCK_DEADLOCK: + { + throw deadlock (); + } + default: + { + throw database_exception (e, sqlstate, message); + } + } + } + + void + translate_error (connection& c) + { + MYSQL* h (c.handle ()); + translate_error (c, + mysql_errno (h), + mysql_sqlstate (h), + mysql_error (h)); + } + + void + translate_error (connection& c, MYSQL_STMT* h) + { + translate_error (c, + mysql_stmt_errno (h), + mysql_stmt_sqlstate (h), + mysql_stmt_error (h)); + } + } +} -- cgit v1.1