summaryrefslogtreecommitdiff
path: root/include/boost/sqlite/detail/catch.hpp
blob: e5b053ff2456518d9f9e5024cc59c3f399fe0251 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_SQLITE_DETAIL_CATCH_HPP
#define BOOST_SQLITE_DETAIL_CATCH_HPP

#include <boost/sqlite/detail/config.hpp>
#include <boost/sqlite/detail/exception.hpp>
#include <boost/sqlite/error.hpp>
#include <boost/sqlite/result.hpp>

#include <boost/system/system_error.hpp>

#include <stdexcept>


BOOST_SQLITE_BEGIN_NAMESPACE
namespace detail
{

template<typename Func, typename ... Args>
void execute_context_function_impl(std::false_type /* is_void */,
                                   sqlite3_context * ctx,
                                   Func && func, Args && ... args)
    noexcept(noexcept(std::forward<Func>(func)(std::forward<Args>(args)...)))
{
  set_result(ctx, std::forward<Func>(func)(std::forward<Args>(args)...));
}

template<typename Func, typename ... Args>
void execute_context_function_impl(std::true_type  /* is_void */,
                                   sqlite3_context * ctx,
                                   Func && func, Args && ... args)
    noexcept(noexcept(std::forward<Func>(func)(std::forward<Args>(args)...)))
{
  std::forward<Func>(func)(std::forward<Args>(args)...);
}

template<typename T>
int extract_error(char * &errMsg, result<T> & res)
{
  error err = std::move(res).error();
  if (err.info)
    errMsg = err.info.release();
  return err.code;
}


template<typename Func, typename ... Args>
void execute_context_function(sqlite3_context * ctx,
                              Func && func, Args && ... args) noexcept
{
  using return_type = decltype(func(std::forward<Args>(args)...));
#if !defined(BOOST_NO_EXCEPTIONS)
  try
  {
#endif
    execute_context_function_impl(std::is_void<return_type>{}, ctx,
                                  std::forward<Func>(func),
                                  std::forward<Args>(args)...);
#if !defined(BOOST_NO_EXCEPTIONS)
  }
  catch(boost::system::system_error & se)
  {
     const auto msg = boost::sqlite::detail::get_message(se);
    if (!msg.empty())
      sqlite3_result_error(ctx, msg.data(), msg.size());
    if (se.code().category() == boost::sqlite::sqlite_category())
      sqlite3_result_error_code(ctx, se.code().value());
  }
  catch(std::bad_alloc    &) { sqlite3_result_error_nomem(ctx); }
  catch(std::length_error &) { sqlite3_result_error_toobig(ctx); }
  catch(std::out_of_range &) { sqlite3_result_error_code(ctx, SQLITE_RANGE);}
  catch(std::logic_error &le)
  {
    sqlite3_result_error(ctx, le.what(), std::strlen(le.what()));
    sqlite3_result_error_code(ctx, SQLITE_MISUSE);
  }
  catch(std::exception & ex)
  {
    sqlite3_result_error(ctx, ex.what(), std::strlen(ex.what()));
  }
  catch(...) {sqlite3_result_error_code(ctx, SQLITE_ERROR); }
#endif
}

}
BOOST_SQLITE_END_NAMESPACE

#if defined(BOOST_NO_EXCEPTIONS)
#define BOOST_SQLITE_TRY
#define BOOST_SQLITE_CATCH_RESULT(ctx)
#define BOOST_SQLITE_CATCH_ASSIGN_STR_AND_RETURN(msg)
#define BOOST_SQLITE_CATCH_AND_RETURN()

#else

#define BOOST_SQLITE_TRY try
#define BOOST_SQLITE_CATCH_RESULT(ctx)                                       \
catch(boost::system::system_error & se)                                      \
{                                                                            \
  if (se.code().category() == boost::sqlite::sqlite_category())              \
    sqlite3_result_error_code(ctx, se.code().value());                       \
  const auto msg = boost::sqlite::detail::get_message(se);                   \
  if (!msg.empty())                                                          \
    sqlite3_result_error(ctx, msg.data(), msg.size());                       \
}                                                                            \
catch(std::bad_alloc    &) { sqlite3_result_error_nomem(ctx); }              \
catch(std::length_error &) { sqlite3_result_error_toobig(ctx); }             \
catch(std::out_of_range &) { sqlite3_result_error_code(ctx, SQLITE_RANGE);}  \
catch(std::logic_error &le)                                                  \
{                                                                            \
  sqlite3_result_error(ctx, le.what(), std::strlen(le.what()));              \
  sqlite3_result_error_code(ctx, SQLITE_MISUSE);                             \
}                                                                            \
catch(std::exception & ex)                                                   \
{                                                                            \
  sqlite3_result_error(ctx, ex.what(), std::strlen(ex.what()));              \
}                                                                            \
catch(...) {sqlite3_result_error_code(ctx, SQLITE_ERROR); }


#define BOOST_SQLITE_CATCH_ASSIGN_STR_AND_RETURN(msg)                        \
catch (boost::system::system_error & se)                                     \
{                                                                            \
  auto code = SQLITE_ERROR;                                                  \
  if (se.code().category() == boost::sqlite::sqlite_category())              \
    code = se.code().value();                                                \
  const auto pre = boost::sqlite::detail::get_message(se);                   \
  msg = boost::sqlite::error_info(pre).release();                            \
  return code;                                                               \
}                                                                            \
catch(std::bad_alloc    &) { return SQLITE_NOMEM; }                          \
catch(std::length_error &) { return SQLITE_TOOBIG; }                         \
catch(std::out_of_range &) { return SQLITE_RANGE;}                           \
catch(std::logic_error &le)                                                  \
{                                                                            \
    msg = boost::sqlite::error_info(le.what()).release();                    \
    return SQLITE_MISUSE;                                                    \
}                                                                            \
catch(std::exception & ex)                                                   \
{                                                                            \
    msg = boost::sqlite::error_info(ex.what()).release();                    \
    return SQLITE_ERROR;                                                     \
}                                                                            \
catch(...) {return SQLITE_ERROR; }                                           \


#define BOOST_SQLITE_CATCH_AND_RETURN()                                      \
catch (boost::system::system_error & se)                                     \
{                                                                            \
  auto code = SQLITE_ERROR;                                                  \
  if (se.code().category() == boost::sqlite::sqlite_category())              \
    code = se.code().value();                                                \
  return code;                                                               \
}                                                                            \
catch(std::bad_alloc    &) { return SQLITE_NOMEM; }                          \
catch(std::length_error &) { return SQLITE_TOOBIG; }                         \
catch(std::out_of_range &) { return SQLITE_RANGE;}                           \
catch(std::logic_error  &) { return SQLITE_MISUSE;}                          \
catch(...) { return SQLITE_ERROR; }                                          \

#endif

#endif //BOOST_SQLITE_DETAIL_CATCH_HPP