OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqliteConnector Class Reference

#include <SqliteConnector.h>

+ Collaboration diagram for SqliteConnector:

Classes

struct  NullableResult
 

Public Types

enum  BindType { BindType::TEXT = 1, BindType::BLOB, BindType::NULL_TYPE }
 

Public Member Functions

 SqliteConnector (const std::string &dbName, const std::string &dir=".")
 
 SqliteConnector (sqlite3 *db)
 
 SqliteConnector ()
 
virtual ~SqliteConnector ()
 
virtual void query (const std::string &queryString)
 
virtual void query_with_text_params (std::string const &query_only)
 
template<typename STRING_CONTAINER >
void query_with_text_params (STRING_CONTAINER const &query_and_text_params)
 
virtual void query_with_text_params (const std::string &queryString, const std::vector< std::string > &text_param)
 
virtual void query_with_text_params (const std::string &queryString, const std::vector< std::string > &text_params, const std::vector< BindType > &bind_types)
 
virtual void query_with_text_param (const std::string &queryString, const std::string &text_param)
 
virtual void batch_insert (const std::string &table_name, std::vector< std::vector< std::string >> &insert_vals)
 
virtual size_t getNumRows () const
 
virtual size_t getNumCols () const
 
template<typename T >
getData (const int row, const int col)
 
bool isNull (const int row, const int col) const
 
auto getSqlitePtr () const
 

Public Attributes

std::vector< std::string > columnNames
 
std::vector< int > columnTypes
 

Private Member Functions

void throwError ()
 

Private Attributes

sqlite3 * db_
 
std::string dbName_
 
bool atFirstResult_
 
std::vector< std::vector
< NullableResult > > 
results_
 
size_t numCols_
 
size_t numRows_
 

Detailed Description

Definition at line 32 of file SqliteConnector.h.

Member Enumeration Documentation

Enumerator
TEXT 
BLOB 
NULL_TYPE 

Definition at line 53 of file SqliteConnector.h.

53 { TEXT = 1, BLOB, NULL_TYPE };
#define TEXT

Constructor & Destructor Documentation

SqliteConnector::SqliteConnector ( const std::string &  dbName,
const std::string &  dir = "." 
)
SqliteConnector::SqliteConnector ( sqlite3 *  db)

Definition at line 47 of file SqliteConnector.cpp.

47 : db_(db) {}
SqliteConnector::SqliteConnector ( )
inline

Definition at line 36 of file SqliteConnector.h.

36 {}
SqliteConnector::~SqliteConnector ( )
virtual

Definition at line 49 of file SqliteConnector.cpp.

References db_, and dbName_.

49  {
50  if (!dbName_.empty()) {
51  sqlite3_close(db_);
52  }
53 }
std::string dbName_

Member Function Documentation

void SqliteConnector::batch_insert ( const std::string &  table_name,
std::vector< std::vector< std::string >> &  insert_vals 
)
virtual

Definition at line 153 of file SqliteConnector.cpp.

References db_, query(), and throwError().

154  {
155  const size_t num_rows = insert_vals.size();
156  if (!num_rows) {
157  return;
158  }
159  const size_t num_cols(insert_vals[0].size());
160  if (!num_cols) {
161  return;
162  }
163  std::string paramertized_query = "INSERT INTO " + table_name + " VALUES(";
164  for (size_t col_idx = 0; col_idx < num_cols - 1; ++col_idx) {
165  paramertized_query += "?, ";
166  }
167  paramertized_query += "?)";
168 
169  query("BEGIN TRANSACTION");
170 
171  sqlite3_stmt* stmt;
172  int returnCode =
173  sqlite3_prepare_v2(db_, paramertized_query.c_str(), -1, &stmt, nullptr);
174  if (returnCode != SQLITE_OK) {
175  throwError();
176  }
177 
178  for (size_t r = 0; r < num_rows; ++r) {
179  const auto& row_insert_vals = insert_vals[r];
180  int num_params = 1;
181  for (const auto& insert_field : row_insert_vals) {
182  returnCode = sqlite3_bind_text(stmt,
183  num_params++,
184  insert_field.c_str(),
185  insert_field.size(),
186  SQLITE_TRANSIENT);
187  if (returnCode != SQLITE_OK) {
188  throwError();
189  }
190  }
191  returnCode = sqlite3_step(stmt);
192  if (returnCode != SQLITE_DONE) {
193  throwError();
194  }
195  sqlite3_reset(stmt);
196  }
197  sqlite3_finalize(stmt);
198  query("END TRANSACTION");
199 }
virtual void query(const std::string &queryString)

