• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {WebSocketManager} from '../../webSocket/WebSocketManager';
19
20importScripts('sql-wasm.js');
21// @ts-ignore
22import { temp_init_sql_list } from './TempSql';
23import { execProtoForWorker } from './data-trafic/utils/ExecProtoForWorker';
24import { TraficEnum } from './data-trafic/utils/QueryEnum';
25
26let conn: unknown = null;
27let enc = new TextEncoder();
28let dec = new TextDecoder();
29const REQ_BUF_SIZE = 4 * 1024 * 1024;
30let uploadSoActionId: string = '';
31const failedArray: Array<string> = [];
32self.onerror = function (error): void { };
33
34self.onmessage = async (e: unknown): Promise<void> => {
35  //@ts-ignore
36  const action = e.data.action;
37  //@ts-ignore
38  const id = e.data.id;
39  if (action === 'open') {
40    //@ts-ignore
41    let array = new Uint8Array(e.data.buffer);
42    // @ts-ignore
43    initSqlJs({ locateFile: (filename) => `${filename}` }).then((SQL: unknown) => {
44      // @ts-ignore
45      conn = new SQL.Database(array);
46      self.postMessage({ id: id, ready: true, index: 0 });
47      // @ts-ignore
48      if (temp_init_sql_list && temp_init_sql_list.length > 0) {
49        // @ts-ignore
50        temp_init_sql_list.forEach((item, index) => {
51          // @ts-ignore
52          let r = conn.exec(item);
53          self.postMessage({
54            id: id,
55            ready: true,
56            index: index + 1,
57          });
58        });
59      }
60      self.postMessage({ id: id, init: true });
61    });
62  } else if (action === 'close') {
63  } else if (action === 'exec' || action === 'exec-buf' || action === 'exec-metric') {
64    try {
65      //@ts-ignore
66      let sql = e.data.sql;
67      //@ts-ignore
68      let params = e.data.params;
69      // @ts-ignore
70      const stmt = conn.prepare(sql);
71      stmt.bind(params);
72      let res = [];
73      while (stmt.step()) {
74        //@ts-ignore
75        res.push(stmt.getAsObject());
76      }
77      stmt.free();
78      // @ts-ignore
79      self.postMessage({ id: id, results: res });
80    } catch (err) {
81      self.postMessage({
82        id: id,
83        results: [],
84        //@ts-ignore
85        error: err.message,
86      });
87    }
88  } else if (action === 'exec-proto') {
89    //@ts-ignore
90    e.data.params.trafic = TraficEnum.Memory;
91    //@ts-ignore
92    execProtoForWorker(e.data, (sql: string) => {
93      try {
94        // @ts-ignore
95        const stmt = conn.prepare(sql);
96        let res = [];
97        while (stmt.step()) {
98          //@ts-ignore
99          res.push(stmt.getAsObject());
100        }
101        stmt.free();
102        return res;
103      } catch (err: unknown) {
104        console.log(err);
105        return [];
106      }
107    });
108  } else if (action === 'upload-so') {
109    onmessageByUploadSoAction(e);
110  }
111};
112
113function onmessageByUploadSoAction(e: unknown): void {
114  // @ts-ignore
115  uploadSoActionId = e.data.id;
116  // @ts-ignore
117  const result = 'ok';
118  self.postMessage({
119    id: uploadSoActionId,
120    action: 'upload-so',
121    results: { result: result, failedArray: failedArray },
122  });
123}