OmniSciDB  471d68cefb
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 = ".");
35  SqliteConnector(sqlite3* db);
37  virtual ~SqliteConnector();
38  virtual void query(const std::string& queryString);
39 
40  virtual void query_with_text_params(std::string const& query_only) {
41  query(query_only);
42  }
43  template <typename STRING_CONTAINER>
44  void query_with_text_params(STRING_CONTAINER const& query_and_text_params) {
46  *query_and_text_params.begin(),
47  std::vector<std::string>{std::next(query_and_text_params.begin()),
48  query_and_text_params.end()});
49  }
50  virtual void query_with_text_params(const std::string& queryString,
51  const std::vector<std::string>& text_param);
52 
53  enum class BindType { TEXT = 1, BLOB, NULL_TYPE };
54  virtual void query_with_text_params(const std::string& queryString,
55  const std::vector<std::string>& text_params,
56  const std::vector<BindType>& bind_types);
57 
58  virtual void query_with_text_param(const std::string& queryString,
59  const std::string& text_param);
60 
61  virtual size_t getNumRows() const { return numRows_; }
62  virtual size_t getNumCols() const { return numCols_; }
63 
64  template <typename T>
65  T getData(const int row, const int col) {
66  assert(row < static_cast<int>(numRows_));
67  assert(col < static_cast<int>(numCols_));
68  return boost::lexical_cast<T>(results_[col][row].result);
69  }
70 
71  bool isNull(const int row, const int col) const {
72  assert(row < static_cast<int>(numRows_));
73  assert(col < static_cast<int>(numCols_));
74  return results_[col][row].is_null;
75  }
76 
77  std::vector<std::string> columnNames; // make this public for easy access
78  std::vector<int> columnTypes;
79 
80  auto getSqlitePtr() const { return db_; }
81 
82  private:
83  struct NullableResult {
84  const std::string result;
85  const bool is_null;
86  };
87 
88  void throwError();
89 
90  sqlite3* db_;
91  std::string dbName_;
93  std::vector<std::vector<NullableResult>> results_;
94  size_t numCols_;
95  size_t numRows_;
96 };
97 
98 #endif // SQLITE_CONNECTOR
T getData(const int row, const int col)
virtual void query_with_text_params(std::string const &query_only)
virtual void query(const std::string &queryString)
std::vector< std::vector< NullableResult > > results_
std::vector< int > columnTypes
bool isNull(const int row, const int col) const
std::string dbName_
void query_with_text_params(STRING_CONTAINER const &query_and_text_params)
virtual ~SqliteConnector()
virtual size_t getNumCols() const
std::vector< std::string > columnNames
auto getSqlitePtr() const
virtual void query_with_text_param(const std::string &queryString, const std::string &text_param)
virtual size_t getNumRows() const