boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
collation.hpp
1//
2// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BOOST_SQLITE_COLLATION_HPP
9#define BOOST_SQLITE_COLLATION_HPP
10
11#include <boost/sqlite/connection.hpp>
12#include <boost/sqlite/detail/exception.hpp>
13
14BOOST_SQLITE_BEGIN_NAMESPACE
15
17
48template<typename Func>
50 connection & conn,
51 cstring_ref name,
52 Func && func,
53 typename std::enable_if<
54 std::is_convertible<
55 decltype(func(string_view(), string_view())),
56 int>::value,
57 system::error_code>::type & ec)
58{
59 using func_type = typename std::decay<Func>::type;
60 auto f = new (memory_tag{}) func_type(std::forward<Func>(func));
61 if (f == nullptr)
62 {
63 BOOST_SQLITE_ASSIGN_EC(ec, SQLITE_NOMEM);
64 return;
65 }
66 auto res = sqlite3_create_collation_v2(
67 conn.handle(),
68 name.c_str(),
69 SQLITE_UTF8,
70 f,
71 +[](void * data, int len_l, const void * str_l, int len_r, const void * str_r) -> int
72 {
73 string_view l(static_cast<const char*>(str_l), len_l);
74 string_view r(static_cast<const char*>(str_r), len_r);
75 auto & impl = (*static_cast<func_type*>(data));
76 static_assert(noexcept(impl(l, r)),
77 "Collation function must be noexcept");
78 return impl(l, r);
79 },
80 +[](void * p) { delete_(static_cast<func_type*>(p)); }
81 );
82 if (res != SQLITE_OK)
83 BOOST_SQLITE_ASSIGN_EC(ec, res);
84}
85
86
87template<typename Func>
89 connection & conn,
90 cstring_ref name,
91 Func && func)
92#if !defined(BOOST_SQLITE_GENERATING_DOCS)
93 -> typename std::enable_if<
94 std::is_convertible<
95 decltype(func(string_view(), string_view())),
96 int>::value>::type
97#endif
98{
99 system::error_code ec;
100 create_collation(conn, name, std::forward<Func>(func), ec);
101 if (ec)
102 detail::throw_error_code(ec, BOOST_CURRENT_LOCATION);
103}
105
106BOOST_SQLITE_END_NAMESPACE
107
108#endif //BOOST_SQLITE_COLLATION_HPP
void create_collation(connection &conn, cstring_ref name, Func &&func, typename std::enable_if< std::is_convertible< decltype(func(string_view(), string_view())), int >::value, system::error_code >::type &ec)
Definition collation.hpp:49
main object for a connection to a database.
Small wrapper for a null-terminated string that can be directly passed to C APIS.