summaryrefslogtreecommitdiff
path: root/libmysqlpp
diff options
context:
space:
mode:
Diffstat (limited to 'libmysqlpp')
-rw-r--r--libmysqlpp/my-column.cpp4
-rw-r--r--libmysqlpp/my-command.cpp6
-rw-r--r--libmysqlpp/my-connection.cpp21
-rw-r--r--libmysqlpp/my-connection.h6
-rw-r--r--libmysqlpp/my-error.cpp24
-rw-r--r--libmysqlpp/my-error.h17
-rw-r--r--libmysqlpp/my-modifycommand.cpp4
-rw-r--r--libmysqlpp/my-selectcommand.cpp10
-rw-r--r--libmysqlpp/unittests/testmysql.cpp16
9 files changed, 60 insertions, 48 deletions
diff --git a/libmysqlpp/my-column.cpp b/libmysqlpp/my-column.cpp
index cc690be..14c0a58 100644
--- a/libmysqlpp/my-column.cpp
+++ b/libmysqlpp/my-column.cpp
@@ -26,10 +26,12 @@ MySQL::StringColumn::StringColumn(const char * name, unsigned int field, MYSQL_B
b->buffer_length = len;
b->length = &length;
}
+
MySQL::StringColumn::~StringColumn()
{
delete[] value;
}
+
void
MySQL::StringColumn::apply(DB::HandleField & h) const
{
@@ -40,6 +42,7 @@ MySQL::StringColumn::apply(DB::HandleField & h) const
h.string(value, length);
}
}
+
MySQL::NullColumn::NullColumn(const char * name, unsigned int field, MYSQL_BIND * b) :
ColumnBase(name, field)
{
@@ -48,6 +51,7 @@ MySQL::NullColumn::NullColumn(const char * name, unsigned int field, MYSQL_BIND
b->buffer = NULL;
b->buffer_length = 0;
}
+
void
MySQL::NullColumn::apply(DB::HandleField & h) const
{
diff --git a/libmysqlpp/my-command.cpp b/libmysqlpp/my-command.cpp
index bfa25e6..defb8a7 100644
--- a/libmysqlpp/my-command.cpp
+++ b/libmysqlpp/my-command.cpp
@@ -10,10 +10,10 @@ MySQL::Command::Command(const Connection * conn, const std::string & sql) :
paramsNeedBinding(false)
{
if (!stmt) {
- throw Error(mysql_error(&conn->conn));
+ throw Error(&conn->conn);
}
if (mysql_stmt_prepare(stmt, sql.c_str(), sql.length())) {
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
}
binds.resize(mysql_stmt_param_count(stmt));
if (binds.size()) {
@@ -165,7 +165,7 @@ MySQL::Command::bindParams()
{
if (paramsNeedBinding) {
if (mysql_stmt_bind_param(stmt, &binds.front())) {
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
paramsNeedBinding = false;
}
}
diff --git a/libmysqlpp/my-connection.cpp b/libmysqlpp/my-connection.cpp
index 3e1aed3..4aa0ab9 100644
--- a/libmysqlpp/my-connection.cpp
+++ b/libmysqlpp/my-connection.cpp
@@ -9,6 +9,11 @@
NAMEDFACTORY("mysql", MySQL::Connection, DB::ConnectionFactory);
+MySQL::ConnectionError::ConnectionError(MYSQL * m) :
+ MySQL::Error(m)
+{
+}
+
class Opts {
public:
Opts() { port = 3306; }
@@ -66,10 +71,10 @@ MySQL::Connection::Connection(const std::string & str) :
}
if (mysql_real_connect(&conn, ~o.server, ~o.user, ~o.password, ~o.database,
o.port, ~o.unix_socket, CLIENT_LOCAL_FILES | CLIENT_MULTI_STATEMENTS) == NULL) {
- throw ConnectionError();
+ throw ConnectionError(&conn);
}
if (mysql_set_character_set(&conn, "utf8")) {
- throw ConnectionError();
+ throw ConnectionError(&conn);
}
}
@@ -83,7 +88,7 @@ MySQL::Connection::finish() const
{
if (txDepth != 0) {
rollbackTx();
- throw Error("Transaction still open");
+ throw DB::TransactionStillOpen();
}
}
@@ -92,7 +97,7 @@ MySQL::Connection::beginTx() const
{
if (txDepth == 0) {
if (mysql_autocommit(&conn, 0)) {
- throw Error(mysql_error(&conn));
+ throw Error(&conn);
}
rolledback = false;
}
@@ -107,7 +112,7 @@ MySQL::Connection::commitTx() const
}
if (--txDepth == 0) {
if (mysql_commit(&conn)) {
- throw Error(mysql_error(&conn));
+ throw Error(&conn);
}
}
return txDepth;
@@ -118,7 +123,7 @@ MySQL::Connection::rollbackTx() const
{
if (--txDepth == 0) {
if (mysql_rollback(&conn)) {
- throw Error(mysql_error(&conn));
+ throw Error(&conn);
}
}
else {
@@ -149,7 +154,7 @@ void
MySQL::Connection::ping() const
{
if (mysql_ping(&conn)) {
- throw Error(mysql_error(&conn));
+ throw Error(&conn);
}
}
@@ -244,7 +249,7 @@ MySQL::Connection::endBulkUpload(const char * msg) const
if (!msg) {
if (ctx->loadReturn) {
ctx.reset();
- throw Error(mysql_error(&conn));
+ throw Error(&conn);
}
}
ctx.reset();
diff --git a/libmysqlpp/my-connection.h b/libmysqlpp/my-connection.h
index 1f7ec5a..d52bc06 100644
--- a/libmysqlpp/my-connection.h
+++ b/libmysqlpp/my-connection.h
@@ -7,7 +7,13 @@
#include <boost/shared_ptr.hpp>
namespace MySQL {
+ class ConnectionError : public virtual Error, public virtual DB::ConnectionError {
+ public:
+ ConnectionError(MYSQL *);
+ };
+
class LoadContext;
+
class Connection : public DB::Connection {
public:
Connection(const std::string & info);
diff --git a/libmysqlpp/my-error.cpp b/libmysqlpp/my-error.cpp
index c35a3a1..4867e0d 100644
--- a/libmysqlpp/my-error.cpp
+++ b/libmysqlpp/my-error.cpp
@@ -1,29 +1,19 @@
#include "my-error.h"
#include <string.h>
-MySQL::Error::Error() :
- msg(NULL)
+MySQL::Error::Error(MYSQL * m) :
+ msg(mysql_error(m))
{
}
-MySQL::Error::Error(const MySQL::Error & e) :
- msg(e.msg ? strdup(e.msg) : NULL)
+MySQL::Error::Error(MYSQL_STMT * m) :
+ msg(mysql_stmt_error(m))
{
}
-MySQL::Error::Error(const char * e) :
- msg(e ? strdup(e) : NULL)
+std::string
+MySQL::Error::message() const throw()
{
-}
-
-MySQL::Error::~Error() throw()
-{
- free(msg);
-}
-
-const char *
-MySQL::Error::what() const throw()
-{
- return msg ? msg : "No message";
+ return msg;
}
diff --git a/libmysqlpp/my-error.h b/libmysqlpp/my-error.h
index b4f93b2..b694843 100644
--- a/libmysqlpp/my-error.h
+++ b/libmysqlpp/my-error.h
@@ -2,21 +2,18 @@
#define MY_ERROR_H
#include <error.h>
+#include <mysql.h>
+#include <exception.h>
namespace MySQL {
- class Error : public DB::Error {
+ class Error : public AdHoc::Exception<DB::Error> {
public:
- Error();
- Error(const Error &);
- Error(const char *);
- ~Error() throw();
-
- const char * what() const throw();
+ Error(MYSQL_STMT *);
+ Error(MYSQL *);
+ std::string message() const throw() override;
private:
- char * msg;
- };
- class ConnectionError : public Error, public virtual DB::ConnectionError {
+ std::string msg;
};
}
diff --git a/libmysqlpp/my-modifycommand.cpp b/libmysqlpp/my-modifycommand.cpp
index c66c628..9326afe 100644
--- a/libmysqlpp/my-modifycommand.cpp
+++ b/libmysqlpp/my-modifycommand.cpp
@@ -19,11 +19,11 @@ MySQL::ModifyCommand::execute(bool anc)
{
bindParams();
if (mysql_stmt_execute(stmt)) {
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
}
int rows = mysql_stmt_affected_rows(stmt);
if (rows == 0 && !anc) {
- throw Error("No rows affected");
+ throw DB::NoRowsAffected();
}
return rows;
}
diff --git a/libmysqlpp/my-selectcommand.cpp b/libmysqlpp/my-selectcommand.cpp
index 257f33f..5be6bf0 100644
--- a/libmysqlpp/my-selectcommand.cpp
+++ b/libmysqlpp/my-selectcommand.cpp
@@ -62,21 +62,21 @@ MySQL::SelectCommand::execute()
case MYSQL_TYPE_GEOMETRY:
default:
mysql_free_result(prepare_meta_result);
- throw Error("Unexpected type");
+ throw DB::ColumnTypeNotSupported();
}
}
mysql_free_result(prepare_meta_result);
if (mysql_stmt_bind_result(stmt, &fields.front())) {
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
}
prepared = true;
}
if (!executed) {
if (mysql_stmt_execute(stmt)) {
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
}
if (mysql_stmt_store_result(stmt)) {
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
}
executed = true;
}
@@ -93,7 +93,7 @@ MySQL::SelectCommand::fetch()
executed = false;
return false;
default:
- throw Error(mysql_stmt_error(stmt));
+ throw Error(stmt);
}
}
diff --git a/libmysqlpp/unittests/testmysql.cpp b/libmysqlpp/unittests/testmysql.cpp
index 827ae84..f8c46b6 100644
--- a/libmysqlpp/unittests/testmysql.cpp
+++ b/libmysqlpp/unittests/testmysql.cpp
@@ -2,10 +2,12 @@
#include <boost/test/unit_test.hpp>
#include <my-mock.h>
+#include <my-error.h>
#include <definedDirs.h>
-#include <dbpp/modifycommand.h>
-#include <dbpp/selectcommand.h>
-#include <dbpp/column.h>
+#include <modifycommand.h>
+#include <selectcommand.h>
+#include <column.h>
+#include <connection.h>
#include <testCore.h>
#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>
@@ -157,5 +159,13 @@ BOOST_AUTO_TEST_CASE( insertId )
delete ro;
}
+BOOST_AUTO_TEST_CASE( errors )
+{
+ auto ro = DB::MockDatabase::openConnectionTo("mysqlmock");
+ BOOST_REQUIRE_THROW(ro->execute("nonsense"), DB::Error);
+ delete ro;
+ BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("mysql", "server=nohost"), DB::ConnectionError);
+}
+
BOOST_AUTO_TEST_SUITE_END();