+ Here is the call graph for this function:

template<typename T >
T SqliteConnector::getData ( const int  row,
const int  col 
)
inline

Definition at line 68 of file SqliteConnector.h.

References numCols_, numRows_, results_, and omnisci.dtypes::T.

Referenced by Catalog_Namespace::Catalog::addColumn(), Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::delDictionary(), Catalog_Namespace::Catalog::getCustomExpressionFromConnector(), migrations::MigrationMgr::migrateDateInDaysMetadata(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), SqliteMemDatabase::runSelect(), Catalog_Namespace::Catalog::updateDefaultColumnValues(), Catalog_Namespace::Catalog::updateDeletedColumnIndicator(), Catalog_Namespace::Catalog::updateDictionaryNames(), Catalog_Namespace::Catalog::updateDictionarySchema(), Catalog_Namespace::Catalog::updateFrontendViewSchema(), Catalog_Namespace::Catalog::updateLinkSchema(), Catalog_Namespace::Catalog::updatePageSize(), and Catalog_Namespace::Catalog::updateTableDescriptorSchema().

68  {
69  assert(row < static_cast<int>(numRows_));
70  assert(col < static_cast<int>(numCols_));
71  return boost::lexical_cast<T>(results_[col][row].result);
72  }
std::vector< std::vector< NullableResult > > results_

+ Here is the caller graph for this function:

virtual size_t SqliteConnector::getNumCols ( ) const
inlinevirtual

Definition at line 65 of file SqliteConnector.h.

References numCols_.

Referenced by SqliteMemDatabase::runSelect().

65 { return numCols_; }

+ Here is the caller graph for this function:

auto SqliteConnector::getSqlitePtr ( ) const
inline

Definition at line 83 of file SqliteConnector.h.

References db_.

83 { return db_; }
bool SqliteConnector::isNull ( const int  row,
const int  col 
) const
inline

Definition at line 74 of file SqliteConnector.h.

References numCols_, numRows_, and results_.

Referenced by Catalog_Namespace::Catalog::buildMaps(), and SqliteMemDatabase::runSelect().

74  {
75  assert(row < static_cast<int>(numRows_));
76  assert(col < static_cast<int>(numCols_));
77  return results_[col][row].is_null;
78  }
std::vector< std::vector< NullableResult > > results_

+ Here is the caller graph for this function:

void SqliteConnector::query ( const std::string &  queryString)
virtual

Definition at line 149 of file SqliteConnector.cpp.

References query_with_text_params().

Referenced by batch_insert(), Catalog_Namespace::Catalog::buildCustomExpressionsMap(), Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::deleteMetadataForDashboards(), migrations::MigrationMgr::migrateDateInDaysMetadata(), query_with_text_params(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), SqliteMemDatabase::runSelect(), Catalog_Namespace::Catalog::updateCustomExpressionsSchema(), Catalog_Namespace::Catalog::updateDefaultColumnValues(), Catalog_Namespace::Catalog::updateDeletedColumnIndicator(), Catalog_Namespace::Catalog::updateDictionaryNames(), Catalog_Namespace::Catalog::updateDictionarySchema(), Catalog_Namespace::Catalog::updateFixlenArrayColumns(), Catalog_Namespace::Catalog::updateFrontendViewAndLinkUsers(), Catalog_Namespace::Catalog::updateFrontendViewSchema(), Catalog_Namespace::Catalog::updateFrontendViewsToDashboards(), Catalog_Namespace::Catalog::updateFsiSchemas(), Catalog_Namespace::Catalog::updateGeoColumns(), Catalog_Namespace::Catalog::updateLinkSchema(), Catalog_Namespace::Catalog::updateLogicalToPhysicalTableLinkSchema(), Catalog_Namespace::Catalog::updateLogicalToPhysicalTableMap(), Catalog_Namespace::Catalog::updatePageSize(), and Catalog_Namespace::Catalog::updateTableDescriptorSchema().

