OmniSciDB  ca0c39ec8f
 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 2022 HEAVY.AI, 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 
23 #ifndef SQLITE_CONNECTOR
24 #define SQLITE_CONNECTOR
25 
26 #include <boost/lexical_cast.hpp>
27 #include <cassert>
28 #include <string>
29 #include <vector>
30 
31 #include <sqlite3.h>
32 
34  public:
35  SqliteConnector(const std::string& dbName, const std::string& dir = ".");
36  SqliteConnector(sqlite3* db);
38  virtual ~SqliteConnector();
39  virtual void query(const std::string& queryString);
40 
41  virtual void query_with_text_params(std::string const& query_only) {
42  query(query_only);
43  }
44  template <typename STRING_CONTAINER>
45  void query_with_text_params(STRING_CONTAINER const& query_and_text_params) {
47  *query_and_text_params.begin(),
48  std::vector<std::string>{std::next(query_and_text_params.begin()),
49  query_and_text_params.end()});
50  }
51  virtual void query_with_text_params(const std::string& queryString,
52  const std::vector<std::string>& text_param);
53 
54  enum class BindType { TEXT = 1, BLOB, NULL_TYPE };
55  virtual void query_with_text_params(const std::string& queryString,
56  const std::vector<std::string>& text_params,
57  const std::vector<BindType>& bind_types);
58 
59  virtual void query_with_text_param(const std::string& queryString,
60  const std::string& text_param);
61 
62  virtual void batch_insert(const std::string& table_name,
63  std::vector<std::vector<std::string>>& insert_vals);
64 
65  virtual size_t getNumRows() const { return numRows_; }
66  virtual size_t getNumCols() const { return numCols_; }
67 
68  template <typename T>
69  T getData(const int row, const int col) {
70  assert(row < static_cast<int>(numRows_));
71  assert(col < static_cast<int>(numCols_));
72  return boost::lexical_cast<T>(results_[col][row].result);
73  }
74 
75  bool isNull(const int row, const int col) const {
76  assert(row < static_cast<int>(numRows_));
77  assert(col < static_cast<int>(numCols_));
78  return results_[col][row].is_null;
79  }
80 
81  std::vector<std::string> columnNames; // make this public for easy access
82  std::vector<int> columnTypes;
83 
84  auto getSqlitePtr() const { return db_; }
85 
86  private:
87  struct NullableResult {
88  const std::string result;
89  const bool is_null;
90  };
91 
92  void throwError();
93 
94  sqlite3* db_;
95  std::string dbName_;
97  std::vector<std::vector<NullableResult>> results_;
98  size_t numCols_;
99  size_t numRows_;
100 };
101 
102 #endif // SQLITE_CONNECTOR
T getData(const int row, const int col)
virtual void query_with_text_params(std::string const &query_only)
virtual void batch_insert(const std::string &table_name, std::vector< std::vector< std::string >> &insert_vals)
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