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

#include <WriteStreamSQLite.h>

Inheritance diagram for souffle::WriteStreamSQLite:
Inheritance graph
Collaboration diagram for souffle::WriteStreamSQLite:
Collaboration graph

Public Member Functions

 WriteStreamSQLite (const std::map< std::string, std::string > &rwOperation, const SymbolTable &symbolTable, const RecordTable &recordTable)
 
 ~WriteStreamSQLite () override
 
- Public Member Functions inherited from souffle::WriteStream
template<typename T >
void writeAll (const T &relation)
 
template<>
void writeNext (const RamDomain *tuple)
 
template<typename T >
void writeSize (const T &relation)
 
 WriteStream (const std::map< std::string, std::string > &rwOperation, const SymbolTable &symbolTable, const RecordTable &recordTable)
 
- Public Member Functions inherited from souffle::SerialisationStream< true >
virtual ~SerialisationStream ()=default
 

Protected Member Functions

void writeNextTuple (const RamDomain *tuple) override
 
void writeNullary () override
 
- Protected Member Functions inherited from souffle::WriteStream
void outputADT (std::ostream &destination, const RamDomain value, const std::string &name)
 
void outputRecord (std::ostream &destination, const RamDomain value, const std::string &name)
 
template<typename Tuple >
void writeNext (const Tuple tuple)
 
virtual void writeSize (std::size_t)
 
- Protected Member Functions inherited from souffle::SerialisationStream< true >
 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)
 

Private Member Functions

void createRelationTable ()
 
void createRelationView ()
 
void createSymbolTable ()
 
void createTables ()
 
void executeSQL (const std::string &sql, sqlite3 *db)
 
uint64_t getSymbolTableID (int index)
 
uint64_t getSymbolTableIDFromDB (int index)
 
void openDB ()
 
void prepareInsertStatement ()
 
void prepareStatements ()
 
void prepareSymbolInsertStatement ()
 
void prepareSymbolSelectStatement ()
 
void throwError (const std::string &message)
 

Static Private Member Functions

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

Private Attributes

sqlite3 * db = nullptr
 
const std::string dbFilename
 
std::unordered_map< uint64_t, uint64_t > dbSymbolTable
 
sqlite3_stmt * insertStatement = nullptr
 
const std::string relationName
 
sqlite3_stmt * symbolInsertStatement = nullptr
 
sqlite3_stmt * symbolSelectStatement = nullptr
 
const std::string symbolTableName = "__SymbolTable"
 

Additional Inherited Members

- Protected Types inherited from souffle::SerialisationStream< true >
using RO = std::conditional_t< readOnlyTables, const A, A >
 
- Protected Attributes inherited from souffle::WriteStream
const bool summary
 
- Protected Attributes inherited from souffle::SerialisationStream< true >
size_t arity
 
size_t auxiliaryArity
 
RO< RecordTable > & recordTable
 
RO< SymbolTable > & symbolTable
 
std::vector< std::string > typeAttributes
 
Json types
 

Detailed Description

Definition at line 40 of file WriteStreamSQLite.h.

Constructor & Destructor Documentation

◆ WriteStreamSQLite()

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

Definition at line 42 of file WriteStreamSQLite.h.

