1/** 2 * @file Describe the file 3 * Copyright (c) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import { Log } from '@ohos/common/src/main/ets/utils/Log'; 18import { insertDefaultCalendar } from '@ohos/datamanager/src/main/ets/processor/calendars/CalendarsProcessor'; 19import CalendarDataHelper from '@ohos/datamanager/src/main/ets/utils/CalendarDataHelper'; 20import getTableByUri from '@ohos/datamanager/src/main/ets/utils/CalendarUriHelper'; 21import factory from '@ohos/datamanager/src/main/ets/processor/DatabaseProcessorFactory'; 22import data_rdb from '@ohos.data.relationalStore'; 23import dataSharePredicates from '@ohos.data.dataSharePredicates'; 24 25let rdbStore: data_rdb.RdbStore; 26let TAG = 'DataShareAbilityDelegate'; 27 28/** 29 * the delegate of CalendarData's DatabaseProcessor 30 * 31 * @since 2022-06-15 32 */ 33class DataShareAbilityDelegate { 34 async init(): Promise<boolean> { 35 Log.info(TAG, 'init start'); 36 try { 37 rdbStore = await CalendarDataHelper.getInstance().getRdbStore() as data_rdb.RdbStore; 38 await insertDefaultCalendar(rdbStore); 39 } catch (err) { 40 Log.error(TAG, 'init err'); 41 return false; 42 } 43 Log.info(TAG, 'init end'); 44 return true; 45 } 46 47 insertByHighAuthority(uri: string, value: data_rdb.ValuesBucket, callback: Function) { 48 if (!rdbStore) { 49 this.getDb(); 50 Log.warn(TAG, `insertByHighAuthority uri: ${uri} rdbStore is null`) 51 return; 52 } 53 Log.info(TAG, `insertByHighAuthority uri: ${uri}`); 54 let table = getTableByUri(uri); 55 Log.info(TAG, `insertByHighAuthority table: ${table}`); 56 57 const processor = factory.getDatabaseProcessor(table); 58 if (processor !== null && processor !== undefined) { 59 processor.insertByHighAuthority(rdbStore, uri, value, callback); 60 return; 61 } else { 62 Log.error(TAG, 'insertByHighAuthority with invalid processor'); 63 } 64 } 65 66 batchInsertByHighAuthority(uri: string, value: data_rdb.ValuesBucket[], callback: Function) { 67 if (!rdbStore) { 68 this.getDb(); 69 Log.warn(TAG, `insert uri: ${uri} rdbStore is null`) 70 return; 71 } 72 Log.info(TAG, `insert uri: ${uri}`); 73 let table = getTableByUri(uri); 74 Log.info(TAG, `insert table: ${table}`); 75 76 const processor = factory.getDatabaseProcessor(table); 77 if (processor !== null && processor !== undefined) { 78 processor.batchInsertByHighAuthority(rdbStore, uri, value, callback); 79 return; 80 } else { 81 Log.error(TAG, 'insert with invalid processor'); 82 } 83 } 84 85 async getDb() { 86 rdbStore = await CalendarDataHelper.getInstance().getRdbStore() as data_rdb.RdbStore; 87 } 88 89 insertByLowAuthority(uri: string, value: data_rdb.ValuesBucket, callback: Function) { 90 if (!rdbStore) { 91 Log.warn(TAG, `insertByLowAuthority uri: ${uri} rdbStore is null`) 92 this.getDb(); 93 return; 94 } 95 Log.info(TAG, `insertByLowAuthority uri: ${uri}`); 96 let table = getTableByUri(uri); 97 Log.info(TAG, `insertByLowAuthority table: ${table}`); 98 99 const processor = factory.getDatabaseProcessor(table); 100 if (processor !== null && processor !== undefined) { 101 processor.insertByLowAuthority(rdbStore, uri, value, callback); 102 return; 103 } else { 104 Log.error(TAG, 'insertByLowAuthority with invalid processor'); 105 } 106 } 107 108 batchInsertByLowAuthority(uri: string, value: data_rdb.ValuesBucket[], callback: Function) { 109 110 if (!rdbStore) { 111 Log.warn(TAG, `insert uri: ${uri} rdbStore is null`) 112 this.getDb(); 113 return; 114 } 115 Log.info(TAG, `insert uri: ${uri}`); 116 let table = getTableByUri(uri); 117 Log.info(TAG, `insert table: ${table}`); 118 119 const processor = factory.getDatabaseProcessor(table); 120 if (processor !== null && processor !== undefined) { 121 processor.batchInsertByLowAuthority(rdbStore, uri, value, callback); 122 return; 123 } else { 124 Log.error(TAG, 'insert with invalid processor'); 125 } 126 } 127 128 deleteByHighAuthority(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 129 if (!rdbStore) { 130 this.getDb(); 131 Log.warn(TAG, `deleteByHighAuthority uri: ${uri} rdbStore is null`) 132 return; 133 } 134 Log.info(TAG, `deleteByHighAuthority uri: ${uri}`); 135 let table = getTableByUri(uri); 136 Log.info(TAG, `deleteByHighAuthority table: ${table}`); 137 138 const processor = factory.getDatabaseProcessor(table); 139 if (processor !== null && processor !== undefined) { 140 processor.deleteByHighAuthority(rdbStore, uri, predicates, callback); 141 return; 142 } else { 143 Log.error(TAG, 'deleteByHighAuthority with invalid processor'); 144 } 145 } 146 147 deleteByLowAuthority(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 148 if (!rdbStore) { 149 this.getDb(); 150 Log.warn(TAG, `deleteByLowAuthority uri: ${uri} rdbStore is null`) 151 return; 152 } 153 Log.info(TAG, `deleteByLowAuthority uri: ${uri}`); 154 let table = getTableByUri(uri); 155 Log.info(TAG, `deleteByLowAuthority table: ${table}`); 156 157 const processor = factory.getDatabaseProcessor(table); 158 if (processor !== null && processor !== undefined) { 159 processor.deleteByLowAuthority(rdbStore, uri, predicates, callback); 160 return; 161 } else { 162 Log.error(TAG, 'deleteByLowAuthority with invalid processor'); 163 } 164 } 165 166 updateByHighAuthority(uri: string, value: data_rdb.ValuesBucket, 167 predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 168 if (!rdbStore) { 169 this.getDb(); 170 Log.warn(TAG, `updateByHighAuthority uri: ${uri} rdbStore is null`) 171 return; 172 } 173 Log.info(TAG, `updateByHighAuthority uri: ${uri}`); 174 let table = getTableByUri(uri); 175 Log.info(TAG, `updateByHighAuthority table: ${table}`); 176 177 const processor = factory.getDatabaseProcessor(table); 178 if (processor !== null && processor !== undefined) { 179 processor.updateByHighAuthority(rdbStore, uri, value, predicates, callback); 180 return; 181 } else { 182 Log.error(TAG, 'updateByHighAuthority with invalid processor'); 183 } 184 } 185 186 updateByLowAuthority(uri: string, value: data_rdb.ValuesBucket, 187 predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 188 if (!rdbStore) { 189 this.getDb(); 190 Log.warn(TAG, `updateByLowAuthority uri: ${uri} rdbStore is null`) 191 return; 192 } 193 Log.info(TAG, `updateByLowAuthority uri: ${uri}`); 194 let table = getTableByUri(uri); 195 Log.info(TAG, `updateByLowAuthority table: ${table}`); 196 197 const processor = factory.getDatabaseProcessor(table); 198 if (processor !== null && processor !== undefined) { 199 processor.updateByLowAuthority(rdbStore, uri, value, predicates, callback); 200 return; 201 } else { 202 Log.error(TAG, 'updateByLowAuthority with invalid processor'); 203 } 204 } 205 206 queryByHighAuthority(uri: string, columns: Array<string>, 207 predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 208 if (!rdbStore) { 209 this.getDb(); 210 Log.warn(TAG, `queryByHighAuthority uri: ${uri} rdbStore is null`) 211 return; 212 } 213 Log.info(TAG, `queryByHighAuthority uri: ${uri}`); 214 const table = getTableByUri(uri); 215 Log.info(TAG, `queryByHighAuthority table: ${table}`); 216 217 const processor = factory.getDatabaseProcessor(table); 218 if (processor !== null && processor !== undefined) { 219 processor.queryByHighAuthority(rdbStore, uri, columns, predicates, callback); 220 return; 221 } else { 222 Log.error(TAG, 'queryByHighAuthority with invalid processor'); 223 } 224 } 225 226 queryByLowAuthority(uri: string, columns: Array<string>, 227 predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 228 if (!rdbStore) { 229 this.getDb(); 230 Log.warn(TAG, `queryByLowAuthority uri: ${uri} rdbStore is null`) 231 return; 232 } 233 Log.info(TAG, `queryByLowAuthority uri: ${uri}`); 234 const table = getTableByUri(uri); 235 Log.info(TAG, `queryByLowAuthority table: ${table}`); 236 237 const processor = factory.getDatabaseProcessor(table); 238 if (processor !== null && processor !== undefined) { 239 processor.queryByLowAuthority(rdbStore, uri, columns, predicates, callback); 240 return; 241 } else { 242 Log.error(TAG, 'queryByLowAuthority with invalid processor'); 243 } 244 } 245} 246 247export default new DataShareAbilityDelegate()