1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_predicates_utils.h"
17
18 #include "js_logger.h"
19 #include "js_utils.h"
20 #include "napi_data_ability_predicates.h"
21 #include "napi_rdb_predicates.h"
22 #include "predicates_utils.h"
23
24 using namespace OHOS::AppDataMgrJsKit;
25
26 namespace OHOS {
27 namespace DataAbilityJsKit {
CreateRdbPredicates(napi_env env,napi_callback_info info)28 napi_value CreateRdbPredicates(napi_env env, napi_callback_info info)
29 {
30 size_t argc = 2;
31 napi_value args[2] = { 0 };
32 napi_value thiz;
33 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thiz, nullptr));
34 NAPI_ASSERT(env, argc == 2, "DataAbilityJsKit::CreateRdbPredicates Invalid argvs!");
35
36 LOG_DEBUG("DataAbilityJsKit::CreateRdbPredicates argc is %{public}zu", argc);
37 napi_valuetype valueType;
38 NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
39 NAPI_ASSERT(env, valueType == napi_string, "Table name should be a string.");
40 std::string tableName = JSUtils::Convert2String(env, args[0]);
41
42 NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
43 NAPI_ASSERT(env, valueType == napi_object, "Table name should be an object.");
44 DataAbilityPredicatesProxy *dataAbilityPredicatesProxy = nullptr;
45 NAPI_CALL(env, napi_unwrap(env, args[1], reinterpret_cast<void **>(&dataAbilityPredicatesProxy)));
46
47 auto absPredicates = dataAbilityPredicatesProxy->GetPredicates();
48 auto predicates = std::make_shared<NativeRdb::RdbPredicates>(tableName);
49 NativeRdb::PredicatesUtils::SetWhereClauseAndArgs(
50 predicates.get(), absPredicates->GetWhereClause(), absPredicates->GetWhereArgs());
51 NativeRdb::PredicatesUtils::SetAttributes(predicates.get(), absPredicates->IsDistinct(),
52 absPredicates->GetIndex(), absPredicates->GetGroup(), absPredicates->GetOrder(), absPredicates->GetLimit(),
53 absPredicates->GetOffset());
54
55 return RdbJsKit::RdbPredicatesProxy::NewInstance(env, predicates);
56 }
57
InitPredicatesUtils(napi_env env,napi_value exports)58 napi_value InitPredicatesUtils(napi_env env, napi_value exports)
59 {
60 LOG_INFO("Init InitPredicatesUtils");
61 napi_property_descriptor properties[] = {
62 DECLARE_NAPI_FUNCTION("createRdbPredicates", CreateRdbPredicates),
63 };
64 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
65 LOG_INFO("Init InitPredicatesUtils end");
66 return exports;
67 }
68 } // namespace DataAbilityJsKit
69 } // namespace OHOS
70