Skip to content

Commit

Permalink
add new abi api
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberhan123 committed Dec 13, 2023
1 parent dc9d00a commit 954f97c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/cmake-build-debug/
/cmake-build-release/
/cmake-build-release-visual-studio/
2 changes: 0 additions & 2 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 20 additions & 22 deletions sqlite-abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include "json.hpp"
#include <string>

sqlite3* sqlite3_abi_init(const char* path) {
sqlite3* db;
sqlite3 *sqlite3_abi_init(const char *path) {
sqlite3 *db;
const auto result = sqlite3_open(path, &db);
if (result != SQLITE_OK) {
if (result != SQLITE_OK && db == nullptr) {
return nullptr;
}
return db;
}

const char* sqlite3_abi_exec(sqlite3* db, const char* sql) {
const char *sqlite3_abi_exec(sqlite3 *db, const char *sql) {
nlohmann::json resultArray = nlohmann::json::array();
auto state = sqlite3_exec(db, sql, [](void* data, int argc, char** argv, char** colName) -> int {
auto state = sqlite3_exec(db, sql, [](void *data, int argc, char **argv, char **colName) -> int {
auto row = nlohmann::json::object();
for (int i = 0; i < argc; i++) {
row[colName[i]] = argv[i] ? argv[i] : "NULL";
Expand All @@ -24,24 +24,22 @@ const char* sqlite3_abi_exec(sqlite3* db, const char* sql) {
}, nullptr, nullptr);

if (state != SQLITE_OK) {
nlohmann::json errorJson = {
{"code", SQLITE_ERROR},
{"message", "Error executing query: " + std::string(sqlite3_errmsg(db))},
{"list", nlohmann::json::array()}
};
const auto errorStr = errorJson.dump();
const auto resultStr = new char[errorStr.size() + 1];
std::strcpy(resultStr, errorStr.c_str());
return resultStr;
return "";
}

nlohmann::json successJson = {
{"code", SQLITE_OK},
{"message", "Query executed successfully"},
{"list", resultArray}
};
const std::string successStr = successJson.dump();
const std::string successStr = resultArray.dump();
const auto resultStr = new char[successStr.size() + 1];
std::strcpy(resultStr, successStr.c_str());
std::memcpy(resultStr, successStr.c_str(), successStr.size());
return resultStr;
}

int sqlite3_abi_close(sqlite3 *db) {
return sqlite3_close(db);
}

const char *sqlite3_abi_errmsg(sqlite3 *db) {
return sqlite3_errmsg(db);
}

int sqlite3_abi_errcode(sqlite3 *db) {
return sqlite3_errcode(db);
}
16 changes: 11 additions & 5 deletions sqlite-abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@
#ifdef SQLITE_ABI_SHARED
#if defined(_WIN32) && !defined(__MINGW32__)
#ifdef SQLITE_ABI_BUILD
#define SQLITE_ABI_API __declspec(dllexport)
#define SQLITE_ABI_API __declspec(dllexport)
#else
#define SQLITE_ABI_API __declspec(dllimport)
#define SQLITE_ABI_API __declspec(dllimport)
#endif
#else
#define SQLITE_ABI_API __attribute__((visibility("default")))
#endif
#else
#define SQLITE_ABI_API
#define SQLITE_ABI_API
#endif

#ifdef __cplusplus
extern "C" {
#endif

SQLITE_ABI_API sqlite3* sqlite3_abi_init(const char* path);
SQLITE_ABI_API sqlite3 *sqlite3_abi_init(const char *path);

SQLITE_ABI_API const char* sqlite3_abi_exec(sqlite3* db, const char* sql);
SQLITE_ABI_API const char *sqlite3_abi_exec(sqlite3 *db, const char *sql);

SQLITE_ABI_API int sqlite3_abi_close(sqlite3 *db);

SQLITE_ABI_API const char *sqlite3_abi_errmsg(sqlite3 *db);

SQLITE_ABI_API int sqlite3_abi_errcode(sqlite3 *db);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 954f97c

Please sign in to comment.