已合并
业务开发-接口优化 #292
AtomGit-Bot创建于 2023年6月28日
业务开发-接口优化 #292
已合并
从refs/pull/292/head合入到master
共 4 个文件变更+52-34
Mplugins/data-studio/src/main/java/com/nctigba/datastudio/compatible/opengauss/DatabaseObjectSQLServiceImpl.java+1-1
| @@ -91,7 +91,7 @@ public class DatabaseObjectSQLServiceImpl implements DatabaseObjectSQLService { | |||
| 91 | 91 | ||
| 92 | public String databaseAttributeSQL(DatabaseNameDTO request) { | 92 | public String databaseAttributeSQL(DatabaseNameDTO request) { |
| 93 | log.info("databaseAttributeSQL request is: " + request); | 93 | log.info("databaseAttributeSQL request is: " + request); |
| 94 | - String ddl = String.format(DATABASE_ATTRIBUTE_SQL + request.getDatabaseName()); | 94 | + String ddl = String.format(DATABASE_ATTRIBUTE_SQL , request.getDatabaseName()); |
| 95 | log.info("databaseAttributeSQL DDL is: " + ddl); | 95 | log.info("databaseAttributeSQL DDL is: " + ddl); |
| 96 | return ddl; | 96 | return ddl; |
| 97 | } | 97 | } |
Mplugins/data-studio/src/main/java/com/nctigba/datastudio/compatible/opengauss/SequenceObjectSQLServiceImpl.java+4-3
| @@ -12,6 +12,7 @@ import com.nctigba.datastudio.model.dto.DatabaseSequenceDdlDTO; | |||
| 12 | import com.nctigba.datastudio.util.DebugUtils; | 12 | import com.nctigba.datastudio.util.DebugUtils; |
| 13 | import com.nctigba.datastudio.util.LocaleString; | 13 | import com.nctigba.datastudio.util.LocaleString; |
| 14 | import lombok.extern.slf4j.Slf4j; | 14 | import lombok.extern.slf4j.Slf4j; |
| 15 | +import org.apache.commons.lang3.StringUtils; | ||
| 15 | import org.opengauss.admin.common.exception.CustomException; | 16 | import org.opengauss.admin.common.exception.CustomException; |
| 16 | import org.springframework.beans.factory.annotation.Autowired; | 17 | import org.springframework.beans.factory.annotation.Autowired; |
| 17 | import org.springframework.stereotype.Service; | 18 | import org.springframework.stereotype.Service; |
| @@ -84,13 +85,13 @@ public class SequenceObjectSQLServiceImpl implements SequenceObjectSQLService { | |||
| 84 | if (request.getCycle().equals("CYCLE")) { | 85 | if (request.getCycle().equals("CYCLE")) { |
| 85 | ddl = ddl + LF + CYCLE_KEYWORD_SQL; | 86 | ddl = ddl + LF + CYCLE_KEYWORD_SQL; |
| 86 | } | 87 | } |
| 87 | - if (!Objects.nonNull(request.getTableSchema())) { | 88 | + if (StringUtils.isNotEmpty(request.getTableSchema())) { |
| 88 | ddl = ddl + LF + OWNED_KEYWORD_SQL + DebugUtils.containsSqlInjection(request.getTableSchema()) + POINT; | 89 | ddl = ddl + LF + OWNED_KEYWORD_SQL + DebugUtils.containsSqlInjection(request.getTableSchema()) + POINT; |
| 89 | } | 90 | } |
| 90 | - if (!Objects.nonNull(request.getTableName())) { | 91 | + if (StringUtils.isNotEmpty(request.getTableName())) { |
| 91 | ddl = ddl + DebugUtils.containsSqlInjection(request.getTableName()) + POINT; | 92 | ddl = ddl + DebugUtils.containsSqlInjection(request.getTableName()) + POINT; |
| 92 | } | 93 | } |
| 93 | - if (!Objects.nonNull(request.getTableColumn())) { | 94 | + if (StringUtils.isNotEmpty(request.getTableColumn())) { |
| 94 | ddl = ddl + DebugUtils.containsSqlInjection(request.getTableColumn()); | 95 | ddl = ddl + DebugUtils.containsSqlInjection(request.getTableColumn()); |
| 95 | } | 96 | } |
| 96 | log.info("splicingSequenceDDL response is: " + ddl); | 97 | log.info("splicingSequenceDDL response is: " + ddl); |
| @@ -351,7 +351,7 @@ public class SqlConstants { | |||
| 351 | public static final String QUERY_SCHEMA_SQL = "select pn.oid, pn.nspname, pr.rolname, pd.description from pg_namespace pn " + | 351 | public static final String QUERY_SCHEMA_SQL = "select pn.oid, pn.nspname, pr.rolname, pd.description from pg_namespace pn " + |
| 352 | "left join pg_description pd on pn.oid = pd.objoid left join pg_roles pr " + | 352 | "left join pg_description pd on pn.oid = pd.objoid left join pg_roles pr " + |
| 353 | "on pr.oid = pn.nspowner where pn.oid = %s;"; | 353 | "on pr.oid = pn.nspowner where pn.oid = %s;"; |
| 354 | - public static final String CREATE_SCHEMA_SQL = "create schema "; | 354 | + public static final String CREATE_SCHEMA_SQL = "create schema %s;"; |
| 355 | public static final String CREATE_SCHEMA_DDL_SQL = "create schema %s authorization %s;"; | 355 | public static final String CREATE_SCHEMA_DDL_SQL = "create schema %s authorization %s;"; |
| 356 | public static final String ALTER_SCHEMA_NAME_SQL = "alter schema %s rename to %s;"; | 356 | public static final String ALTER_SCHEMA_NAME_SQL = "alter schema %s rename to %s;"; |
| 357 | public static final String ALTER_SCHEMA_OWNER_SQL = "alter schema %s owner to %s;"; | 357 | public static final String ALTER_SCHEMA_OWNER_SQL = "alter schema %s owner to %s;"; |
Mplugins/data-studio/src/main/java/com/nctigba/datastudio/service/impl/sql/ExportServiceImpl.java+46-29
| @@ -50,6 +50,10 @@ import java.util.ArrayList; | |||
| 50 | import java.util.Date; | 50 | import java.util.Date; |
| 51 | import java.util.List; | 51 | import java.util.List; |
| 52 | import java.util.Map; | 52 | import java.util.Map; |
| 53 | +import java.util.concurrent.ExecutionException; | ||
| 54 | +import java.util.concurrent.ExecutorService; | ||
| 55 | +import java.util.concurrent.Executors; | ||
| 56 | +import java.util.concurrent.Future; | ||
| 53 | import java.util.stream.Collectors; | 57 | import java.util.stream.Collectors; |
| 54 | 58 | ||
| 55 | import static com.nctigba.datastudio.constants.CommonConstants.FUNCTION; | 59 | import static com.nctigba.datastudio.constants.CommonConstants.FUNCTION; |
| @@ -207,9 +211,7 @@ public class ExportServiceImpl implements ExportService { | |||
| 207 | ) { | 211 | ) { |
| 208 | StringBuilder columnSb = new StringBuilder(); | 212 | StringBuilder columnSb = new StringBuilder(); |
| 209 | List<String> columnList = request.getColumnList(); | 213 | List<String> columnList = request.getColumnList(); |
| 210 | - columnList.forEach(column -> { | 214 | + columnList.forEach(column -> columnSb.append(column).append(COMMA)); |
| 211 | - columnSb.append(column).append(COMMA); | ||
| 212 | - }); | ||
| 213 | log.info("ExportService exportExcel columnSb: " + columnSb); | 215 | log.info("ExportService exportExcel columnSb: " + columnSb); |
| 214 | 216 | ||
| 215 | String tableName = request.getTableName(); | 217 | String tableName = request.getTableName(); |
| @@ -297,36 +299,51 @@ public class ExportServiceImpl implements ExportService { | |||
| 297 | exportRequest.setViewList(viewList); | 299 | exportRequest.setViewList(viewList); |
| 298 | exportRequest.setSequenceList(sequenceList); | 300 | exportRequest.setSequenceList(sequenceList); |
| 299 | 301 | ||
| 300 | - sb.append(addHeader(false, schema, SCHEMA, schema)).append(CREATE_SCHEMA_SQL).append(schema) | 302 | + sb.append(addHeader(false, schema, SCHEMA, schema)).append(String.format(CREATE_SCHEMA_SQL, schema)); |
| 301 | - .append(getTableDdl(exportRequest)).append(getFunctionDdl(exportRequest)) | 303 | + sb.append(asyncExecute(exportRequest)); |
| 302 | - .append(getViewDdl(exportRequest)).append(getSequenceDdl(exportRequest)); | 304 | + |
| 303 | } | 305 | } |
| 304 | 306 | ||
| 305 | String fileName = getFileName(dataFlag, SCHEMA, schemaList); | 307 | String fileName = getFileName(dataFlag, SCHEMA, schemaList); |
| 306 | DebugUtils.exportFile(fileName, sb.toString(), response); | 308 | DebugUtils.exportFile(fileName, sb.toString(), response); |
| 307 | } | 309 | } |
| 308 | 310 | ||
| 311 | + private String asyncExecute(ExportRequest exportRequest) throws ExecutionException, InterruptedException { | ||
| 312 | + ExecutorService executorService = Executors.newFixedThreadPool(20); | ||
| 313 | + List<Future<String>> futureList = new ArrayList<>(); | ||
| 314 | + futureList.add(executorService.submit(() -> getTableDdl(exportRequest))); | ||
| 315 | + futureList.add(executorService.submit(() -> getFunctionDdl(exportRequest))); | ||
| 316 | + futureList.add(executorService.submit(() -> getViewDdl(exportRequest))); | ||
| 317 | + futureList.add(executorService.submit(() -> getSequenceDdl(exportRequest))); | ||
| 318 | + | ||
| 319 | + StringBuilder sb = new StringBuilder(); | ||
| 320 | + for (Future<String> future : futureList) { | ||
| 321 | + sb.append(future.get()); | ||
| 322 | + } | ||
| 323 | + return sb.toString(); | ||
| 324 | + } | ||
| 325 | + | ||
| 309 | private String getTableDdl(ExportRequest request) throws Exception { | 326 | private String getTableDdl(ExportRequest request) throws Exception { |
| 310 | log.info("ExportService getTableDdl request: " + request); | 327 | log.info("ExportService getTableDdl request: " + request); |
| 311 | StringBuilder sb = new StringBuilder(); | 328 | StringBuilder sb = new StringBuilder(); |
| 312 | String uuid = request.getUuid(); | 329 | String uuid = request.getUuid(); |
| 313 | String schema = request.getSchema(); | 330 | String schema = request.getSchema(); |
| 314 | 331 | ||
| 315 | - List<String> tableList = request.getTableList(); | 332 | + try ( |
| 316 | - for (String name : tableList) { | 333 | + Connection connection = connectionConfig.connectDatabase(request.getUuid()); |
| 317 | - SelectDataQuery dto = new SelectDataQuery(); | 334 | + Statement statement = connection.createStatement() |
| 318 | - dto.setUuid(uuid); | 335 | + ) { |
| 319 | - dto.setSchema(schema); | 336 | + List<String> tableList = request.getTableList(); |
| 320 | - dto.setTableName(name); | 337 | + for (String name : tableList) { |
| 338 | + SelectDataQuery dto = new SelectDataQuery(); | ||
| 339 | + dto.setUuid(uuid); | ||
| 340 | + dto.setSchema(schema); | ||
| 341 | + dto.setTableName(name); | ||
| 321 | 342 | ||
| 322 | - String ddl = tableDataService.tableDdl(dto).get(RESULT); | 343 | + String ddl = tableDataService.tableDdl(dto).get(RESULT); |
| 323 | - sb.append(addHeader(false, name, TABLE, schema)); | 344 | + sb.append(addHeader(false, name, TABLE, schema)); |
| 324 | - sb.append(ddl); | 345 | + sb.append(ddl); |
| 325 | 346 | ||
| 326 | - try ( | ||
| 327 | - Connection connection = connectionConfig.connectDatabase(request.getUuid()); | ||
| 328 | - Statement statement = connection.createStatement() | ||
| 329 | - ) { | ||
| 330 | if (request.isDataFlag()) { | 347 | if (request.isDataFlag()) { |
| 331 | String tableData = getTableData(statement, request.getSchema(), name); | 348 | String tableData = getTableData(statement, request.getSchema(), name); |
| 332 | if (StringUtils.isNotEmpty(tableData)) { | 349 | if (StringUtils.isNotEmpty(tableData)) { |
| @@ -345,17 +362,17 @@ public class ExportServiceImpl implements ExportService { | |||
| 345 | String uuid = request.getUuid(); | 362 | String uuid = request.getUuid(); |
| 346 | String schema = request.getSchema(); | 363 | String schema = request.getSchema(); |
| 347 | 364 | ||
| 348 | - List<Integer> functionMap = request.getFunctionMap(); | 365 | + try ( |
| 349 | - for (Integer oid : functionMap) { | 366 | + Connection connection = connectionConfig.connectDatabase(request.getUuid()); |
| 350 | - DatabaseFunctionSPDTO dto = new DatabaseFunctionSPDTO(); | 367 | + Statement statement = connection.createStatement() |
| 351 | - dto.setUuid(uuid); | 368 | + ) { |
| 352 | - dto.setSchema(schema); | 369 | + List<Integer> functionMap = request.getFunctionMap(); |
| 353 | - dto.setOid(oid.toString()); | 370 | + for (Integer oid : functionMap) { |
| 371 | + DatabaseFunctionSPDTO dto = new DatabaseFunctionSPDTO(); | ||
| 372 | + dto.setUuid(uuid); | ||
| 373 | + dto.setSchema(schema); | ||
| 374 | + dto.setOid(oid.toString()); | ||
| 354 | 375 | ||
| 355 | - try ( | ||
| 356 | - Connection connection = connectionConfig.connectDatabase(request.getUuid()); | ||
| 357 | - Statement statement = connection.createStatement() | ||
| 358 | - ) { | ||
| 359 | String name = Strings.EMPTY; | 376 | String name = Strings.EMPTY; |
| 360 | ResultSet nameResultSet = statement.executeQuery(String.format(PROC_SQL, oid)); | 377 | ResultSet nameResultSet = statement.executeQuery(String.format(PROC_SQL, oid)); |
| 361 | if (nameResultSet.next()) { | 378 | if (nameResultSet.next()) { |