summaryrefslogtreecommitdiff
path: root/libodbcpp/odbc-error.cpp
blob: d2379aee23ffb1e17c77c542aff8fceb22023e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "odbc-error.h"
#include "error.h"
#include <array>
#include <compileTimeFormatter.h>
#include <cstddef>
// IWYU pragma: no_include <boost/test/unit_test.hpp>

namespace AdHoc {
	StreamWriterT('5') {
		template<std::size_t l, typename... Pn>
		static void
		write(stream & s, const std::array<SQLCHAR, l> & sqlstatus, const Pn &... pn)
		{
			static_assert(l > 5);
			s.write(reinterpret_cast<const char * const>(sqlstatus.data()), 5);
			StreamWriter::next(s, pn...);
		}
	};
}

AdHocFormatter(ODBCErrorWithInfo, "%?: %?: %5: \"%?\"");
AdHocFormatter(ODBCErrorInvalidHandle, "(%?) Invalid handle passed into function");
AdHocFormatter(ODBCErrorNoData, "(%?) No error data available for record");
AdHocFormatter(ODBCError, "Failed to get diagnostics for return code %?");
ODBC::Error::Error(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle)
{
	std::array<SQLCHAR, 6> sqlstatus {};
	SQLINTEGER sqlerr;
	std::array<SQLCHAR, 12800> sqlerrmsg {};

	SQLRETURN rc = SQLGetDiagRec(
			handletype, handle, 1, sqlstatus.data(), &sqlerr, sqlerrmsg.data(), sqlerrmsg.size(), nullptr);
	switch (rc) {
		case SQL_SUCCESS:
		case SQL_SUCCESS_WITH_INFO:
			msg = ODBCErrorWithInfo::get(err, sqlerr, sqlstatus, sqlerrmsg.data());
			break;

		case SQL_INVALID_HANDLE:
			msg = ODBCErrorInvalidHandle::get(err);
			break;

		case SQL_NO_DATA:
			msg = ODBCErrorNoData::get(err);
			break;

		case SQL_ERROR:
		default:
			msg = ODBCError::get(err);
			break;
	}
}

std::string
ODBC::Error::message() const noexcept
{
	return msg;
}