BatchInsert

Signed-off-by: zhangdi <zhangdi126@huawei.com>
This commit is contained in:
zhangdi 2024-09-28 10:16:25 +08:00 committed by zhangdi
parent 854db108fc
commit 8ee8845170
45 changed files with 1003 additions and 30 deletions

View File

@ -18,6 +18,7 @@
#include "napi_async_call.h"
#include "napi_rdb_predicates.h"
#include "values_buckets.h"
namespace OHOS {
namespace RelationalStoreJsKit {
@ -37,6 +38,7 @@ struct RdbStoreContext : public RdbStoreContextBase {
std::vector<std::string> columns;
ValuesBucket valuesBucket;
std::vector<ValuesBucket> valuesBuckets;
ValuesBuckets sharedValuesBuckets;
std::map<std::string, ValueObject> numberMaps;
std::vector<ValueObject> bindArgs;
int64_t int64Output;

View File

@ -0,0 +1,24 @@
diff a/frameworks/js/napi/relationalstore/include/napi_rdb_context.h b/frameworks/js/napi/relationalstore/include/napi_rdb_context.h (rejected hunks)
@@ -18,6 +18,7 @@
#include "napi_async_call.h"
#include "napi_rdb_predicates.h"
+#include "values_buckets.h"
namespace OHOS {
namespace RelationalStoreJsKit {
@@ -37,6 +38,7 @@ struct RdbStoreContext : public RdbStoreContextBase {
std::vector<std::string> columns;
ValuesBucket valuesBucket;
std::vector<ValuesBucket> valuesBuckets;
+ ValuesBuckets sharedValuesBuckets;
std::map<std::string, ValueObject> numberMaps;
std::vector<ValueObject> bindArgs;
int64_t int64Output;
@@ -75,4 +77,4 @@ struct RdbStoreContext : public RdbStoreContextBase {
};
} // namespace RelationalStoreJsKit
} // namespace OHOS
-#endif // NAPI_RDB_CONTEXT_H
\ No newline at end of file
+#endif // NAPI_RDB_CONTEXT_H

View File

@ -599,7 +599,7 @@ int ParseValuesBuckets(const napi_env env, const napi_value arg, std::shared_ptr
CHECK_RETURN_SET(status == napi_ok, std::make_shared<InnerError>("napi_get_element failed."));
CHECK_RETURN_ERR(ParseValuesBucket(env, obj, context) == OK);
context->valuesBuckets.push_back(std::move(context->valuesBucket));
context->sharedValuesBuckets.Put(context->valuesBucket);
context->valuesBucket.Clear();
}
return OK;
@ -661,7 +661,9 @@ napi_value RdbStoreProxy::BatchInsert(napi_env env, napi_callback_info info)
auto exec = [context]() -> int {
CHECK_RETURN_ERR(context->rdbStore != nullptr);
auto rdbStore = std::move(context->rdbStore);
return rdbStore->BatchInsert(context->int64Output, context->tableName, context->valuesBuckets);
auto [ret, output] = rdbStore->BatchInsert(context->tableName, context->sharedValuesBuckets);
context->int64Output = output;
return ret;
};
auto output = [context](napi_env env, napi_value &result) {
napi_status status = napi_create_int64(env, context->int64Output, &result);

View File

@ -0,0 +1,21 @@
diff a/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp b/frameworks/js/napi/relationalstore/src/napi_rdb_store.cpp (rejected hunks)
@@ -597,7 +597,7 @@ int ParseValuesBuckets(const napi_env env, const napi_value arg, std::shared_ptr
CHECK_RETURN_SET(status == napi_ok, std::make_shared<InnerError>("napi_get_element failed."));
CHECK_RETURN_ERR(ParseValuesBucket(env, obj, context) == OK);
- context->valuesBuckets.push_back(std::move(context->valuesBucket));
+ context->sharedValuesBuckets.Put(context->valuesBucket);
context->valuesBucket.Clear();
}
return OK;
@@ -659,7 +659,9 @@ napi_value RdbStoreProxy::BatchInsert(napi_env env, napi_callback_info info)
auto exec = [context]() -> int {
CHECK_RETURN_ERR(context->rdbStore != nullptr);
auto rdbStore = std::move(context->rdbStore);
- return rdbStore->BatchInsert(context->int64Output, context->tableName, context->valuesBuckets);
+ auto [ret, output] = rdbStore->BatchInsert(context->tableName, context->sharedValuesBuckets);
+ context->int64Output = output;
+ return ret;
};
auto output = [context](napi_env env, napi_value &result) {
napi_status status = napi_create_int64(env, context->int64Output, &result);

View File

@ -39,6 +39,7 @@ public:
int32_t Step() override;
int32_t Reset() override;
int32_t Execute(const std::vector<ValueObject>& args) override;
int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>>& args) override;
std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject>& args) override;
int32_t Changes() const override;
int64_t LastInsertRowId() const override;
@ -55,6 +56,7 @@ public:
private:
friend class RdConnection;
int Prepare(GRD_DB *db, const std::string &sql);
int32_t Bind(const std::vector<std::reference_wrapper<ValueObject>>& args);
int InnerBindBlobTypeArgs(const ValueObject &bindArg, uint32_t index) const;
int IsValid(int index) const;

View File

@ -0,0 +1,24 @@
diff a/frameworks/native/rdb/include/rd_statement.h b/frameworks/native/rdb/include/rd_statement.h (rejected hunks)
@@ -39,6 +39,7 @@ public:
int32_t Step() override;
int32_t Reset() override;
int32_t Execute(const std::vector<ValueObject>& args) override;
+ int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>>& args) override;
std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject>& args) override;
int32_t Changes() const override;
int64_t LastInsertRowId() const override;
@@ -55,6 +56,7 @@ public:
private:
friend class RdConnection;
int Prepare(GRD_DB *db, const std::string &sql);
+ int32_t Bind(const std::vector<std::reference_wrapper<ValueObject>>& args);
int InnerBindBlobTypeArgs(const ValueObject &bindArg, uint32_t index) const;
int IsValid(int index) const;
@@ -73,4 +75,4 @@ private:
};
} // namespace NativeRdb
} // namespace OHOS
-#endif // NATIVE_RDB_RD_STATEMENT_H
\ No newline at end of file
+#endif // NATIVE_RDB_RD_STATEMENT_H

View File

