OmniSciDB  72c90bc290
 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 
40  void reset(const std::string& path) {
41  sqlite3_close(db_);
42  sqlite3_open(path.c_str(), &db_);
43  }
44 
45  virtual void query(const std::string& queryString);
46 
47  virtual void query_with_text_params(std::string const& query_only) {
48  query(query_only);
49  }
50  template <typename STRING_CONTAINER>
51  void query_with_text_params(STRING_CONTAINER const& query_and_text_params) {
53  *query_and_text_params.begin(),
54  std::vector<std::string>{std::next(query_and_text_params.begin()),
55  query_and_text_params.end()});
56  }
57  virtual void query_with_text_params(const std::string& queryString,
58  const std::vector<std::string>& text_param);
59 
60  enum class BindType { TEXT = 1, BLOB, NULL_TYPE };
61  virtual void query_with_text_params(const std::string& queryString,
62  const std::vector<std::string>& text_params,
63  const std::vector<BindType>& bind_types);
64 
65  virtual void query_with_text_param(const std::string& queryString,
66  const std::string& text_param);
67 
68  virtual void batch_insert(const std::string& table_name,
69  std::vector<std::vector<std::string>>& insert_vals);
70 
71  virtual size_t getNumRows() const { return numRows_; }
72  virtual size_t getNumCols() const { return numCols_; }
73 
74  template <typename T>
75  T getData(const int row, const int col) {
76  assert(row < static_cast<int>(numRows_));
77  assert(col < static_cast<int>(numCols_));
78  return boost::lexical_cast<T>(results_[col][row].result);
79  }
80 
81  bool isNull(const int row, const int col) const {
82  assert(row < static_cast<int>(numRows_));
83  assert(col < static_cast<int>(numCols_));
84  return results_[col][row].is_null;
85  }
86 
87  std::vector<std::string> columnNames; // make this public for easy access
88  std::vector<int> columnTypes;
89 
90  auto getSqlitePtr() const { return db_; }
91 
92  private:
93  struct NullableResult {
94  const std::string result;
95  const bool is_null;
96  };
97 
98  void throwError();
99 
100  sqlite3* db_;
101  std::string dbName_;
103  std::vector<std::vector<NullableResult>> results_;
104  size_t numCols_;
105  size_t numRows_;
106 };
107 
108 #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_
void reset(const std::string &path)
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