1/* 2 * Copyright (C) 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 16import Extension from '@ohos.application.DataShareExtensionAbility' 17import rdb from '@ohos.data.rdb'; 18 19let DB_NAME = "book.db"; 20let TBL_NAME = "book"; 21let DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS " 22+ TBL_NAME 23+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)"; 24 25let rdbStore; 26 27function sleep(time) { 28 return new Promise(resolve => 29 setTimeout(resolve,time) 30 ) } async function output() { 31 let out = await sleep(1000); 32 console.log('1'); 33 return out; 34} 35 36export default class DataShareExtAbility extends Extension { 37 async onCreate(want, callback) { 38 console.log('[DataShareServer]: DataShareExtAbility onCreate, want:' + want.abilityName); 39 console.log('[DataShareServer]: DataShareExtAbility onCreate this.context.databaseDir:' + this.context.databaseDir); 40 // @ts-ignore 41 rdbStore = await rdb.getRdbStore(this.context, { name: DB_NAME }, 1); 42 console.log('[DataShareServer]: DataShareExtAbility getRdbStore done'); 43 await rdbStore.executeSql(DDL_TBL_CREATE, []); 44 console.log('[DataShareServer]: DataShareExtAbility executeSql done'); 45 let err = {"code":0}; 46 callback(err); 47 console.log('[DataShareServer]: DataShareExtAbility onCreate end'); 48 } 49 50 async getFileTypes(uri: string, mimeTypeFilter: string, callback) { 51 console.log('[DataShareServer]: [getFileTypes] enter'); 52 let ret = new Array("type01", "type00", "type03"); 53 console.log('[DataShareServer]: [getFileTypes] leave, ret:' + ret); 54 let err = {"code":0}; 55 await callback(err,ret); 56 return ret; 57 } 58 59 async openFile(uri: string, mode: string, callback) { 60 console.log('[DataShareServer]: [openFile] enter'); 61 let ret = 1; 62 let err = {"code":0}; 63 await callback(err,ret); 64 console.log('[DataShareServer]: [openFile] leave, ret:' + ret); 65 } 66 67 async insert(uri, value, callback) { 68 console.log('[DataShareServer]: [insert] enter'); 69 if (value == null) { 70 console.error('[DataShareServer]: [insert] invalid valueBuckets'); 71 return; 72 } 73 console.log('[DataShareServer]: [insert] value = ' + value); 74 console.log('[DataShareServer]: [insert] value = ' + JSON.stringify(value)); 75 await rdbStore.insert(TBL_NAME, value, function (err, ret) { 76 console.log('[DataShareServer]: <<Provider>> [insert] callback ret:' + ret); 77 if (callback != undefined) { 78 callback(err, ret); 79 } 80 }); 81 console.log('[DataShareServer]: [insert] leave'); 82 } 83 84 async update(uri, predicates, value, callback) { 85 console.log('[DataShareServer]: [update] enter'); 86 if (predicates == null || predicates == undefined) { 87 console.error('[DataShareServer]: <<Provider>> [update] invalid predicates'); 88 return; 89 } 90 console.log('[DataShareServer]: [update] values = ' + value); 91 console.log('[DataShareServer]: [update] values = ' + JSON.stringify(value)); 92 console.log('[DataShareServer]: [update] predicates = ' + predicates); 93 console.log('[DataShareServer]: [update] predicates = ' + JSON.stringify(predicates)); 94 try { 95 await rdbStore.update(TBL_NAME, value, predicates, function (err, ret) { 96 console.log('[DataShareServer]: [update] callback ret:' + ret); 97 console.log('[DataShareServer]: [update] callback err:' + err); 98 if (callback != undefined) { 99 callback(err, ret); 100 } 101 }); 102 } catch (err) { 103 console.error('[DataShareServer]: [update] error' + err); 104 } 105 sleep(1); 106 console.log('[DataShareServer]: [update] leave'); 107 } 108 109 async delete(uri, predicates, callback) { 110 console.log('[DataShareServer]: [delete] enter'); 111 if (predicates == null || predicates == undefined) { 112 console.error('[DataShareServer]: [delete] invalid predicates'); 113 return; 114 } 115 console.log('[DataShareServer]: [delete] predicates = ' + predicates); 116 console.log('[DataShareServer]: [delete] predicates = ' + JSON.stringify(predicates)); 117 try { 118 await rdbStore.delete(TBL_NAME, predicates, function (err, ret) { 119 console.log('[DataShareServer]: [delete] ret:' + ret); 120 if (callback != undefined) { 121 callback(err, ret); 122 } 123 }); 124 } catch (err) { 125 console.error('[DataShareServer]: [delete] error' + err); 126 } 127 console.log('[DataShareServer]: [delete] leave'); 128 } 129 130 async query(uri, predicates, columns, callback) { 131 console.log('[DataShareServer]: [query] enter'); 132 if (predicates == null || predicates == undefined) { 133 console.error('[DataShareServer]: [query] invalid predicates'); 134 } 135 console.log('[DataShareServer]: [query] values = ' + columns); 136 console.log('[DataShareServer]: [query] values = ' + JSON.stringify(columns)); 137 console.log('[DataShareServer]: [query] predicates = ' + predicates); 138 console.log('[DataShareServer]: [query] predicates = ' + JSON.stringify(predicates)); 139 try { 140 await rdbStore.query(TBL_NAME, predicates, columns, function (err, resultSet) { 141 console.log('[DataShareServer]: [query] ret: ' + resultSet); 142 if (resultSet != undefined) { 143 console.log('[DataShareServer]: [query] resultSet.rowCount: ' + resultSet.rowCount); 144 } 145 if (callback != undefined) { 146 callback(err, resultSet); 147 } 148 }); 149 } catch (err) { 150 console.error('[DataShareServer]: [query] error' + err); 151 } 152 153 console.log('[DataShareServer]: [query] leave'); 154 } 155 156 async getType(uri: string, callback) { 157 console.log('[DataShareServer]: [getType] enter'); 158 let ret = "image"; 159 console.log('[DataShareServer]: [getType] leave, ret:' + ret); 160 let err = {"code":0}; 161 await callback(err,ret); 162 return ret; 163 } 164 165 async batchInsert(uri: string, valueBuckets, callback) { 166 console.log('[DataShareServer]: [batchInsert] enter'); 167 if (valueBuckets == null || valueBuckets.length == undefined) { 168 console.error('[DataShareServer]: <<Provider>> [batchInsert] invalid valueBuckets'); 169 return; 170 } 171 console.log('[DataShareServer]: [batchInsert] valueBuckets.length:' + valueBuckets.length); 172 let resultNum = valueBuckets.length 173 await valueBuckets.forEach(vb => { 174 console.log('[DataShareServer]: [batchInsert] vb:' + JSON.stringify(vb)); 175 rdbStore.insert(TBL_NAME, vb, function (err, ret) { 176 console.log('[DataShareServer]: [batchInsert] callback ret:' + ret); 177 if (callback != undefined) { 178 callback(err, resultNum); 179 } 180 }); 181 }); 182 console.log('[DataShareServer]: [batchInsert] leave'); 183 } 184 185 async normalizeUri(uri: string, callback) { 186 console.log('[DataShareServer]: [normalizeUri] enter'); 187 let ret = uri; 188 let err = {"code":0}; 189 await callback(err, ret); 190 console.log('[DataShareServer]: [normalizeUri] leave, ret:' + ret); 191 } 192 193 async denormalizeUri(uri: string, callback) { 194 console.log('[DataShareServer]: [denormalizeUri] enter'); 195 let ret = uri; 196 let err = {"code":0}; 197 await callback(err, ret); 198 console.log('[DataShareServer]: [denormalizeUri] leave, ret:' + ret); 199 } 200};