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 "common.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 namespace OHOS {
25 namespace DataAbilityJsKit {
CreateRdbPredicates(napi_env env,napi_callback_info info)26 napi_value CreateRdbPredicates(napi_env env, napi_callback_info info)
27 {
28 size_t argc = 2;
29 napi_value args[2] = { 0 };
30 napi_value _this;
31 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
32 NAPI_ASSERT(env, argc == 2, "DataAbilityJsKit::CreateRdbPredicates Invalid argvs!");
33
34 LOG_DEBUG("DataAbilityJsKit::CreateRdbPredicates argc is %{public}zu", argc);
35 napi_valuetype valueType;
36 NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
37 NAPI_ASSERT(env, valueType == napi_string, "Table name should be a string.");
38 std::string tableName = JsKit::JSUtils::Convert2String(env, args[0], JsKit::JSUtils::DEFAULT_BUF_SIZE);
39
40 NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
41 NAPI_ASSERT(env, valueType == napi_object, "Table name should be an object.");
42 DataAbilityPredicatesProxy *dataAbilityPredicatesProxy = nullptr;
43 NAPI_CALL(env, napi_unwrap(env, args[1], reinterpret_cast<void **>(&dataAbilityPredicatesProxy)));
44
45 auto absPredicates = dataAbilityPredicatesProxy->GetPredicates();
46 auto predicates = std::make_shared<NativeRdb::RdbPredicates>(tableName);
47 NativeRdb::PredicatesUtils::SetWhereClauseAndArgs(
48 predicates.get(), absPredicates->GetWhereClause(), absPredicates->GetWhereArgs());
49 NativeRdb::PredicatesUtils::SetAttributes(predicates.get(), absPredicates->IsDistinct(),
50 absPredicates->GetIndex(), absPredicates->GetGroup(), absPredicates->GetOrder(), absPredicates->GetLimit(),
51 absPredicates->GetOffset());
52
53 return RdbJsKit::RdbPredicatesProxy::NewInstance(env, predicates);
54 }
55
InitPredicatesUtils(napi_env env,napi_value exports)56 napi_value InitPredicatesUtils(napi_env env, napi_value exports)
57 {
58 LOG_INFO("Init InitPredicatesUtils");
59 napi_property_descriptor properties[] = {
60 DECLARE_NAPI_FUNCTION("createRdbPredicates", CreateRdbPredicates),
61 };
62 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
63 LOG_INFO("Init InitPredicatesUtils end");
64 return exports;
65 }
66 } // namespace DataAbilityJsKit
67 } // namespace OHOS
68