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 16importScripts('sql-wasm.js'); 17// @ts-ignore 18import { temp_init_sql_list } from './TempSql'; 19import { execProtoForWorker } from './data-trafic/utils/ExecProtoForWorker'; 20import { TraficEnum } from './data-trafic/utils/QueryEnum'; 21 22let conn: unknown = null; 23 24self.onerror = function (error): void { }; 25 26self.onmessage = async (e: unknown): Promise<void> => { 27 //@ts-ignore 28 const action = e.data.action; 29 //@ts-ignore 30 const id = e.data.id; 31 if (action === 'open') { 32 //@ts-ignore 33 let array = new Uint8Array(e.data.buffer); 34 // @ts-ignore 35 initSqlJs({ locateFile: (filename) => `${filename}` }).then((SQL: unknown) => { 36 // @ts-ignore 37 conn = new SQL.Database(array); 38 self.postMessage({ id: id, ready: true, index: 0 }); 39 // @ts-ignore 40 if (temp_init_sql_list && temp_init_sql_list.length > 0) { 41 // @ts-ignore 42 temp_init_sql_list.forEach((item, index) => { 43 // @ts-ignore 44 let r = conn.exec(item); 45 self.postMessage({ 46 id: id, 47 ready: true, 48 index: index + 1, 49 }); 50 }); 51 } 52 self.postMessage({ id: id, init: true }); 53 }); 54 } else if (action === 'close') { 55 } else if (action === 'exec' || action === 'exec-buf' || action === 'exec-metric') { 56 try { 57 //@ts-ignore 58 let sql = e.data.sql; 59 //@ts-ignore 60 let params = e.data.params; 61 // @ts-ignore 62 const stmt = conn.prepare(sql); 63 stmt.bind(params); 64 let res = []; 65 while (stmt.step()) { 66 //@ts-ignore 67 res.push(stmt.getAsObject()); 68 } 69 stmt.free(); 70 // @ts-ignore 71 self.postMessage({ id: id, results: res }); 72 } catch (err) { 73 self.postMessage({ 74 id: id, 75 results: [], 76 //@ts-ignore 77 error: err.message, 78 }); 79 } 80 } else if (action === 'exec-proto') { 81 //@ts-ignore 82 e.data.params.trafic = TraficEnum.Memory; 83 //@ts-ignore 84 execProtoForWorker(e.data, (sql: string) => { 85 try { 86 // @ts-ignore 87 const stmt = conn.prepare(sql); 88 let res = []; 89 while (stmt.step()) { 90 //@ts-ignore 91 res.push(stmt.getAsObject()); 92 } 93 stmt.free(); 94 return res; 95 } catch (err: unknown) { 96 console.log(err); 97 return []; 98 } 99 }); 100 } 101}; 102