OmniSciDB  0fdbebe030
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SqliteConnector.cpp
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 
23 #include "SqliteConnector.h"
24 #include <iostream>
25 
26 using std::cout;
27 using std::endl;
28 using std::runtime_error;
29 using std::string;
30 
31 SqliteConnector::SqliteConnector(const string& dbName, const string& dir)
32  : dbName_(dbName) {
33  string connectString(dir);
34  if (connectString.size() > 0 && connectString[connectString.size() - 1] != '/') {
35  connectString.push_back('/');
36  }
37  connectString += dbName;
38  int returnCode = sqlite3_open(connectString.c_str(), &db_);
39  if (returnCode != SQLITE_OK) {
40  throwError();
41  }
42 }
43 
44 SqliteConnector::SqliteConnector(sqlite3* db) : db_(db) {}
45 
47  if (!dbName_.empty()) {
48  sqlite3_close(db_);
49  }
50 }
51 
53  string errorMsg(sqlite3_errmsg(db_));
54  throw runtime_error("Sqlite3 Error: " + errorMsg);
55 }
56 
58  const std::string& queryString,
59  const std::vector<std::string>& text_params) {
60  atFirstResult_ = true;
61  numRows_ = 0;
62  numCols_ = 0;
63  columnNames.clear();
64  columnTypes.clear();
65  results_.clear();
66  sqlite3_stmt* stmt;
67  int returnCode = sqlite3_prepare_v2(db_, queryString.c_str(), -1, &stmt, nullptr);
68  if (returnCode != SQLITE_OK) {
69  throwError();
70  }
71 
72  int numParams_ = 1;
73  for (auto text_param : text_params) {
74  returnCode = sqlite3_bind_text(
75  stmt, numParams_++, text_param.c_str(), text_param.size(), SQLITE_TRANSIENT);
76  if (returnCode != SQLITE_OK) {
77  throwError();
78  }
79  }
80 
81  do {
82  returnCode = sqlite3_step(stmt);
83  if (returnCode != SQLITE_ROW && returnCode != SQLITE_DONE) {
84  throwError();
85  }
86  if (returnCode == SQLITE_DONE) {
87  break;
88  }
89  if (atFirstResult_) {
90  numCols_ = sqlite3_column_count(stmt);
91  for (size_t c = 0; c < numCols_; ++c) {
92  columnNames.emplace_back(sqlite3_column_name(stmt, c));
93  columnTypes.push_back(sqlite3_column_type(stmt, c));
94  }
95  results_.resize(numCols_);
96  atFirstResult_ = false;
97  }
98  numRows_++;
99  for (size_t c = 0; c < numCols_; ++c) {
100  auto col_text = reinterpret_cast<const char*>(sqlite3_column_text(stmt, c));
101  bool is_null = sqlite3_column_type(stmt, c) == SQLITE_NULL;
102  assert(is_null == !col_text);
103  results_[c].push_back(NullableResult{is_null ? "" : col_text, is_null});
104  }
105  } while (1 == 1); // Loop control in break statement above
106 
107  sqlite3_finalize(stmt);
108 }
109 
110 void SqliteConnector::query_with_text_param(const std::string& queryString,
111  const std::string& text_param) {
112  query_with_text_params(queryString, std::vector<std::string>{text_param});
113 }
114 
115 void SqliteConnector::query(const std::string& queryString) {
116  query_with_text_params(queryString, std::vector<std::string>{});
117 }
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
int64_t const int32_t sz assert(dest)
std::string dbName_
bool is_null(const T &v, const SQLTypeInfo &t)
SqliteConnector(const std::string &dbName, const std::string &dir=".")
virtual ~SqliteConnector()
std::vector< std::string > columnNames
virtual void query_with_text_param(const std::string &queryString, const std::string &text_param)