• 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.util.Arrays;
23 import java.util.Map;
24 import java.util.Set;
25 
26 import static ohos.devtools.views.common.LayoutConstants.INDEX_FIVE;
27 import static ohos.devtools.views.common.LayoutConstants.NEGATIVE_ONE;
28 
29 /**
30  * Database table structure maintenance
31  *
32  * @since 2021/10/26
33  */
34 public class DataTableHelper {
35     private static final Logger LOGGER = LogManager.getLogger(DataTableHelper.class);
36 
DataTableHelper()37     private DataTableHelper() {
38     }
39 
40     /**
41      * Get the database table name by SQL.
42      *
43      * @param sql sql
44      * @return String
45      */
getTableNameBySql(String sql)46     public static String getTableNameBySql(String sql) {
47         if (ProfilerLogManager.isInfoEnabled()) {
48             LOGGER.info("getTableNameBySql");
49         }
50         int tableIndex = sql.indexOf("TABLE");
51         if (tableIndex != NEGATIVE_ONE) {
52             String tableName = sql.substring(tableIndex + INDEX_FIVE, sql.indexOf("("));
53             return tableName.trim();
54         }
55         return "";
56     }
57 
58     /**
59      * sql Placeholder
60      *
61      * @param size size
62      * @return String
63      */
sqlPlaceholder(int size)64     public static String sqlPlaceholder(int size) {
65         if (ProfilerLogManager.isInfoEnabled()) {
66             LOGGER.info("sqlPlaceholder");
67         }
68         StringBuffer stringBuffer = new StringBuffer();
69         for (int index = 0; index < size; index++) {
70             if (index == (size - 1)) {
71                 stringBuffer.append("?");
72             } else {
73                 stringBuffer.append("?,");
74             }
75         }
76         return stringBuffer.toString();
77     }
78 
79     /**
80      * Map to String
81      *
82      * @param map Map<String, Object>
83      * @return String
84      */
getDeleteCondition(Map<String, Object> map)85     public static String getDeleteCondition(Map<String, Object> map) {
86         if (ProfilerLogManager.isInfoEnabled()) {
87             LOGGER.info("getDeleteCondition");
88         }
89         Set<String> keySet = map.keySet();
90         // Convert set collection to array
91         String[] keyArray = keySet.toArray(new String[keySet.size()]);
92         // Sort the array (ascending order)
93         Arrays.sort(keyArray);
94         // Because String splicing efficiency will be very low, so switch to String Builder
95         StringBuilder stringBuffer = new StringBuilder();
96         for (int i = 0; i < keyArray.length; i++) {
97             // If the parameter value is empty, it does not participate in the signature.
98             // This method trim() removes spaces
99             if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
100                 stringBuffer.append(keyArray[i]);
101                 if (map.get(keyArray[i]) instanceof String) {
102                     stringBuffer.append(" = '").append(String.valueOf(map.get(keyArray[i])).trim()).append("'");
103                 } else {
104                     stringBuffer.append(" = `").append(String.valueOf(map.get(keyArray[i])).trim()).append("`");
105                 }
106             }
107             if (i != keyArray.length - 1) {
108                 stringBuffer.append(" and ");
109             }
110         }
111         return stringBuffer.toString();
112     }
113 
114     /**
115      * Map to String
116      *
117      * @param map Map<String, Object>
118      * @return String
119      */
mapToString(Map<String, Object> map)120     public static String mapToString(Map<String, Object> map) {
121         if (ProfilerLogManager.isInfoEnabled()) {
122             LOGGER.info("mapToString");
123         }
124         Set<String> keySet = map.keySet();
125         // Convert set collection to array
126         String[] keyArray = keySet.toArray(new String[keySet.size()]);
127         // Sort the array (ascending order)
128         Arrays.sort(keyArray);
129         StringBuilder stringBuffer = new StringBuilder();
130         for (int i = 0; i < keyArray.length; i++) {
131             // If the parameter value is empty, it does not participate in the signature.
132             // This method trim() removes spaces
133             if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
134                 stringBuffer.append(keyArray[i]).append(" = '").append(String.valueOf(map.get(keyArray[i])).trim())
135                     .append("'");
136             }
137             if (i != keyArray.length - 1) {
138                 stringBuffer.append("=");
139             }
140         }
141         return stringBuffer.toString();
142     }
143 }
144