• 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 #include "profile_database.h"
17 
18 #include <cstdio>
19 #include <mutex>
20 #include <unistd.h>
21 
22 #include "common.h"
23 #include "contacts_account.h"
24 #include "contacts_columns.h"
25 #include "contacts_type.h"
26 #include "hilog_wrapper.h"
27 
28 namespace OHOS {
29 namespace Contacts {
30 std::shared_ptr<ProfileDatabase> ProfileDatabase::profileDatabase_ = nullptr;
31 std::shared_ptr<OHOS::NativeRdb::RdbStore> ProfileDatabase::store_ = nullptr;
32 static std::string g_databaseName;
33 namespace {
34 std::mutex g_mtx;
35 } // namespace
36 
ProfileDatabase()37 ProfileDatabase::ProfileDatabase()
38 {
39     int errCode = OHOS::NativeRdb::E_OK;
40     g_databaseName = ContactsPath::RDB_PATH + "profile.db";
41     OHOS::NativeRdb::RdbStoreConfig config(g_databaseName);
42     HILOG_INFO("ProfileDatabase g_databaseName :%{public}s", g_databaseName.c_str());
43     SqliteOpenHelperProfileCallback sqliteOpenHelperCallback;
44     store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode);
45     if (errCode != OHOS::NativeRdb::E_OK) {
46         HILOG_ERROR("ProfileDatabase errCode :%{public}d", errCode);
47     } else {
48         HILOG_INFO("ProfileDatabase errCode :%{public}d", errCode);
49     }
50     if (errCode != OHOS::NativeRdb::E_OK) {
51         HILOG_ERROR("ProfileDatabase rebase open error :%{public}d", errCode);
52         return;
53     }
54     std::shared_ptr<ContactsAccount> contactsAccount = ContactsAccount::GetInstance();
55     contactsAccount->PrepopulateCommonAccountTypes(store_);
56     ContactsType contactsType;
57     contactsType.PrepopulateCommonTypes(store_);
58 }
59 
GetInstance()60 std::shared_ptr<ProfileDatabase> ProfileDatabase::GetInstance()
61 {
62     if (profileDatabase_ == nullptr) {
63         g_mtx.lock();
64         profileDatabase_.reset(new ProfileDatabase());
65         g_mtx.unlock();
66     }
67     return profileDatabase_;
68 }
69 
DestroyInstanceAndRestore(std::string restorePath)70 void ProfileDatabase::DestroyInstanceAndRestore(std::string restorePath)
71 {
72     g_mtx.lock();
73     if (access(restorePath.c_str(), F_OK) != 0) {
74         HILOG_ERROR("Restore file %{private}s does not exist", restorePath.c_str());
75         g_mtx.unlock();
76         return;
77     }
78     OHOS::NativeRdb::RdbHelper::DeleteRdbStore(g_databaseName);
79     OHOS::NativeRdb::RdbHelper::ClearCache();
80     profileDatabase_ = nullptr;
81     Restore(restorePath);
82     g_mtx.unlock();
83 }
84 
Restore(std::string restorePath)85 bool ProfileDatabase::Restore(std::string restorePath)
86 {
87     if (rename(restorePath.c_str(), g_databaseName.c_str())) {
88         HILOG_ERROR("Restore rename oldFileName = %{private}s to newFileName  %{private}s", restorePath.c_str(),
89             g_databaseName.c_str());
90         return true;
91     }
92     return false;
93 }
94 
OnCreate(OHOS::NativeRdb::RdbStore & store)95 int SqliteOpenHelperProfileCallback::OnCreate(OHOS::NativeRdb::RdbStore &store)
96 {
97     store.ExecuteSql(CREATE_CONTACT);
98     store.ExecuteSql(CREATE_CONTACT_INDEX);
99     store.ExecuteSql(CREATE_RAW_CONTACT);
100     store.ExecuteSql(CREATE_RAW_CONTACT_INDEX);
101     store.ExecuteSql(CREATE_CONTACT_DATA);
102     store.ExecuteSql(CREATE_CONTACT_INDEX_DATA1);
103     store.ExecuteSql(CREATE_CONTACT_INDEX_DATA2);
104     store.ExecuteSql(CREATE_CONTACT_BLOCKLIST);
105     store.ExecuteSql(CREATE_LOCAL_LANG);
106     store.ExecuteSql(CREATE_ACCOUNT);
107     store.ExecuteSql(CREATE_PHOTO_FILES);
108     store.ExecuteSql(CREATE_CONTACT_TYPE);
109     store.ExecuteSql(CREATE_GROUPS);
110     store.ExecuteSql(CREATE_DELETED_RAW_CONTACT);
111     store.ExecuteSql(CREATE_SEARCH_CONTACT);
112     store.ExecuteSql(CREATE_SEARCH_CONTACT_INDEX1);
113     store.ExecuteSql(CREATE_SEARCH_CONTACT_INDEX2);
114     store.ExecuteSql(CREATE_SEARCH_CONTACT_VIEW);
115     store.ExecuteSql(MERGE_INFO);
116     store.ExecuteSql(CREATE_VIEW_CONTACT_DATA);
117     store.ExecuteSql(CREATE_VIEW_RAW_CONTACT);
118     store.ExecuteSql(CREATE_VIEW_CONTACT);
119     store.ExecuteSql(CREATE_VIEW_GROUPS);
120     store.ExecuteSql(CREATE_VIEW_DELETED);
121     store.ExecuteSql(INSERT_DELETE_RAW_CONTACT);
122     store.ExecuteSql(UPDATE_RAW_CONTACT_VERSION);
123     store.ExecuteSql(UPDATE_CONTACT_DATA_VERSION);
124     store.ExecuteSql(INSERT_CONTACT_QUICK_SEARCH);
125     store.ExecuteSql(CREATE_DATABASE_BACKUP_TASK);
126     store.ExecuteSql(CREATE_INSERT_BACKUP_TIME);
127     store.ExecuteSql(UPDATE_CONTACT_BY_INSERT_CONTACT_DATA);
128     store.ExecuteSql(UPDATE_CONTACT_BY_DELETE_CONTACT_DATA);
129     store.ExecuteSql(UPDATE_CONTACT_BY_UPDATE_CONTACT_DATA);
130     store.ExecuteSql(MERGE_INFO_INDEX);
131     return OHOS::NativeRdb::E_OK;
132 }
133 
OnUpgrade(OHOS::NativeRdb::RdbStore & store,int oldVersion,int newVersion)134 int SqliteOpenHelperProfileCallback::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int oldVersion, int newVersion)
135 {
136     HILOG_INFO("OnUpgrade oldVersion is %{public}d , newVersion is %{public}d", oldVersion, newVersion);
137     if (oldVersion < newVersion && newVersion == DATABASE_NEW_VERSION) {
138         store.ExecuteSql("ALTER TABLE database_backup_task ADD COLUMN sync TEXT");
139     }
140     store.SetVersion(newVersion);
141     return OHOS::NativeRdb::E_OK;
142 }
143 
OnDowngrade(OHOS::NativeRdb::RdbStore & store,int oldVersion,int newVersion)144 int SqliteOpenHelperProfileCallback::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int oldVersion, int newVersion)
145 {
146     HILOG_INFO("OnDowngrade oldVersion is %{public}d , newVersion is %{public}d", oldVersion, newVersion);
147     if (oldVersion > newVersion && newVersion == DATABASE_OPEN_VERSION) {
148         store.ExecuteSql(
149             "CREATE TABLE IF NOT EXISTS database_backup (id INTEGER PRIMARY KEY AUTOINCREMENT, backup_time "
150             "TEXT, backup_path TEXT, remarks TEXT)");
151         store.ExecuteSql(
152             "INSERT INTO database_backup(id, backup_time, backup_path, remarks) SELECT id, "
153             "backup_time, backup_path, remarks FROM database_backup_task");
154         store.ExecuteSql("DROP table database_backup_task");
155         store.ExecuteSql("ALTER table database_backup RENAME TO database_backup_task");
156         store.ExecuteSql(CREATE_INSERT_BACKUP_TIME);
157     }
158     int ret = store.SetVersion(newVersion);
159     return ret;
160 }
161 } // namespace Contacts
162 } // namespace OHOS