OmniSciDB  b24e664e58
 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 
45  sqlite3_close(db_);
46 }
47 
49  string errorMsg(sqlite3_errmsg(db_));
50  throw runtime_error("Sqlite3 Error: " + errorMsg);
51 }
52 
54  const std::string& queryString,
55  const std::vector<std::string>& text_params) {
56  atFirstResult_ = true;
57  numRows_ = 0;
58  numCols_ = 0;
59  columnNames.clear();
60  columnTypes.clear();
61  results_.clear();
62  sqlite3_stmt* stmt;
63  int returnCode = sqlite3_prepare_v2(db_, queryString.c_str(), -1, &stmt, nullptr);
64  if (returnCode != SQLITE_OK) {
65  throwError();
66  }
67 
68  int numParams_ = 1;
69  for (auto text_param : text_params) {
70  returnCode = sqlite3_bind_text(
71  stmt, numParams_++, text_param.c_str(), text_param.size(), SQLITE_TRANSIENT);
72  if (returnCode != SQLITE_OK) {
73  throwError();
74  }
75  }
76 
77  do {
78  returnCode = sqlite3_step(stmt);
79  if (returnCode != SQLITE_ROW && returnCode != SQLITE_DONE) {
80  throwError();
81  }
82  if (returnCode == SQLITE_DONE) {
83  break;
84  }
85  if (atFirstResult_) {
86  numCols_ = sqlite3_column_count(stmt);
87  for (size_t c = 0; c < numCols_; ++c) {
88  columnNames.emplace_back(sqlite3_column_name(stmt, c));
89  columnTypes.push_back(sqlite3_column_type(stmt, c));
90  }
91  results_.resize(numCols_);
92  atFirstResult_ = false;
93  }
94  numRows_++;
95  for (size_t c = 0; c < numCols_; ++c) {
96  auto col_text = reinterpret_cast<const char*>(sqlite3_column_text(stmt, c));
97  bool is_null = sqlite3_column_type(stmt, c) == SQLITE_NULL;
98  assert(is_null == !col_text);
99  results_[c].push_back(NullableResult{is_null ? "" : col_text, is_null});
100  }
101  } while (1 == 1); // Loop control in break statement above
102 
103  sqlite3_finalize(stmt);
104 }
105 
106 void SqliteConnector::query_with_text_param(const std::string& queryString,
107  const std::string& text_param) {
108  query_with_text_params(queryString, std::vector<std::string>{text_param});
109 }
110 
111 void SqliteConnector::query(const std::string& queryString) {
112  query_with_text_params(queryString, std::vector<std::string>{});
113 }
void query_with_text_params(std::string const &query_only)
void query(const std::string &queryString)
std::vector< std::vector< NullableResult > > results_
std::vector< int > columnTypes
int64_t const int32_t sz assert(dest)
bool is_null(const T &v, const SQLTypeInfo &t)
SqliteConnector(const std::string &dbName, const std::string &dir=".")
std::vector< std::string > columnNames
void query_with_text_param(const std::string &queryString, const std::string &text_param)