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

#include <SqliteConnector.h>

+ Collaboration diagram for SqliteConnector:

Classes

struct  NullableResult
 

Public Member Functions

 SqliteConnector (const std::string &dbName, const std::string &dir=".")
 
 ~SqliteConnector ()
 
void query (const std::string &queryString)
 
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)
 
void query_with_text_params (const std::string &queryString, const std::vector< std::string > &text_param)
 
void query_with_text_param (const std::string &queryString, const std::string &text_param)
 
size_t getNumRows () const
 
size_t getNumCols () const
 
template<typename T >
getData (const int row, const int col)
 
bool isNull (const int row, const int col) 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.

Constructor & Destructor Documentation

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

Definition at line 31 of file SqliteConnector.cpp.

References db_, and throwError().

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 }
std::string dbName_

+ Here is the call graph for this function:

SqliteConnector::~SqliteConnector ( )

Definition at line 44 of file SqliteConnector.cpp.

References db_.

44  {
45  sqlite3_close(db_);
46 }

Member Function Documentation

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

Definition at line 55 of file SqliteConnector.h.

References assert(), numCols_, numRows_, and results_.

Referenced by Catalog_Namespace::Catalog::addColumn(), Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::checkDateInDaysColumnMigration(), Catalog_Namespace::Catalog::createDashboard(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::doDropTable(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::replaceDashboard(), Catalog_Namespace::Catalog::setColumnDictionary(), Catalog_Namespace::Catalog::setColumnSharedDictionary(), 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().

55  {
56  assert(row < static_cast<int>(numRows_));
57  assert(col < static_cast<int>(numCols_));
58  return boost::lexical_cast<T>(results_[col][row].result);
59  }
std::vector< std::vector< NullableResult > > results_
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t SqliteConnector::getNumCols ( ) const
inline

Definition at line 52 of file SqliteConnector.h.

References numCols_.

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

Definition at line 61 of file SqliteConnector.h.

References assert(), numCols_, numRows_, and results_.

Referenced by Catalog_Namespace::Catalog::buildMaps().

61  {
62  assert(row < static_cast<int>(numRows_));
63  assert(col < static_cast<int>(numCols_));
64  return results_[col][row].is_null;
65  }
std::vector< std::vector< NullableResult > > results_
int64_t const int32_t sz assert(dest)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 111 of file SqliteConnector.cpp.

References query_with_text_params().

Referenced by Catalog_Namespace::Catalog::buildMaps(), Catalog_Namespace::Catalog::checkDateInDaysColumnMigration(), Catalog_Namespace::Catalog::createDashboard(), Catalog_Namespace::Catalog::createDashboardSystemRoles(), Catalog_Namespace::Catalog::createLink(), Catalog_Namespace::Catalog::createTable(), Catalog_Namespace::Catalog::deleteMetadataForDashboard(), Catalog_Namespace::Catalog::dropTable(), query_with_text_params(), Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions(), Catalog_Namespace::Catalog::renameColumn(), Catalog_Namespace::Catalog::renamePhysicalTable(), Catalog_Namespace::Catalog::replaceDashboard(), 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::updateLinkSchema(), Catalog_Namespace::Catalog::updateLogicalToPhysicalTableLinkSchema(), Catalog_Namespace::Catalog::updateLogicalToPhysicalTableMap(), Catalog_Namespace::Catalog::updatePageSize(), and Catalog_Namespace::Catalog::updateTableDescriptorSchema().

111  {
112  query_with_text_params(queryString, std::vector<std::string>{});
113 }
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 
)

Definition at line 106 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::doDropTable(), Catalog_Namespace::Catalog::dropTable(), and Catalog_Namespace::Catalog::setColumnDictionary().

107  {
108  query_with_text_params(queryString, std::vector<std::string>{text_param});
109 }
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_params ( std::string const &  query_only)
inline
template<typename STRING_CONTAINER >
void SqliteConnector::query_with_text_params ( STRING_CONTAINER const &  query_and_text_params)
inline

Definition at line 40 of file SqliteConnector.h.

References query_with_text_params().

40  {
42  *query_and_text_params.begin(),
43  std::vector<std::string>{std::next(query_and_text_params.begin()),
44  query_and_text_params.end()});
45  }
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 
)

Definition at line 53 of file SqliteConnector.cpp.

References assert(), atFirstResult_, columnNames, columnTypes, db_, anonymous_namespace{TypedDataAccessors.h}::is_null(), numCols_, numRows_, results_, and throwError().

55  {
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 }
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)
std::vector< std::string > columnNames

+ Here is the call graph for this function:

void SqliteConnector::throwError ( )
private

Definition at line 48 of file SqliteConnector.cpp.

References db_.

Referenced by query_with_text_params(), and SqliteConnector().

48  {
49  string errorMsg(sqlite3_errmsg(db_));
50  throw runtime_error("Sqlite3 Error: " + errorMsg);
51 }

+ Here is the caller graph for this function:

Member Data Documentation

bool SqliteConnector::atFirstResult_
private

Definition at line 80 of file SqliteConnector.h.

Referenced by query_with_text_params().

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

Definition at line 67 of file SqliteConnector.h.

Referenced by query_with_text_params().

std::vector<int> SqliteConnector::columnTypes

Definition at line 68 of file SqliteConnector.h.

Referenced by query_with_text_params().

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

Definition at line 79 of file SqliteConnector.h.

size_t SqliteConnector::numCols_
private

Definition at line 82 of file SqliteConnector.h.

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

size_t SqliteConnector::numRows_
private

Definition at line 83 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 81 of file SqliteConnector.h.

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


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