blob: 243088b76de4818b61c85418cc2624ee2f9a6356 [file] [log] [blame]
Mickey Sweatt7f177902015-06-10 17:20:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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
22#include "sqlite3-statement.hpp"
23
24#include <sqlite3.h>
25
26namespace ndn {
27namespace util {
28
29Sqlite3Statement::~Sqlite3Statement()
30{
31 sqlite3_finalize(m_stmt);
32}
33
34Sqlite3Statement::Sqlite3Statement(sqlite3* database, const std::string& statement)
35{
36 int res = sqlite3_prepare_v2(database, statement.c_str(), -1, &m_stmt, nullptr);
37 if (res != SQLITE_OK)
38 throw std::domain_error("bad SQL statement: " + statement);
39}
40
41int
42Sqlite3Statement::bind(int index, const char* value, size_t size, void(*destructor)(void*))
43{
44 return sqlite3_bind_text(m_stmt, index, value, size, destructor);
45}
46
47int
48Sqlite3Statement::bind(int index, const std::string& value, void(*destructor)(void*))
49{
50 return sqlite3_bind_text(m_stmt, index, value.c_str(), value.size(), destructor);
51}
52
53int
54Sqlite3Statement::bind(int index, const void* buf, size_t size, void(*destructor)(void*))
55{
56 return sqlite3_bind_blob(m_stmt, index, buf, size, destructor);
57}
58
59int
60Sqlite3Statement::bind(int index, const Block& block, void(*destructor)(void*))
61{
62 return sqlite3_bind_blob(m_stmt, index, block.wire(), block.size(), destructor);
63}
64
65int
66Sqlite3Statement::bind(int index, int number)
67{
68 return sqlite3_bind_int(m_stmt, index, number);
69}
70
71std::string
72Sqlite3Statement::getString(int column)
73{
74 return std::string(reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, column)),
75 sqlite3_column_bytes(m_stmt, column));
76}
77
78Block
79Sqlite3Statement::getBlock(int column)
80{
81 return Block(sqlite3_column_blob(m_stmt, column), sqlite3_column_bytes(m_stmt, column));
82}
83
84int
85Sqlite3Statement::getInt(int column)
86{
87 return sqlite3_column_int(m_stmt, column);
88}
89
90
91const uint8_t*
92Sqlite3Statement::getBlob(int column)
93{
94 return static_cast<const uint8_t*>(sqlite3_column_blob(m_stmt, column));
95}
96
97int
98Sqlite3Statement::getSize(int column)
99{
100 return sqlite3_column_bytes(m_stmt, column);
101}
102
103int
104Sqlite3Statement::step()
105{
106 return sqlite3_step(m_stmt);
107}
108
109Sqlite3Statement::operator sqlite3_stmt*()
110{
111 return m_stmt;
112}
113
114} // namespace util
115} // namespace ndn