• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef CONTACT_DATABASE_H
17 #define CONTACT_DATABASE_H
18 
19 #include "datashare_predicates.h"
20 #include "datashare_values_bucket.h"
21 #include "rdb_errno.h"
22 #include "rdb_helper.h"
23 #include "rdb_open_callback.h"
24 #include "rdb_predicates.h"
25 #include "rdb_store.h"
26 #include "result_set.h"
27 
28 #include "calllog_database.h"
29 #include "profile_database.h"
30 
31 namespace OHOS {
32 namespace Contacts {
33 class ContactsDataBase {
34 public:
35     static std::shared_ptr<ContactsDataBase> GetInstance();
36     static std::shared_ptr<OHOS::NativeRdb::RdbStore> store_;
37     static std::shared_ptr<OHOS::NativeRdb::RdbStore> contactStore_;
38 
39     std::vector<OHOS::NativeRdb::ValuesBucket> ResultSetToValuesBucket(
40         std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> &resultSet);
41     void GetContactByValue(int &rawContactId, OHOS::NativeRdb::ValueObject &value);
42     int64_t InsertRawContact(std::string table, OHOS::NativeRdb::ValuesBucket value);
43     int64_t InsertContactData(std::string table, OHOS::NativeRdb::ValuesBucket value);
44     int64_t InsertGroup(std::string table, OHOS::NativeRdb::ValuesBucket value);
45     int64_t InsertBlockList(std::string table, OHOS::NativeRdb::ValuesBucket value);
46     int UpdateContactData(OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &rdbPredicates);
47     int UpdateRawContact(OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &rdbPredicates);
48     int UpdateGroup(OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &rdbPredicates);
49     int UpdateBlockList(OHOS::NativeRdb::ValuesBucket values, OHOS::NativeRdb::RdbPredicates &rdbPredicates);
50     int DeleteGroup(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
51     int DeleteContactData(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
52     int DeleteContact(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
53     int DeleteRawContact(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
54     int DeleteBlockList(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
55     std::unique_ptr<OHOS::NativeRdb::AbsSharedResultSet> Query(
56         OHOS::NativeRdb::RdbPredicates &rdbPredicates, std::vector<std::string> &columns);
57     int CompletelyDelete(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
58     int BeginTransaction();
59     int Commit();
60     int RollBack();
61     static void DestroyInstanceAndRestore(std::string restorePath);
62     std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> SelectCandidate();
63     int Split(DataShare::DataSharePredicates predicates);
64     int ContactMerge();
65     int ReContactMerge(DataShare::DataSharePredicates predicates);
66     int DeleteRecord(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
67     int GetTypeId(std::string typeText);
68     void DeleteExecute(std::shared_ptr<OHOS::NativeRdb::RdbStore> &store,
69         std::vector<OHOS::NativeRdb::ValuesBucket> queryValuesBucket);
70     void InsertMergeData(std::shared_ptr<OHOS::NativeRdb::RdbStore> &store, std::vector<int> &rawContactIdVector);
71     void MarkMerge(std::shared_ptr<OHOS::NativeRdb::RdbStore> &store);
72     void MergeUpdateTask(
73         std::shared_ptr<OHOS::NativeRdb::RdbStore> &store, std::vector<int> &rawContactIdVector, bool isDeleted);
74     void DeleteRecordInsert(std::shared_ptr<OHOS::NativeRdb::RdbStore> &store,
75         std::vector<OHOS::NativeRdb::ValuesBucket> &queryValuesBucket);
76     void DeletedAsyncTask(std::shared_ptr<OHOS::NativeRdb::RdbStore> &store,
77         std::vector<OHOS::NativeRdb::ValuesBucket> &queryValuesBucket);
78 
79 private:
80     ContactsDataBase();
81     ContactsDataBase(const ContactsDataBase &);
82     static std::shared_ptr<ContactsDataBase> contactDataBase_;
83     static std::shared_ptr<CallLogDataBase> callLogDataBase_;
84 
85     std::vector<int> QueryContactDataRawContactId(
86         OHOS::NativeRdb::RdbPredicates &rdbPredicates, std::vector<std::string> &types);
87     std::string StructureDeleteContactJson(
88         OHOS::NativeRdb::ValuesBucket rawContactValues, std::string rawContactIdColumn, int rawContactId);
89     int DeleteExecute(std::vector<OHOS::NativeRdb::ValuesBucket> &queryValuesBucket);
90     int DeleteRawContactLocal(int contactId, int rawContactId, std::string backupData, std::string disPlayName);
91     std::vector<OHOS::NativeRdb::ValuesBucket> DeleteContactQuery(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
92     std::vector<OHOS::NativeRdb::ValuesBucket> DeleteRawContactQuery(OHOS::NativeRdb::RdbPredicates &rdbPredicates);
93     int DeleteLocal(int rawContactId, std::string contactId);
94     static bool Restore(std::string restorePath);
95     int GetTypeText(
96         OHOS::NativeRdb::ValuesBucket &contactDataValues, int &typeId, int &rawContactId, std::string &typeText);
97     int CompletelyDeleteCommit(int retCode);
98 };
99 
100 class SqliteOpenHelperContactCallback : public OHOS::NativeRdb::RdbOpenCallback {
101 public:
102     int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override;
103     int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
104     int OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
105 };
106 } // namespace Contacts
107 } // namespace OHOS
108 #endif // CONTACT_DATABASE_H
109