@ -82,6 +82,7 @@ public:
const RdbStoreConfig &GetConfig();
int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &values) override;
int BatchInsert(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values) override;
std::pair<int, int64_t> BatchInsert(const std::string& table, const ValuesBuckets& values) override;
int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) override;
int InsertWithConflictResolution(int64_t &outRowId, const std::string &table, const ValuesBucket &values,
ConflictResolution conflictResolution) override;
@ -198,6 +199,8 @@ public:
private:
using ExecuteSqls = std::vector<std::pair<std::string, std::vector<std::vector<ValueObject>>>>;
using ExecuteSqlsRef =
std::vector<std::pair<std::string, std::vector<std::vector<std::reference_wrapper<ValueObject>>>>>;
using Stmt = std::shared_ptr<Statement>;
using RdbParam = DistributedRdb::RdbSyncerParam;
@ -211,6 +214,7 @@ private:
int CheckAttach(const std::string &sql);
std::pair<int32_t, Stmt> BeginExecuteSql(const std::string &sql);
ExecuteSqls GenerateSql(const std::string& table, const std::vector<ValuesBucket>& buckets, int limit);
ExecuteSqlsRef GenerateSql(const std::string& table, const ValuesBuckets& buckets, int limit);
int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath);
void SetAssetStatus(const ValueObject &val, int32_t status);
void DoCloudSync(const std::string &table);
@ -244,7 +248,8 @@ private:
int UpdateWithConflictResolutionEntry(int &changedRows, const std::string &table, const ValuesBucket &values,
const std::string &whereClause, const std::vector<ValueObject> &bindArgs,
ConflictResolution conflictResolution);
int BatchInsertEntry(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values);
template<typename T>
int BatchInsertEntry(const std::string& table, const T& values, size_t rowSize, int64_t& outInsertNum);
int ExecuteSqlEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs);
std::pair<int32_t, ValueObject> ExecuteEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs,
int64_t trxId);

View File

@ -0,0 +1,36 @@
diff a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h (rejected hunks)
@@ -82,6 +82,7 @@ public:
const RdbStoreConfig &GetConfig();
int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &values) override;
int BatchInsert(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values) override;
+ std::pair<int, int64_t> BatchInsert(const std::string& table, const ValuesBuckets& values) override;
int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) override;
int InsertWithConflictResolution(int64_t &outRowId, const std::string &table, const ValuesBucket &values,
ConflictResolution conflictResolution) override;
@@ -198,6 +199,8 @@ public:
private:
using ExecuteSqls = std::vector<std::pair<std::string, std::vector<std::vector<ValueObject>>>>;
+ using ExecuteSqlsRef =
+ std::vector<std::pair<std::string, std::vector<std::vector<std::reference_wrapper<ValueObject>>>>>;
using Stmt = std::shared_ptr<Statement>;
using RdbParam = DistributedRdb::RdbSyncerParam;
@@ -211,6 +214,7 @@ private:
int CheckAttach(const std::string &sql);
std::pair<int32_t, Stmt> BeginExecuteSql(const std::string &sql);
ExecuteSqls GenerateSql(const std::string& table, const std::vector<ValuesBucket>& buckets, int limit);
+ ExecuteSqlsRef GenerateSql(const std::string& table, const ValuesBuckets& buckets, int limit);
int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath);
int ExecuteSqlInner(const std::string &sql, const std::vector<ValueObject> &bindArgs);
void SetAssetStatus(const ValueObject &val, int32_t status);
@@ -245,7 +249,8 @@ private:
int UpdateWithConflictResolutionEntry(int &changedRows, const std::string &table, const ValuesBucket &values,
const std::string &whereClause, const std::vector<ValueObject> &bindArgs,
ConflictResolution conflictResolution);
- int BatchInsertEntry(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values);
+ template<typename T>
+ int BatchInsertEntry(const std::string& table, const T& values, size_t rowSize, int64_t& outInsertNum);
int ExecuteSqlEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs);
std::pair<int32_t, ValueObject> ExecuteEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs,
int64_t trxId);

View File

@ -48,8 +48,9 @@ public:
const AbsRdbPredicates &predicates, const std::vector<std::string> &columns, const std::string &logTable);
static std::string GetSqlArgs(size_t size);
static ExecuteSqls MakeExecuteSqls(
const std::string &sql, std::vector<ValueObject> &&args, int fieldSize, int limit);
template<typename T>
static std::vector<std::pair<std::string, std::vector<std::vector<T>>>> MakeExecuteSqls(
const std::string &sql, const std::vector<T> &args, int fieldSize, int limit);
private:
static void AppendClause(std::string &builder, const std::string &name,
const std::string &clause, const std::string &table = "");

View File

@ -0,0 +1,22 @@
diff a/frameworks/native/rdb/include/sqlite_sql_builder.h b/frameworks/native/rdb/include/sqlite_sql_builder.h (rejected hunks)
@@ -27,6 +27,8 @@ namespace NativeRdb {
class SqliteSqlBuilder {
public:
using ExecuteSqls = std::vector<std::pair<std::string, std::vector<std::vector<ValueObject>>>>;
+ using ExecuteSqlsRef =
+ std::vector<std::pair<std::string, std::vector<std::vector<std::reference_wrapper<ValueObject>>>>>;
SqliteSqlBuilder();
~SqliteSqlBuilder();
static std::string BuildDeleteString(const std::string &tableName, const std::string &index,
@@ -54,8 +56,9 @@ public:
const AbsRdbPredicates &predicates, const std::vector<std::string> &columns, const std::string &logTable);
static std::string GetSqlArgs(size_t size);
- static ExecuteSqls MakeExecuteSqls(
- const std::string &sql, std::vector<ValueObject> &&args, int fieldSize, int limit);
+ template<typename T>
+ static std::vector<std::pair<std::string, std::vector<std::vector<T>>>> MakeExecuteSqls(
+ const std::string &sql, const std::vector<T> &args, int fieldSize, int limit);
private:
static void AppendClause(std::string &builder, const std::string &name,
const std::string &clause, const std::string &table = "");

View File

@ -44,6 +44,7 @@ public:
int Reset() override;
int Finalize() override;
int Execute(const std::vector<ValueObject> &args) override;
int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>> &args) override;
std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject> &args) override;
int Changes() const override;
int64_t LastInsertRowId() const override;
@ -82,6 +83,7 @@ private:
int Prepare(sqlite3 *dbHandle, const std::string &sql);
int BindArgs(const std::vector<ValueObject> &bindArgs);
int BindArgs(const std::vector<std::reference_wrapper<ValueObject>> &bindArgs);
int IsValid(int index) const;
int InnerStep();
int InnerFinalize();

View File

@ -0,0 +1,24 @@
diff a/frameworks/native/rdb/include/sqlite_statement.h b/frameworks/native/rdb/include/sqlite_statement.h (rejected hunks)
@@ -44,6 +44,7 @@ public:
int Reset() override;
int Finalize() override;
int Execute(const std::vector<ValueObject> &args) override;
+ int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>> &args) override;
std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject> &args) override;
int Changes() const override;
int64_t LastInsertRowId() const override;
@@ -82,6 +83,7 @@ private:
int Prepare(sqlite3 *dbHandle, const std::string &sql);
int BindArgs(const std::vector<ValueObject> &bindArgs);
+ int BindArgs(const std::vector<std::reference_wrapper<ValueObject>> &bindArgs);
int IsValid(int index) const;
int InnerStep();
int InnerFinalize();
@@ -109,4 +111,4 @@ private:
};
} // namespace NativeRdb
} // namespace OHOS
-#endif
\ No newline at end of file
+#endif

View File

@ -39,6 +39,7 @@ public:
virtual int32_t Reset() = 0;
virtual int32_t Finalize() = 0;
virtual int32_t Execute(const std::vector<ValueObject> &args = {}) = 0;
virtual int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>> &args) = 0;
virtual std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject> &args = {}) = 0;
virtual int32_t Changes() const = 0;

