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 #define LOG_TAG "NapiPredicatesUtils"
16 #include "napi_predicates_utils.h"
17 
18 #include "js_utils.h"
19 #include "logger.h"
20 #include "napi_data_ability_predicates.h"
21 #include "napi_rdb_predicates.h"
22 #include "predicates_utils.h"
23 
24 using namespace OHOS::Rdb;
25 using namespace OHOS::AppDataMgrJsKit;
26 
27 namespace OHOS {
28 namespace DataAbilityJsKit {
CreateRdbPredicates(napi_env env,napi_callback_info info)29 napi_value CreateRdbPredicates(napi_env env, napi_callback_info info)
30 {
31     size_t argc = 2;
32     napi_value args[2] = { 0 };
33     napi_value thiz = nullptr;
34     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thiz, nullptr));
35     // "2" argc is tablename and the object that contains the predicates
36     NAPI_ASSERT(env, argc == 2, "DataAbilityJsKit::CreateRdbPredicates Invalid argvs!");
37 
38     LOG_DEBUG("DataAbilityJsKit::CreateRdbPredicates argc is %{public}zu", argc);
39     napi_valuetype valueType = napi_undefined;
40     NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
41     NAPI_ASSERT(env, valueType == napi_string, "Table name should be a string.");
42     std::string tableName = JSUtils::Convert2String(env, args[0]);
43 
44     NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
45     NAPI_ASSERT(env, valueType == napi_object, "Table name should be an object.");
46     DataAbilityPredicatesProxy *dataAbilityPredicatesProxy = nullptr;
47     NAPI_CALL(env, napi_unwrap(env, args[1], reinterpret_cast<void **>(&dataAbilityPredicatesProxy)));
48 
49     auto absPredicates = dataAbilityPredicatesProxy->GetPredicates();
50     auto predicates = new NativeRdb::RdbPredicates(tableName);
51     NativeRdb::PredicatesUtils::SetWhereClauseAndArgs(
52         predicates, absPredicates->GetWhereClause(), absPredicates->GetBindArgs());
53     NativeRdb::PredicatesUtils::SetAttributes(predicates, absPredicates->IsDistinct(),
54         absPredicates->GetIndex(), absPredicates->GetGroup(), absPredicates->GetOrder(), absPredicates->GetLimit(),
55         absPredicates->GetOffset());
56 
57     return RdbJsKit::RdbPredicatesProxy::NewInstance(env, std::shared_ptr<NativeRdb::RdbPredicates>(predicates));
58 }
59 
InitPredicatesUtils(napi_env env,napi_value exports)60 napi_value InitPredicatesUtils(napi_env env, napi_value exports)
61 {
62     LOG_INFO("Init InitPredicatesUtils");
63     napi_property_descriptor properties[] = {
64         DECLARE_NAPI_FUNCTION("createRdbPredicates", CreateRdbPredicates),
65     };
66     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
67     return exports;
68 }
69 } // namespace DataAbilityJsKit
70 } // namespace OHOS
71