• 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';
18
19import { insertDefaultCalendar } from '@ohos/datamanager/src/main/ets/processor/calendars/CalendarsProcessor';
20import CalendarDataHelper from '@ohos/datamanager/src/main/ets/utils/CalendarDataHelper';
21import getTableByUri from '@ohos/datamanager/src/main/ets/utils/CalendarUriHelper';
22import factory from '@ohos/datamanager/src/main/ets/processor/DatabaseProcessorFactory';
23
24let rdbStore;
25let TAG = "DataShareAbilityDelegate";
26
27/**
28 * the delegate of CalendarData's DatabaseProcessor
29 *
30 * @since 2022-06-15
31 */
32export default {
33  async init(): Promise<boolean> {
34    Log.info(TAG, 'init start');
35    try {
36      rdbStore = await CalendarDataHelper.getInstance().getRdbStore();
37      await insertDefaultCalendar(rdbStore);
38    } catch (err) {
39      Log.error(TAG, 'init err');
40      return false;
41    }
42    Log.info(TAG, 'init end');
43    return true;
44  },
45
46  insertByHighAuthority(uri, value, callback) {
47    Log.info(TAG, `insert uri: ${uri}`);
48    let table = getTableByUri(uri);
49    Log.info(TAG, `insert table: ${table}`);
50
51    const processor = factory.getDatabaseProcessor(table);
52    if (processor !== null && processor !== undefined) {
53      processor.insertByHighAuthority(rdbStore, uri, value, callback);
54      return;
55    } else {
56      Log.error(TAG, 'insert with invalid processor');
57    }
58  },
59
60  insertByLowAuthority(uri: string, value, callback) {
61    Log.info(TAG, `insert uri: ${uri}`);
62    let table = getTableByUri(uri);
63    Log.info(TAG, `insert table: ${table}`);
64
65    const processor = factory.getDatabaseProcessor(table);
66    if (processor !== null && processor !== undefined) {
67      processor.insertByLowAuthority(rdbStore, uri, value, callback);
68      return;
69    } else {
70      Log.error(TAG, 'insert with invalid processor');
71    }
72  },
73
74  deleteByHighAuthority(uri, predicates, callback) {
75    Log.info(TAG, `delete uri: ${uri}`);
76    let table = getTableByUri(uri);
77    Log.info(TAG, `delete table: ${table}`);
78
79    const processor = factory.getDatabaseProcessor(table);
80    if (processor !== null && processor !== undefined) {
81      processor.deleteByHighAuthority(rdbStore, uri, predicates, callback);
82      return;
83    } else {
84      Log.error(TAG, 'delete with invalid processor');
85    }
86  },
87
88  deleteByLowAuthority(uri, predicates, callback) {
89    Log.info(TAG, `delete uri: ${uri}`);
90    let table = getTableByUri(uri);
91    Log.info(TAG, `delete table: ${table}`);
92
93    const processor = factory.getDatabaseProcessor(table);
94    if (processor !== null && processor !== undefined) {
95      processor.deleteByLowAuthority(rdbStore, uri, predicates, callback);
96      return;
97    } else {
98      Log.error(TAG, 'delete with invalid processor');
99    }
100  },
101
102  updateByHighAuthority(uri, value, predicates, callback) {
103    Log.info(TAG, `update uri: ${uri}`);
104    let table = getTableByUri(uri);
105    Log.info(TAG, `update table: ${table}`);
106
107    const processor = factory.getDatabaseProcessor(table);
108    if (processor !== null && processor !== undefined) {
109      processor.updateByHighAuthority(rdbStore, uri, value, predicates, callback);
110      return;
111    } else {
112      Log.error(TAG, 'update with invalid processor');
113    }
114  },
115
116  updateByLowAuthority(uri, value, predicates, callback) {
117    Log.info(TAG, `update uri: ${uri}`);
118    let table = getTableByUri(uri);
119    Log.info(TAG, `update table: ${table}`);
120
121    const processor = factory.getDatabaseProcessor(table);
122    if (processor !== null && processor !== undefined) {
123      processor.updateByLowAuthority(rdbStore, uri, value, predicates, callback);
124      return;
125    } else {
126      Log.error(TAG, 'update with invalid processor');
127    }
128  },
129
130
131  queryByHighAuthority(uri, columns, predicates, callback) {
132    Log.info(TAG, `query uri: ${uri}`);
133    const table = getTableByUri(uri);
134    Log.info(TAG, `query table: ${table}`);
135
136    const processor = factory.getDatabaseProcessor(table);
137    if (processor !== null && processor !== undefined) {
138      processor.queryByHighAuthority(rdbStore, uri, columns, predicates, callback);
139      return;
140    } else {
141      Log.error(TAG, 'query with invalid processor');
142    }
143  },
144
145  queryByLowAuthority(uri, columns, predicates, callback) {
146    Log.info(TAG, `query uri: ${uri}`);
147    const table = getTableByUri(uri);
148    Log.info(TAG, `query table: ${table}`);
149
150    const processor = factory.getDatabaseProcessor(table);
151    if (processor !== null && processor !== undefined) {
152      processor.queryByLowAuthority(rdbStore, uri, columns, predicates, callback);
153      return;
154    } else {
155      Log.error(TAG, 'query with invalid processor');
156    }
157  }
158}