View File

@ -0,0 +1,9 @@
diff a/frameworks/native/rdb/include/statement.h b/frameworks/native/rdb/include/statement.h (rejected hunks)
@@ -39,6 +39,7 @@ public:
virtual int32_t Reset() = 0;
virtual int32_t Finalize() = 0;
virtual int32_t Execute(const std::vector<ValueObject> &args = {}) = 0;
+ virtual int32_t Execute(const std::vector<std::reference_wrapper<ValueObject>> &args) = 0;
virtual std::pair<int, ValueObject> ExecuteForValue(const std::vector<ValueObject> &args = {}) = 0;
virtual int32_t Changes() const = 0;

View File

@ -38,8 +38,8 @@ public:
~RdbStoreImpl() override;
const RdbStoreConfig &GetConfig();
int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &values) override;
int BatchInsert(
int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values) override;
int BatchInsert(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values) override;
std::pair<int, int64_t> BatchInsert(const std::string& table, const ValuesBuckets& values) override;
int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) override;
int InsertWithConflictResolution(int64_t &outRowId, const std::string &table, const ValuesBucket &values,
ConflictResolution conflictResolution) override;
@ -108,6 +108,8 @@ public:
private:
using ExecuteSqls = std::vector<std::pair<std::string, std::vector<std::vector<ValueObject>>>>;
using ExecuteSqlsRef =
std::vector<std::pair<std::string, std::vector<std::vector<std::reference_wrapper<ValueObject>>>>>;
using Stmt = std::shared_ptr<Statement>;
using RdbParam = DistributedRdb::RdbSyncerParam;
@ -120,6 +122,7 @@ private:
int CheckAttach(const std::string &sql);
std::pair<int32_t, Stmt> BeginExecuteSql(const std::string &sql);
ExecuteSqls GenerateSql(const std::string& table, const std::vector<ValuesBucket>& buckets, int limit);
ExecuteSqlsRef GenerateSql(const std::string& table, const ValuesBuckets& buckets, int limit);
int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath);
int ExecuteSqlInner(const std::string &sql, const std::vector<ValueObject> &bindArgs);
void SetAssetStatus(const ValueObject &val, int32_t status);
@ -136,7 +139,8 @@ private:
int UpdateWithConflictResolutionEntry(int &changedRows, const std::string &table, const ValuesBucket &values,
const std::string &whereClause, const std::vector<ValueObject> &bindArgs,
ConflictResolution conflictResolution);
int BatchInsertEntry(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values);
template<typename T>
int BatchInsertEntry(const std::string& table, const T& values, size_t rowSize, int64_t& outInsertNum);
int ExecuteSqlEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs);
std::pair<int32_t, ValueObject> ExecuteEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs,
int64_t trxId);

View File

@ -0,0 +1,39 @@
diff a/frameworks/native/rdb/mock/include/rdb_store_impl.h b/frameworks/native/rdb/mock/include/rdb_store_impl.h (rejected hunks)
@@ -38,8 +38,8 @@ public:
~RdbStoreImpl() override;
const RdbStoreConfig &GetConfig();
int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &values) override;
- int BatchInsert(
- int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values) override;
+ int BatchInsert(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values) override;
+ std::pair<int, int64_t> BatchInsert(const std::string& table, const ValuesBuckets& values) override;
int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) override;
int InsertWithConflictResolution(int64_t &outRowId, const std::string &table, const ValuesBucket &values,
ConflictResolution conflictResolution) override;
@@ -108,6 +108,8 @@ public:
private:
using ExecuteSqls = std::vector<std::pair<std::string, std::vector<std::vector<ValueObject>>>>;
+ using ExecuteSqlsRef =
+ std::vector<std::pair<std::string, std::vector<std::vector<std::reference_wrapper<ValueObject>>>>>;
using Stmt = std::shared_ptr<Statement>;
using RdbParam = DistributedRdb::RdbSyncerParam;
@@ -120,6 +122,7 @@ private:
int CheckAttach(const std::string &sql);
std::pair<int32_t, Stmt> BeginExecuteSql(const std::string &sql);
ExecuteSqls GenerateSql(const std::string& table, const std::vector<ValuesBucket>& buckets, int limit);
+ ExecuteSqlsRef GenerateSql(const std::string& table, const ValuesBuckets& buckets, int limit);
int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath);
int ExecuteSqlInner(const std::string &sql, const std::vector<ValueObject> &bindArgs);
void SetAssetStatus(const ValueObject &val, int32_t status);
@@ -136,7 +139,8 @@ private:
int UpdateWithConflictResolutionEntry(int &changedRows, const std::string &table, const ValuesBucket &values,
const std::string &whereClause, const std::vector<ValueObject> &bindArgs,
ConflictResolution conflictResolution);
- int BatchInsertEntry(int64_t& outInsertNum, const std::string& table, const std::vector<ValuesBucket>& values);
+ template<typename T>
+ int BatchInsertEntry(const std::string& table, const T& values, size_t rowSize, int64_t& outInsertNum);
int ExecuteSqlEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs);
std::pair<int32_t, ValueObject> ExecuteEntry(const std::string& sql, const std::vector<ValueObject>& bindArgs,
int64_t trxId);

View File

@ -69,6 +69,11 @@ bool BigInteger::operator==(const BigInteger& other)
return value_ == other.value_;
}
bool BigInteger::operator<(const BigInteger &rhs)
{
return value_ < rhs.value_;
}
int32_t BigInteger::Sign() const
{
return sign_;

View File

@ -0,0 +1,20 @@
diff a/frameworks/native/rdb/src/big_integer.cpp b/frameworks/native/rdb/src/big_integer.cpp (rejected hunks)
@@ -69,6 +69,11 @@ bool BigInteger::operator==(const BigInteger& other)
return value_ == other.value_;
}
+bool BigInteger::operator<(const BigInteger &rhs)
+{
+ return value_ < rhs.value_;
+}
+
int32_t BigInteger::Sign() const
{
return sign_;
@@ -88,4 +93,4 @@ std::vector<uint64_t> BigInteger::Value() const
{
return value_;
}
-}
\ No newline at end of file
+}

View File