149  {
150  query_with_text_params(queryString, std::vector<std::string>{});
151 }
virtual void query_with_text_params(std::string const &query_only)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SqliteConnector::query_with_text_param ( const std::string &  queryString,
const std::string &  text_param 
)
virtual

Definition at line 144 of file SqliteConnector.cpp.

References query_with_text_params().

Referenced by Catalog_Namespace::Catalog::createTable(), and Catalog_Namespace::Catalog::delDictionary().

145  {
146  query_with_text_params(queryString, std::vector<std::string>{text_param});
147 }
virtual void query_with_text_params(std::string const &query_only)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

virtual void SqliteConnector::query_with_text_params ( std::string const query_only)
inlinevirtual

Definition at line 40 of file SqliteConnector.h.

References query().

Referenced by Catalog_Namespace::Catalog::addColumn(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::deleteMetadataForDashboards(), Catalog_Namespace::Catalog::dropColumn(), migrations::MigrationMgr::migrateDateInDaysMetadata(), query(), query_with_text_param(), query_with_text_params(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::updateFixlenArrayColumns(), Catalog_Namespace::Catalog::updateGeoColumns(), and Catalog_Namespace::Catalog::updateLogicalToPhysicalTableMap().

40  {
41  query(query_only);
42  }
virtual void query(const std::string &queryString)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename STRING_CONTAINER >
void SqliteConnector::query_with_text_params ( STRING_CONTAINER const query_and_text_params)
inline

Definition at line 44 of file SqliteConnector.h.

References query_with_text_params().

44  {
46  *query_and_text_params.begin(),
47  std::vector<std::string>{std::next(query_and_text_params.begin()),
48  query_and_text_params.end()});
49  }
virtual void query_with_text_params(std::string const &query_only)

+ Here is the call graph for this function:

void SqliteConnector::query_with_text_params ( const std::string &  queryString,
const std::vector< std::string > &  text_param 
)
virtual

Definition at line 138 of file SqliteConnector.cpp.

References query_with_text_params().

140  {
141  query_with_text_params(queryString, text_params, {});
142 }
virtual void query_with_text_params(std::string const &query_only)

+ Here is the call graph for this function:

void SqliteConnector::query_with_text_params ( const std::string &  queryString,
const std::vector< std::string > &  text_params,
const std::vector< BindType > &  bind_types 
)
virtual

Definition at line 71 of file SqliteConnector.cpp.

References atFirstResult_, BLOB, CHECK, CHECK_EQ, columnNames, columnTypes, db_, get_column_datum(), is_null(), NULL_TYPE, numCols_, numRows_, results_, and throwError().

73  {
74  if (!bind_types.empty()) {
75  CHECK_EQ(text_params.size(), bind_types.size());
76  }
77 
78  atFirstResult_ = true;
79  numRows_ = 0;
80  numCols_ = 0;
81  columnNames.clear();
82  columnTypes.clear();
83  results_.clear();
84  sqlite3_stmt* stmt;
85  int returnCode = sqlite3_prepare_v2(db_, queryString.c_str(), -1, &stmt, nullptr);
86  if (returnCode != SQLITE_OK) {
87  throwError();
88  }
89 
90  int num_params = 1;
91  for (auto text_param : text_params) {
92  if (!bind_types.empty() && bind_types[num_params - 1] == BindType::BLOB) {
93  returnCode = sqlite3_bind_blob(
94  stmt, num_params++, text_param.c_str(), text_param.size(), SQLITE_TRANSIENT);
95  } else if (!bind_types.empty() && bind_types[num_params - 1] == BindType::NULL_TYPE) {
96  returnCode = sqlite3_bind_null(stmt, num_params++);
97  } else {
98  returnCode = sqlite3_bind_text(
99  stmt, num_params++, text_param.c_str(), text_param.size(), SQLITE_TRANSIENT);
100  }
101  if (returnCode != SQLITE_OK) {
102  throwError();
103  }
104  }
105 
106  do {
107  returnCode = sqlite3_step(stmt);
108  if (returnCode != SQLITE_ROW && returnCode != SQLITE_DONE) {
109  throwError();
110  }
111  if (returnCode == SQLITE_DONE) {
112  break;
113  }
114  if (atFirstResult_) {
115  numCols_ = sqlite3_column_count(stmt);
116  for (size_t c = 0; c < numCols_; ++c) {
117  columnNames.emplace_back(sqlite3_column_name(stmt, c));
118  columnTypes.push_back(sqlite3_column_type(stmt, c));
119  }
120  results_.resize(numCols_);
121  atFirstResult_ = false;
122  }
123  numRows_++;
124  for (size_t c = 0; c < numCols_; ++c) {
125  auto column_type = sqlite3_column_type(stmt, c);
126  bool is_null = (column_type == SQLITE_NULL);
127  auto col_text = get_column_datum(column_type, stmt, c);
128  if (is_null) {
129  CHECK(col_text.empty());
130  }
131  results_[c].emplace_back(NullableResult{col_text, is_null});
132  }
133  } while (1 == 1); // Loop control in break statement above
134 
135  sqlite3_finalize(stmt);
136 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
std::string get_column_datum(int column_type, sqlite3_stmt *stmt, size_t column_index)
CONSTEXPR DEVICE bool is_null(const T &value)
std::vector< std::vector< NullableResult > > results_
std::vector< int > columnTypes
#define CHECK(condition)
Definition: Logger.h:211
std::vector< std::string > columnNames

+ Here is the call graph for this function:

void SqliteConnector::throwError ( )
private

Definition at line 55 of file SqliteConnector.cpp.

References db_.

Referenced by batch_insert(), and query_with_text_params().

55  {
56  string errorMsg(sqlite3_errmsg(db_));
57  throw runtime_error("Sqlite3 Error: " + errorMsg);
58 }

+ Here is the caller graph for this function:

Member Data Documentation

bool SqliteConnector::atFirstResult_
private

Definition at line 95 of file SqliteConnector.h.

Referenced by query_with_text_params().

std::vector<std::string> SqliteConnector::columnNames

Definition at line 80 of file SqliteConnector.h.

Referenced by query_with_text_params().

std::vector<int> SqliteConnector::columnTypes

Definition at line 81 of file SqliteConnector.h.

Referenced by query_with_text_params(), and SqliteMemDatabase::runSelect().

sqlite3* SqliteConnector::db_
private
std::string SqliteConnector::dbName_
private

Definition at line 94 of file SqliteConnector.h.

Referenced by ~SqliteConnector().

size_t SqliteConnector::numCols_
private

Definition at line 97 of file SqliteConnector.h.

Referenced by getData(), getNumCols(), isNull(), and query_with_text_params().

size_t SqliteConnector::numRows_
private

Definition at line 98 of file SqliteConnector.h.

Referenced by getData(), getNumRows(), isNull(), and query_with_text_params().

std::vector<std::vector<NullableResult> > SqliteConnector::results_
private

Definition at line 96 of file SqliteConnector.h.

Referenced by getData(), isNull(), and query_with_text_params().


The documentation for this class was generated from the following files: