souffle  2.0.2-371-g6315b36
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
souffle::ReadStreamSQLite Class Reference

#include <ReadStreamSQLite.h>

Inheritance diagram for souffle::ReadStreamSQLite:
Inheritance graph
Collaboration diagram for souffle::ReadStreamSQLite:
Collaboration graph

Public Member Functions

 ReadStreamSQLite (const std::map< std::string, std::string > &rwOperation, SymbolTable &symbolTable, RecordTable &recordTable)
 
 ~ReadStreamSQLite () override
 
- Public Member Functions inherited from souffle::ReadStream
template<typename T >
void readAll (T &relation)
 
- Public Member Functions inherited from souffle::SerialisationStream< false >
virtual ~SerialisationStream ()=default
 

Protected Member Functions

void checkTableExists ()
 
void executeSQL (const std::string &sql)
 
void openDB ()
 
void prepareSelectStatement ()
 
Own< RamDomain[]> readNextTuple () override
 Read and return the next tuple. More...
 
void throwError (const std::string &message)
 
- Protected Member Functions inherited from souffle::ReadStream
void consumeChar (const std::string &str, char c, size_t &pos)
 Read past given character, consuming any preceding whitespace. More...
 
void consumeWhiteSpace (const std::string &str, size_t &pos)
 Advance position in the string until first non-whitespace character. More...
 
RamDomain readADT (const std::string &source, const std::string &adtName, size_t pos=0, size_t *charactersRead=nullptr)
 
std::string readAlphanumeric (const std::string &source, size_t &pos)
 Read the next alphanumeric sequence (corresponding to IDENT). More...
 
RamDomain readRecord (const std::string &source, const std::string &recordTypeName, size_t pos=0, size_t *charactersRead=nullptr)
 Read a record from a string. More...
 
 ReadStream (const std::map< std::string, std::string > &rwOperation, SymbolTable &symTab, RecordTable &recTab)
 
std::string readUntil (const std::string &source, const std::string stopChars, const size_t pos, size_t *charactersRead)
 
- Protected Member Functions inherited from souffle::SerialisationStream< false >
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, const std::map< std::string, std::string > &rwOperation)
 
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types)
 
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types, std::vector< std::string > relTypes, size_t auxArity=0)
 

Static Protected Member Functions

static std::string getFileName (const std::map< std::string, std::string > &rwOperation)
 Return given filename or construct from relation name. More...
 

Protected Attributes

sqlite3 * db = nullptr
 
const std::string dbFilename
 
const std::string relationName
 
sqlite3_stmt * selectStatement = nullptr
 
- Protected Attributes inherited from souffle::SerialisationStream< false >
size_t arity
 
size_t auxiliaryArity
 
RO< RecordTable > & recordTable
 
RO< SymbolTable > & symbolTable
 
std::vector< std::string > typeAttributes
 
Json types
 

Additional Inherited Members

- Protected Types inherited from souffle::SerialisationStream< false >
using RO = std::conditional_t< readOnlyTables, const A, A >
 

Detailed Description

Definition at line 39 of file ReadStreamSQLite.h.

Constructor & Destructor Documentation

◆ ReadStreamSQLite()

souffle::ReadStreamSQLite::ReadStreamSQLite ( const std::map< std::string, std::string > &  rwOperation,
SymbolTable symbolTable,
RecordTable recordTable 
)
inline

Definition at line 41 of file ReadStreamSQLite.h.

46  {
47  sqlite3_finalize(selectStatement);
48  sqlite3_close(db);

◆ ~ReadStreamSQLite()

souffle::ReadStreamSQLite::~ReadStreamSQLite ( )
inlineoverride

Definition at line 50 of file ReadStreamSQLite.h.

51  :
52  /**
53  * Read and return the next tuple.

References selectStatement.

Member Function Documentation

◆ checkTableExists()

void souffle::ReadStreamSQLite::checkTableExists ( )
inlineprotected

Definition at line 137 of file ReadStreamSQLite.h.

140  {
141  throwError("SQLite error in sqlite3_prepare_v2: ");
142  }
143 
144  if (sqlite3_step(tableStatement) == SQLITE_ROW) {
145  int count = sqlite3_column_int(tableStatement, 0);
146  if (count > 0) {
147  sqlite3_finalize(tableStatement);
148  return;
149  }
150  }
151  sqlite3_finalize(tableStatement);
152  throw std::invalid_argument(
153  "Required table or view does not exist in " + dbFilename + " for relation " + relationName);
154  }
155 
156  /**
157  * Return given filename or construct from relation name.
158  * Default name is [configured path]/[relation name].sqlite

References throwError().

Here is the call graph for this function:

◆ executeSQL()

void souffle::ReadStreamSQLite::executeSQL ( const std::string &  sql)
inlineprotected

Definition at line 97 of file ReadStreamSQLite.h.

99  {
100  std::stringstream error;
101  error << "SQLite error in sqlite3_exec: " << sqlite3_errmsg(db) << "\n";
102  error << "SQL error: " << errorMessage << "\n";
103  error << "SQL: " << sql << "\n";
104  sqlite3_free(errorMessage);
105  throw std::invalid_argument(error.str());
106  }
107  }
108 
109  void throwError(const std::string& message) {
110  std::stringstream error;
111  error << message << sqlite3_errmsg(db) << "\n";

◆ getFileName()

static std::string souffle::ReadStreamSQLite::getFileName ( const std::map< std::string, std::string > &  rwOperation)
inlinestaticprotected

Return given filename or construct from relation name.

Default name is [configured path]/[relation name].sqlite

Parameters
rwOperationmap of IO configuration options
Returns
input filename

Definition at line 167 of file ReadStreamSQLite.h.

169  {
170  name = getOr(rwOperation, "fact-dir", ".") + "/" + name;
171  }
172  return name;
173  }
174 
175  const std::string dbFilename;
176  const std::string relationName;
177  sqlite3_stmt* selectStatement = nullptr;

◆ openDB()

void souffle::ReadStreamSQLite::openDB ( )
inlineprotected

Definition at line 128 of file ReadStreamSQLite.h.

133  {
134  sqlite3_stmt* tableStatement;
135  std::stringstream selectSQL;

◆ prepareSelectStatement()

void souffle::ReadStreamSQLite::prepareSelectStatement ( )
inlineprotected

Definition at line 119 of file ReadStreamSQLite.h.

119  {
120  throwError("SQLite error in sqlite3_prepare_v2: ");
121  }
122  }
123 
124  void openDB() {
125  if (sqlite3_open(dbFilename.c_str(), &db) != SQLITE_OK) {
126  throwError("SQLite error in sqlite3_open: ");

References throwError().

Here is the call graph for this function:

◆ readNextTuple()

Own<RamDomain[]> souffle::ReadStreamSQLite::readNextTuple ( )
inlineoverrideprotectedvirtual

Read and return the next tuple.

Returns nullptr if no tuple was readable.

Returns

Implements souffle::ReadStream.

Definition at line 62 of file ReadStreamSQLite.h.

66  {
67  std::string element(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, column)));
68 
69  if (element.empty()) {
70  element = "n/a";
71  }
72 
73  try {
74  auto&& ty = typeAttributes.at(column);
75  switch (ty[0]) {
76  case 's': tuple[column] = symbolTable.unsafeLookup(element); break;
77  case 'i':
78  case 'u':
79  case 'f':
80  case 'r': tuple[column] = RamSignedFromString(element); break;
81  default: fatal("invalid type attribute: `%c`", ty[0]);
82  }
83  } catch (...) {
84  std::stringstream errorMessage;
85  errorMessage << "Error converting number in column " << (column) + 1;
86  throw std::invalid_argument(errorMessage.str());
87  }
88  }
89 
90  return tuple;
91  }
92 
93  void executeSQL(const std::string& sql) {
94  assert(db && "Database connection is closed");
95 

◆ throwError()

void souffle::ReadStreamSQLite::throwError ( const std::string &  message)
inlineprotected

Definition at line 113 of file ReadStreamSQLite.h.

115  {
116  std::stringstream selectSQL;
117  selectSQL << "SELECT * FROM '" << relationName << "'";

Referenced by checkTableExists(), and prepareSelectStatement().

Field Documentation

◆ db

sqlite3* souffle::ReadStreamSQLite::db = nullptr
protected

Definition at line 182 of file ReadStreamSQLite.h.

◆ dbFilename

const std::string souffle::ReadStreamSQLite::dbFilename
protected

Definition at line 179 of file ReadStreamSQLite.h.

◆ relationName

const std::string souffle::ReadStreamSQLite::relationName
protected

Definition at line 180 of file ReadStreamSQLite.h.

◆ selectStatement

sqlite3_stmt* souffle::ReadStreamSQLite::selectStatement = nullptr
protected

Definition at line 181 of file ReadStreamSQLite.h.

Referenced by ~ReadStreamSQLite().


The documentation for this class was generated from the following file:
souffle::RamSignedFromString
RamSigned RamSignedFromString(const std::string &str, std::size_t *position=nullptr, const int base=10)
Converts a string to a RamSigned.
Definition: StringUtil.h:51
souffle::ReadStreamSQLite::openDB
void openDB()
Definition: ReadStreamSQLite.h:128
souffle::ReadStreamSQLite::executeSQL
void executeSQL(const std::string &sql)
Definition: ReadStreamSQLite.h:97
souffle::ReadStreamSQLite::relationName
const std::string relationName
Definition: ReadStreamSQLite.h:180
souffle::SerialisationStream< false >::symbolTable
RO< SymbolTable > & symbolTable
Definition: SerialisationStream.h:71
souffle::SerialisationStream< false >::typeAttributes
std::vector< std::string > typeAttributes
Definition: SerialisationStream.h:74
souffle::getOr
C::mapped_type const & getOr(const C &container, typename C::key_type key, const typename C::mapped_type &defaultValue)
Get value for a given key; if not found, return default value.
Definition: ContainerUtil.h:111
souffle::ReadStreamSQLite::dbFilename
const std::string dbFilename
Definition: ReadStreamSQLite.h:179
souffle::test::count
int count(const C &c)
Definition: table_test.cpp:40
souffle::ReadStreamSQLite::throwError
void throwError(const std::string &message)
Definition: ReadStreamSQLite.h:113
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
souffle::ReadStreamSQLite::selectStatement
sqlite3_stmt * selectStatement
Definition: ReadStreamSQLite.h:181
souffle::ReadStreamSQLite::db
sqlite3 * db
Definition: ReadStreamSQLite.h:182