• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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 type rdb from '@ohos.data.relationalStore'
17import DataShareResultSet from '@ohos.data.DataShareResultSet'
18import { logger } from '../utils/Logger'
19
20const TAG: string = 'Contact'
21
22export class Contact {
23  id: number
24  name: string
25  phone: string
26  email: string
27  address: string
28  remark: string
29}
30
31export function arrayToString(phones: string[]) {
32  let phoneResult = ''
33  if (phones.length === 1) {
34    phoneResult += phones[0]
35  } else {
36    for (let i = 0;i < phones.length; i++) {
37      phoneResult += phones[i]
38      if (i < phones.length - 1) {
39        phoneResult += ','
40      }
41    }
42  }
43  return phoneResult
44}
45
46export function getListFromResultSet(resultSet: rdb.ResultSet | DataShareResultSet) {
47  logger.info(TAG, 'getListFromResultSet')
48  let contacts: Array<Contact> = []
49  logger.info(TAG, `row count:${resultSet.rowCount}`)
50  resultSet.goToFirstRow()
51  for (let i = 0;i < resultSet.rowCount; i++) {
52    let contact: Contact = {
53      id: resultSet.getDouble(resultSet.getColumnIndex('id')),
54      name: resultSet.getString(resultSet.getColumnIndex('name')),
55      phone: resultSet.getString(resultSet.getColumnIndex('phone')),
56      email: resultSet.getString(resultSet.getColumnIndex('email')),
57      address: resultSet.getString(resultSet.getColumnIndex('address')),
58      remark: resultSet.getString(resultSet.getColumnIndex('remark'))
59    }
60    if (!contacts.includes(contact)) {
61      contacts.push(contact)
62    }
63    resultSet.goToNextRow()
64  }
65  resultSet.close()
66  contacts.sort((src1: Contact, src2: Contact) => {
67    return src1.name.localeCompare(src2.name, 'zh')
68  })
69  return contacts
70}