@ -245,24 +245,33 @@ int32_t RdStatement::Prepare(const std::string& sql)
}
int32_t RdStatement::Bind(const std::vector<ValueObject>& args)
{
std::vector<std::reference_wrapper<ValueObject>> refArgs;
for (auto &object : args) {
refArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
}
return Bind(refArgs);
}
int32_t RdStatement::Bind(const std::vector<std::reference_wrapper<ValueObject>>& args)
{
uint32_t index = 1;
int ret = E_OK;
for (auto &arg : args) {
switch (arg.GetType()) {
switch (arg.get().GetType()) {
case ValueObjectType::TYPE_NULL: {
ret = RdUtils::RdSqlBindNull(stmtHandle_, index);
break;
}
case ValueObjectType::TYPE_INT: {
int64_t value = 0;
arg.GetLong(value);
arg.get().GetLong(value);
ret = RdUtils::RdSqlBindInt64(stmtHandle_, index, value);
break;
}
case ValueObjectType::TYPE_DOUBLE: {
double doubleVal = 0;
arg.GetDouble(doubleVal);
arg.get().GetDouble(doubleVal);
ret = RdUtils::RdSqlBindDouble(stmtHandle_, index, doubleVal);
break;
}
@ -313,6 +322,15 @@ int32_t RdStatement::Reset()
}
int32_t RdStatement::Execute(const std::vector<ValueObject>& args)
{
std::vector<std::reference_wrapper<ValueObject>> refArgs;
for (auto &object : args) {
refArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
}
return Execute(refArgs);
}
int32_t RdStatement::Execute(const std::vector<std::reference_wrapper<ValueObject>>& args)
{
if (!readOnly_ && strcmp(sql_.c_str(), GlobalExpr::PRAGMA_VERSION) == 0) {
// It has already set version in prepare procedure

View File

@ -0,0 +1,63 @@
diff a/frameworks/native/rdb/src/rd_statement.cpp b/frameworks/native/rdb/src/rd_statement.cpp (rejected hunks)
@@ -116,7 +116,7 @@ int RdStatement::Prepare(GRD_DB *db, const std::string &newSql)
(!EndWithNull(newSql, curIdx) && !TryEatSymbol(newSql, ';', curIdx))) {
return E_INCORRECT_SQL;
}
-
+
readOnly_ = false;
sql_ = newSql;
return setPragmas_["user_version"](version);
@@ -245,24 +245,33 @@ int32_t RdStatement::Prepare(const std::string& sql)
}
int32_t RdStatement::Bind(const std::vector<ValueObject>& args)
+{
+ std::vector<std::reference_wrapper<ValueObject>> refArgs;
+ for (auto &object : args) {
+ refArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
+ }
+ return Bind(refArgs);
+}
+
+int32_t RdStatement::Bind(const std::vector<std::reference_wrapper<ValueObject>>& args)
{
uint32_t index = 1;
int ret = E_OK;
for (auto &arg : args) {
- switch (arg.GetType()) {
+ switch (arg.get().GetType()) {
case ValueObjectType::TYPE_NULL: {
ret = RdUtils::RdSqlBindNull(stmtHandle_, index);
break;
}
case ValueObjectType::TYPE_INT: {
int64_t value = 0;
- arg.GetLong(value);
+ arg.get().GetLong(value);
ret = RdUtils::RdSqlBindInt64(stmtHandle_, index, value);
break;
}
case ValueObjectType::TYPE_DOUBLE: {
double doubleVal = 0;
- arg.GetDouble(doubleVal);
+ arg.get().GetDouble(doubleVal);
ret = RdUtils::RdSqlBindDouble(stmtHandle_, index, doubleVal);
break;
}
@@ -313,6 +322,15 @@ int32_t RdStatement::Reset()
}
int32_t RdStatement::Execute(const std::vector<ValueObject>& args)
+{
+ std::vector<std::reference_wrapper<ValueObject>> refArgs;
+ for (auto &object : args) {
+ refArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
+ }
+ return Execute(refArgs);
+}
+
+int32_t RdStatement::Execute(const std::vector<std::reference_wrapper<ValueObject>>& args)
{
if (!readOnly_ && strcmp(sql_.c_str(), GlobalExpr::PRAGMA_VERSION) == 0) {
// It has already set version in prepare procedure

View File

@ -42,6 +42,7 @@
#include "sqlite_statement.h"
#include "sqlite_utils.h"
#include "step_result_set.h"
#include "values_buckets.h"
#include "task_executor.h"
#include "traits.h"
@ -354,16 +355,24 @@ int RdbStoreImpl::Insert(int64_t &outRowId, const std::string &table, const Valu
int RdbStoreImpl::BatchInsert(int64_t &outInsertNum, const std::string &table, const std::vector<ValuesBucket> &values)
{
SqlStatistic sqlStatistic("", SqlStatistic::Step::STEP_TOTAL);
return BatchInsertEntry(outInsertNum, table, values);
return BatchInsertEntry(table, values, values.size(), outInsertNum);
}
int RdbStoreImpl::BatchInsertEntry(int64_t &outInsertNum, const std::string &table,
const std::vector<ValuesBucket> &values)
std::pair<int, int64_t> RdbStoreImpl::BatchInsert(const std::string &table, const ValuesBuckets &values)
{
SqlStatistic sqlStatistic("", SqlStatistic::Step::STEP_TOTAL);
int64_t rowSize = 0;
auto ret = BatchInsertEntry(table, values, values.RowSize(), rowSize);
return { ret, rowSize };
}
template<typename T>
int RdbStoreImpl::BatchInsertEntry(const std::string &table, const T &values, size_t rowSize, int64_t &outInsertNum)
{
if (isReadOnly_ || (config_.GetDBType() == DB_VECTOR)) {
return E_NOT_SUPPORT;
}
if (values.empty()) {
if (rowSize == 0) {
outInsertNum = 0;
return E_OK;
}
@ -374,7 +383,7 @@ int RdbStoreImpl::BatchInsertEntry(int64_t &outInsertNum, const std::string &tab
auto executeSqlArgs = GenerateSql(table, values, connection->GetMaxVariable());
if (executeSqlArgs.empty()) {
LOG_ERROR("empty, table=%{public}s, values:%{public}zu, max number:%{public}d.", table.c_str(),
values.size(), connection->GetMaxVariable());
rowSize, connection->GetMaxVariable());
return E_INVALID_ARGS;
}
#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
@ -396,7 +405,7 @@ int RdbStoreImpl::BatchInsertEntry(int64_t &outInsertNum, const std::string &tab
}
}
connection = nullptr;
outInsertNum = static_cast<int64_t>(values.size());
outInsertNum = static_cast<int64_t>(rowSize);
DoCloudSync(table);
return E_OK;
}
@ -442,7 +451,37 @@ RdbStoreImpl::ExecuteSqls RdbStoreImpl::GenerateSql(const std::string& table, co
}
sql.pop_back();
sql.append(") VALUES ");
return SqliteSqlBuilder::MakeExecuteSqls(sql, std::move(args), fields.size(), limit);
return SqliteSqlBuilder::MakeExecuteSqls(sql, args, fields.size(), limit);
}
RdbStoreImpl::ExecuteSqlsRef RdbStoreImpl::GenerateSql(
const std::string& table, const ValuesBuckets& buckets, int limit)
{
auto [fields, values] = buckets.GetFieldsAndValues();
auto columnSize = fields->size();
auto rowSize = buckets.RowSize();
LOG_INFO("columnSize=%{public}zu, rowSize=%{public}zu", columnSize, rowSize);
ValueObject object;
std::reference_wrapper<ValueObject> emptyRef(object);
std::vector<std::reference_wrapper<ValueObject>> args(columnSize * rowSize, emptyRef);
std::string sql = "INSERT OR REPLACE INTO " + table + " (";
size_t columnIndex = 0;
for (auto &field : *fields) {
for (size_t row = 0; row < rowSize; ++row) {
auto [errorCode, value] = buckets.Get(row, std::ref(field));
if (errorCode != E_OK) {
LOG_ERROR("not found %{public}s in row=%{public}zu", field.c_str(), row);
return ExecuteSqlsRef();
}
args[columnIndex + row * columnSize] = value;
}
columnIndex++;
sql.append(field).append(",");
}
sql.pop_back();
sql.append(") VALUES ");
return SqliteSqlBuilder::MakeExecuteSqls(sql, args, columnSize, limit);
}
int RdbStoreImpl::Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &values)

View File

@ -0,0 +1,104 @@
diff a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp (rejected hunks)
@@ -42,6 +42,7 @@
#include "sqlite_statement.h"
#include "sqlite_utils.h"
#include "step_result_set.h"
+#include "values_buckets.h"
#include "task_executor.h"
#include "traits.h"
@@ -353,16 +354,24 @@ int RdbStoreImpl::Insert(int64_t &outRowId, const std::string &table, const Valu
int RdbStoreImpl::BatchInsert(int64_t &outInsertNum, const std::string &table, const std::vector<ValuesBucket> &values)
{
SqlStatistic sqlStatistic("", SqlStatistic::Step::STEP_TOTAL);
- return BatchInsertEntry(outInsertNum, table, values);
+ return BatchInsertEntry(table, values, values.size(), outInsertNum);
}
-int RdbStoreImpl::BatchInsertEntry(int64_t &outInsertNum, const std::string &table,
- const std::vector<ValuesBucket> &values)
+std::pair<int, int64_t> RdbStoreImpl::BatchInsert(const std::string &table, const ValuesBuckets &values)
+{
+ SqlStatistic sqlStatistic("", SqlStatistic::Step::STEP_TOTAL);
+ int64_t rowSize = 0;
+ auto ret = BatchInsertEntry(table, values, values.RowSize(), rowSize);
+ return { ret, rowSize };
+}
+
+template<typename T>
+int RdbStoreImpl::BatchInsertEntry(const std::string &table, const T &values, size_t rowSize, int64_t &outInsertNum)
{
if (isReadOnly_ || (config_.GetDBType() == DB_VECTOR)) {
return E_NOT_SUPPORT;
}
- if (values.empty()) {
+ if (rowSize == 0) {
outInsertNum = 0;
return E_OK;
}
@@ -373,7 +382,7 @@ int RdbStoreImpl::BatchInsertEntry(int64_t &outInsertNum, const std::string &tab
auto executeSqlArgs = GenerateSql(table, values, connection->GetMaxVariable());
if (executeSqlArgs.empty()) {
LOG_ERROR("empty, table=%{public}s, values:%{public}zu, max number:%{public}d.", table.c_str(),
- values.size(), connection->GetMaxVariable());
+ rowSize, connection->GetMaxVariable());
return E_INVALID_ARGS;
}
#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
@@ -395,7 +404,7 @@ int RdbStoreImpl::BatchInsertEntry(int64_t &outInsertNum, const std::string &tab
}
}
connection = nullptr;
- outInsertNum = static_cast<int64_t>(values.size());
+ outInsertNum = static_cast<int64_t>(rowSize);
DoCloudSync(table);
return E_OK;
}
@@ -441,7 +450,37 @@ RdbStoreImpl::ExecuteSqls RdbStoreImpl::GenerateSql(const std::string& table, co
}
sql.pop_back();
sql.append(") VALUES ");
- return SqliteSqlBuilder::MakeExecuteSqls(sql, std::move(args), fields.size(), limit);
+ return SqliteSqlBuilder::MakeExecuteSqls(sql, args, fields.size(), limit);
+}
+
+RdbStoreImpl::ExecuteSqlsRef RdbStoreImpl::GenerateSql(
+ const std::string& table, const ValuesBuckets& buckets, int limit)
+{
+ auto [fields, values] = buckets.GetFieldsAndValues();
+ auto columnSize = fields->size();
+ auto rowSize = buckets.RowSize();
+ LOG_INFO("columnSize=%{public}zu, rowSize=%{public}zu", columnSize, rowSize);
+
+ ValueObject object;
+ std::reference_wrapper<ValueObject> emptyRef(object);
+ std::vector<std::reference_wrapper<ValueObject>> args(columnSize * rowSize, emptyRef);
+ std::string sql = "INSERT OR REPLACE INTO " + table + " (";
+ size_t columnIndex = 0;
+ for (auto &field : *fields) {
+ for (size_t row = 0; row < rowSize; ++row) {
+ auto [errorCode, value] = buckets.Get(row, std::ref(field));
+ if (errorCode != E_OK) {
+ LOG_ERROR("not found %{public}s in row=%{public}zu", field.c_str(), row);
+ return ExecuteSqlsRef();
+ }
+ args[columnIndex + row * columnSize] = value;
+ }
+ columnIndex++;
+ sql.append(field).append(",");
+ }
+ sql.pop_back();
+ sql.append(") VALUES ");
+ return SqliteSqlBuilder::MakeExecuteSqls(sql, args, columnSize, limit);
}
int RdbStoreImpl::Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &values)
@@ -1748,7 +1787,7 @@ int RdbStoreImpl::GetDestPath(const std::string &backupPath, std::string &destPa
return E_ERROR;
}
}
-
+
if (access(destPath.c_str(), F_OK) != E_OK) {
LOG_ERROR("The backupFilePath does not exists.");
return E_INVALID_FILE_PATH;

View File

@ -321,14 +321,14 @@ SqliteSqlBuilder::ExecuteSqls SqliteSqlBuilder::MakeExecuteSqls(
return sqlStr;
};
std::string executeSql;
ExecuteSqls executeSqls;
std::vector<std::pair<std::string, std::vector<std::vector<T>>>> executeSqls;
auto start = args.begin();
if (executeTimes != 0) {
executeSql = appendAgsSql(maxRowNumbersOneTimes);
std::vector<std::vector<ValueObject>> sqlArgs;
std::vector<std::vector<T>> sqlArgs;
size_t maxVariableNumbers = maxRowNumbersOneTimes * static_cast<size_t>(fieldSize);
for (size_t i = 0; i < executeTimes; ++i) {
std::vector<ValueObject> bindValueArgs(start, start + maxVariableNumbers);
std::vector<T> bindValueArgs(start, start + maxVariableNumbers);
sqlArgs.emplace_back(std::move(bindValueArgs));
start += maxVariableNumbers;
}

View File

@ -0,0 +1,64 @@
diff a/frameworks/native/rdb/src/sqlite_sql_builder.cpp b/frameworks/native/rdb/src/sqlite_sql_builder.cpp (rejected hunks)
@@ -334,22 +334,20 @@ std::string SqliteSqlBuilder::GetSqlArgs(size_t size)
return args;
}
-SqliteSqlBuilder::ExecuteSqls SqliteSqlBuilder::MakeExecuteSqls(
- const std::string &sql, std::vector<ValueObject> &&args, int fieldSize, int limit)
+template<typename T>
+std::vector<std::pair<std::string, std::vector<std::vector<T>>>> SqliteSqlBuilder::MakeExecuteSqls(
+ const std::string &sql, const std::vector<T> &args, int fieldSize, int limit)
{
if (fieldSize == 0) {
- return ExecuteSqls();
+ return {};
}
size_t rowNumbers = args.size() / static_cast<size_t>(fieldSize);
size_t maxRowNumbersOneTimes = static_cast<size_t>(limit / fieldSize);
if (maxRowNumbersOneTimes == 0) {
- return ExecuteSqls();
+ return {};
}
size_t executeTimes = rowNumbers / maxRowNumbersOneTimes;
size_t remainingRows = rowNumbers % maxRowNumbersOneTimes;
- LOG_DEBUG("rowNumbers %{public}zu, maxRowNumbersOneTimes %{public}zu, executeTimes %{public}zu,"
- "remainingRows %{public}zu, fieldSize %{public}d, limit %{public}d",
- rowNumbers, maxRowNumbersOneTimes, executeTimes, remainingRows, fieldSize, limit);
std::string singleRowSqlArgs = "(" + SqliteSqlBuilder::GetSqlArgs(fieldSize) + ")";
auto appendAgsSql = [&singleRowSqlArgs, &sql] (size_t rowNumber) {
std::string sqlStr = sql;
@@ -360,14 +358,14 @@ SqliteSqlBuilder::ExecuteSqls SqliteSqlBuilder::MakeExecuteSqls(
return sqlStr;
};
std::string executeSql;
- ExecuteSqls executeSqls;
+ std::vector<std::pair<std::string, std::vector<std::vector<T>>>> executeSqls;
auto start = args.begin();
if (executeTimes != 0) {
executeSql = appendAgsSql(maxRowNumbersOneTimes);
- std::vector<std::vector<ValueObject>> sqlArgs;
+ std::vector<std::vector<T>> sqlArgs;
size_t maxVariableNumbers = maxRowNumbersOneTimes * static_cast<size_t>(fieldSize);
for (size_t i = 0; i < executeTimes; ++i) {
- std::vector<ValueObject> bindValueArgs(start, start + maxVariableNumbers);
+ std::vector<T> bindValueArgs(start, start + maxVariableNumbers);
sqlArgs.emplace_back(std::move(bindValueArgs));
start += maxVariableNumbers;
}
@@ -376,10 +374,15 @@ SqliteSqlBuilder::ExecuteSqls SqliteSqlBuilder::MakeExecuteSqls(
if (remainingRows != 0) {
executeSql = appendAgsSql(remainingRows);
- std::vector<std::vector<ValueObject>> sqlArgs(1, std::vector<ValueObject>(start, args.end()));
+ std::vector<std::vector<T>> sqlArgs(1, std::vector<T>(start, args.end()));
executeSqls.emplace_back(std::make_pair(executeSql, std::move(sqlArgs)));
}
return executeSqls;
}
+
+template SqliteSqlBuilder::ExecuteSqls SqliteSqlBuilder::MakeExecuteSqls(
+ const std::string &sql, const std::vector<ValueObject> &args, int fieldSize, int limit);
+template SqliteSqlBuilder::ExecuteSqlsRef SqliteSqlBuilder::MakeExecuteSqls(
+ const std::string &sql, const std::vector<std::reference_wrapper<ValueObject>> &args, int fieldSize, int limit);
} // namespace NativeRdb
} // namespace OHOS

View File

@ -138,6 +138,15 @@ void SqliteStatement::ReadFile2Buffer()
}
int SqliteStatement::BindArgs(const std::vector<ValueObject> &bindArgs)
{
std::vector<std::reference_wrapper<ValueObject>> refBindArgs;
for (auto &object : bindArgs) {
refBindArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
}
return BindArgs(refBindArgs);
}
int SqliteStatement::BindArgs(const std::vector<std::reference_wrapper<ValueObject>> &bindArgs)
{
SqlStatistic sqlStatistic("", SqlStatistic::Step::STEP_PREPARE, seqId_);
if (bound_) {
@ -147,12 +156,12 @@ int SqliteStatement::BindArgs(const std::vector<ValueObject> &bindArgs)
bound_ = true;
int index = 1;
for (auto &arg : bindArgs) {
auto action = ACTIONS[arg.value.index()];
auto action = ACTIONS[arg.get().value.index()];
if (action == nullptr) {
LOG_ERROR("not support the type %{public}zu", arg.value.index());
LOG_ERROR("not support the type %{public}zu", arg.get().value.index());
return E_INVALID_ARGS;
}
auto errCode = action(stmt_, index, arg.value);
auto errCode = action(stmt_, index, arg.get().value);
if (errCode != SQLITE_OK) {
LOG_ERROR("Bind has error: %{public}d, sql: %{public}s, errno %{public}d", errCode, sql_.c_str(), errno);
return SQLiteError::ErrNo(errCode);
@ -327,6 +336,15 @@ int SqliteStatement::Finalize()
}
int SqliteStatement::Execute(const std::vector<ValueObject> &args)
{
std::vector<std::reference_wrapper<ValueObject>> refArgs;
for (auto &object : args) {
refArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
}
return Execute(refArgs);
}
int32_t SqliteStatement::Execute(const std::vector<std::reference_wrapper<ValueObject>> &args)
{
int count = static_cast<int>(args.size());
if (count != numParameters_) {

View File

@ -0,0 +1,49 @@
diff a/frameworks/native/rdb/src/sqlite_statement.cpp b/frameworks/native/rdb/src/sqlite_statement.cpp (rejected hunks)
@@ -137,6 +137,15 @@ void SqliteStatement::ReadFile2Buffer()
}
int SqliteStatement::BindArgs(const std::vector<ValueObject> &bindArgs)
+{
+ std::vector<std::reference_wrapper<ValueObject>> refBindArgs;
+ for (auto &object : bindArgs) {
+ refBindArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
+ }
+ return BindArgs(refBindArgs);
+}
+
+int SqliteStatement::BindArgs(const std::vector<std::reference_wrapper<ValueObject>> &bindArgs)
{
SqlStatistic sqlStatistic("", SqlStatistic::Step::STEP_PREPARE, seqId_);
if (bound_) {
@@ -146,12 +155,12 @@ int SqliteStatement::BindArgs(const std::vector<ValueObject> &bindArgs)
bound_ = true;
int index = 1;
for (auto &arg : bindArgs) {
- auto action = ACTIONS[arg.value.index()];
+ auto action = ACTIONS[arg.get().value.index()];
if (action == nullptr) {
- LOG_ERROR("not support the type %{public}zu", arg.value.index());
+ LOG_ERROR("not support the type %{public}zu", arg.get().value.index());
return E_INVALID_ARGS;
}
- auto errCode = action(stmt_, index, arg.value);
+ auto errCode = action(stmt_, index, arg.get().value);
if (errCode != SQLITE_OK) {
LOG_ERROR("Bind has error: %{public}d, sql: %{public}s, errno %{public}d", errCode, sql_.c_str(), errno);
return SQLiteError::ErrNo(errCode);
@@ -325,6 +334,15 @@ int SqliteStatement::Finalize()
}
int SqliteStatement::Execute(const std::vector<ValueObject> &args)
+{
+ std::vector<std::reference_wrapper<ValueObject>> refArgs;
+ for (auto &object : args) {
+ refArgs.emplace_back(std::ref(const_cast<ValueObject&>(object)));
+ }
+ return Execute(refArgs);
+}
+
+int32_t SqliteStatement::Execute(const std::vector<std::reference_wrapper<ValueObject>> &args)
{
int count = static_cast<int>(args.size());
if (count != numParameters_) {

View File

@ -341,5 +341,40 @@ int ValueObject::Get(T &output) const
output = static_cast<T>(*v);
return E_OK;
}
bool ValueObject::operator<(const ValueObject &rhs) const
{
if (GetType() != rhs.GetType()) {
return GetType() < rhs.GetType();
}
bool result = true;
switch (GetType()) {
case TYPE_INT:
result = int64_t(*this) < int64_t(rhs);
break;
case TYPE_DOUBLE:
result = double(*this) < double(rhs);
break;
case TYPE_STRING:
result = std::string(*this) < std::string(rhs);
break;
case TYPE_BOOL:
result = bool(*this) < bool(rhs);
break;
case TYPE_BLOB:
result = Blob(*this) < Blob(rhs);
break;
case TYPE_VECS:
result = FloatVector(*this) < FloatVector(rhs);
break;
case TYPE_BIGINT:
result = BigInt(*this) < BigInt(rhs);
break;
default:
break;
}
return result;
}
} // namespace NativeRdb
} // namespace OHOS

View File

@ -0,0 +1,42 @@
diff a/frameworks/native/rdb/src/value_object.cpp b/frameworks/native/rdb/src/value_object.cpp (rejected hunks)
@@ -341,5 +341,40 @@ int ValueObject::Get(T &output) const
output = static_cast<T>(*v);
return E_OK;
}
+
+bool ValueObject::operator<(const ValueObject &rhs) const
+{
+ if (GetType() != rhs.GetType()) {
+ return GetType() < rhs.GetType();
+ }
+
+ bool result = true;
+ switch (GetType()) {
+ case TYPE_INT:
+ result = int64_t(*this) < int64_t(rhs);
+ break;
+ case TYPE_DOUBLE:
+ result = double(*this) < double(rhs);
+ break;
+ case TYPE_STRING:
+ result = std::string(*this) < std::string(rhs);
+ break;
+ case TYPE_BOOL:
+ result = bool(*this) < bool(rhs);
+ break;
+ case TYPE_BLOB:
+ result = Blob(*this) < Blob(rhs);
+ break;
+ case TYPE_VECS:
+ result = FloatVector(*this) < FloatVector(rhs);
+ break;
+ case TYPE_BIGINT:
+ result = BigInt(*this) < BigInt(rhs);
+ break;
+ default:
+ break;
+ }
+ return result;
+}
} // namespace NativeRdb
} // namespace OHOS

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "values_buckets.h"
#include "rdb_errno.h"
namespace OHOS {
namespace NativeRdb {
ValuesBuckets::ValuesBuckets()
{
fields_ = std::make_shared<std::set<std::string>>();
values_ = std::make_shared<std::set<ValueObject>>();
}
size_t ValuesBuckets::RowSize() const
{
return buckets_.size();
}
std::pair<ValuesBuckets::FieldsType, ValuesBuckets::ValuesType> ValuesBuckets::GetFieldsAndValues() const
{
return { fields_, values_ };
}
void ValuesBuckets::Put(const ValuesBucket &bucket)
{
BucketType row;
for (const auto &[field, value] : bucket.values_) {
auto fieldResult = fields_->insert(field);
auto valueResult = values_->insert(value);
row.insert(std::make_pair(std::ref(const_cast<std::string &>(*fieldResult.first)),
std::ref(const_cast<ValueObject &>(*valueResult.first))));
}
buckets_.push_back(std::move(row));
}
std::pair<int, ValuesBuckets::ValueType> ValuesBuckets::Get(size_t row, const FieldType &field) const
{
ValueObject emptyObject;
ValueType emptyRef(emptyObject);
if (row >= buckets_.size()) {
return { E_INVALID_ARGS, emptyRef };
}
auto &bucket = buckets_[row];
auto it = bucket.find(field);
if (it == bucket.end()) {
return { E_INVALID_ARGS, emptyRef };
}
return { E_OK, it->second };
}
}
}

View File

@ -41,6 +41,7 @@ base_sources = [
"${relational_store_native_path}/rdb/src/string_utils.cpp",
"${relational_store_native_path}/rdb/src/value_object.cpp",
"${relational_store_native_path}/rdb/src/values_bucket.cpp",
"${relational_store_native_path}/rdb/src/values_buckets.cpp",
]
if (!is_ohos) {

View File

@ -0,0 +1,9 @@
diff a/interfaces/inner_api/rdb/BUILD.gn b/interfaces/inner_api/rdb/BUILD.gn (rejected hunks)
@@ -41,6 +41,7 @@ base_sources = [
"${relational_store_native_path}/rdb/src/string_utils.cpp",
"${relational_store_native_path}/rdb/src/value_object.cpp",
"${relational_store_native_path}/rdb/src/values_bucket.cpp",
+ "${relational_store_native_path}/rdb/src/values_buckets.cpp",
]
if (!is_ohos) {

View File

@ -33,6 +33,7 @@ public:
BigInteger &operator=(const BigInteger &other);
BigInteger &operator=(BigInteger &&other);
bool operator==(const BigInteger &other);
bool operator<(const BigInteger &rhs);
int32_t Sign() const;
size_t Size() const;

View File

@ -0,0 +1,9 @@
diff a/interfaces/inner_api/rdb/include/big_integer.h b/interfaces/inner_api/rdb/include/big_integer.h (rejected hunks)
@@ -33,6 +33,7 @@ public:
BigInteger &operator=(const BigInteger &other);
BigInteger &operator=(BigInteger &&other);
bool operator==(const BigInteger &other);
+ bool operator<(const BigInteger &rhs);
int32_t Sign() const;
size_t Size() const;

View File

@ -26,6 +26,7 @@
#include "result_set.h"
#include "value_object.h"
#include "values_bucket.h"
#include "values_buckets.h"
#include "rdb_types.h"
#include "rdb_common.h"
#include "rdb_errno.h"
@ -104,6 +105,17 @@ public:
virtual int BatchInsert(
int64_t &outInsertNum, const std::string &table, const std::vector<ValuesBucket> &values) = 0;
/**
* @brief Inserts a batch of data into the target table.
*
* @param table Indicates the target table.
* @param values Indicates the rows of data {@link ValuesBuckets} to be inserted into the table.
*/
virtual std::pair<int, int64_t> BatchInsert(const std::string &table, const ValuesBuckets &values)
{
return { E_NOT_SUPPORT, 0 };
}
/**
* @brief Replaces a row of data into the target table.
*

View File

@ -0,0 +1,27 @@
diff a/interfaces/inner_api/rdb/include/rdb_store.h b/interfaces/inner_api/rdb/include/rdb_store.h (rejected hunks)
@@ -26,6 +26,7 @@
#include "result_set.h"
#include "value_object.h"
#include "values_bucket.h"
+#include "values_buckets.h"
#include "rdb_types.h"
#include "rdb_common.h"
#include "rdb_errno.h"
@@ -104,6 +105,17 @@ public:
virtual int BatchInsert(
int64_t &outInsertNum, const std::string &table, const std::vector<ValuesBucket> &values) = 0;
+ /**
+ * @brief Inserts a batch of data into the target table.
+ *
+ * @param table Indicates the target table.
+ * @param values Indicates the rows of data {@link ValuesBuckets} to be inserted into the table.
+ */
+ virtual std::pair<int, int64_t> BatchInsert(const std::string &table, const ValuesBuckets &values)
+ {
+ return { E_NOT_SUPPORT, 0 };
+ }
+
/**
* @brief Replaces a row of data into the target table.
*

View File

@ -379,6 +379,8 @@ public:
return value;
}
bool operator<(const ValueObject &rhs) const;
private:
template<class T>
int Get(T &output) const;

View File

@ -0,0 +1,19 @@
diff a/interfaces/inner_api/rdb/include/value_object.h b/interfaces/inner_api/rdb/include/value_object.h (rejected hunks)
@@ -225,7 +225,7 @@ public:
* @param val Indicates an Assets input parameter.
*/
API_EXPORT ValueObject(BigInt val);
-
+
/**
* @brief Constructor.
* This constructor is used to convert the FloatVector input parameter to a value of type ValueObject.
@@ -374,6 +374,8 @@ public:
return value;
}
+ bool operator<(const ValueObject &rhs) const;
+
private:
template<class T>
int Get(T &output) const;

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NATIVE_RDB_VALUES_BUCKETS_H
#define NATIVE_RDB_VALUES_BUCKETS_H
#include <map>
#include <set>
#include <memory>
#include "value_object.h"
#include "values_bucket.h"
namespace OHOS {
namespace NativeRdb {
class API_EXPORT ValuesBuckets {
public:
using FieldsType = std::shared_ptr<std::set<std::string>>;
using ValuesType = std::shared_ptr<std::set<ValueObject>>;
using FieldType = std::reference_wrapper<const std::string>;
using ValueType = std::reference_wrapper<ValueObject>;
using BucketType = std::map<FieldType, ValueType, std::less<std::string>>;
API_EXPORT ValuesBuckets();
API_EXPORT size_t RowSize() const;
API_EXPORT std::pair<FieldsType, ValuesType> GetFieldsAndValues() const;
API_EXPORT void Put(const ValuesBucket &bucket);
API_EXPORT std::pair<int, ValueType> Get(size_t row, const FieldType &field) const;
private:
FieldsType fields_;
ValuesType values_;
std::vector<BucketType> buckets_;
};
} // namespace NativeRdb
} // namespace OHOS
#endif

View File

@ -32,6 +32,7 @@ public:
BigInteger &operator=(const BigInteger &other);
BigInteger &operator=(BigInteger &&other);
bool operator==(const BigInteger &other);
bool operator<(const BigInteger &rhs);
int32_t Sign() const;
size_t Size() const;

View File

@ -0,0 +1,9 @@
diff a/interfaces/inner_api/rdb/mock/include/big_integer.h b/interfaces/inner_api/rdb/mock/include/big_integer.h (rejected hunks)
@@ -32,6 +32,7 @@ public:
BigInteger &operator=(const BigInteger &other);
BigInteger &operator=(BigInteger &&other);
bool operator==(const BigInteger &other);
+ bool operator<(const BigInteger &rhs);
int32_t Sign() const;
size_t Size() const;

View File

@ -28,6 +28,8 @@
#include "result_set.h"
#include "value_object.h"
#include "values_bucket.h"
#include "values_buckets.h"
namespace OHOS::NativeRdb {
class RdbStore {
public:
@ -39,6 +41,10 @@ public:
virtual int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &values) = 0;
virtual int BatchInsert(int64_t &outInsertNum, const std::string &table,
const std::vector<ValuesBucket> &values) = 0;
virtual std::pair<int, int64_t> BatchInsert(const std::string &table, const ValuesBuckets &values)
{
return { E_NOT_SUPPORT, 0 };
}
virtual int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &values) = 0;
virtual int InsertWithConflictResolution(
int64_t &outRowId, const std::string &table, const ValuesBucket &values,

View File

@ -0,0 +1,21 @@
diff a/interfaces/inner_api/rdb/mock/include/rdb_store.h b/interfaces/inner_api/rdb/mock/include/rdb_store.h (rejected hunks)
@@ -28,6 +28,8 @@
#include "result_set.h"
#include "value_object.h"
#include "values_bucket.h"
+#include "values_buckets.h"
+
namespace OHOS::NativeRdb {
class RdbStore {
public:
@@ -39,6 +41,10 @@ public:
virtual int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &values) = 0;
virtual int BatchInsert(int64_t &outInsertNum, const std::string &table,
const std::vector<ValuesBucket> &values) = 0;
+ virtual std::pair<int, int64_t> BatchInsert(const std::string &table, const ValuesBuckets &values)
+ {
+ return { E_NOT_SUPPORT, 0 };
+ }
virtual int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &values) = 0;
virtual int InsertWithConflictResolution(
int64_t &outRowId, const std::string &table, const ValuesBucket &values,

View File

@ -325,6 +325,8 @@ public:
return value;
}
bool operator<(const ValueObject &rhs) const;
private:
template<class T>
int Get(T &output) const;

View File

@ -0,0 +1,10 @@
diff a/interfaces/inner_api/rdb/mock/include/value_object.h b/interfaces/inner_api/rdb/mock/include/value_object.h (rejected hunks)
@@ -325,6 +325,8 @@ public:
return value;
}
+ bool operator<(const ValueObject &rhs) const;
+
private:
template<class T>
int Get(T &output) const;

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NATIVE_RDB_VALUES_BUCKETS_H
#define NATIVE_RDB_VALUES_BUCKETS_H
#include <map>
#include <set>
#include <memory>
#include "value_object.h"
#include "values_bucket.h"
namespace OHOS {
namespace NativeRdb {
class ValuesBuckets {
public:
using FieldsType = std::shared_ptr<std::set<std::string>>;
using ValuesType = std::shared_ptr<std::set<ValueObject>>;
using FieldType = std::reference_wrapper<const std::string>;
using ValueType = std::reference_wrapper<ValueObject>;
using BucketType = std::map<FieldType, ValueType, std::less<std::string>>;
ValuesBuckets();
size_t RowSize() const;
std::pair<FieldsType, ValuesType> GetFieldsAndValues() const;
void Put(const ValuesBucket &bucket);
std::pair<int, ValueType> Get(size_t row, const FieldType &field) const;
private:
FieldsType fields_;
ValuesType values_;
std::vector<BucketType> buckets_;
};
} // namespace NativeRdb
} // namespace OHOS
#endif