31 using std::runtime_error;
36 string connectString(dir);
37 if (connectString.size() > 0 && connectString[connectString.size() - 1] !=
'/') {
38 connectString.push_back(
'/');
40 connectString += dbName;
41 int returnCode = sqlite3_open(connectString.c_str(), &db_);
42 if (returnCode != SQLITE_OK) {
56 string errorMsg(sqlite3_errmsg(
db_));
57 throw runtime_error(
"Sqlite3 Error: " + errorMsg);
60 std::string
get_column_datum(
int column_type, sqlite3_stmt* stmt,
size_t column_index) {
61 const char* datum_ptr;
62 if (column_type == SQLITE_BLOB) {
63 datum_ptr =
static_cast<const char*
>(sqlite3_column_blob(stmt, column_index));
65 datum_ptr =
reinterpret_cast<const char*
>(sqlite3_column_text(stmt, column_index));
67 size_t datum_size = sqlite3_column_bytes(stmt, column_index);
68 return {datum_ptr, datum_size};
72 const std::vector<std::string>& text_params,
73 const std::vector<BindType>& bind_types) {
74 if (!bind_types.empty()) {
75 CHECK_EQ(text_params.size(), bind_types.size());
85 int returnCode = sqlite3_prepare_v2(
db_, queryString.c_str(), -1, &stmt,
nullptr);
86 if (returnCode != SQLITE_OK) {
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);
96 returnCode = sqlite3_bind_null(stmt, num_params++);
98 returnCode = sqlite3_bind_text(
99 stmt, num_params++, text_param.c_str(), text_param.size(), SQLITE_TRANSIENT);
101 if (returnCode != SQLITE_OK) {
107 returnCode = sqlite3_step(stmt);
108 if (returnCode != SQLITE_ROW && returnCode != SQLITE_DONE) {
111 if (returnCode == SQLITE_DONE) {
115 numCols_ = sqlite3_column_count(stmt);
116 for (
size_t c = 0; c <
numCols_; ++c) {
117 columnNames.emplace_back(sqlite3_column_name(stmt, c));
118 columnTypes.push_back(sqlite3_column_type(stmt, c));
124 for (
size_t c = 0; c <
numCols_; ++c) {
125 auto column_type = sqlite3_column_type(stmt, c);
126 bool is_null = (column_type == SQLITE_NULL);
129 CHECK(col_text.empty());
135 sqlite3_finalize(stmt);
139 const std::string& queryString,
140 const std::vector<std::string>& text_params) {
145 const std::string& text_param) {
154 std::vector<std::vector<std::string>>& insert_vals) {
155 const size_t num_rows = insert_vals.size();
159 const size_t num_cols(insert_vals[0].size());
163 std::string paramertized_query =
"INSERT INTO " + table_name +
" VALUES(";
164 for (
size_t col_idx = 0; col_idx < num_cols - 1; ++col_idx) {
165 paramertized_query +=
"?, ";
167 paramertized_query +=
"?)";
169 query(
"BEGIN TRANSACTION");
173 sqlite3_prepare_v2(
db_, paramertized_query.c_str(), -1, &stmt,
nullptr);
174 if (returnCode != SQLITE_OK) {
178 for (
size_t r = 0; r < num_rows; ++r) {
179 const auto& row_insert_vals = insert_vals[r];
181 for (
const auto& insert_field : row_insert_vals) {
182 returnCode = sqlite3_bind_text(stmt,
184 insert_field.c_str(),
187 if (returnCode != SQLITE_OK) {
191 returnCode = sqlite3_step(stmt);
192 if (returnCode != SQLITE_DONE) {
197 sqlite3_finalize(stmt);
198 query(
"END TRANSACTION");
virtual void query_with_text_params(std::string const &query_only)
virtual void batch_insert(const std::string &table_name, std::vector< std::vector< std::string >> &insert_vals)
virtual void query(const std::string &queryString)
std::string get_column_datum(int column_type, sqlite3_stmt *stmt, size_t column_index)
CONSTEXPR DEVICE bool is_null(const T &value)
std::vector< std::vector< NullableResult > > results_
std::vector< int > columnTypes
virtual ~SqliteConnector()
std::vector< std::string > columnNames
virtual void query_with_text_param(const std::string &queryString, const std::string &text_param)