35 class ReadStreamSQLite :
public ReadStream {
66 for (column = 0; column <
arity; column++) {
67 std::string element(
reinterpret_cast<const char*
>(sqlite3_column_text(
selectStatement, column)));
69 if (element.empty()) {
81 default:
fatal(
"invalid type attribute: `%c`", ty[0]);
84 std::stringstream errorMessage;
85 errorMessage <<
"Error converting number in column " << (column) + 1;
86 throw std::invalid_argument(errorMessage.str());
94 assert(
db &&
"Database connection is closed");
96 char* errorMessage =
nullptr;
98 int rc = sqlite3_exec(
db, sql.c_str(),
nullptr,
nullptr, &errorMessage);
99 if (rc != SQLITE_OK) {
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());
110 std::stringstream error;
111 error << message << sqlite3_errmsg(
db) <<
"\n";
112 throw std::invalid_argument(error.str());
116 std::stringstream selectSQL;
118 const char*
tail =
nullptr;
120 throwError(
"SQLite error in sqlite3_prepare_v2: ");
125 if (sqlite3_open(
dbFilename.c_str(), &
db) != SQLITE_OK) {
128 sqlite3_extended_result_codes(
db, 1);
134 sqlite3_stmt* tableStatement;
135 std::stringstream selectSQL;
136 selectSQL <<
"SELECT count(*) FROM sqlite_master WHERE type IN ('table', 'view') AND ";
138 const char*
tail =
nullptr;
140 if (sqlite3_prepare_v2(
db, selectSQL.str().c_str(), -1, &tableStatement, &
tail) != SQLITE_OK) {
141 throwError(
"SQLite error in sqlite3_prepare_v2: ");
144 if (sqlite3_step(tableStatement) == SQLITE_ROW) {
145 int count = sqlite3_column_int(tableStatement, 0);
147 sqlite3_finalize(tableStatement);
151 sqlite3_finalize(tableStatement);
152 throw std::invalid_argument(
163 static std::string
getFileName(
const std::map<std::string, std::string>& rwOperation) {
166 auto name =
getOr(rwOperation,
"dbname", rwOperation.at(
"name") +
".sqlite");
167 name =
getOr(rwOperation,
"filename", name);
169 if (name.front() !=
'/') {
170 name =
getOr(rwOperation,
"fact-dir",
".") +
"/" + name;
178 sqlite3*
db =
nullptr;
185 return mk<ReadStreamSQLite>(rwOperation, symbolTable, recordTable);
188 const std::string&
getName()
const override {
189 static const std::string name =
"sqlite";