• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 package ohos.devtools.datasources.databases.databasepool;
17 
18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 
22 import java.lang.reflect.Field;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * Data results help
30  *
31  * @since 2021/10/26
32  */
33 public class DataBaseRsHelp<T> {
34     private static final Logger LOGGER = LogManager.getLogger(DataBaseRsHelp.class);
35 
36     /**
37      * Get the database table name by SQL.
38      *
39      * @param instanceClass generic paradigm
40      * @param rs result set
41      * @return List <T> T
42      * @throws IllegalAccessException IllegalAccessException
43      * @throws InstantiationException InstantiationException
44      * @throws NoSuchFieldException NoSuchFieldException
45      */
util(T instanceClass, ResultSet rs)46     public List<T> util(T instanceClass, ResultSet rs)
47         throws IllegalAccessException, InstantiationException, NoSuchFieldException {
48         if (ProfilerLogManager.isInfoEnabled()) {
49             LOGGER.info("util");
50         }
51         List<T> list = new ArrayList<T>();
52         Class aClass = instanceClass.getClass();
53         Field[] fs = aClass.getDeclaredFields();
54         if (rs != null) {
55             while (true) {
56                 try {
57                     if (!rs.next()) {
58                         break;
59                     }
60                     T instance = (T) aClass.newInstance();
61                     for (int index = 0; index < fs.length; index++) {
62                         Field declaredField = instance.getClass().getDeclaredField(fs[index].getName());
63                         declaredField.setAccessible(true);
64                         if (declaredField.getType().getName().equals(String.class.getName())) {
65                             declaredField.set(instance, rs.getString(fs[index].getName()));
66                         } else if (declaredField.getType().getName().equals(int.class.getName())) {
67                             declaredField.set(instance, rs.getInt(fs[index].getName()));
68                         } else {
69                             continue;
70                         }
71                     }
72                     list.add(instance);
73                 } catch (SQLException throwAbles) {
74                     LOGGER.error(throwAbles.getMessage());
75                 }
76             }
77         }
78         // Return results
79         return list;
80     }
81 
82 }
83