sqlite3-statement.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
23 
24 #include <sqlite3.h>
25 
26 namespace ndn::util {
27 
29 {
30  sqlite3_finalize(m_stmt);
31 }
32 
33 Sqlite3Statement::Sqlite3Statement(sqlite3* database, const std::string& statement)
34 {
35  int res = sqlite3_prepare_v2(database, statement.data(), -1, &m_stmt, nullptr);
36  if (res != SQLITE_OK)
37  NDN_THROW(std::domain_error("bad SQL statement: " + statement));
38 }
39 
40 int
41 Sqlite3Statement::bind(int index, const char* value, size_t size, void(*destructor)(void*))
42 {
43  return sqlite3_bind_text(m_stmt, index, value, size, destructor);
44 }
45 
46 int
47 Sqlite3Statement::bind(int index, const std::string& value, void(*destructor)(void*))
48 {
49  return sqlite3_bind_text(m_stmt, index, value.data(), value.size(), destructor);
50 }
51 
52 int
53 Sqlite3Statement::bind(int index, const void* buf, size_t size, void(*destructor)(void*))
54 {
55  return sqlite3_bind_blob(m_stmt, index, buf, size, destructor);
56 }
57 
58 int
59 Sqlite3Statement::bind(int index, const Block& block, void(*destructor)(void*))
60 {
61  return sqlite3_bind_blob(m_stmt, index, block.data(), block.size(), destructor);
62 }
63 
64 int
65 Sqlite3Statement::bind(int index, int number)
66 {
67  return sqlite3_bind_int(m_stmt, index, number);
68 }
69 
70 std::string
72 {
73  return std::string(reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, column)),
74  sqlite3_column_bytes(m_stmt, column));
75 }
76 
77 Block
79 {
80  return Block(make_span(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, column)),
81  sqlite3_column_bytes(m_stmt, column)));
82 }
83 
84 int
86 {
87  return sqlite3_column_int(m_stmt, column);
88 }
89 
90 const uint8_t*
92 {
93  return static_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, column));
94 }
95 
96 int
98 {
99  return sqlite3_column_bytes(m_stmt, column);
100 }
101 
102 int
104 {
105  return sqlite3_step(m_stmt);
106 }
107 
108 Sqlite3Statement::operator sqlite3_stmt*()
109 {
110  return m_stmt;
111 }
112 
113 } // namespace ndn::util
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
const uint8_t * data() const
Returns a raw pointer to the beginning of the encoded wire, i.e., the whole TLV.
Definition: block.cpp:256
size_t size() const
Returns the size of the encoded wire, i.e., of the whole TLV.
Definition: block.cpp:265
Sqlite3Statement(sqlite3 *database, const std::string &statement)
Initialize and prepare Sqlite3 statement.
int bind(int index, const char *value, size_t size, void(*destructor)(void *))
Bind a string to the statement.
int getInt(int column)
Get an integer from column.
const uint8_t * getBlob(int column)
Get a pointer of byte blob from column.
Block getBlock(int column)
Get a block from column.
~Sqlite3Statement()
Finalize the statement.
int getSize(int column)
Get the size of column.
int step()
Wrapper of sqlite3_step.
std::string getString(int column)
Get a string from column.
#define NDN_THROW(e)
Definition: exception.hpp:56