From 68ef56e5bcc43c500dd18037e817efebb3463e95 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 27 Jun 2023 19:57:26 +0800 Subject: [PATCH 01/29] GetModifyTime Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/include/js_utils.h | 3 + frameworks/js/napi/common/src/js_utils.cpp | 10 ++++ .../relationalstore/include/napi_rdb_store.h | 1 + .../relationalstore/src/napi_rdb_store.cpp | 30 ++++++++++ .../native/rdb/include/rdb_store_impl.h | 3 + frameworks/native/rdb/src/rdb_store_impl.cpp | 58 +++++++++++++++++++ interfaces/inner_api/rdb/include/rdb_store.h | 18 ++++++ interfaces/inner_api/rdb/include/rdb_types.h | 11 ++++ 8 files changed, 134 insertions(+) diff --git a/frameworks/js/napi/common/include/js_utils.h b/frameworks/js/napi/common/include/js_utils.h index 5f0785251..795a02baa 100644 --- a/frameworks/js/napi/common/include/js_utils.h +++ b/frameworks/js/napi/common/include/js_utils.h @@ -27,6 +27,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "rdb_types.h" namespace OHOS { namespace AppDataMgrJsKit { @@ -45,6 +46,7 @@ struct JsFeatureSpace { bool isComponent; }; +using Date = OHOS::DistributedRdb::Date; #ifndef ADD_JS_PROPERTY #define ADD_JS_PROPERTY(env, object, value, member) \ napi_set_named_property((env), (object), #member, Convert2JSValue((env), (value).member)) @@ -106,6 +108,7 @@ napi_value Convert2JSValue(napi_env env, double value); napi_value Convert2JSValue(napi_env env, bool value); napi_value Convert2JSValue(napi_env env, const std::map &value); napi_value Convert2JSValue(napi_env env, const std::monostate &value); +napi_value Convert2JSValue(napi_env env, Date date); template napi_value Convert2JSValue(napi_env env, const T &value); diff --git a/frameworks/js/napi/common/src/js_utils.cpp b/frameworks/js/napi/common/src/js_utils.cpp index 685d4c4b7..22ad4330b 100644 --- a/frameworks/js/napi/common/src/js_utils.cpp +++ b/frameworks/js/napi/common/src/js_utils.cpp @@ -451,6 +451,16 @@ napi_value JSUtils::Convert2JSValue(napi_env env, bool value) return jsValue; } +napi_value JSUtils::Convert2JSValue(napi_env env, Date date) +{ + napi_value jsValue; + napi_status status = napi_create_date(env, date, &jsValue); + if (status != napi_ok) { + return nullptr; + } + return jsValue; +} + napi_value JSUtils::Convert2JSValue(napi_env env, const std::map &value) { napi_value jsValue; diff --git a/frameworks/js/napi/relationalstore/include/napi_rdb_store.h b/frameworks/js/napi/relationalstore/include/napi_rdb_store.h index fcadeea4a..790c50581 100644 --- a/frameworks/js/napi/relationalstore/include/napi_rdb_store.h +++ b/frameworks/js/napi/relationalstore/include/napi_rdb_store.h @@ -70,6 +70,7 @@ private: static napi_value ObtainDistributedTableName(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); static napi_value CloudSync(napi_env env, napi_callback_info info); + static napi_value GetModifyTime(napi_env env, napi_callback_info info); static napi_value OnEvent(napi_env env, napi_callback_info info); static napi_value OffEvent(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index 29710b4c2..b10ef7095 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -158,6 +158,7 @@ void RdbStoreProxy::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("obtainDistributedTableName", ObtainDistributedTableName), DECLARE_NAPI_FUNCTION("sync", Sync), DECLARE_NAPI_FUNCTION("cloudSync", CloudSync), + DECLARE_NAPI_FUNCTION("GetModifyTime", GetModifyTime), DECLARE_NAPI_FUNCTION("on", OnEvent), DECLARE_NAPI_FUNCTION("off", OffEvent), #endif @@ -1193,6 +1194,35 @@ napi_value RdbStoreProxy::CloudSync(napi_env env, napi_callback_info info) return AsyncCall::Call(env, context); } +napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) +{ + LOG_DEBUG("RdbStoreProxy::GetModifyTime start"); + size_t argc = 3; + napi_value argv[3] = { nullptr }; + napi_value self = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr); + RDB_NAPI_ASSERT(env, status == napi_ok && argc == 3, std::make_shared("3")); + + std::string table; + auto cvStatus = JSUtils::Convert2Value(env, argv[0], table); + RDB_NAPI_ASSERT(env, cvStatus == napi_ok && !table.empty(), std::make_shared("table", "string")); + std::string columnName; + cvStatus = JSUtils::Convert2Value(env, argv[1], columnName); + RDB_NAPI_ASSERT( + env, cvStatus == napi_ok && !columnName.empty(), std::make_shared("columnName", "string")); + std::vector PKey; + cvStatus = JSUtils::Convert2Value(env, argv[2], PKey); + RDB_NAPI_ASSERT(env, cvStatus == napi_ok && !PKey.empty(), + std::make_shared("PRIKey", "number or string")); + + auto proxy = GetNativeInstance(env, self); + RDB_NAPI_ASSERT(env, proxy != nullptr, std::make_shared("RdbStore", "valid")); + + auto result = proxy->rdbStore_->GetModifyTime(table, columnName, PKey); + RDB_NAPI_ASSERT(env, !result.empty(), std::make_shared(14800000)); + return JSUtils::Convert2JSValue(env, result); +} + napi_value RdbStoreProxy::OnDataChangeEvent(napi_env env, size_t argc, napi_value *argv) { napi_valuetype type; diff --git a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h index 73929f4ee..e6f444760 100644 --- a/frameworks/native/rdb/include/rdb_store_impl.h +++ b/frameworks/native/rdb/include/rdb_store_impl.h @@ -118,6 +118,9 @@ public: // user must use UDID bool DropDeviceData(const std::vector& devices, const DropOption& option) override; + std::map GetModifyTime( + const std::string &table, const std::string &columnName, std::vector &PKey) override; + private: int InnerOpen(); int InnerInsert(int64_t &outRowId, const std::string &table, ValuesBucket values, diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index b24e27694..4a3d8621f 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -21,6 +21,7 @@ #include "logger.h" #include "rdb_errno.h" +#include "rdb_store.h" #include "rdb_trace.h" #include "rdb_sql_utils.h" #include "sqlite_global_config.h" @@ -36,6 +37,7 @@ #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) #include "iresult_set.h" +#include "raw_data_parser.h" #include "rdb_device_manager_adapter.h" #include "rdb_manager_impl.h" #include "relational_store_manager.h" @@ -243,6 +245,62 @@ int RdbStoreImpl::InsertWithConflictResolution(int64_t &outRowId, const std::str return InnerInsert(outRowId, table, initialValues, conflictResolution); } +std::map RdbStoreImpl::GetModifyTime( + const std::string &table, const std::string &columnName, std::vector &PKey) +{ + if (table.empty() || columnName.empty() || PKey.empty()) { + LOG_ERROR("invalid para."); + return {}; + } + if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT || + PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { + LOG_ERROR("invalid PRIKey type."); + return {}; + } + + auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); + std::vector> hashKeys; + hashKeys.reserve(PKey.size()); + std::map tmp; + std::map result; + for (const auto &key : PKey) { + DistributedDB::Type value; + if (!RawDataParser::Convert(key.value, value)) { + continue; + } + tmp[table] = value; + auto hashKey = DistributedDB::RelationalStoreManager::CalcPrimaryKeyHash(tmp); + if (hashKey.empty()) { + LOG_DEBUG("hash key fail"); + continue; + } + hashKeys.emplace_back(std::move(hashKey)); + PRIKey tmpKey; + RawDataParser::Convert(key.value, tmpKey); + result[tmpKey] = Date(0); + } + + std::string sql; + sql.append("select timestamp/10000 from "); // 百ns > ms + sql.append(logTable); + sql.append(" where hash_key=?"); + auto resultSet = QueryByStep(sql, std::move(PKey)); + int count = 0; + if (resultSet->GetRowCount(count) != E_OK || count != result.size()) { + resultSet->Close(); + return {}; + } + auto it = result.begin(); + for (int i = 0; i < count; i++) { + int64_t timeStamp; + resultSet->GetLong(i, timeStamp); + it->second = Date(timeStamp); + it++; + } + resultSet->Close(); + return result; +} + int RdbStoreImpl::InnerInsert(int64_t &outRowId, const std::string &table, ValuesBucket values, ConflictResolution conflictResolution) { diff --git a/interfaces/inner_api/rdb/include/rdb_store.h b/interfaces/inner_api/rdb/include/rdb_store.h index b420aec9d..ec32866b0 100644 --- a/interfaces/inner_api/rdb/include/rdb_store.h +++ b/interfaces/inner_api/rdb/include/rdb_store.h @@ -69,6 +69,12 @@ public: * @brief Use RdbStoreObserver replace DistributedRdb::RdbStoreObserver namespace. */ using RdbStoreObserver = DistributedRdb::RdbStoreObserver; + using PRIKey = RdbStoreObserver::PrimaryKey; + + /** + * @brief Use Date replace DistributedRdb::Date namespace. + */ + using Date = DistributedRdb::Date; /** * @brief Destructor. @@ -413,6 +419,18 @@ public: * @param option Indicates the drop option. */ virtual bool DropDeviceData(const std::vector& devices, const DropOption& option) = 0; + + /** + * @brief Get the the specified column modify time. + * + * @param table Indicates the specified table. + * @param columnName Indicates the column. + * @param PRIKey Indicates the primary key. + * + * @return Returns the specified column modify time. + */ + virtual std::map GetModifyTime( + const std::string &table, const std::string &columnName, std::vector &PKey) = 0; }; } // namespace OHOS::NativeRdb #endif diff --git a/interfaces/inner_api/rdb/include/rdb_types.h b/interfaces/inner_api/rdb/include/rdb_types.h index 62e024893..028b4f500 100644 --- a/interfaces/inner_api/rdb/include/rdb_types.h +++ b/interfaces/inner_api/rdb/include/rdb_types.h @@ -142,6 +142,17 @@ struct PredicatesMemo { std::vector operations_; }; +struct Date { + Date() {} + Date(int64_t date) : date(date) {} + operator double() const + { + return static_cast(date); + } + int64_t date; +}; + + enum SubscribeMode { REMOTE, CLOUD, -- Gitee From 2d247f61d5f4274968f84b9497ea3405417c5057 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 27 Jun 2023 20:01:07 +0800 Subject: [PATCH 02/29] update Signed-off-by: zuojiangjiang --- interfaces/inner_api/rdb/include/rdb_types.h | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/inner_api/rdb/include/rdb_types.h b/interfaces/inner_api/rdb/include/rdb_types.h index 028b4f500..f7fea8b50 100644 --- a/interfaces/inner_api/rdb/include/rdb_types.h +++ b/interfaces/inner_api/rdb/include/rdb_types.h @@ -152,7 +152,6 @@ struct Date { int64_t date; }; - enum SubscribeMode { REMOTE, CLOUD, -- Gitee From 1b0188cc970d31f01b50b4f25735033adf79969f Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 27 Jun 2023 20:13:49 +0800 Subject: [PATCH 03/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 4a3d8621f..9d774c118 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -293,7 +293,11 @@ std::map RdbStoreImpl::GetModifyTime( auto it = result.begin(); for (int i = 0; i < count; i++) { int64_t timeStamp; - resultSet->GetLong(i, timeStamp); + auto err = resultSet->GetLong(i, timeStamp); + if (err != E_OK) { + LOG_ERROR("query err:%{public}d", err); + return {}; + } it->second = Date(timeStamp); it++; } -- Gitee From 124f0442fa676ef5205eb0e8524f1a7596045d73 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 27 Jun 2023 21:25:01 +0800 Subject: [PATCH 04/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 9d774c118..aa4ff64e9 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -296,6 +296,7 @@ std::map RdbStoreImpl::GetModifyTime( auto err = resultSet->GetLong(i, timeStamp); if (err != E_OK) { LOG_ERROR("query err:%{public}d", err); + resultSet->Close(); return {}; } it->second = Date(timeStamp); -- Gitee From aa47eb398a3a0b5169fe22f917ffe0b5d3e05ff6 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Wed, 28 Jun 2023 16:02:26 +0800 Subject: [PATCH 05/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/include/rdb_store_impl.h | 1 + frameworks/native/rdb/include/sqlite_connection.h | 2 ++ frameworks/native/rdb/include/step_result_set.h | 3 +++ frameworks/native/rdb/src/rdb_store_impl.cpp | 5 +++++ frameworks/native/rdb/src/sqlite_connection.cpp | 14 ++++++++++++++ frameworks/native/rdb/src/step_result_set.cpp | 14 ++++++++++++-- 6 files changed, 37 insertions(+), 2 deletions(-) diff --git a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h index e6f444760..71e6969c4 100644 --- a/frameworks/native/rdb/include/rdb_store_impl.h +++ b/frameworks/native/rdb/include/rdb_store_impl.h @@ -91,6 +91,7 @@ public: std::string GetFileType(); std::shared_ptr QueryByStep(const std::string &sql, const std::vector &selectionArgs) override; + std::shared_ptr QueryByStep(const std::string &sql, std::vector &&args) override; std::shared_ptr QueryByStep( const AbsRdbPredicates &predicates, const std::vector columns) override; std::shared_ptr Query( diff --git a/frameworks/native/rdb/include/sqlite_connection.h b/frameworks/native/rdb/include/sqlite_connection.h index ec242bf2a..945f5ee13 100644 --- a/frameworks/native/rdb/include/sqlite_connection.h +++ b/frameworks/native/rdb/include/sqlite_connection.h @@ -47,6 +47,8 @@ public: const std::vector &bindArgs = std::vector()); std::shared_ptr BeginStepQuery(int &errCode, const std::string &sql, const std::vector &selectionArgs) const; + std::shared_ptr BeginStepQuery(int &errCode, const std::string &sql, + const std::vector &args) const; int DesFinalize(); int EndStepQuery(); void SetInTransaction(bool transaction); diff --git a/frameworks/native/rdb/include/step_result_set.h b/frameworks/native/rdb/include/step_result_set.h index 119371285..c7c4c2071 100644 --- a/frameworks/native/rdb/include/step_result_set.h +++ b/frameworks/native/rdb/include/step_result_set.h @@ -31,6 +31,8 @@ class StepResultSet : public AbsResultSet { public: StepResultSet(std::shared_ptr rdb, SqliteConnectionPool *connectionPool, const std::string &sql, const std::vector &selectionArgs); + StepResultSet(std::shared_ptr rdb, SqliteConnectionPool *pool, const std::string &sql, + std::vector &&args); ~StepResultSet() override; int GetAllColumnNames(std::vector &columnNames) override; @@ -66,6 +68,7 @@ private: SqliteConnectionPool *connectionPool_; std::string sql; std::vector selectionArgs; + std::vector args_; // Whether reach the end of this result set or not bool isAfterLast; // The value indicates the row count of the result set diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index aa4ff64e9..12676955e 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -1247,6 +1247,11 @@ std::shared_ptr RdbStoreImpl::QueryByStep(const std::string &sql, return std::make_shared(shared_from_this(), connectionPool, sql, selectionArgs); } +std::shared_ptr RdbStoreImpl::QueryByStep(const std::string &sql, std::vector &&args) +{ + return std::make_shared(shared_from_this(), connectionPool, sql, std::move(args)); +} + #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) int RdbStoreImpl::SetDistributedTables(const std::vector &tables, int32_t type, const DistributedRdb::DistributedConfig &distributedConfig) diff --git a/frameworks/native/rdb/src/sqlite_connection.cpp b/frameworks/native/rdb/src/sqlite_connection.cpp index fbdec8079..073497db4 100644 --- a/frameworks/native/rdb/src/sqlite_connection.cpp +++ b/frameworks/native/rdb/src/sqlite_connection.cpp @@ -706,6 +706,20 @@ std::shared_ptr SqliteConnection::BeginStepQuery( return stepStatement; } +std::shared_ptr SqliteConnection::BeginStepQuery(int &errCode, const std::string &sql, + const std::vector &args) const +{ + errCode = stepStatement->Prepare(dbHandle, sql); + if (errCode != E_OK) { + return nullptr; + } + errCode = stepStatement->BindArguments(args); + if (errCode != E_OK) { + return nullptr; + } + return stepStatement; +} + int SqliteConnection::DesFinalize() { int errCode = 0; diff --git a/frameworks/native/rdb/src/step_result_set.cpp b/frameworks/native/rdb/src/step_result_set.cpp index bea159a0d..97cfff203 100644 --- a/frameworks/native/rdb/src/step_result_set.cpp +++ b/frameworks/native/rdb/src/step_result_set.cpp @@ -31,7 +31,17 @@ using namespace OHOS::Rdb; StepResultSet::StepResultSet(std::shared_ptr rdb, SqliteConnectionPool *connectionPool, const std::string &sql, const std::vector &selectionArgs) - : rdb(rdb), connectionPool_(connectionPool), sql(sql), selectionArgs(selectionArgs), isAfterLast(false), + : rdb(rdb), connectionPool_(connectionPool), sql(sql), isAfterLast(false), + rowCount(INIT_POS), sqliteStatement(nullptr), connection_(connectionPool_->AcquireConnection(true)) +{ + for (auto arg : selectionArgs) { + args_.push_back(ValueObject(std::move(arg))); + } +} + +StepResultSet::StepResultSet(std::shared_ptr rdb, SqliteConnectionPool *pool, + const std::string &sql, std::vector &&args) + : rdb(rdb), connectionPool_(pool), sql(sql), args_(std::move(args)), isAfterLast(false), rowCount(INIT_POS), sqliteStatement(nullptr), connection_(connectionPool_->AcquireConnection(true)) { } @@ -256,7 +266,7 @@ int StepResultSet::PrepareStep() } int errCode; - sqliteStatement = connection_->BeginStepQuery(errCode, sql, selectionArgs); + sqliteStatement = connection_->BeginStepQuery(errCode, sql, args_); if (sqliteStatement == nullptr) { connection_->EndStepQuery(); LOG_ERROR("BeginStepQuery ret is %{public}d", errCode); -- Gitee From def4ad70b2de873d201a305a5640f22392efddc2 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Wed, 28 Jun 2023 16:15:53 +0800 Subject: [PATCH 06/29] update Signed-off-by: zuojiangjiang --- interfaces/inner_api/rdb/include/rdb_store.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/interfaces/inner_api/rdb/include/rdb_store.h b/interfaces/inner_api/rdb/include/rdb_store.h index ec32866b0..9785961c1 100644 --- a/interfaces/inner_api/rdb/include/rdb_store.h +++ b/interfaces/inner_api/rdb/include/rdb_store.h @@ -188,6 +188,13 @@ public: */ virtual std::shared_ptr QueryByStep( const std::string &sql, const std::vector &selectionArgs = std::vector()) = 0; + /** + * @brief Queries data in the database based on SQL statement. + * + * @param sql Indicates the SQL statement to execute. + * @param selectionArgs Indicates the selection arguments. + */ + virtual std::shared_ptr QueryByStep(const std::string &sql, std::vector &&args) = 0; /** * @brief Executes an SQL statement that contains specified parameters. -- Gitee From 633ade65e568289321cd97f7c4014bdcde516ba6 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Wed, 28 Jun 2023 16:33:51 +0800 Subject: [PATCH 07/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index b10ef7095..8edbe252a 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -158,7 +158,7 @@ void RdbStoreProxy::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("obtainDistributedTableName", ObtainDistributedTableName), DECLARE_NAPI_FUNCTION("sync", Sync), DECLARE_NAPI_FUNCTION("cloudSync", CloudSync), - DECLARE_NAPI_FUNCTION("GetModifyTime", GetModifyTime), + DECLARE_NAPI_FUNCTION("getModifyTime", GetModifyTime), DECLARE_NAPI_FUNCTION("on", OnEvent), DECLARE_NAPI_FUNCTION("off", OffEvent), #endif -- Gitee From f73bc8dd71dd8a0f3188f193a74a1dc02dad7d36 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Wed, 28 Jun 2023 19:27:56 +0800 Subject: [PATCH 08/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/include/js_utils.h | 2 - frameworks/js/napi/common/src/js_utils.cpp | 10 ----- .../include/napi_rdb_js_utils.h | 10 +++++ .../relationalstore/src/napi_rdb_js_utils.cpp | 41 +++++++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/frameworks/js/napi/common/include/js_utils.h b/frameworks/js/napi/common/include/js_utils.h index 795a02baa..ae615c486 100644 --- a/frameworks/js/napi/common/include/js_utils.h +++ b/frameworks/js/napi/common/include/js_utils.h @@ -46,7 +46,6 @@ struct JsFeatureSpace { bool isComponent; }; -using Date = OHOS::DistributedRdb::Date; #ifndef ADD_JS_PROPERTY #define ADD_JS_PROPERTY(env, object, value, member) \ napi_set_named_property((env), (object), #member, Convert2JSValue((env), (value).member)) @@ -108,7 +107,6 @@ napi_value Convert2JSValue(napi_env env, double value); napi_value Convert2JSValue(napi_env env, bool value); napi_value Convert2JSValue(napi_env env, const std::map &value); napi_value Convert2JSValue(napi_env env, const std::monostate &value); -napi_value Convert2JSValue(napi_env env, Date date); template napi_value Convert2JSValue(napi_env env, const T &value); diff --git a/frameworks/js/napi/common/src/js_utils.cpp b/frameworks/js/napi/common/src/js_utils.cpp index 22ad4330b..685d4c4b7 100644 --- a/frameworks/js/napi/common/src/js_utils.cpp +++ b/frameworks/js/napi/common/src/js_utils.cpp @@ -451,16 +451,6 @@ napi_value JSUtils::Convert2JSValue(napi_env env, bool value) return jsValue; } -napi_value JSUtils::Convert2JSValue(napi_env env, Date date) -{ - napi_value jsValue; - napi_status status = napi_create_date(env, date, &jsValue); - if (status != napi_ok) { - return nullptr; - } - return jsValue; -} - napi_value JSUtils::Convert2JSValue(napi_env env, const std::map &value) { napi_value jsValue; diff --git a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h index 97a3cbbf7..c72a17c95 100644 --- a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h +++ b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h @@ -27,11 +27,17 @@ namespace OHOS::AppDataMgrJsKit { namespace JSUtils { using Asset = OHOS::NativeRdb::AssetValue; using RowEntity = OHOS::NativeRdb::RowEntity; +using Date = OHOS::DistributedRdb::Date; +using PRIKey = OHOS::DistributedRdb::RdbStoreObserver::PrimaryKey; +using valueObject = OHOS::NativeRdb::ValueObject; using JSChangeInfo = OHOS::RelationalStoreJsKit::NapiRdbStoreObserver::JSChangeInfo; template<> int32_t Convert2Value(napi_env env, napi_value input, Asset &output); +template<> +int32_t Convert2Value(napi_env env, napi_value input, valueObject &output); + template<> napi_value Convert2JSValue(napi_env env, const Asset &value); @@ -50,6 +56,10 @@ template<> napi_value Convert2JSValue(napi_env env, const DistributedRdb::Details &details); template<> napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value); +template<> +napi_value Convert2JSValue(napi_env env, const std::map &value); +template<> +napi_value Convert2JSValue(napi_env env, const Date &date); }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit #endif // RDB_JSKIT_NAPI_RDB_JS_UTILS_H diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index 91011b854..33e8540b3 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -76,6 +76,12 @@ int32_t Convert2Value(napi_env env, napi_value input, DistributedRdb::Distribute return ret; } +template<> +int32_t Convert2Value(napi_env env, napi_value input, valueObject &output) +{ + return Convert2Value(env, input, output.value); +} + template<> napi_value Convert2JSValue(napi_env env, const Asset &value) { @@ -205,5 +211,40 @@ napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value) ADD_JS_PROPERTY(env, object, value, deleted); return object; } + +template<> +napi_value Convert2JSValue(napi_env env, const std::map &value) +{ + napi_value jsValue; + napi_status status = napi_create_array_with_length(env, value.size(), &jsValue); + if (status != napi_ok) { + return nullptr; + } + + int index = 0; + for (const auto &[key, date] : value) { + napi_value jsElement; + status = napi_create_array_with_length(env, SYNC_RESULT_ELEMNT_NUM, &jsElement); + if (status != napi_ok) { + return nullptr; + } + napi_set_element(env, jsElement, 0, Convert2JSValue(env, key)); + napi_set_element(env, jsElement, 1, Convert2JSValue(env, date)); + napi_set_element(env, jsValue, index++, jsElement); + } + + return jsValue; +} + +template<> +napi_value Convert2JSValue(napi_env env, const Date &date) +{ + napi_value jsValue; + napi_status status = napi_create_date(env, date, &jsValue); + if (status != napi_ok) { + return nullptr; + } + return jsValue; +} }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit \ No newline at end of file -- Gitee From 25653acba0f3dd0d00b4ed3e4767f704c6593b24 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Wed, 28 Jun 2023 19:29:05 +0800 Subject: [PATCH 09/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/include/js_utils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/js/napi/common/include/js_utils.h b/frameworks/js/napi/common/include/js_utils.h index ae615c486..5f0785251 100644 --- a/frameworks/js/napi/common/include/js_utils.h +++ b/frameworks/js/napi/common/include/js_utils.h @@ -27,7 +27,6 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" -#include "rdb_types.h" namespace OHOS { namespace AppDataMgrJsKit { -- Gitee From 35ad06e544ced0dc46b27bd3ec52108ab35a6e66 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Thu, 29 Jun 2023 19:49:11 +0800 Subject: [PATCH 10/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 12676955e..fe6e8f82e 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -259,7 +259,7 @@ std::map RdbStoreImpl::GetModifyTime( } auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); - std::vector> hashKeys; + std::vector hashKeys; hashKeys.reserve(PKey.size()); std::map tmp; std::map result; @@ -274,7 +274,7 @@ std::map RdbStoreImpl::GetModifyTime( LOG_DEBUG("hash key fail"); continue; } - hashKeys.emplace_back(std::move(hashKey)); + hashKeys.emplace_back(ValueObject(hashKey)); PRIKey tmpKey; RawDataParser::Convert(key.value, tmpKey); result[tmpKey] = Date(0); @@ -284,7 +284,7 @@ std::map RdbStoreImpl::GetModifyTime( sql.append("select timestamp/10000 from "); // 百ns > ms sql.append(logTable); sql.append(" where hash_key=?"); - auto resultSet = QueryByStep(sql, std::move(PKey)); + auto resultSet = QueryByStep(sql, std::move(hashKeys)); int count = 0; if (resultSet->GetRowCount(count) != E_OK || count != result.size()) { resultSet->Close(); -- Gitee From 23d46b9da54790acdf28261746ee4d72efdf43c5 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 10:04:05 +0800 Subject: [PATCH 11/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index fe6e8f82e..b6a6b8f21 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -252,7 +252,7 @@ std::map RdbStoreImpl::GetModifyTime( LOG_ERROR("invalid para."); return {}; } - if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT || + if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT && PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { LOG_ERROR("invalid PRIKey type."); return {}; -- Gitee From 0446c4d858a5bd5414179e4fe644b19b7b94f231 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 10:07:34 +0800 Subject: [PATCH 12/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index 8edbe252a..fead5dfef 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -1219,7 +1219,7 @@ napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) RDB_NAPI_ASSERT(env, proxy != nullptr, std::make_shared("RdbStore", "valid")); auto result = proxy->rdbStore_->GetModifyTime(table, columnName, PKey); - RDB_NAPI_ASSERT(env, !result.empty(), std::make_shared(14800000)); + RDB_NAPI_ASSERT(env, !result.empty(), std::make_shared(E_INNER_ERROR)); return JSUtils::Convert2JSValue(env, result); } -- Gitee From 50d243ec91af3e34fa51f9a8678670c9715b4d08 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 11:43:56 +0800 Subject: [PATCH 13/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 32 +++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index b6a6b8f21..dca0a6da0 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -260,9 +260,10 @@ std::map RdbStoreImpl::GetModifyTime( auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); std::vector hashKeys; + std::vector priKeys; hashKeys.reserve(PKey.size()); + priKeys.reserve(PKey.size()); std::map tmp; - std::map result; for (const auto &key : PKey) { DistributedDB::Type value; if (!RawDataParser::Convert(key.value, value)) { @@ -277,32 +278,35 @@ std::map RdbStoreImpl::GetModifyTime( hashKeys.emplace_back(ValueObject(hashKey)); PRIKey tmpKey; RawDataParser::Convert(key.value, tmpKey); - result[tmpKey] = Date(0); + priKeys.emplace_back(std::move(tmpKey)); } std::string sql; sql.append("select timestamp/10000 from "); // 百ns > ms sql.append(logTable); sql.append(" where hash_key=?"); - auto resultSet = QueryByStep(sql, std::move(hashKeys)); - int count = 0; - if (resultSet->GetRowCount(count) != E_OK || count != result.size()) { - resultSet->Close(); - return {}; - } - auto it = result.begin(); - for (int i = 0; i < count; i++) { + std::map result; + auto it = priKeys.begin(); + for (auto &haskKey : hashKeys) { + std::vector bindArg = { haskKey }; + auto resultSet = QueryByStep(sql, std::move(bindArg)); + int count = 0; + if (resultSet == nullptr || resultSet->GetRowCount() != E_OK || count <= 0) { + LOG_ERROR("get resultSet err."); + return {}; + } + resultSet->GoToRow(0); int64_t timeStamp; - auto err = resultSet->GetLong(i, timeStamp); + auto err = resultSet->GetLong(0, timeStamp); if (err != E_OK) { - LOG_ERROR("query err:%{public}d", err); + LOG_ERROR("query err:%{public}d.", err); resultSet->Close(); return {}; } - it->second = Date(timeStamp); + result[*it] = Data(timeStamp); it++; + resultSet->Close(); } - resultSet->Close(); return result; } -- Gitee From 37ebf32012d069294d00221e799bafc16816af10 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 11:51:09 +0800 Subject: [PATCH 14/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index dca0a6da0..47305a44b 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -291,7 +291,7 @@ std::map RdbStoreImpl::GetModifyTime( std::vector bindArg = { haskKey }; auto resultSet = QueryByStep(sql, std::move(bindArg)); int count = 0; - if (resultSet == nullptr || resultSet->GetRowCount() != E_OK || count <= 0) { + if (resultSet == nullptr || resultSet->GetRowCount(count) != E_OK || count <= 0) { LOG_ERROR("get resultSet err."); return {}; } @@ -303,7 +303,7 @@ std::map RdbStoreImpl::GetModifyTime( resultSet->Close(); return {}; } - result[*it] = Data(timeStamp); + result[*it] = Date(timeStamp); it++; resultSet->Close(); } -- Gitee From 3ef4c520f4437b3398f3015daf1af2fbb71aed98 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 11:57:18 +0800 Subject: [PATCH 15/29] update Signed-off-by: zuojiangjiang --- interfaces/inner_api/rdb/include/rdb_store.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/interfaces/inner_api/rdb/include/rdb_store.h b/interfaces/inner_api/rdb/include/rdb_store.h index 9785961c1..bac5b219b 100644 --- a/interfaces/inner_api/rdb/include/rdb_store.h +++ b/interfaces/inner_api/rdb/include/rdb_store.h @@ -188,13 +188,6 @@ public: */ virtual std::shared_ptr QueryByStep( const std::string &sql, const std::vector &selectionArgs = std::vector()) = 0; - /** - * @brief Queries data in the database based on SQL statement. - * - * @param sql Indicates the SQL statement to execute. - * @param selectionArgs Indicates the selection arguments. - */ - virtual std::shared_ptr QueryByStep(const std::string &sql, std::vector &&args) = 0; /** * @brief Executes an SQL statement that contains specified parameters. @@ -438,6 +431,14 @@ public: */ virtual std::map GetModifyTime( const std::string &table, const std::string &columnName, std::vector &PKey) = 0; + + /** + * @brief Queries data in the database based on SQL statement. + * + * @param sql Indicates the SQL statement to execute. + * @param args Indicates the selection arguments. + */ + virtual std::shared_ptr QueryByStep(const std::string &sql, std::vector &&args) = 0; }; } // namespace OHOS::NativeRdb #endif -- Gitee From 1286424ff486c1851ebb479d298344e70685e287 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 15:52:30 +0800 Subject: [PATCH 16/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 130 +++++++++---------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 47305a44b..f8d2faba4 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -109,6 +109,71 @@ void RdbStoreImpl::GetSchema(const RdbStoreConfig &config) }); } } + +std::map RdbStoreImpl::GetModifyTime( + const std::string &table, const std::string &columnName, std::vector &PKey) +{ + if (table.empty() || columnName.empty() || PKey.empty()) { + LOG_ERROR("invalid para."); + return {}; + } + if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT && + PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { + LOG_ERROR("invalid PRIKey type."); + return {}; + } + + auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); + std::vector hashKeys; + std::vector priKeys; + hashKeys.reserve(PKey.size()); + priKeys.reserve(PKey.size()); + std::map tmp; + for (const auto &key : PKey) { + DistributedDB::Type value; + if (!RawDataParser::Convert(key.value, value)) { + continue; + } + tmp[table] = value; + auto hashKey = DistributedDB::RelationalStoreManager::CalcPrimaryKeyHash(tmp); + if (hashKey.empty()) { + LOG_DEBUG("hash key fail"); + continue; + } + hashKeys.emplace_back(ValueObject(hashKey)); + PRIKey tmpKey; + RawDataParser::Convert(key.value, tmpKey); + priKeys.emplace_back(std::move(tmpKey)); + } + + std::string sql; + sql.append("select timestamp/10000 from "); // 百ns > ms + sql.append(logTable); + sql.append(" where hash_key=?"); + std::map result; + auto it = priKeys.begin(); + for (auto &haskKey : hashKeys) { + std::vector bindArg = { haskKey }; + auto resultSet = QueryByStep(sql, std::move(bindArg)); + int count = 0; + if (resultSet == nullptr || resultSet->GetRowCount(count) != E_OK || count <= 0) { + LOG_ERROR("get resultSet err."); + return {}; + } + resultSet->GoToRow(0); + int64_t timeStamp; + auto err = resultSet->GetLong(0, timeStamp); + if (err != E_OK) { + LOG_ERROR("query err:%{public}d.", err); + resultSet->Close(); + return {}; + } + result[*it] = Date(timeStamp); + it++; + resultSet->Close(); + } + return result; +} #endif RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config, int &errCode) @@ -245,71 +310,6 @@ int RdbStoreImpl::InsertWithConflictResolution(int64_t &outRowId, const std::str return InnerInsert(outRowId, table, initialValues, conflictResolution); } -std::map RdbStoreImpl::GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) -{ - if (table.empty() || columnName.empty() || PKey.empty()) { - LOG_ERROR("invalid para."); - return {}; - } - if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT && - PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { - LOG_ERROR("invalid PRIKey type."); - return {}; - } - - auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); - std::vector hashKeys; - std::vector priKeys; - hashKeys.reserve(PKey.size()); - priKeys.reserve(PKey.size()); - std::map tmp; - for (const auto &key : PKey) { - DistributedDB::Type value; - if (!RawDataParser::Convert(key.value, value)) { - continue; - } - tmp[table] = value; - auto hashKey = DistributedDB::RelationalStoreManager::CalcPrimaryKeyHash(tmp); - if (hashKey.empty()) { - LOG_DEBUG("hash key fail"); - continue; - } - hashKeys.emplace_back(ValueObject(hashKey)); - PRIKey tmpKey; - RawDataParser::Convert(key.value, tmpKey); - priKeys.emplace_back(std::move(tmpKey)); - } - - std::string sql; - sql.append("select timestamp/10000 from "); // 百ns > ms - sql.append(logTable); - sql.append(" where hash_key=?"); - std::map result; - auto it = priKeys.begin(); - for (auto &haskKey : hashKeys) { - std::vector bindArg = { haskKey }; - auto resultSet = QueryByStep(sql, std::move(bindArg)); - int count = 0; - if (resultSet == nullptr || resultSet->GetRowCount(count) != E_OK || count <= 0) { - LOG_ERROR("get resultSet err."); - return {}; - } - resultSet->GoToRow(0); - int64_t timeStamp; - auto err = resultSet->GetLong(0, timeStamp); - if (err != E_OK) { - LOG_ERROR("query err:%{public}d.", err); - resultSet->Close(); - return {}; - } - result[*it] = Date(timeStamp); - it++; - resultSet->Close(); - } - return result; -} - int RdbStoreImpl::InnerInsert(int64_t &outRowId, const std::string &table, ValuesBucket values, ConflictResolution conflictResolution) { -- Gitee From e938bd4a63100f5617c1e0b7bed85e737e27062f Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 16:41:11 +0800 Subject: [PATCH 17/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/mock/include/rdb_store_impl.h | 1 + frameworks/native/rdb/mock/include/sqlite_connection.h | 2 ++ frameworks/native/rdb/mock/include/step_result_set.h | 3 +++ interfaces/inner_api/rdb/mock/include/rdb_store.h | 1 + 4 files changed, 7 insertions(+) diff --git a/frameworks/native/rdb/mock/include/rdb_store_impl.h b/frameworks/native/rdb/mock/include/rdb_store_impl.h index 6da70afbe..1d41bac95 100644 --- a/frameworks/native/rdb/mock/include/rdb_store_impl.h +++ b/frameworks/native/rdb/mock/include/rdb_store_impl.h @@ -83,6 +83,7 @@ public: std::string GetFileType(); std::shared_ptr QueryByStep(const std::string &sql, const std::vector &selectionArgs) override; + std::shared_ptr QueryByStep(const std::string &sql, std::vector &&args) override; std::shared_ptr Query( const AbsRdbPredicates &predicates, const std::vector columns) override; int Count(int64_t &outValue, const AbsRdbPredicates &predicates) override; diff --git a/frameworks/native/rdb/mock/include/sqlite_connection.h b/frameworks/native/rdb/mock/include/sqlite_connection.h index 284464f7f..e730de68f 100644 --- a/frameworks/native/rdb/mock/include/sqlite_connection.h +++ b/frameworks/native/rdb/mock/include/sqlite_connection.h @@ -45,6 +45,8 @@ public: const std::vector &bindArgs = std::vector()); std::shared_ptr BeginStepQuery(int &errCode, const std::string &sql, const std::vector &selectionArgs) const; + std::shared_ptr BeginStepQuery(int &errCode, const std::string &sql, + const std::vector &args) const; int DesFinalize(); int EndStepQuery(); void SetInTransaction(bool transaction); diff --git a/frameworks/native/rdb/mock/include/step_result_set.h b/frameworks/native/rdb/mock/include/step_result_set.h index 119371285..c7c4c2071 100644 --- a/frameworks/native/rdb/mock/include/step_result_set.h +++ b/frameworks/native/rdb/mock/include/step_result_set.h @@ -31,6 +31,8 @@ class StepResultSet : public AbsResultSet { public: StepResultSet(std::shared_ptr rdb, SqliteConnectionPool *connectionPool, const std::string &sql, const std::vector &selectionArgs); + StepResultSet(std::shared_ptr rdb, SqliteConnectionPool *pool, const std::string &sql, + std::vector &&args); ~StepResultSet() override; int GetAllColumnNames(std::vector &columnNames) override; @@ -66,6 +68,7 @@ private: SqliteConnectionPool *connectionPool_; std::string sql; std::vector selectionArgs; + std::vector args_; // Whether reach the end of this result set or not bool isAfterLast; // The value indicates the row count of the result set diff --git a/interfaces/inner_api/rdb/mock/include/rdb_store.h b/interfaces/inner_api/rdb/mock/include/rdb_store.h index 37c7e3061..75e03a152 100644 --- a/interfaces/inner_api/rdb/mock/include/rdb_store.h +++ b/interfaces/inner_api/rdb/mock/include/rdb_store.h @@ -82,6 +82,7 @@ public: virtual bool IsReadOnly() const = 0; virtual bool IsMemoryRdb() const = 0; virtual int Restore(const std::string backupPath, const std::vector &newKey = std::vector()) = 0; + virtual std::shared_ptr QueryByStep(const std::string &sql, std::vector &&args) = 0; }; } // namespace OHOS::NativeRdb #endif -- Gitee From b4188dd09c59712b15337dcba944f3b2baccc36e Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 30 Jun 2023 19:52:51 +0800 Subject: [PATCH 18/29] update Signed-off-by: zuojiangjiang --- .../js/napi/relationalstore/include/napi_rdb_js_utils.h | 4 ++-- frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h index c72a17c95..48cc6d090 100644 --- a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h +++ b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h @@ -29,14 +29,14 @@ using Asset = OHOS::NativeRdb::AssetValue; using RowEntity = OHOS::NativeRdb::RowEntity; using Date = OHOS::DistributedRdb::Date; using PRIKey = OHOS::DistributedRdb::RdbStoreObserver::PrimaryKey; -using valueObject = OHOS::NativeRdb::ValueObject; +using ValueObject = OHOS::NativeRdb::ValueObject; using JSChangeInfo = OHOS::RelationalStoreJsKit::NapiRdbStoreObserver::JSChangeInfo; template<> int32_t Convert2Value(napi_env env, napi_value input, Asset &output); template<> -int32_t Convert2Value(napi_env env, napi_value input, valueObject &output); +int32_t Convert2Value(napi_env env, napi_value input, ValueObject &output); template<> napi_value Convert2JSValue(napi_env env, const Asset &value); diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index 33e8540b3..8080e0815 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -77,7 +77,7 @@ int32_t Convert2Value(napi_env env, napi_value input, DistributedRdb::Distribute } template<> -int32_t Convert2Value(napi_env env, napi_value input, valueObject &output) +int32_t Convert2Value(napi_env env, napi_value input, ValueObject &output) { return Convert2Value(env, input, output.value); } -- Gitee From 26568de8f3058b7f4107c4a9cd5892fb83cc43a8 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 1 Jul 2023 09:38:56 +0800 Subject: [PATCH 19/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/include/js_utils.h | 27 +++++++++++++++++++ .../include/napi_rdb_js_utils.h | 3 --- .../relationalstore/src/napi_rdb_js_utils.cpp | 24 ----------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/frameworks/js/napi/common/include/js_utils.h b/frameworks/js/napi/common/include/js_utils.h index 5f0785251..4fbb7fadb 100644 --- a/frameworks/js/napi/common/include/js_utils.h +++ b/frameworks/js/napi/common/include/js_utils.h @@ -113,6 +113,9 @@ napi_value Convert2JSValue(napi_env env, const T &value); template napi_value Convert2JSValue(napi_env env, const std::vector &value); +template +napi_value Convert2JSValue(napi_env env, const std::map &value); + template napi_value Convert2JSValue(napi_env env, const std::variant &value); @@ -179,6 +182,30 @@ int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::vector return napi_ok; } +template +napi_value JSUtils::Convert2JSValue(napi_env env, const std::map &value) +{ + napi_value jsValue; + napi_status status = napi_create_array_with_length(env, value.size(), &jsValue); + if (status != napi_ok) { + return nullptr; + } + + int index = 0; + for (const auto &item : value) { + napi_value jsElement; + status = napi_create_array_with_length(env, SYNC_RESULT_ELEMNT_NUM, &jsElement); + if (status != napi_ok) { + return nullptr; + } + napi_set_element(env, jsElement, 0, Convert2JSValue(env, item.first)); + napi_set_element(env, jsElement, 1, Convert2JSValue(env, item.second)); + napi_set_element(env, jsValue, index++, jsElement); + } + + return jsValue; +} + template int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::variant &value) { diff --git a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h index 48cc6d090..76cdcaf2e 100644 --- a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h +++ b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h @@ -28,7 +28,6 @@ namespace JSUtils { using Asset = OHOS::NativeRdb::AssetValue; using RowEntity = OHOS::NativeRdb::RowEntity; using Date = OHOS::DistributedRdb::Date; -using PRIKey = OHOS::DistributedRdb::RdbStoreObserver::PrimaryKey; using ValueObject = OHOS::NativeRdb::ValueObject; using JSChangeInfo = OHOS::RelationalStoreJsKit::NapiRdbStoreObserver::JSChangeInfo; @@ -57,8 +56,6 @@ napi_value Convert2JSValue(napi_env env, const DistributedRdb::Details &details) template<> napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value); template<> -napi_value Convert2JSValue(napi_env env, const std::map &value); -template<> napi_value Convert2JSValue(napi_env env, const Date &date); }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index 8080e0815..3a2e22ca3 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -212,30 +212,6 @@ napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value) return object; } -template<> -napi_value Convert2JSValue(napi_env env, const std::map &value) -{ - napi_value jsValue; - napi_status status = napi_create_array_with_length(env, value.size(), &jsValue); - if (status != napi_ok) { - return nullptr; - } - - int index = 0; - for (const auto &[key, date] : value) { - napi_value jsElement; - status = napi_create_array_with_length(env, SYNC_RESULT_ELEMNT_NUM, &jsElement); - if (status != napi_ok) { - return nullptr; - } - napi_set_element(env, jsElement, 0, Convert2JSValue(env, key)); - napi_set_element(env, jsElement, 1, Convert2JSValue(env, date)); - napi_set_element(env, jsValue, index++, jsElement); - } - - return jsValue; -} - template<> napi_value Convert2JSValue(napi_env env, const Date &date) { -- Gitee From 8b2d2e4b4273fa79a312a1b459b8e529d29cec03 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 1 Jul 2023 15:47:06 +0800 Subject: [PATCH 20/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/src/js_utils.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/common/src/js_utils.cpp b/frameworks/js/napi/common/src/js_utils.cpp index 685d4c4b7..8f8039eb6 100644 --- a/frameworks/js/napi/common/src/js_utils.cpp +++ b/frameworks/js/napi/common/src/js_utils.cpp @@ -231,7 +231,19 @@ int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, double &output) int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, int64_t &output) { - return napi_invalid_arg; + napi_valuetype type; + napi_status status = napi_typeof(env, jsValue, &type); + if (status != napi_ok || type != napi_number) { + LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); + return napi_invalid_arg; + } + + status = napi_get_value_int64(env, jsValue, &output); + if (status != napi_ok) { + LOG_DEBUG("Convert2Value napi_get_value_int64 failed, status = %{public}d", status); + return status; + } + return status; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::string &output) -- Gitee From 909d867cbea408b47ce7fe2bfb278c3299865b9e Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 1 Jul 2023 19:37:09 +0800 Subject: [PATCH 21/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/src/js_utils.cpp | 14 +------------- frameworks/native/rdb/src/rdb_store_impl.cpp | 1 + 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/frameworks/js/napi/common/src/js_utils.cpp b/frameworks/js/napi/common/src/js_utils.cpp index 8f8039eb6..685d4c4b7 100644 --- a/frameworks/js/napi/common/src/js_utils.cpp +++ b/frameworks/js/napi/common/src/js_utils.cpp @@ -231,19 +231,7 @@ int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, double &output) int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, int64_t &output) { - napi_valuetype type; - napi_status status = napi_typeof(env, jsValue, &type); - if (status != napi_ok || type != napi_number) { - LOG_DEBUG("napi_typeof failed status = %{public}d type = %{public}d", status, type); - return napi_invalid_arg; - } - - status = napi_get_value_int64(env, jsValue, &output); - if (status != napi_ok) { - LOG_DEBUG("Convert2Value napi_get_value_int64 failed, status = %{public}d", status); - return status; - } - return status; + return napi_invalid_arg; } int32_t JSUtils::Convert2Value(napi_env env, napi_value jsValue, std::string &output) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index f8d2faba4..e9022e600 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -118,6 +118,7 @@ std::map RdbStoreImpl::GetModifyTime( return {}; } if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT && + PKey.begin()->GetType() != ValueObject::TypeId::TYPE_DOUBLE && PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { LOG_ERROR("invalid PRIKey type."); return {}; -- Gitee From 0749ff1c8168aaf6e3edacdb6e712706f5b4a124 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 1 Jul 2023 20:21:07 +0800 Subject: [PATCH 22/29] update Signed-off-by: zuojiangjiang --- frameworks/native/rdb/src/rdb_store_impl.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index e9022e600..a065772ae 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -117,8 +117,7 @@ std::map RdbStoreImpl::GetModifyTime( LOG_ERROR("invalid para."); return {}; } - if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_INT && - PKey.begin()->GetType() != ValueObject::TypeId::TYPE_DOUBLE && + if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_DOUBLE && PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { LOG_ERROR("invalid PRIKey type."); return {}; -- Gitee From 7a5320f92ed874b74a1e33bf86ce2a92e06b22d3 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sat, 1 Jul 2023 21:59:45 +0800 Subject: [PATCH 23/29] update Signed-off-by: zuojiangjiang --- interfaces/inner_api/rdb/include/rdb_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/inner_api/rdb/include/rdb_types.h b/interfaces/inner_api/rdb/include/rdb_types.h index f7fea8b50..38721257d 100644 --- a/interfaces/inner_api/rdb/include/rdb_types.h +++ b/interfaces/inner_api/rdb/include/rdb_types.h @@ -193,7 +193,7 @@ public: CHG_TYPE_DELETE, CHG_TYPE_BUTT }; - using PrimaryKey = std::variant; + using PrimaryKey = std::variant; using ChangeInfo = std::map[CHG_TYPE_BUTT]>; using PrimaryFields = std::map; virtual void OnChange(const std::vector &devices) = 0; // networkid -- Gitee From 909accc1926b2c888361b965fa33ff9a34871605 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 2 Jul 2023 19:59:59 +0800 Subject: [PATCH 24/29] update Signed-off-by: zuojiangjiang --- frameworks/js/napi/common/include/js_utils.h | 28 +++-- .../include/napi_rdb_js_utils.h | 7 +- .../relationalstore/src/napi_rdb_js_utils.cpp | 24 +++- .../relationalstore/src/napi_rdb_store.cpp | 2 +- .../native/rdb/include/rdb_store_impl.h | 5 +- frameworks/native/rdb/src/rdb_store_impl.cpp | 103 ++++++++++++------ interfaces/inner_api/rdb/include/rdb_store.h | 2 +- 7 files changed, 113 insertions(+), 58 deletions(-) diff --git a/frameworks/js/napi/common/include/js_utils.h b/frameworks/js/napi/common/include/js_utils.h index 4fbb7fadb..0f2f3d86f 100644 --- a/frameworks/js/napi/common/include/js_utils.h +++ b/frameworks/js/napi/common/include/js_utils.h @@ -119,6 +119,21 @@ napi_value Convert2JSValue(napi_env env, const std::map &value); template napi_value Convert2JSValue(napi_env env, const std::variant &value); +template +std::string ToString(const T &key); + +template +std::enable_if_t, std::string> ConvertMapKey(const K &key) +{ + return ToString(key); +} + +template +std::enable_if_t, const std::string &> ConvertMapKey(const K &key) +{ + return key; +} + template int32_t GetCPPValue(napi_env env, napi_value jsValue, T &value) { @@ -186,23 +201,18 @@ template napi_value JSUtils::Convert2JSValue(napi_env env, const std::map &value) { napi_value jsValue; - napi_status status = napi_create_array_with_length(env, value.size(), &jsValue); + napi_status status = napi_create_object(env, &jsValue); if (status != napi_ok) { return nullptr; } - int index = 0; - for (const auto &item : value) { - napi_value jsElement; - status = napi_create_array_with_length(env, SYNC_RESULT_ELEMNT_NUM, &jsElement); + for (const auto &[key, val] : value) { + const std::string &name = ConvertMapKey(key); + status = napi_set_named_property(env, jsValue, name.c_str(), Convert2JSValue(env, val)); if (status != napi_ok) { return nullptr; } - napi_set_element(env, jsElement, 0, Convert2JSValue(env, item.first)); - napi_set_element(env, jsElement, 1, Convert2JSValue(env, item.second)); - napi_set_element(env, jsValue, index++, jsElement); } - return jsValue; } diff --git a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h index 76cdcaf2e..2255744f6 100644 --- a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h +++ b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h @@ -28,15 +28,12 @@ namespace JSUtils { using Asset = OHOS::NativeRdb::AssetValue; using RowEntity = OHOS::NativeRdb::RowEntity; using Date = OHOS::DistributedRdb::Date; -using ValueObject = OHOS::NativeRdb::ValueObject; using JSChangeInfo = OHOS::RelationalStoreJsKit::NapiRdbStoreObserver::JSChangeInfo; +using PRIKey = OHOS::DistributedRdb::RdbStoreObserver::PrimaryKey; template<> int32_t Convert2Value(napi_env env, napi_value input, Asset &output); -template<> -int32_t Convert2Value(napi_env env, napi_value input, ValueObject &output); - template<> napi_value Convert2JSValue(napi_env env, const Asset &value); @@ -57,6 +54,8 @@ template<> napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value); template<> napi_value Convert2JSValue(napi_env env, const Date &date); +template<> +std::string ToString(const T &key); }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit #endif // RDB_JSKIT_NAPI_RDB_JS_UTILS_H diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index 3a2e22ca3..a6cc5146f 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -76,12 +76,6 @@ int32_t Convert2Value(napi_env env, napi_value input, DistributedRdb::Distribute return ret; } -template<> -int32_t Convert2Value(napi_env env, napi_value input, ValueObject &output) -{ - return Convert2Value(env, input, output.value); -} - template<> napi_value Convert2JSValue(napi_env env, const Asset &value) { @@ -222,5 +216,23 @@ napi_value Convert2JSValue(napi_env env, const Date &date) } return jsValue; } + +template<> +std::string ToString(const T &key) +{ + auto strVal = std::get_if(&key); + if (strVal != nullptr) { + return *strVal; + } + auto intVal = std::get_if(&key); + if (intVal != nullptr) { + return std::to_string(*intVal); + } + auto dbVal = std::get_if(&key); + if (dbVal != nullptr) { + return std::to_string(*dbVal); + } + return {}; +} }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit \ No newline at end of file diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index fead5dfef..92675ebd6 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -1210,7 +1210,7 @@ napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) cvStatus = JSUtils::Convert2Value(env, argv[1], columnName); RDB_NAPI_ASSERT( env, cvStatus == napi_ok && !columnName.empty(), std::make_shared("columnName", "string")); - std::vector PKey; + std::vector PKey; cvStatus = JSUtils::Convert2Value(env, argv[2], PKey); RDB_NAPI_ASSERT(env, cvStatus == napi_ok && !PKey.empty(), std::make_shared("PRIKey", "number or string")); diff --git a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h index 71e6969c4..9ee842cdd 100644 --- a/frameworks/native/rdb/include/rdb_store_impl.h +++ b/frameworks/native/rdb/include/rdb_store_impl.h @@ -120,7 +120,7 @@ public: bool DropDeviceData(const std::vector& devices, const DropOption& option) override; std::map GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) override; + const std::string &table, const std::string &columnName, std::vector &PKey) override; private: int InnerOpen(); @@ -138,6 +138,9 @@ private: void DoCloudSync(const std::string &table); int InnerBackup(const std::string databasePath, const std::vector destEncryptKey = std::vector()); + std::map GetModifyTimeByRowId( + const std::string &logTable, std::vector &PKey); + std::string GetSqlArgs(size_t size); const RdbStoreConfig rdbStoreConfig; SqliteConnectionPool *connectionPool; diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index a065772ae..f163c74bb 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -111,69 +111,100 @@ void RdbStoreImpl::GetSchema(const RdbStoreConfig &config) } std::map RdbStoreImpl::GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) + const std::string &table, const std::string &columnName, std::vector &PKey) { if (table.empty() || columnName.empty() || PKey.empty()) { LOG_ERROR("invalid para."); return {}; } - if (PKey.begin()->GetType() != ValueObject::TypeId::TYPE_DOUBLE && - PKey.begin()->GetType() != ValueObject::TypeId::TYPE_STRING) { - LOG_ERROR("invalid PRIKey type."); - return {}; - } auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); + if (columnName == "rowid") { + return GetModifyTimeByRowId(logTable, PKey); + } std::vector hashKeys; - std::vector priKeys; hashKeys.reserve(PKey.size()); - priKeys.reserve(PKey.size()); + std::map, PRIKey> keyMap; std::map tmp; for (const auto &key : PKey) { DistributedDB::Type value; - if (!RawDataParser::Convert(key.value, value)) { - continue; - } - tmp[table] = value; + RawDataParser::Convert(key.value, value); + tmp[columnName] = value; auto hashKey = DistributedDB::RelationalStoreManager::CalcPrimaryKeyHash(tmp); if (hashKey.empty()) { LOG_DEBUG("hash key fail"); continue; } hashKeys.emplace_back(ValueObject(hashKey)); - PRIKey tmpKey; - RawDataParser::Convert(key.value, tmpKey); - priKeys.emplace_back(std::move(tmpKey)); + keyMap[hashKey] = key; } std::string sql; - sql.append("select timestamp/10000 from "); // 百ns > ms + sql.append("select hash_key, timestamp/10000 from "); sql.append(logTable); - sql.append(" where hash_key=?"); + sql.append(" where hash_key in ("); + sql.append(GetSqlArgs(hashKeys.size())); + sql.append(")"); + auto resultSet = QueryByStep(sql, std::move(hashKeys)); + int count = 0; + if (resultSet == nullptr || resultSet->GetRowCount(count) != E_OK || count <= 0) { + LOG_ERROR("get resultSet err."); + return {}; + } std::map result; - auto it = priKeys.begin(); - for (auto &haskKey : hashKeys) { - std::vector bindArg = { haskKey }; - auto resultSet = QueryByStep(sql, std::move(bindArg)); - int count = 0; - if (resultSet == nullptr || resultSet->GetRowCount(count) != E_OK || count <= 0) { - LOG_ERROR("get resultSet err."); - return {}; - } - resultSet->GoToRow(0); + for (int i = 0; i < count; i++) { + resultSet->GoToRow(i); + std::vector hashKey; int64_t timeStamp; - auto err = resultSet->GetLong(0, timeStamp); - if (err != E_OK) { - LOG_ERROR("query err:%{public}d.", err); - resultSet->Close(); - return {}; - } - result[*it] = Date(timeStamp); - it++; - resultSet->Close(); + resultSet->GetBlob(0, hashKey); + resultSet->GetLong(1, timeStamp); + result[keyMap[hashKey]] = Date(timeStamp); + } + return result; +} + +std::map RdbStoreImpl::GetModifyTimeByRowId( + const std::string &logTable, std::vector &PKey) +{ + std::string sql; + sql.append("select data_key, timestamp/10000 from "); + sql.append(logTable); + sql.append(" where data_key in ("); + sql.append(GetSqlArgs(PKey.size())); + sql.append(")"); + std::vector args; + args.reserve(PKey.size()) + for (auto &key : PKey) { + ValueObject::Type value; + RawDataParser::Convert(key, value); + args.emplace_back(ValueObject(value)); + } + auto resultSet = QueryByStep(sql, std::move(args)); + int count = 0; + if (resultSet == nullptr || resultSet->GetRowCount(count) != E_OK || count <= 0) { + LOG_ERROR("get resultSet err."); + return {}; + } + std::map result; + for (int i = 0; i < count; i++) { + resultSet->GoToRow(i); + int rowId; + int64_t timeStamp; + resultSet->GetInt(0, rowId); + resultSet->GetLong(1, timeStamp); + result[rowId] = Date(timeStamp); } return result; } + +std::string RdbStoreImpl::GetSqlArgs(size_t size) +{ + std::string args((size << 1) - 1, '?'); + for (size_t i = 1; i < size; ++i) { + args[(i << 1) - 1] = ','; + } + return args; +} #endif RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config, int &errCode) diff --git a/interfaces/inner_api/rdb/include/rdb_store.h b/interfaces/inner_api/rdb/include/rdb_store.h index bac5b219b..6195163d6 100644 --- a/interfaces/inner_api/rdb/include/rdb_store.h +++ b/interfaces/inner_api/rdb/include/rdb_store.h @@ -430,7 +430,7 @@ public: * @return Returns the specified column modify time. */ virtual std::map GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) = 0; + const std::string &table, const std::string &columnName, std::vector &PKey) = 0; /** * @brief Queries data in the database based on SQL statement. -- Gitee From 10e4e201cda6dc9c6c846125c8e041227e80f0c3 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 2 Jul 2023 20:12:53 +0800 Subject: [PATCH 25/29] update Signed-off-by: zuojiangjiang --- .../js/napi/relationalstore/include/napi_rdb_js_utils.h | 2 +- frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp | 2 +- frameworks/native/rdb/src/rdb_store_impl.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h index 2255744f6..5f25014de 100644 --- a/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h +++ b/frameworks/js/napi/relationalstore/include/napi_rdb_js_utils.h @@ -55,7 +55,7 @@ napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value); template<> napi_value Convert2JSValue(napi_env env, const Date &date); template<> -std::string ToString(const T &key); +std::string ToString(const PRIKey &key); }; // namespace JSUtils } // namespace OHOS::AppDataMgrJsKit #endif // RDB_JSKIT_NAPI_RDB_JS_UTILS_H diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index a6cc5146f..6c75770e4 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -218,7 +218,7 @@ napi_value Convert2JSValue(napi_env env, const Date &date) } template<> -std::string ToString(const T &key) +std::string ToString(const PRIKey &key) { auto strVal = std::get_if(&key); if (strVal != nullptr) { diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index f163c74bb..837ba7f6e 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -128,7 +128,7 @@ std::map RdbStoreImpl::GetModifyTime( std::map tmp; for (const auto &key : PKey) { DistributedDB::Type value; - RawDataParser::Convert(key.value, value); + RawDataParser::Convert(key, value); tmp[columnName] = value; auto hashKey = DistributedDB::RelationalStoreManager::CalcPrimaryKeyHash(tmp); if (hashKey.empty()) { @@ -173,7 +173,7 @@ std::map RdbStoreImpl::GetModifyTimeByRowId( sql.append(GetSqlArgs(PKey.size())); sql.append(")"); std::vector args; - args.reserve(PKey.size()) + args.reserve(PKey.size()); for (auto &key : PKey) { ValueObject::Type value; RawDataParser::Convert(key, value); -- Gitee From bf8c48ba9d3c92060afe82b60882850bfef67e72 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 2 Jul 2023 20:26:23 +0800 Subject: [PATCH 26/29] update Signed-off-by: zuojiangjiang --- .../relationalstore/src/napi_rdb_js_utils.cpp | 2 +- frameworks/native/rdb/include/rdb_store_impl.h | 4 ++-- frameworks/native/rdb/src/rdb_store_impl.cpp | 18 +++++++++--------- interfaces/inner_api/rdb/include/rdb_store.h | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp index 6c75770e4..56e9cf5ef 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_js_utils.cpp @@ -230,7 +230,7 @@ std::string ToString(const PRIKey &key) } auto dbVal = std::get_if(&key); if (dbVal != nullptr) { - return std::to_string(*dbVal); + return std::to_string(static_cast(*dbVal)); } return {}; } diff --git a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h index 9ee842cdd..9513c3882 100644 --- a/frameworks/native/rdb/include/rdb_store_impl.h +++ b/frameworks/native/rdb/include/rdb_store_impl.h @@ -120,7 +120,7 @@ public: bool DropDeviceData(const std::vector& devices, const DropOption& option) override; std::map GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) override; + const std::string &table, const std::string &columnName, std::vector &keys) override; private: int InnerOpen(); @@ -139,7 +139,7 @@ private: int InnerBackup(const std::string databasePath, const std::vector destEncryptKey = std::vector()); std::map GetModifyTimeByRowId( - const std::string &logTable, std::vector &PKey); + const std::string &logTable, std::vector &keys); std::string GetSqlArgs(size_t size); const RdbStoreConfig rdbStoreConfig; diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 837ba7f6e..4d9e23517 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -111,22 +111,22 @@ void RdbStoreImpl::GetSchema(const RdbStoreConfig &config) } std::map RdbStoreImpl::GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) + const std::string &table, const std::string &columnName, std::vector &keys) { - if (table.empty() || columnName.empty() || PKey.empty()) { + if (table.empty() || columnName.empty() || keys.empty()) { LOG_ERROR("invalid para."); return {}; } auto logTable = DistributedDB::RelationalStoreManager::GetDistributedLogTableName(table); if (columnName == "rowid") { - return GetModifyTimeByRowId(logTable, PKey); + return GetModifyTimeByRowId(logTable, keys); } std::vector hashKeys; - hashKeys.reserve(PKey.size()); + hashKeys.reserve(keys.size()); std::map, PRIKey> keyMap; std::map tmp; - for (const auto &key : PKey) { + for (const auto &key : keys) { DistributedDB::Type value; RawDataParser::Convert(key, value); tmp[columnName] = value; @@ -164,17 +164,17 @@ std::map RdbStoreImpl::GetModifyTime( } std::map RdbStoreImpl::GetModifyTimeByRowId( - const std::string &logTable, std::vector &PKey) + const std::string &logTable, std::vector &keys) { std::string sql; sql.append("select data_key, timestamp/10000 from "); sql.append(logTable); sql.append(" where data_key in ("); - sql.append(GetSqlArgs(PKey.size())); + sql.append(GetSqlArgs(keys.size())); sql.append(")"); std::vector args; - args.reserve(PKey.size()); - for (auto &key : PKey) { + args.reserve(keys.size()); + for (auto &key : keys) { ValueObject::Type value; RawDataParser::Convert(key, value); args.emplace_back(ValueObject(value)); diff --git a/interfaces/inner_api/rdb/include/rdb_store.h b/interfaces/inner_api/rdb/include/rdb_store.h index 6195163d6..fe99849a3 100644 --- a/interfaces/inner_api/rdb/include/rdb_store.h +++ b/interfaces/inner_api/rdb/include/rdb_store.h @@ -430,7 +430,7 @@ public: * @return Returns the specified column modify time. */ virtual std::map GetModifyTime( - const std::string &table, const std::string &columnName, std::vector &PKey) = 0; + const std::string &table, const std::string &columnName, std::vector &keys) = 0; /** * @brief Queries data in the database based on SQL statement. -- Gitee From 25624e694530caf84dfe03073d6b94e055bf6efe Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 2 Jul 2023 21:41:49 +0800 Subject: [PATCH 27/29] update Signed-off-by: zuojiangjiang --- .../relationalstore/src/napi_rdb_store.cpp | 67 +++++++++++++------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index 92675ebd6..8c7a4dea2 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -77,6 +77,7 @@ struct RdbStoreContext : public Context { std::string aliasName; std::string pathName; std::string srcName; + std::string columnName; int32_t enumArg; int32_t distributedType; int32_t syncMode; @@ -85,6 +86,8 @@ struct RdbStoreContext : public Context { DistributedRdb::SyncResult syncResult; napi_value cloudSyncCallback = nullptr; std::shared_ptr rdbPredicates = nullptr; + std::vector keys; + std::map modifyTime; RdbStoreContext() : predicatesProxy(nullptr), int64Output(0), intOutput(0), enumArg(-1), @@ -252,6 +255,20 @@ int ParseTableName(const napi_env &env, const napi_value &arg, std::shared_ptr context) +{ + context->columnName = JSUtils::Convert2String(env, arg); + CHECK_RETURN_SET(!context->columnName.empty(), std::make_shared("columnName", "not string")); + return OK; +} + +int ParsePrimaryKey(const napi_env &env, const napi_value &arg, std::shared_ptr context) +{ + JSUtils::Convert2Value(env, arg, context->keys); + CHECK_RETURN_SET(!context->keys.empty(), std::make_shared("PRIKey", "not number or string")); + return OK; +} + int ParseDevice(const napi_env &env, const napi_value &arg, std::shared_ptr context) { context->device = JSUtils::Convert2String(env, arg); @@ -1197,30 +1214,38 @@ napi_value RdbStoreProxy::CloudSync(napi_env env, napi_callback_info info) napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) { LOG_DEBUG("RdbStoreProxy::GetModifyTime start"); - size_t argc = 3; - napi_value argv[3] = { nullptr }; - napi_value self = nullptr; - napi_status status = napi_get_cb_info(env, info, &argc, argv, &self, nullptr); - RDB_NAPI_ASSERT(env, status == napi_ok && argc == 3, std::make_shared("3")); + auto context = std::make_shared(); + auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { + CHECK_RETURN_SET_E(argc == 3, std::make_shared("3")); + CHECK_RETURN(OK == ParserThis(env, self, context)); + CHECK_RETURN(OK == ParseTableName(env, argv[0], context)); + CHECK_RETURN(OK == ParseColumnName(env, argv[1], context)); + CHECK_RETURN(OK == ParsePrimaryKey(env, argv[2], context)); + }; + auto exec = [context]() -> int { + LOG_DEBUG("RdbStoreProxy::GetModifyTime Async"); + auto *obj = reinterpret_cast(context->boundObj); + context->modifyTime = obj->rdbStore_->GetModifyTime(context->tableName, context->columnName, context->keys); + return context->modifyTime.empty() ? E_ERROR : E_OK; + }; + auto output = [context](napi_env env, napi_value &result) { + LOG_DEBUG("RdbStoreProxy::GetModifyTime output"); + result = JSUtils::Convert2JSValue(env, context->modifyTime); + CHECK_RETURN_SET_E(result != nullptr, std::make_shared(E_ERROR)); + }; + context->SetAction(env, info, input, exec, output); - std::string table; - auto cvStatus = JSUtils::Convert2Value(env, argv[0], table); - RDB_NAPI_ASSERT(env, cvStatus == napi_ok && !table.empty(), std::make_shared("table", "string")); - std::string columnName; - cvStatus = JSUtils::Convert2Value(env, argv[1], columnName); - RDB_NAPI_ASSERT( - env, cvStatus == napi_ok && !columnName.empty(), std::make_shared("columnName", "string")); - std::vector PKey; - cvStatus = JSUtils::Convert2Value(env, argv[2], PKey); - RDB_NAPI_ASSERT(env, cvStatus == napi_ok && !PKey.empty(), - std::make_shared("PRIKey", "number or string")); + CHECK_RETURN_NULL(context->error == nullptr || context->error->GetCode() == OK); + return AsyncCall::Call(env, context); +} - auto proxy = GetNativeInstance(env, self); - RDB_NAPI_ASSERT(env, proxy != nullptr, std::make_shared("RdbStore", "valid")); +napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) +{ + LOG_DEBUG("RdbStoreProxy::GetModifyTime start"); + auto context = std::make_shared(); + auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { - auto result = proxy->rdbStore_->GetModifyTime(table, columnName, PKey); - RDB_NAPI_ASSERT(env, !result.empty(), std::make_shared(E_INNER_ERROR)); - return JSUtils::Convert2JSValue(env, result); + }; } napi_value RdbStoreProxy::OnDataChangeEvent(napi_env env, size_t argc, napi_value *argv) -- Gitee From 2bf58d68cf03247a0e4155ad6e83158477470315 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 2 Jul 2023 21:51:31 +0800 Subject: [PATCH 28/29] update Signed-off-by: zuojiangjiang --- .../js/napi/relationalstore/src/napi_rdb_store.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index 8c7a4dea2..7ee42a0af 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -1239,15 +1239,6 @@ napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) return AsyncCall::Call(env, context); } -napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) -{ - LOG_DEBUG("RdbStoreProxy::GetModifyTime start"); - auto context = std::make_shared(); - auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { - - }; -} - napi_value RdbStoreProxy::OnDataChangeEvent(napi_env env, size_t argc, napi_value *argv) { napi_valuetype type; -- Gitee From 6510e09701554de59a5dff8f70aae89850d26c65 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Sun, 2 Jul 2023 22:43:02 +0800 Subject: [PATCH 29/29] udpate Signed-off-by: zuojiangjiang --- frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp index 7ee42a0af..d5927d596 100644 --- a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp +++ b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp @@ -1216,7 +1216,7 @@ napi_value RdbStoreProxy::GetModifyTime(napi_env env, napi_callback_info info) LOG_DEBUG("RdbStoreProxy::GetModifyTime start"); auto context = std::make_shared(); auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) { - CHECK_RETURN_SET_E(argc == 3, std::make_shared("3")); + CHECK_RETURN_SET_E(argc == 3, std::make_shared("3 - 4")); CHECK_RETURN(OK == ParserThis(env, self, context)); CHECK_RETURN(OK == ParseTableName(env, argv[0], context)); CHECK_RETURN(OK == ParseColumnName(env, argv[1], context)); -- Gitee