OmniSciDB  8a228a1076
SqliteConnector Class Reference

#include <SqliteConnector.h>

+ Collaboration diagram for SqliteConnector:

Classes

struct  NullableResult
 

Public Types

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

Public Member Functions

 SqliteConnector (const std::string &dbName, const std::string &dir=".")
 
 SqliteConnector (sqlite3 *db)
 
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 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

◆ BindType

Enumerator
TEXT 
BLOB 

Definition at line 52 of file SqliteConnector.h.

52 { TEXT = 1, BLOB };

Constructor & Destructor Documentation

◆ SqliteConnector() [1/2]

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

◆ SqliteConnector() [2/2]

SqliteConnector::SqliteConnector ( sqlite3 *  db)

Definition at line 47 of file SqliteConnector.cpp.

47 : db_(db) {}

◆ ~SqliteConnector()

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

◆ getData()

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

Definition at line 64 of file SqliteConnector.h.

References numCols_, numRows_, and results_.

Referenced by Catalog_Namespace::Catalog::addColumn(), Catalog_Namespace::Catalog::addForeignTableDetails(), Catalog_Namespace::Catalog::buildForeignServerMap(), Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::createDashboard(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createForeignServerNoLocks(), Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::delDictionary(), Catalog_Namespace::Catalog::dropForeignServer(), Catalog_Namespace::Catalog::dropFsiSchemasAndTables(), Catalog_Namespace::Catalog::executeDropTableSqliteQueries(), Catalog_Namespace::Catalog::getForeignServerFromStorage(), Catalog_Namespace::Catalog::getForeignServersForUser(), migrations::MigrationMgr::migrateDateInDaysMetadata(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::replaceDashboard(), SqliteMemDatabase::runSelect(), Catalog_Namespace::Catalog::setColumnDictionary(), Catalog_Namespace::Catalog::setForeignServerProperty(), 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().

64  {
65  assert(row < static_cast<int>(numRows_));
66  assert(col < static_cast<int>(numCols_));
67  return boost::lexical_cast<T>(results_[col][row].result);
68  }
std::vector< std::vector< NullableResult > > results_
+ Here is the caller graph for this function:

◆ getNumCols()

virtual size_t SqliteConnector::getNumCols ( ) const
inlinevirtual

Definition at line 61 of file SqliteConnector.h.

References numCols_.

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

61 { return numCols_; }
+ Here is the caller graph for this function:

◆ getNumRows()

virtual size_t SqliteConnector::getNumRows ( ) const
inlinevirtual

Definition at line 60 of file SqliteConnector.h.

References numRows_.

Referenced by Catalog_Namespace::Catalog::addForeignTableDetails(), Catalog_Namespace::Catalog::buildForeignServerMap(), Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::createDashboard(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createForeignServerNoLocks(), Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::dropForeignServer(), Catalog_Namespace::Catalog::dropFsiSchemasAndTables(), Catalog_Namespace::Catalog::executeDropTableSqliteQueries(), Catalog_Namespace::Catalog::getForeignServerFromStorage(), Catalog_Namespace::Catalog::getForeignServersForUser(), migrations::MigrationMgr::migrateDateInDaysMetadata(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::replaceDashboard(), SqliteMemDatabase::runSelect(), Catalog_Namespace::Catalog::setForeignServerProperty(), 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::updateGeoColumns(), Catalog_Namespace::Catalog::updateLinkSchema(), Catalog_Namespace::Catalog::updatePageSize(), and Catalog_Namespace::Catalog::updateTableDescriptorSchema().

60 { return numRows_; }
+ Here is the caller graph for this function:

◆ getSqlitePtr()

auto SqliteConnector::getSqlitePtr ( ) const
inline

Definition at line 79 of file SqliteConnector.h.

References db_.

79 { return db_; }

◆ isNull()

bool SqliteConnector::isNull ( const int  row,
const int  col 
) const
inline

Definition at line 70 of file SqliteConnector.h.

References numCols_, numRows_, and results_.

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

70  {
71  assert(row < static_cast<int>(numRows_));
72  assert(col < static_cast<int>(numCols_));
73  return results_[col][row].is_null;
74  }
std::vector< std::vector< NullableResult > > results_
+ Here is the caller graph for this function:

◆ query()

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

Definition at line 147 of file SqliteConnector.cpp.

References query_with_text_params().

Referenced by Catalog_Namespace::Catalog::addForeignTableDetails(), Catalog_Namespace::Catalog::buildForeignServerMap(), Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::createDashboard(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createFsiSchemasAndDefaultServers(), Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::deleteMetadataForDashboards(), Catalog_Namespace::Catalog::dropForeignServer(), Catalog_Namespace::Catalog::dropFsiSchemasAndTables(), Catalog_Namespace::Catalog::dropTable(), migrations::MigrationMgr::migrateDateInDaysMetadata(), query_with_text_params(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::renameColumn(), Catalog_Namespace::Catalog::renamePhysicalTable(), Catalog_Namespace::Catalog::replaceDashboard(), SqliteMemDatabase::runSelect(), 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::updateGeoColumns(), Catalog_Namespace::Catalog::updateLinkSchema(), Catalog_Namespace::Catalog::updateLogicalToPhysicalTableLinkSchema(), Catalog_Namespace::Catalog::updateLogicalToPhysicalTableMap(), Catalog_Namespace::Catalog::updatePageSize(), and Catalog_Namespace::Catalog::updateTableDescriptorSchema().

147  {
148  query_with_text_params(queryString, std::vector<std::string>{});
149 }
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:

◆ query_with_text_param()

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

Definition at line 142 of file SqliteConnector.cpp.

References query_with_text_params().

Referenced by Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::delDictionary(), Catalog_Namespace::Catalog::dropForeignServer(), Catalog_Namespace::Catalog::dropTable(), Catalog_Namespace::Catalog::executeDropTableSqliteQueries(), and Catalog_Namespace::Catalog::setColumnDictionary().

143  {
144  query_with_text_params(queryString, std::vector<std::string>{text_param});
145 }
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:

◆ query_with_text_params() [1/4]

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

Definition at line 39 of file SqliteConnector.h.

References query().

Referenced by Catalog_Namespace::Catalog::addColumn(), Catalog_Namespace::Catalog::addReferenceToForeignDict(), Catalog_Namespace::Catalog::createDashboard(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createForeignServerNoLocks(), Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::deleteMetadataForDashboards(), Catalog_Namespace::Catalog::dropColumn(), Catalog_Namespace::Catalog::dropForeignServer(), Catalog_Namespace::Catalog::executeDropTableSqliteQueries(), Catalog_Namespace::Catalog::getForeignServerFromStorage(), Catalog_Namespace::Catalog::getForeignServersForUser(), migrations::MigrationMgr::migrateDateInDaysMetadata(), query(), query_with_text_param(), query_with_text_params(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::renameColumn(), Catalog_Namespace::Catalog::renamePhysicalTable(), Catalog_Namespace::Catalog::replaceDashboard(), Catalog_Namespace::Catalog::setColumnDictionary(), Catalog_Namespace::Catalog::setColumnSharedDictionary(), Catalog_Namespace::Catalog::setForeignServerProperty(), Catalog_Namespace::Catalog::updateFixlenArrayColumns(), Catalog_Namespace::Catalog::updateGeoColumns(), and Catalog_Namespace::Catalog::updateLogicalToPhysicalTableMap().

39  {
40  query(query_only);
41  }
virtual void query(const std::string &queryString)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ query_with_text_params() [2/4]

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

Definition at line 43 of file SqliteConnector.h.

References query_with_text_params().

43  {
45  *query_and_text_params.begin(),
46  std::vector<std::string>{std::next(query_and_text_params.begin()),
47  query_and_text_params.end()});
48  }
virtual void query_with_text_params(std::string const &query_only)
+ Here is the call graph for this function:

◆ query_with_text_params() [3/4]

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

Definition at line 136 of file SqliteConnector.cpp.

References query_with_text_params().

138  {
139  query_with_text_params(queryString, text_params, {});
140 }
virtual void query_with_text_params(std::string const &query_only)
+ Here is the call graph for this function:

◆ query_with_text_params() [4/4]

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(), anonymous_namespace{TypedDataAccessors.h}::is_null(), 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 {
96  returnCode = sqlite3_bind_text(
97  stmt, num_params++, text_param.c_str(), text_param.size(), SQLITE_TRANSIENT);
98  }
99  if (returnCode != SQLITE_OK) {
100  throwError();
101  }
102  }
103 
104  do {
105  returnCode = sqlite3_step(stmt);
106  if (returnCode != SQLITE_ROW && returnCode != SQLITE_DONE) {
107  throwError();
108  }
109  if (returnCode == SQLITE_DONE) {
110  break;
111  }
112  if (atFirstResult_) {
113  numCols_ = sqlite3_column_count(stmt);
114  for (size_t c = 0; c < numCols_; ++c) {
115  columnNames.emplace_back(sqlite3_column_name(stmt, c));
116  columnTypes.push_back(sqlite3_column_type(stmt, c));
117  }
118  results_.resize(numCols_);
119  atFirstResult_ = false;
120  }
121  numRows_++;
122  for (size_t c = 0; c < numCols_; ++c) {
123  auto column_type = sqlite3_column_type(stmt, c);
124  bool is_null = (column_type == SQLITE_NULL);
125  auto col_text = get_column_datum(column_type, stmt, c);
126  if (is_null) {
127  CHECK(col_text.empty());
128  }
129  results_[c].emplace_back(NullableResult{col_text, is_null});
130  }
131  } while (1 == 1); // Loop control in break statement above
132 
133  sqlite3_finalize(stmt);
134 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::string get_column_datum(int column_type, sqlite3_stmt *stmt, size_t column_index)
std::vector< std::vector< NullableResult > > results_
std::vector< int > columnTypes
bool is_null(const T &v, const SQLTypeInfo &t)
#define CHECK(condition)
Definition: Logger.h:197
std::vector< std::string > columnNames
+ Here is the call graph for this function:

◆ throwError()

void SqliteConnector::throwError ( )
private

Definition at line 55 of file SqliteConnector.cpp.

References db_.

Referenced by 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

◆ atFirstResult_

bool SqliteConnector::atFirstResult_
private

Definition at line 91 of file SqliteConnector.h.

Referenced by query_with_text_params().

◆ columnNames

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

Definition at line 76 of file SqliteConnector.h.

Referenced by query_with_text_params().

◆ columnTypes

std::vector<int> SqliteConnector::columnTypes

Definition at line 77 of file SqliteConnector.h.

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

◆ db_

sqlite3* SqliteConnector::db_
private

◆ dbName_

std::string SqliteConnector::dbName_
private

Definition at line 90 of file SqliteConnector.h.

Referenced by ~SqliteConnector().

◆ numCols_

size_t SqliteConnector::numCols_
private

Definition at line 93 of file SqliteConnector.h.

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

◆ numRows_

size_t SqliteConnector::numRows_
private

Definition at line 94 of file SqliteConnector.h.

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

◆ results_

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

Definition at line 92 of file SqliteConnector.h.

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


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