OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SqliteConnector.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #ifndef SQLITE_CONNECTOR
23 #define SQLITE_CONNECTOR
24 
25 #include <boost/lexical_cast.hpp>
26 #include <cassert>
27 #include <string>
28 #include <vector>
29 
30 #include <sqlite3.h>
31 
33  public:
34  SqliteConnector(const std::string& dbName, const std::string& dir = ".");
36  void query(const std::string& queryString);
37 
38  void query_with_text_params(std::string const& query_only) { query(query_only); }
39  template <typename STRING_CONTAINER>
40  void query_with_text_params(STRING_CONTAINER const& query_and_text_params) {
42  *query_and_text_params.begin(),
43  std::vector<std::string>{std::next(query_and_text_params.begin()),
44  query_and_text_params.end()});
45  }
46  void query_with_text_params(const std::string& queryString,
47  const std::vector<std::string>& text_param);
48  void query_with_text_param(const std::string& queryString,
49  const std::string& text_param);
50 
51  size_t getNumRows() const { return numRows_; }
52  size_t getNumCols() const { return numCols_; }
53 
54  template <typename T>
55  T getData(const int row, const int col) {
56  assert(row < static_cast<int>(numRows_));
57  assert(col < static_cast<int>(numCols_));
58  return boost::lexical_cast<T>(results_[col][row].result);
59  }
60 
61  bool isNull(const int row, const int col) const {
62  assert(row < static_cast<int>(numRows_));
63  assert(col < static_cast<int>(numCols_));
64  return results_[col][row].is_null;
65  }
66 
67  std::vector<std::string> columnNames; // make this public for easy access
68  std::vector<int> columnTypes;
69 
70  private:
71  struct NullableResult {
72  const std::string result;
73  const bool is_null;
74  };
75 
76  void throwError();
77 
78  sqlite3* db_;
79  std::string dbName_;
81  std::vector<std::vector<NullableResult>> results_;
82  size_t numCols_;
83  size_t numRows_;
84 };
85 
86 #endif // SQLITE_CONNECTOR
T getData(const int row, const int col)
void query_with_text_params(std::string const &query_only)
void query(const std::string &queryString)
std::vector< std::vector< NullableResult > > results_
size_t getNumRows() const
std::vector< int > columnTypes
bool isNull(const int row, const int col) const
int64_t const int32_t sz assert(dest)
std::string dbName_
void query_with_text_params(STRING_CONTAINER const &query_and_text_params)
SqliteConnector(const std::string &dbName, const std::string &dir=".")
size_t getNumCols() const
std::vector< std::string > columnNames
void query_with_text_param(const std::string &queryString, const std::string &text_param)