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