48  {
49  sqlite3_finalize(insertStatement);
50  sqlite3_finalize(symbolInsertStatement);

◆ ~WriteStreamSQLite()

souffle::WriteStreamSQLite::~WriteStreamSQLite ( )
inlineoverride

Definition at line 52 of file WriteStreamSQLite.h.

55  :
56  void writeNullary() override {}
57 

Member Function Documentation

◆ createRelationTable()

void souffle::WriteStreamSQLite::createRelationTable ( )
inlineprivate

Definition at line 200 of file WriteStreamSQLite.h.

201  {
202  createTableText << ",'" << std::to_string(i) << "' ";
203  createTableText << "INTEGER";
204  }
205  }
206  createTableText << ");";
207  executeSQL(createTableText.str(), db);
208  executeSQL("DELETE FROM '_" + relationName + "';", db);
209  }
210 
211  void createRelationView() {
212  // Create view with symbol strings resolved
213  std::stringstream createViewText;

References i.

◆ createRelationView()

void souffle::WriteStreamSQLite::createRelationView ( )
inlineprivate

Definition at line 215 of file WriteStreamSQLite.h.

220  {
221  std::string columnName = std::to_string(i);
222  if (i != 0) {
223  projectionClause << ",";
224  }
225  if (typeAttributes.at(i)[0] == 's') {
226  projectionClause << "'_symtab_" << columnName << "'.symbol AS '" << columnName << "'";
227  fromClause << ",'" << symbolTableName << "' AS '_symtab_" << columnName << "'";
228  if (!firstWhere) {
229  whereClause << " AND ";
230  } else {
231  firstWhere = false;
232  }
233  whereClause << "'_" << relationName << "'.'" << columnName << "' = "
234  << "'_symtab_" << columnName << "'.id";
235  } else {
236  projectionClause << "'_" << relationName << "'.'" << columnName << "'";
237  }
238  }
239  createViewText << "SELECT " << projectionClause.str() << " FROM " << fromClause.str();
240  if (!firstWhere) {
241  createViewText << " WHERE " << whereClause.str();
242  }
243  createViewText << ";";
244  executeSQL(createViewText.str(), db);
245  }
246  void createSymbolTable() {
247  std::stringstream createTableText;
248  createTableText << "CREATE TABLE IF NOT EXISTS '" << symbolTableName << "' ";
249  createTableText << "(id INTEGER PRIMARY KEY, symbol TEXT UNIQUE);";

References i, relationName, symbolTableName, and souffle::SerialisationStream< true >::typeAttributes.

◆ createSymbolTable()

void souffle::WriteStreamSQLite::createSymbolTable ( )
inlineprivate

Definition at line 250 of file WriteStreamSQLite.h.

◆ createTables()

void souffle::WriteStreamSQLite::createTables ( )
inlineprivate

Definition at line 194 of file WriteStreamSQLite.h.

196  {
197  std::stringstream createTableText;
198  createTableText << "CREATE TABLE IF NOT EXISTS '_" << relationName << "' (";

◆ executeSQL()

void souffle::WriteStreamSQLite::executeSQL ( const std::string &  sql,
sqlite3 *  db 
)
inlineprivate

Definition at line 87 of file WriteStreamSQLite.h.

89  {
90  std::stringstream error;
91  error << "SQLite error in sqlite3_exec: " << sqlite3_errmsg(db) << "\n";
92  error << "SQL error: " << errorMessage << "\n";
93  error << "SQL: " << sql << "\n";
94  sqlite3_free(errorMessage);
95  throw std::invalid_argument(error.str());
96  }
97  }
98 
99  void throwError(const std::string& message) {
100  std::stringstream error;
101  error << message << sqlite3_errmsg(db) << "\n";

◆ getFileName()

static std::string souffle::WriteStreamSQLite::getFileName ( const std::map< std::string, std::string > &  rwOperation)
inlinestaticprivate

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 264 of file WriteStreamSQLite.h.

266  {
267  name = getOr(rwOperation, "output-dir", ".") + "/" + name;
268  }
269  return name;
270  }
271 
272  const std::string dbFilename;
273  const std::string relationName;
274  const std::string symbolTableName = "__SymbolTable";

◆ getSymbolTableID()

uint64_t souffle::WriteStreamSQLite::getSymbolTableID ( int  index)
inlineprivate

Definition at line 122 of file WriteStreamSQLite.h.

124  {
125  throwError("SQLite error in sqlite3_bind_text: ");
126  }
127  // Either the insert succeeds and we have a new row id or it already exists and a select is needed.
128  uint64_t rowid;
129  if (sqlite3_step(symbolInsertStatement) != SQLITE_DONE) {
130  // The symbol already exists so select it.
131  rowid = getSymbolTableIDFromDB(index);
132  } else {
133  rowid = sqlite3_last_insert_rowid(db);
134  }
135  sqlite3_clear_bindings(symbolInsertStatement);
136  sqlite3_reset(symbolInsertStatement);
137 
138  dbSymbolTable[index] = rowid;
139  return rowid;
140  }
141 
142  void openDB() {
143  if (sqlite3_open(dbFilename.c_str(), &db) != SQLITE_OK) {
144  throwError("SQLite error in sqlite3_open");

References throwError().

Referenced by writeNextTuple().

Here is the call graph for this function:

◆ getSymbolTableIDFromDB()

uint64_t souffle::WriteStreamSQLite::getSymbolTableIDFromDB ( int  index)
inlineprivate

Definition at line 109 of file WriteStreamSQLite.h.

110  {
111  throwError("SQLite error in sqlite3_step: ");
112  }
113  uint64_t rowid = sqlite3_column_int64(symbolSelectStatement, 0);
114  sqlite3_clear_bindings(symbolSelectStatement);
115  sqlite3_reset(symbolSelectStatement);
116  return rowid;
117  }
118  uint64_t getSymbolTableID(int index) {
119  if (dbSymbolTable.count(index) != 0) {
120  return dbSymbolTable[index];
121  }

◆ openDB()

void souffle::WriteStreamSQLite::openDB ( )
inlineprivate

Definition at line 146 of file WriteStreamSQLite.h.

◆ prepareInsertStatement()

void souffle::WriteStreamSQLite::prepareInsertStatement ( )
inlineprivate

Definition at line 180 of file WriteStreamSQLite.h.

180  {
181  insertSQL << ",@V" << i;
182  }
183  insertSQL << ");";
184  const char* tail = nullptr;
185  if (sqlite3_prepare_v2(db, insertSQL.str().c_str(), -1, &insertStatement, &tail) != SQLITE_OK) {
186  throwError("SQLite error in sqlite3_prepare_v2: ");
187  }
188  }
189 
190  void createTables() {

References i.

◆ prepareStatements()

void souffle::WriteStreamSQLite::prepareStatements ( )
inlineprivate

Definition at line 155 of file WriteStreamSQLite.h.

156  {
157  std::stringstream insertSQL;
158  insertSQL << "INSERT INTO " << symbolTableName;
159  insertSQL << " VALUES(null,@V0);";

◆ prepareSymbolInsertStatement()

void souffle::WriteStreamSQLite::prepareSymbolInsertStatement ( )
inlineprivate

Definition at line 160 of file WriteStreamSQLite.h.

161  {
162  throwError("SQLite error in sqlite3_prepare_v2: ");
163  }
164  }
165 
167  std::stringstream selectSQL;
168  selectSQL << "SELECT id FROM " << symbolTableName;

References throwError().

Here is the call graph for this function:

◆ prepareSymbolSelectStatement()

void souffle::WriteStreamSQLite::prepareSymbolSelectStatement ( )
inlineprivate

Definition at line 170 of file WriteStreamSQLite.h.

171  {
172  throwError("SQLite error in sqlite3_prepare_v2: ");
173  }
174  }
175 
176  void prepareInsertStatement() {
177  std::stringstream insertSQL;
178  insertSQL << "INSERT INTO '_" << relationName << "' VALUES ";

References throwError().

Here is the call graph for this function:

◆ throwError()

void souffle::WriteStreamSQLite::throwError ( const std::string &  message)
inlineprivate

Definition at line 103 of file WriteStreamSQLite.h.

105  {
106  if (sqlite3_bind_text(symbolSelectStatement, 1, symbolTable.unsafeResolve(index).c_str(), -1,
107  SQLITE_TRANSIENT) != SQLITE_OK) {

Referenced by getSymbolTableID(), prepareSymbolInsertStatement(), and prepareSymbolSelectStatement().

◆ writeNextTuple()

void souffle::WriteStreamSQLite::writeNextTuple ( const RamDomain tuple)
inlineoverrideprotectedvirtual

Implements souffle::WriteStream.

Definition at line 62 of file WriteStreamSQLite.h.

62  {
63  case 's': value = getSymbolTableID(tuple[i]); break;
64  default: value = tuple[i]; break;
65  }
66 
67 #if RAM_DOMAIN_SIZE == 64
68  if (sqlite3_bind_int64(insertStatement, i + 1, value) != SQLITE_OK) {
69 #else
70  if (sqlite3_bind_int(insertStatement, i + 1, value) != SQLITE_OK) {
71 #endif
72  throwError("SQLite error in sqlite3_bind_text: ");
73  }
74  }
75  if (sqlite3_step(insertStatement) != SQLITE_DONE) {
76  throwError("SQLite error in sqlite3_step: ");
77  }
78  sqlite3_clear_bindings(insertStatement);
79  sqlite3_reset(insertStatement);
80  }
81 
82 private:
83  void executeSQL(const std::string& sql, sqlite3* db) {
84  assert(db && "Database connection is closed");

References getSymbolTableID(), and i.

Here is the call graph for this function:

◆ writeNullary()

void souffle::WriteStreamSQLite::writeNullary ( )
inlineoverrideprotectedvirtual

Implements souffle::WriteStream.

Definition at line 60 of file WriteStreamSQLite.h.

62 {

Field Documentation

◆ db

sqlite3* souffle::WriteStreamSQLite::db = nullptr
private

Definition at line 284 of file WriteStreamSQLite.h.

◆ dbFilename

const std::string souffle::WriteStreamSQLite::dbFilename
private

Definition at line 276 of file WriteStreamSQLite.h.

◆ dbSymbolTable

std::unordered_map<uint64_t, uint64_t> souffle::WriteStreamSQLite::dbSymbolTable
private

Definition at line 280 of file WriteStreamSQLite.h.

◆ insertStatement

sqlite3_stmt* souffle::WriteStreamSQLite::insertStatement = nullptr
private

Definition at line 281 of file WriteStreamSQLite.h.

◆ relationName

const std::string souffle::WriteStreamSQLite::relationName
private

Definition at line 277 of file WriteStreamSQLite.h.

Referenced by createRelationView().

◆ symbolInsertStatement

sqlite3_stmt* souffle::WriteStreamSQLite::symbolInsertStatement = nullptr
private

Definition at line 282 of file WriteStreamSQLite.h.

◆ symbolSelectStatement

sqlite3_stmt* souffle::WriteStreamSQLite::symbolSelectStatement = nullptr
private

Definition at line 283 of file WriteStreamSQLite.h.

◆ symbolTableName

const std::string souffle::WriteStreamSQLite::symbolTableName = "__SymbolTable"
private

Definition at line 278 of file WriteStreamSQLite.h.

Referenced by createRelationView().


The documentation for this class was generated from the following file:
souffle::WriteStreamSQLite::getSymbolTableID
uint64_t getSymbolTableID(int index)
Definition: WriteStreamSQLite.h:122
souffle::WriteStreamSQLite::createTables
void createTables()
Definition: WriteStreamSQLite.h:194
souffle::WriteStreamSQLite::openDB
void openDB()
Definition: WriteStreamSQLite.h:146
souffle::WriteStreamSQLite::insertStatement
sqlite3_stmt * insertStatement
Definition: WriteStreamSQLite.h:281
souffle::WriteStreamSQLite::db
sqlite3 * db
Definition: WriteStreamSQLite.h:284
souffle::WriteStreamSQLite::dbFilename
const std::string dbFilename
Definition: WriteStreamSQLite.h:276
souffle::WriteStreamSQLite::prepareSymbolSelectStatement
void prepareSymbolSelectStatement()
Definition: WriteStreamSQLite.h:170
souffle::WriteStreamSQLite::writeNullary
void writeNullary() override
Definition: WriteStreamSQLite.h:60
souffle::SerialisationStream< true >::symbolTable
RO< SymbolTable > & symbolTable
Definition: SerialisationStream.h:71
souffle::SerialisationStream< true >::typeAttributes
std::vector< std::string > typeAttributes
Definition: SerialisationStream.h:74
souffle::WriteStreamSQLite::getSymbolTableIDFromDB
uint64_t getSymbolTableIDFromDB(int index)
Definition: WriteStreamSQLite.h:109
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::detail::brie::tail
auto tail(C &s)
Definition: Brie.h:110
i
size_t i
Definition: json11.h:663
souffle::WriteStreamSQLite::relationName
const std::string relationName
Definition: WriteStreamSQLite.h:277
souffle::WriteStreamSQLite::createRelationTable
void createRelationTable()
Definition: WriteStreamSQLite.h:200
souffle::WriteStreamSQLite::createRelationView
void createRelationView()
Definition: WriteStreamSQLite.h:215
souffle::WriteStreamSQLite::dbSymbolTable
std::unordered_map< uint64_t, uint64_t > dbSymbolTable
Definition: WriteStreamSQLite.h:280
souffle::WriteStreamSQLite::prepareInsertStatement
void prepareInsertStatement()
Definition: WriteStreamSQLite.h:180
souffle::WriteStreamSQLite::createSymbolTable
void createSymbolTable()
Definition: WriteStreamSQLite.h:250
souffle::WriteStreamSQLite::symbolTableName
const std::string symbolTableName
Definition: WriteStreamSQLite.h:278
souffle::WriteStreamSQLite::prepareSymbolInsertStatement
void prepareSymbolInsertStatement()
Definition: WriteStreamSQLite.h:160
souffle::WriteStreamSQLite::throwError
void throwError(const std::string &message)
Definition: WriteStreamSQLite.h:103
souffle::WriteStreamSQLite::executeSQL
void executeSQL(const std::string &sql, sqlite3 *db)
Definition: WriteStreamSQLite.h:87
souffle::WriteStreamSQLite::symbolInsertStatement
sqlite3_stmt * symbolInsertStatement
Definition: WriteStreamSQLite.h:282
souffle::WriteStreamSQLite::symbolSelectStatement
sqlite3_stmt * symbolSelectStatement
Definition: WriteStreamSQLite.h:283