• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 { GiteeHelper } from './GiteeHelper'
17import { PRs } from './GiteeHelperData'
18
19const TABLE_NAME = "pr_table"
20export const CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
21  "id INTEGER PRIMARY KEY AUTOINCREMENT," +
22  "url TEXT," +
23  "number TEXT, " +
24  "created_at TEXT, " +
25  "updated_at TEXT, " +
26  "closed_at TEXT, " +
27  "merged_at TEXT, " +
28  "user_name TEXT, " +
29  "title TEXT, " +
30  "body TEXT);";
31export const DROP_TABLE = "DROP TABLE " + TABLE_NAME;
32
33let bg2fg: BlockingQueue<Promise<number>> = new BlockingQueue<Promise<number>>();
34
35export function callRDBWithNativeEACoroutine() {
36  console.log("callRDBWithNativeEACoroutine is not implemented yet")
37}
38
39function DtoS(x?: Date): string {
40  if (x != undefined) {
41    return x.toString()
42  }
43  return 'undefined'
44}
45
46function CreateDataQuery(prs: PRs): string {
47  let query: string = "INSERT INTO " + TABLE_NAME + " " +
48    "( url, number, created_at, updated_at, closed_at, merged_at, user_name, title, body ) VALUES "
49  try {
50  let values = prs.values!;
51  for (let idx = 0; idx < values.length; idx++) {
52    const pr = values[idx]
53    const user = pr.user!
54    query += "('" + pr.url + "', '" + pr.number + "', '" + DtoS(pr.created_at) + "',  '" + DtoS(pr.updated_at) + "',  '" +
55                   DtoS(pr.closed_at) + "',  '" + DtoS(pr.merged_at) + "',  '" + user.name + "',  '" + pr.title + "',  '" + pr.body + "')"
56    if (idx != values.length - 1) query += ", "
57  }
58  query += "; "
59  console.log("query: " + query)
60  } catch (e) {
61    console.log("Error: " + e)
62  }
63  return query
64}
65
66export function GetAll(cb: (ret: number) => void) {
67  let dataRdb = JSRuntime.loadModule('@ohos.data.relationalStore')
68  const query = "select * from " + TABLE_NAME;
69  const storeConfig = {} as JSValue;
70  JSRuntime.setPropertyString(storeConfig, 'name', TABLE_NAME);
71  JSRuntime.setPropertyJSValue(storeConfig, 'securityLevel', dataRdb.SecurityLevel.S1);
72  let ctx = GetCtx();
73
74  let gstore: JSValue = {} as JSValue;
75  let p: Promise<JSValue> = dataRdb.getRdbStore(ctx, storeConfig).then((store: JSValue) => {
76    store.querySql(query).then((res: JSValue) => {
77      res.goToFirstRow();
78      const rowCount = res.rowCount as number;
79      const columnCount = res.columnCount as number;
80      console.log(`inserted rows count: ${rowCount}`)
81      cb(rowCount);
82    })
83  }).catch((e: JSValue) => {
84    console.log(`GetAll QuerySql catch ${e}`)
85  });
86}
87
88
89function GetCtx(): JSValue {
90  let ctx: JSValue = {} as JSValue;
91
92  JSRuntime.setPropertyBoolean(ctx, 'stageMode', true)
93  JSRuntime.setPropertyString(ctx, 'databaseDir', '/data/storage/el2/database/entry')
94  JSRuntime.setPropertyDouble(ctx, 'area', 1)
95  const currentHapModuleInfo = {} as JSValue;
96  JSRuntime.setPropertyString(currentHapModuleInfo, 'name', 'entry');
97  JSRuntime.setPropertyJSValue(ctx, 'currentHapModuleInfo', currentHapModuleInfo)
98  const applicationInfo = {} as JSValue;
99  JSRuntime.setPropertyString(applicationInfo, 'name', 'com.arkoala.app');
100  JSRuntime.setPropertyBoolean(applicationInfo, 'systemApp', false);
101  JSRuntime.setPropertyJSValue(ctx, 'applicationInfo', applicationInfo)
102  return ctx
103}
104
105function DoFetchAndPush(prs: PRs): Promise<number> {
106  let hilog = JSRuntime.loadModule('@ohos.hilog');
107  let dataRdb = JSRuntime.loadModule('@ohos.data.relationalStore')
108
109  console.log("DoFetchAndPush START");
110  hilog.error(0x0000, "koalaShopping", "%{public}s", "I AM INTEROP FROM EACOROUTINE");
111
112  const storeConfig = {} as JSValue;
113  JSRuntime.setPropertyString(storeConfig, 'name', TABLE_NAME);
114  JSRuntime.setPropertyJSValue(storeConfig, 'securityLevel', dataRdb.SecurityLevel.S1);
115  let ctx = GetCtx();
116
117  console.log("DoFetchAndPush before getRdbStore");
118  let RDBstore: JSValue = {};
119  let promise: Promise<JSValue> = dataRdb.getRdbStore(ctx, storeConfig).then((store: JSValue) => {
120    RDBstore = store
121    return {} as JSValue;
122  }).then((x: JSValue)=>{
123    let promise: JSValue = {} as JSValue;
124    promise = RDBstore.executeSql(CREATE_TABLE);
125    return promise;
126  }).catch((e: JSValue) => {
127    console.log(`DoFetchAndPush CATCH 1 ${e}`)
128  }).then((x: JSValue)=>{
129    let promise: JSValue = {} as JSValue;
130    const query = CreateDataQuery(prs);
131    promise = RDBstore.executeSql(query);
132    return promise;
133  }).catch((e: JSValue) => {
134    console.log(`DoFetchAndPush CATCH 2 ${e}`)
135  });
136
137  console.log("DoFetchAndPush END ");
138  return promise;
139}
140
141export function InsertInDBFromParam(prs: PRs): int
142{
143  console.log("InsertInDBFromParam START");
144  bg2fg.push(DoFetchAndPush(prs));
145  console.log("InsertInDBFromParam END");
146  return 42;
147}
148
149let EAW: EAWorker;
150let isEAworkerInitialized = false
151export function callRDBWithManagedEACoroutine(cb: () => void) {
152  if (!isEAworkerInitialized) {
153    isEAworkerInitialized = true;
154    EAW = new EAWorker(true)
155  }
156  console.log("callRDBWithManagedEACoroutine start")
157  GiteeHelper.FetchMockData(0, 200, (prs: PRs) => {
158    EAW.run(() => {
159      return InsertInDBFromParam(prs);
160    });
161  })
162  console.log("callRDBWithManagedEACoroutine end")
163
164  let p = bg2fg.pop();
165  p.then((x: JSValue): number => {
166    console.log("callRDBWithManagedEACoroutine p.then before CB call");
167    cb();
168    console.log("callRDBWithManagedEACoroutine p.then after CB call");
169    return 322;
170  });
171  return p;
172}
173