From 00489fe5eefab42c0a25f71f9abf6ec8d572786d Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 10:57:56 +0100 Subject: [PATCH] lint --- cpp/utils.cpp | 532 +++++++++++++++++++++++++------------------------- cpp/utils.h | 4 +- 2 files changed, 268 insertions(+), 268 deletions(-) diff --git a/cpp/utils.cpp b/cpp/utils.cpp index 79f0ee80..5d67f199 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -14,331 +14,331 @@ namespace opsqlite { namespace jsi = facebook::jsi; jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) { - if (std::holds_alternative(value)) { - return std::get(value); - } else if (std::holds_alternative(value)) { - return jsi::Value(std::get(value)); - } else if (std::holds_alternative(value)) { - return jsi::Value(static_cast(std::get(value))); - } else if (std::holds_alternative(value)) { - return jsi::Value(std::get(value)); - } else if (std::holds_alternative(value)) { - auto str = std::get(value); - return jsi::String::createFromUtf8(rt, str); - } else if (std::holds_alternative(value)) { - auto jsBuffer = std::get(value); - jsi::Function array_buffer_ctor = + if (std::holds_alternative(value)) { + return std::get(value); + } else if (std::holds_alternative(value)) { + return jsi::Value(std::get(value)); + } else if (std::holds_alternative(value)) { + return jsi::Value(static_cast(std::get(value))); + } else if (std::holds_alternative(value)) { + return jsi::Value(std::get(value)); + } else if (std::holds_alternative(value)) { + auto str = std::get(value); + return jsi::String::createFromUtf8(rt, str); + } else if (std::holds_alternative(value)) { + auto jsBuffer = std::get(value); + jsi::Function array_buffer_ctor = rt.global().getPropertyAsFunction(rt, "ArrayBuffer"); - jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)jsBuffer.size) - .getObject(rt); - jsi::ArrayBuffer buf = o.getArrayBuffer(rt); - memcpy(buf.data(rt), jsBuffer.data.get(), jsBuffer.size); - return o; - } - - return jsi::Value::null(); + jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)jsBuffer.size) + .getObject(rt); + jsi::ArrayBuffer buf = o.getArrayBuffer(rt); + memcpy(buf.data(rt), jsBuffer.data.get(), jsBuffer.size); + return o; + } + + return jsi::Value::null(); } JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) { - if (value.isNull() || value.isUndefined()) { - return JSVariant(nullptr); - } else if (value.isBool()) { - return JSVariant(value.getBool()); - } else if (value.isNumber()) { - double doubleVal = value.asNumber(); - int intVal = (int)doubleVal; - long long longVal = (long)doubleVal; - if (intVal == doubleVal) { - return JSVariant(intVal); - } else if (longVal == doubleVal) { - return JSVariant(longVal); + if (value.isNull() || value.isUndefined()) { + return JSVariant(nullptr); + } else if (value.isBool()) { + return JSVariant(value.getBool()); + } else if (value.isNumber()) { + double doubleVal = value.asNumber(); + int intVal = (int)doubleVal; + long long longVal = (long)doubleVal; + if (intVal == doubleVal) { + return JSVariant(intVal); + } else if (longVal == doubleVal) { + return JSVariant(longVal); + } else { + return JSVariant(doubleVal); + } + } else if (value.isString()) { + std::string strVal = value.asString(rt).utf8(rt); + return JSVariant(strVal); + } else if (value.isObject()) { + auto obj = value.asObject(rt); + + if (!obj.isArrayBuffer(rt)) { + throw std::invalid_argument( + "Objects returned by OP-SQLite, are C++ HostObjects and thus cannot " + "store any object, only scalar " + "properties (int, long, double, string, bool) and ArrayBuffers."); + } + + auto buffer = obj.getArrayBuffer(rt); + uint8_t *data = new uint8_t[buffer.size(rt)]; + memcpy(data, buffer.data(rt), buffer.size(rt)); + + return JSVariant(ArrayBuffer{.data = std::shared_ptr{data}, + .size = buffer.size(rt)}); + } else { - return JSVariant(doubleVal); - } - } else if (value.isString()) { - std::string strVal = value.asString(rt).utf8(rt); - return JSVariant(strVal); - } else if (value.isObject()) { - auto obj = value.asObject(rt); - - if (!obj.isArrayBuffer(rt)) { - throw std::invalid_argument( - "Objects returned by OP-SQLite, are C++ HostObjects and thus cannot " - "store any object, only scalar " - "properties (int, long, double, string, bool) and ArrayBuffers."); + throw std::invalid_argument( + "Cannot convert JSI value to C++ Variant value"); } - - auto buffer = obj.getArrayBuffer(rt); - uint8_t *data = new uint8_t[buffer.size(rt)]; - memcpy(data, buffer.data(rt), buffer.size(rt)); - - return JSVariant(ArrayBuffer{.data = std::shared_ptr{data}, - .size = buffer.size(rt)}); - - } else { - throw std::invalid_argument( - "Cannot convert JSI value to C++ Variant value"); - } } std::vector to_string_vec(jsi::Runtime &rt, jsi::Value const &xs) { - jsi::Array values = xs.asObject(rt).asArray(rt); - std::vector res; - for (int ii = 0; ii < values.length(rt); ii++) { - std::string value = values.getValueAtIndex(rt, ii).asString(rt).utf8(rt); - res.push_back(value); - } - return res; + jsi::Array values = xs.asObject(rt).asArray(rt); + std::vector res; + for (int ii = 0; ii < values.length(rt); ii++) { + std::string value = values.getValueAtIndex(rt, ii).asString(rt).utf8(rt); + res.push_back(value); + } + return res; } std::vector to_int_vec(jsi::Runtime &rt, jsi::Value const &xs) { - jsi::Array values = xs.asObject(rt).asArray(rt); - std::vector res; - for (int ii = 0; ii < values.length(rt); ii++) { - int value = static_cast(values.getValueAtIndex(rt, ii).asNumber()); - res.push_back(value); - } - return res; + jsi::Array values = xs.asObject(rt).asArray(rt); + std::vector res; + for (int ii = 0; ii < values.length(rt); ii++) { + int value = static_cast(values.getValueAtIndex(rt, ii).asNumber()); + res.push_back(value); + } + return res; } std::vector to_variant_vec(jsi::Runtime &rt, jsi::Value const &xs) { - std::vector res; - - if (xs.isNull() || xs.isUndefined()) { - return res; - } - - jsi::Array values = xs.asObject(rt).asArray(rt); - - for (int ii = 0; ii < values.length(rt); ii++) { - jsi::Value value = values.getValueAtIndex(rt, ii); - - if (value.isNull() || value.isUndefined()) { - res.push_back(JSVariant(nullptr)); - } else if (value.isBool()) { - res.push_back(JSVariant(value.getBool())); - } else if (value.isNumber()) { - double doubleVal = value.asNumber(); - int intVal = (int)doubleVal; - long long longVal = (long)doubleVal; - if (intVal == doubleVal) { - res.push_back(JSVariant(intVal)); - } else if (longVal == doubleVal) { - res.push_back(JSVariant(longVal)); - } else { - res.push_back(JSVariant(doubleVal)); - } - } else if (value.isString()) { - std::string strVal = value.asString(rt).utf8(rt); - res.push_back(JSVariant(strVal)); - } else if (value.isObject()) { - auto obj = value.asObject(rt); - if (obj.isArrayBuffer(rt)) { - auto buffer = obj.getArrayBuffer(rt); - size_t size = buffer.size(rt); - uint8_t *data = new uint8_t[size]; - // You cannot share raw memory between native and JS - // always copy the data - // see https://github.com/facebook/hermes/pull/419 and - // https://github.com/facebook/hermes/issues/564. - memcpy(data, buffer.data(rt), size); - - res.push_back(JSVariant(ArrayBuffer{ - .data = std::shared_ptr{data}, .size = buffer.size(rt)})); - } else { - throw std::invalid_argument( - "Unknown JSI ArrayBuffer to variant value conversion, received " - "object instead of ArrayBuffer"); - } - } else { - throw std::invalid_argument("Unknown JSI to variant value conversion"); + std::vector res; + + if (xs.isNull() || xs.isUndefined()) { + return res; } - } - - return res; + + jsi::Array values = xs.asObject(rt).asArray(rt); + + for (int ii = 0; ii < values.length(rt); ii++) { + jsi::Value value = values.getValueAtIndex(rt, ii); + + if (value.isNull() || value.isUndefined()) { + res.push_back(JSVariant(nullptr)); + } else if (value.isBool()) { + res.push_back(JSVariant(value.getBool())); + } else if (value.isNumber()) { + double doubleVal = value.asNumber(); + int intVal = (int)doubleVal; + long long longVal = (long)doubleVal; + if (intVal == doubleVal) { + res.push_back(JSVariant(intVal)); + } else if (longVal == doubleVal) { + res.push_back(JSVariant(longVal)); + } else { + res.push_back(JSVariant(doubleVal)); + } + } else if (value.isString()) { + std::string strVal = value.asString(rt).utf8(rt); + res.push_back(JSVariant(strVal)); + } else if (value.isObject()) { + auto obj = value.asObject(rt); + if (obj.isArrayBuffer(rt)) { + auto buffer = obj.getArrayBuffer(rt); + size_t size = buffer.size(rt); + uint8_t *data = new uint8_t[size]; + // You cannot share raw memory between native and JS + // always copy the data + // see https://github.com/facebook/hermes/pull/419 and + // https://github.com/facebook/hermes/issues/564. + memcpy(data, buffer.data(rt), size); + + res.push_back(JSVariant(ArrayBuffer{ + .data = std::shared_ptr{data}, .size = buffer.size(rt)})); + } else { + throw std::invalid_argument( + "Unknown JSI ArrayBuffer to variant value conversion, received " + "object instead of ArrayBuffer"); + } + } else { + throw std::invalid_argument("Unknown JSI to variant value conversion"); + } + } + + return res; } jsi::Value create_js_rows(jsi::Runtime &rt, const BridgeResult &status) { - if (status.type == SQLiteError) { - throw std::invalid_argument(status.message); - } - - jsi::Object res = jsi::Object(rt); - - res.setProperty(rt, "rowsAffected", status.affectedRows); - if (status.affectedRows > 0 && status.insertId != 0) { - res.setProperty(rt, "insertId", jsi::Value(status.insertId)); - } - - size_t row_count = status.rows.size(); - auto rows = jsi::Array(rt, row_count); - - if (row_count > 0) { - for (int i = 0; i < row_count; i++) { - auto row = jsi::Array(rt, status.column_names.size()); - std::vector native_row = status.rows[i]; - for (int j = 0; j < native_row.size(); j++) { - auto value = toJSI(rt, native_row[j]); - row.setValueAtIndex(rt, j, value); - } - rows.setValueAtIndex(rt, i, row); + if (status.type == SQLiteError) { + throw std::invalid_argument(status.message); } - } - res.setProperty(rt, "rawRows", rows); - - size_t column_count = status.column_names.size(); - auto column_array = jsi::Array(rt, column_count); - for (int i = 0; i < column_count; i++) { - auto column = status.column_names.at(i); - column_array.setValueAtIndex(rt, i, toJSI(rt, column)); - } - res.setProperty(rt, "columnNames", std::move(column_array)); - return res; + + jsi::Object res = jsi::Object(rt); + + res.setProperty(rt, "rowsAffected", status.affectedRows); + if (status.affectedRows > 0 && status.insertId != 0) { + res.setProperty(rt, "insertId", jsi::Value(status.insertId)); + } + + size_t row_count = status.rows.size(); + auto rows = jsi::Array(rt, row_count); + + if (row_count > 0) { + for (int i = 0; i < row_count; i++) { + auto row = jsi::Array(rt, status.column_names.size()); + std::vector native_row = status.rows[i]; + for (int j = 0; j < native_row.size(); j++) { + auto value = toJSI(rt, native_row[j]); + row.setValueAtIndex(rt, j, value); + } + rows.setValueAtIndex(rt, i, row); + } + } + res.setProperty(rt, "rawRows", rows); + + size_t column_count = status.column_names.size(); + auto column_array = jsi::Array(rt, column_count); + for (int i = 0; i < column_count; i++) { + auto column = status.column_names.at(i); + column_array.setValueAtIndex(rt, i, toJSI(rt, column)); + } + res.setProperty(rt, "columnNames", std::move(column_array)); + return res; } jsi::Value createResult(jsi::Runtime &rt, BridgeResult status, std::vector *results, std::shared_ptr> metadata) { - jsi::Object res = jsi::Object(rt); - - res.setProperty(rt, "rowsAffected", status.affectedRows); - if (status.affectedRows > 0 && status.insertId != 0) { - res.setProperty(rt, "insertId", jsi::Value(status.insertId)); - } - - size_t rowCount = results->size(); - - auto array = jsi::Array(rt, rowCount); - for (int i = 0; i < rowCount; i++) { - auto obj = results->at(i); - array.setValueAtIndex(rt, i, - jsi::Object::createFromHostObject( - rt, std::make_shared(obj))); - } - res.setProperty(rt, "rows", std::move(array)); - - size_t column_count = metadata->size(); - auto column_array = jsi::Array(rt, column_count); - for (int i = 0; i < column_count; i++) { - auto column = metadata->at(i); - column_array.setValueAtIndex( - rt, i, - jsi::Object::createFromHostObject( - rt, std::make_shared(column))); - } - res.setProperty(rt, "metadata", std::move(column_array)); - - return std::move(res); + jsi::Object res = jsi::Object(rt); + + res.setProperty(rt, "rowsAffected", status.affectedRows); + if (status.affectedRows > 0 && status.insertId != 0) { + res.setProperty(rt, "insertId", jsi::Value(status.insertId)); + } + + size_t rowCount = results->size(); + + auto array = jsi::Array(rt, rowCount); + for (int i = 0; i < rowCount; i++) { + auto obj = results->at(i); + array.setValueAtIndex(rt, i, + jsi::Object::createFromHostObject( + rt, std::make_shared(obj))); + } + res.setProperty(rt, "rows", std::move(array)); + + size_t column_count = metadata->size(); + auto column_array = jsi::Array(rt, column_count); + for (int i = 0; i < column_count; i++) { + auto column = metadata->at(i); + column_array.setValueAtIndex( + rt, i, + jsi::Object::createFromHostObject( + rt, std::make_shared(column))); + } + res.setProperty(rt, "metadata", std::move(column_array)); + + return std::move(res); } jsi::Value create_raw_result(jsi::Runtime &rt, BridgeResult status, const std::vector> *results) { - size_t row_count = results->size(); - jsi::Array res = jsi::Array(rt, row_count); - for (int i = 0; i < row_count; i++) { - auto row = results->at(i); - auto array = jsi::Array(rt, row.size()); - for (int j = 0; j < row.size(); j++) { - array.setValueAtIndex(rt, j, toJSI(rt, row[j])); + size_t row_count = results->size(); + jsi::Array res = jsi::Array(rt, row_count); + for (int i = 0; i < row_count; i++) { + auto row = results->at(i); + auto array = jsi::Array(rt, row.size()); + for (int j = 0; j < row.size(); j++) { + array.setValueAtIndex(rt, j, toJSI(rt, row[j])); + } + res.setValueAtIndex(rt, i, array); } - res.setValueAtIndex(rt, i, array); - } - return res; + return res; } void to_batch_arguments(jsi::Runtime &rt, jsi::Array const &batchParams, std::vector *commands) { - for (int i = 0; i < batchParams.length(rt); i++) { - const jsi::Array &command = + for (int i = 0; i < batchParams.length(rt); i++) { + const jsi::Array &command = batchParams.getValueAtIndex(rt, i).asObject(rt).asArray(rt); - if (command.length(rt) == 0) { - continue; - } - - const std::string query = + if (command.length(rt) == 0) { + continue; + } + + const std::string query = command.getValueAtIndex(rt, 0).asString(rt).utf8(rt); - const jsi::Value &commandParams = command.length(rt) > 1 - ? command.getValueAtIndex(rt, 1) - : jsi::Value::undefined(); - if (!commandParams.isUndefined() && - commandParams.asObject(rt).isArray(rt) && - commandParams.asObject(rt).asArray(rt).length(rt) > 0 && - commandParams.asObject(rt) + const jsi::Value &commandParams = command.length(rt) > 1 + ? command.getValueAtIndex(rt, 1) + : jsi::Value::undefined(); + if (!commandParams.isUndefined() && + commandParams.asObject(rt).isArray(rt) && + commandParams.asObject(rt).asArray(rt).length(rt) > 0 && + commandParams.asObject(rt) .asArray(rt) .getValueAtIndex(rt, 0) .isObject()) { - // This arguments is an array of arrays, like a batch update of a single - // sql command. - const jsi::Array &batchUpdateParams = - commandParams.asObject(rt).asArray(rt); - for (int x = 0; x < batchUpdateParams.length(rt); x++) { - const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x); - auto params = - std::make_shared>(to_variant_vec(rt, p)); - commands->push_back({query, params}); - } - } else { - auto params = std::make_shared>( - to_variant_vec(rt, commandParams)); - commands->push_back({query, params}); + // This arguments is an array of arrays, like a batch update of a single + // sql command. + const jsi::Array &batchUpdateParams = + commandParams.asObject(rt).asArray(rt); + for (int x = 0; x < batchUpdateParams.length(rt); x++) { + const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x); + auto params = + std::make_shared>(to_variant_vec(rt, p)); + commands->push_back({query, params}); + } + } else { + auto params = std::make_shared>( + to_variant_vec(rt, commandParams)); + commands->push_back({query, params}); + } } - } } #ifndef OP_SQLITE_USE_LIBSQL BatchResult importSQLFile(std::string dbName, std::string fileLocation) { - std::string line; - std::ifstream sqFile(fileLocation); - if (sqFile.is_open()) { - try { - int affectedRows = 0; - int commands = 0; - opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr); - while (std::getline(sqFile, line, '\n')) { - if (!line.empty()) { - BridgeResult result = opsqlite_execute(dbName, line, nullptr); - if (result.type == SQLiteError) { - opsqlite_execute(dbName, "ROLLBACK", nullptr); + std::string line; + std::ifstream sqFile(fileLocation); + if (sqFile.is_open()) { + try { + int affectedRows = 0; + int commands = 0; + opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr); + while (std::getline(sqFile, line, '\n')) { + if (!line.empty()) { + BridgeResult result = opsqlite_execute(dbName, line, nullptr); + if (result.type == SQLiteError) { + opsqlite_execute(dbName, "ROLLBACK", nullptr); + sqFile.close(); + return {SQLiteError, result.message, 0, commands}; + } else { + affectedRows += result.affectedRows; + commands++; + } + } + } + sqFile.close(); + opsqlite_execute(dbName, "COMMIT", nullptr); + return {SQLiteOk, "", affectedRows, commands}; + } catch (...) { sqFile.close(); - return {SQLiteError, result.message, 0, commands}; - } else { - affectedRows += result.affectedRows; - commands++; - } + opsqlite_execute(dbName, "ROLLBACK", nullptr); + return {SQLiteError, + "[op-sqlite][loadSQLFile] Unexpected error, transaction was " + "rolledback", + 0, 0}; } - } - sqFile.close(); - opsqlite_execute(dbName, "COMMIT", nullptr); - return {SQLiteOk, "", affectedRows, commands}; - } catch (...) { - sqFile.close(); - opsqlite_execute(dbName, "ROLLBACK", nullptr); - return {SQLiteError, - "[op-sqlite][loadSQLFile] Unexpected error, transaction was " - "rolledback", - 0, 0}; + } else { + return {SQLiteError, "[op-sqlite][loadSQLFile] Could not open file", 0, 0}; } - } else { - return {SQLiteError, "[op-sqlite][loadSQLFile] Could not open file", 0, 0}; - } } #endif bool folder_exists(const std::string &foldername) { - struct stat buffer; - return (stat(foldername.c_str(), &buffer) == 0); + struct stat buffer; + return (stat(foldername.c_str(), &buffer) == 0); } bool file_exists(const std::string &path) { - struct stat buffer; - return (stat(path.c_str(), &buffer) == 0); + struct stat buffer; + return (stat(path.c_str(), &buffer) == 0); } int mkdir(std::string const &path) { - std::filesystem::create_directories(path); - return 0; + std::filesystem::create_directories(path); + return 0; } std::vector parse_string_list(const std::string& str) { diff --git a/cpp/utils.h b/cpp/utils.h index cd2383b5..575954bc 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -8,8 +8,8 @@ #include #include #include -#include #include +#include namespace opsqlite { @@ -46,7 +46,7 @@ bool folder_exists(const std::string &foldername); bool file_exists(const std::string &path); -std::vector parse_string_list(const std::string& str); +std::vector parse_string_list(const std::string &str); } // namespace opsqlite