• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  async getDb() {
67    rdbStore = await CalendarDataHelper.getInstance().getRdbStore() as data_rdb.RdbStore;
68  }
69
70  insertByLowAuthority(uri: string, value: data_rdb.ValuesBucket, callback: Function) {
71    if (!rdbStore) {
72      Log.warn(TAG, `insertByLowAuthority uri: ${uri} rdbStore is null`)
73      this.getDb();
74      return;
75    }
76    Log.info(TAG, `insertByLowAuthority uri: ${uri}`);
77    let table = getTableByUri(uri);
78    Log.info(TAG, `insertByLowAuthority table: ${table}`);
79
80    const processor = factory.getDatabaseProcessor(table);
81    if (processor !== null && processor !== undefined) {
82      processor.insertByLowAuthority(rdbStore, uri, value, callback);
83      return;
84    } else {
85      Log.error(TAG, 'insertByLowAuthority with invalid processor');
86    }
87  }
88
89  deleteByHighAuthority(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
90    if (!rdbStore) {
91      this.getDb();
92      Log.warn(TAG, `deleteByHighAuthority uri: ${uri} rdbStore is null`)
93      return;
94    }
95    Log.info(TAG, `deleteByHighAuthority uri: ${uri}`);
96    let table = getTableByUri(uri);
97    Log.info(TAG, `deleteByHighAuthority table: ${table}`);
98
99    const processor = factory.getDatabaseProcessor(table);
100    if (processor !== null && processor !== undefined) {
101      processor.deleteByHighAuthority(rdbStore, uri, predicates, callback);
102      return;
103    } else {
104      Log.error(TAG, 'deleteByHighAuthority with invalid processor');
105    }
106  }
107
108  deleteByLowAuthority(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
109    if (!rdbStore) {
110      this.getDb();
111      Log.warn(TAG, `deleteByLowAuthority uri: ${uri} rdbStore is null`)
112      return;
113    }
114    Log.info(TAG, `deleteByLowAuthority uri: ${uri}`);
115    let table = getTableByUri(uri);
116    Log.info(TAG, `deleteByLowAuthority table: ${table}`);
117
118    const processor = factory.getDatabaseProcessor(table);
119    if (processor !== null && processor !== undefined) {
120      processor.deleteByLowAuthority(rdbStore, uri, predicates, callback);
121      return;
122    } else {
123      Log.error(TAG, 'deleteByLowAuthority with invalid processor');
124    }
125  }
126
127  updateByHighAuthority(uri: string, value: data_rdb.ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
128    if (!rdbStore) {
129      this.getDb();
130      Log.warn(TAG, `updateByHighAuthority uri: ${uri} rdbStore is null`)
131      return;
132    }
133    Log.info(TAG, `updateByHighAuthority uri: ${uri}`);
134    let table = getTableByUri(uri);
135    Log.info(TAG, `updateByHighAuthority table: ${table}`);
136
137    const processor = factory.getDatabaseProcessor(table);
138    if (processor !== null && processor !== undefined) {
139      processor.updateByHighAuthority(rdbStore, uri, value, predicates, callback);
140      return;
141    } else {
142      Log.error(TAG, 'updateByHighAuthority with invalid processor');
143    }
144  }
145
146  updateByLowAuthority(uri: string, value: data_rdb.ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
147    if (!rdbStore) {
148      this.getDb();
149      Log.warn(TAG, `updateByLowAuthority uri: ${uri} rdbStore is null`)
150      return;
151    }
152    Log.info(TAG, `updateByLowAuthority uri: ${uri}`);
153    let table = getTableByUri(uri);
154    Log.info(TAG, `updateByLowAuthority table: ${table}`);
155
156    const processor = factory.getDatabaseProcessor(table);
157    if (processor !== null && processor !== undefined) {
158      processor.updateByLowAuthority(rdbStore, uri, value, predicates, callback);
159      return;
160    } else {
161      Log.error(TAG, 'updateByLowAuthority with invalid processor');
162    }
163  }
164
165  queryByHighAuthority(uri: string, columns: Array<string>, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
166    if (!rdbStore) {
167      this.getDb();
168      Log.warn(TAG, `queryByHighAuthority uri: ${uri} rdbStore is null`)
169      return;
170    }
171    Log.info(TAG, `queryByHighAuthority uri: ${uri}`);
172    const table = getTableByUri(uri);
173    Log.info(TAG, `queryByHighAuthority table: ${table}`);
174
175    const processor = factory.getDatabaseProcessor(table);
176    if (processor !== null && processor !== undefined) {
177      processor.queryByHighAuthority(rdbStore, uri, columns, predicates, callback);
178      return;
179    } else {
180      Log.error(TAG, 'queryByHighAuthority with invalid processor');
181    }
182  }
183
184  queryByLowAuthority(uri: string, columns: Array<string>, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
185    if (!rdbStore) {
186      this.getDb();
187      Log.warn(TAG, `queryByLowAuthority uri: ${uri} rdbStore is null`)
188      return;
189    }
190    Log.info(TAG, `queryByLowAuthority uri: ${uri}`);
191    const table = getTableByUri(uri);
192    Log.info(TAG, `queryByLowAuthority table: ${table}`);
193
194    const processor = factory.getDatabaseProcessor(table);
195    if (processor !== null && processor !== undefined) {
196      processor.queryByLowAuthority(rdbStore, uri, columns, predicates, callback);
197      return;
198    } else {
199      Log.error(TAG, 'queryByLowAuthority with invalid processor');
200    }
201  }
202}
203
204export default new DataShareAbilityDelegate()