Search in sources :

Example 1 with SQLExecuteResult

use of com.varsql.core.sql.executor.SQLExecuteResult in project varsql by varsqlinfo.

the class FileImportExportServiceImpl method jsonImport.

private SQLExecuteResult jsonImport(FileInfoEntity fileInfo, String conuid) throws SQLException {
    SqlStatementInfo ssi = new SqlStatementInfo();
    ssi.setConuid(conuid);
    ssi.setExportType(VarsqlFileType.JSON.name());
    ssi.addCustom(FileImportExecutor.IMPORT_FILE_PARAM_NAME, FileServiceUtils.getPath(fileInfo.getFilePath()).toUri());
    SQLExecuteResult ser = new FileImportExecutor().execute(ssi);
    return ser;
}
Also used : SqlStatementInfo(com.varsql.core.db.valueobject.SqlStatementInfo) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) FileImportExecutor(com.varsql.core.sql.executor.FileImportExecutor)

Example 2 with SQLExecuteResult

use of com.varsql.core.sql.executor.SQLExecuteResult in project varsql by varsqlinfo.

the class FileImportExportServiceImpl method importFile.

/**
 * @Method Name  : importFile
 * @Method 설명 : import file
 * @작성자   : ytkim
 * @작성일   : 2020. 11. 22.
 * @변경이력  :
 */
public ResponseResult importFile(FileImportInfo fii) {
    String conuid = fii.getConuid();
    String importType = fii.getImportType();
    List<FileInfoEntity> importFileList = fileInfoEntityRepository.findAllById(Arrays.asList(CommUtils.getFileIds(fii.getFileIds())));
    ResponseResult result = new ResponseResult();
    List<FileImportResult> resultInfo = new ArrayList<>();
    importFileList.forEach(fileInfo -> {
        FileImportResult fileImportResult = new FileImportResult();
        try {
            fileImportResult.setFileName(fileInfo.getFileName());
            SQLExecuteResult ser = null;
            if ("sql".equals(importType)) {
                ser = sqlImport(fileInfo, conuid);
            } else if ("xml".equals(importType)) {
                ser = xmlImport(fileInfo, conuid);
            } else if ("json".equals(importType)) {
                ser = jsonImport(fileInfo, conuid);
            } else if ("csv".equals(importType)) {
                ser = csvImport(fileInfo, conuid);
            }
            if (ser.getResultCode() != null) {
                fileImportResult.setResultCode(ser.getResultCode());
            }
            fileImportResult.setResultCount(ser.getExecuteCount());
            fileImportResult.setMessage(ser.getMessage());
        } catch (IOException | SQLException e) {
            fileImportResult.setResultCode(VarsqlAppCode.ERROR);
            fileImportResult.setMessage(e.getMessage());
            logger.error("file import error importType : {} , conuid : {} , fileInfo :{} ", importType, fileInfo);
            logger.error("error message : {}", e.getMessage(), e);
        }
        resultInfo.add(fileImportResult);
    });
    result.setItemList(resultInfo);
    return result;
}
Also used : SQLException(java.sql.SQLException) ResponseResult(com.vartech.common.app.beans.ResponseResult) ArrayList(java.util.ArrayList) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) FileImportResult(com.varsql.web.dto.file.FileImportResult) IOException(java.io.IOException)

Example 3 with SQLExecuteResult

use of com.varsql.core.sql.executor.SQLExecuteResult in project varsql by varsqlinfo.

the class FileImportExportServiceImpl method sqlImport.

/**
 * @method  : sqlImport
 * @desc : sql import
 * @author   : ytkim
 * @date   : 2021. 5. 23.
 * @param fileInfo
 * @param conuid
 * @return
 * @throws IOException
 * @throws SQLException
 */
private SQLExecuteResult sqlImport(FileInfoEntity fileInfo, String conuid) throws IOException, SQLException {
    SqlStatementInfo ssi = new SqlStatementInfo();
    ssi.setConuid(conuid);
    ssi.setSqlParam("{}");
    String insertQuery = FileUtils.readFileToString(FileServiceUtils.getFileInfoToFile(fileInfo));
    ssi.setSql(insertQuery);
    SQLExecuteResult ser = new SqlUpdateExecutor().execute(ssi, new AbstractSQLExecutorHandler() {

        @Override
        public boolean handle(SQLHandlerParameter handleParam) {
            return true;
        }
    });
    return ser;
}
Also used : SqlStatementInfo(com.varsql.core.db.valueobject.SqlStatementInfo) AbstractSQLExecutorHandler(com.varsql.core.sql.executor.handler.AbstractSQLExecutorHandler) SQLHandlerParameter(com.varsql.core.sql.executor.handler.SQLHandlerParameter) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) SqlUpdateExecutor(com.varsql.core.sql.executor.SqlUpdateExecutor)

Example 4 with SQLExecuteResult

use of com.varsql.core.sql.executor.SQLExecuteResult in project varsql by varsqlinfo.

the class FileImportExportServiceImpl method xmlImport.

private SQLExecuteResult xmlImport(FileInfoEntity fileInfo, String conuid) throws SQLException {
    SqlStatementInfo ssi = new SqlStatementInfo();
    ssi.setConuid(conuid);
    ssi.setExportType(VarsqlFileType.XML.name());
    ssi.addCustom(FileImportExecutor.IMPORT_FILE_PARAM_NAME, FileServiceUtils.getPath(fileInfo.getFilePath()).toUri());
    SQLExecuteResult ser = new FileImportExecutor().execute(ssi);
    return ser;
}
Also used : SqlStatementInfo(com.varsql.core.db.valueobject.SqlStatementInfo) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) FileImportExecutor(com.varsql.core.sql.executor.FileImportExecutor)

Example 5 with SQLExecuteResult

use of com.varsql.core.sql.executor.SQLExecuteResult in project varsql by varsqlinfo.

the class SQLServiceImpl method dataExport.

/**
 * 데이터 내보내기.
 * @param paramMap
 * @param req
 */
@SuppressWarnings("rawtypes")
public void dataExport(ParamMap paramMap, SqlExecuteDTO sqlExecuteInfo, HttpServletRequest req, HttpServletResponse res) throws Exception {
    String objectName = sqlExecuteInfo.getObjectName();
    if (!sqlExecuteInfo.getBaseSchema().equals(sqlExecuteInfo.getSchema())) {
        objectName = sqlExecuteInfo.getSchema() + "." + objectName;
    }
    StringBuilder reqQuerySb = new StringBuilder().append("select * from ").append(objectName).append(" where 1=1 ");
    String conditionQuery = paramMap.getString("conditionQuery");
    if (!StringUtils.isBlank(conditionQuery)) {
        conditionQuery = StringUtils.lTrim(conditionQuery);
        if (conditionQuery.startsWith("where")) {
            conditionQuery = conditionQuery.replaceFirst("where", "");
        }
        if (conditionQuery.startsWith("and")) {
            conditionQuery = conditionQuery.replaceFirst("and", "");
        }
        conditionQuery = " and " + conditionQuery;
        reqQuerySb.append(conditionQuery);
    }
    sqlExecuteInfo.setSqlParam("{}");
    sqlExecuteInfo.setSql(reqQuerySb.toString());
    String exportCharset = paramMap.getString("charset", VarsqlConstants.CHAR_SET);
    OutputStream outstream = null;
    AbstractWriter writer = null;
    try {
        VarsqlFileType exportType = sqlExecuteInfo.getExportType();
        Path fileExportPath = FileServiceUtils.getSavePath(UploadFileType.EXPORT);
        String downloadFilePath = FileUtils.pathConcat(fileExportPath.toAbsolutePath().toString(), exportType.concatExtension(VartechUtils.generateUUID()));
        outstream = new FileOutputStream(downloadFilePath);
        if (VarsqlFileType.CSV.equals(exportType)) {
            writer = new CSVWriter(outstream, ',', exportCharset);
        } else if (VarsqlFileType.JSON.equals(exportType)) {
            writer = new JSONWriter(outstream, "row", exportCharset);
        } else if (VarsqlFileType.XML.equals(exportType)) {
            writer = new XMLWriter(outstream, "row", exportCharset);
        } else if (VarsqlFileType.EXCEL.equals(exportType)) {
            writer = new ExcelWriter(outstream);
        } else {
            writer = new SQLWriter(outstream, DBType.getDBType(sqlExecuteInfo.getDbType()), objectName, exportCharset);
        }
        logger.debug("data export downloadFilePath :{} , query : {}", downloadFilePath, sqlExecuteInfo.getSql());
        final String tableName = objectName;
        SQLExecuteResult ser = new SelectExecutor().execute(sqlExecuteInfo, new AbstractSQLExecutorHandler(writer) {

            private boolean firstFlag = true;

            @Override
            public boolean handle(SQLHandlerParameter handleParam) {
                if (firstFlag) {
                    WriteMetadataInfo whi = new WriteMetadataInfo("exportInfo");
                    List<ExportColumnInfo> columns = new LinkedList<ExportColumnInfo>();
                    handleParam.getColumnInfoList().forEach(item -> {
                        ExportColumnInfo gci = new ExportColumnInfo();
                        gci.setName(item.getLabel());
                        gci.setAlias(item.getKey());
                        gci.setType(item.getDbType());
                        gci.setNumber(item.isNumber());
                        gci.setLob(item.isLob());
                        columns.add(gci);
                    });
                    whi.addMetedata("tableName", tableName);
                    whi.addMetedata("columns", columns);
                    getWriter().setMetadata(whi);
                    if (VarsqlFileType.SQL.equals(exportType)) {
                        ((SQLWriter) getWriter()).setColumnInfos(handleParam.getColumnInfoList());
                    }
                    firstFlag = false;
                }
                try {
                    getWriter().addRow(handleParam.getRowObject());
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                    return false;
                }
                return true;
            }
        });
        writer.writeAndClose();
        if (ser.getResultCode() != null) {
            throw new DataDownloadException(ser.getResultCode(), ser.getMessage(), new VarsqlAppException(ser.getMessage()));
        }
        String exportFileName = ValidateUtils.getValidFileName(paramMap.getString("fileName", objectName));
        VarsqlUtils.setResponseDownAttr(res, req, exportType.concatExtension(exportFileName));
        try (FileInputStream fileInputStream = new FileInputStream(new File(downloadFilePath));
            OutputStream downloadStream = res.getOutputStream()) {
            byte[] buf = new byte[8192];
            int read = 0;
            while ((read = fileInputStream.read(buf)) != -1) {
                downloadStream.write(buf, 0, read);
            }
            downloadStream.flush();
            downloadStream.close();
            fileInputStream.close();
        }
    } catch (Exception e) {
        throw e;
    } finally {
        IOUtils.close(writer);
        IOUtils.close(outstream);
    }
}
Also used : JSONWriter(com.vartech.common.io.writer.JSONWriter) SelectExecutor(com.varsql.core.sql.executor.SelectExecutor) ExportColumnInfo(com.varsql.core.sql.beans.ExportColumnInfo) SqlGridDownloadInfo(com.varsql.web.dto.sql.SqlGridDownloadInfo) Connection(java.sql.Connection) SqlHistoryEntity(com.varsql.web.model.entity.sql.SqlHistoryEntity) WriteMetadataInfo(com.vartech.common.io.writer.WriteMetadataInfo) SqlExecuteManager(com.varsql.core.sql.SqlExecuteManager) SqlSource(com.varsql.core.sql.builder.SqlSource) LoggerFactory(org.slf4j.LoggerFactory) VarsqlAppException(com.varsql.web.exception.VarsqlAppException) Autowired(org.springframework.beans.factory.annotation.Autowired) VarsqlUtils(com.varsql.web.util.VarsqlUtils) ParameterMode(com.varsql.core.sql.mapping.ParameterMode) ParamMap(com.vartech.common.app.beans.ParamMap) DatabaseInfo(com.varsql.core.db.valueobject.DatabaseInfo) ResultSet(java.sql.ResultSet) Map(java.util.Map) ConnectionFactory(com.varsql.core.connection.ConnectionFactory) JSONWriter(com.vartech.common.io.writer.JSONWriter) XMLWriter(com.vartech.common.io.writer.XMLWriter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Path(java.nio.file.Path) SQLResultSetUtils(com.varsql.core.sql.util.SQLResultSetUtils) VarsqlFileType(com.varsql.core.common.code.VarsqlFileType) PreparedStatement(java.sql.PreparedStatement) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) CSVWriter(com.vartech.common.io.writer.CSVWriter) List(java.util.List) SqlSourceBuilder(com.varsql.core.sql.builder.SqlSourceBuilder) CallableStatement(java.sql.CallableStatement) SqlStatisticsEntity(com.varsql.web.model.entity.sql.SqlStatisticsEntity) VarsqlFormatterUtil(com.varsql.core.sql.format.VarsqlFormatterUtil) ConvertUtils(com.varsql.web.util.ConvertUtils) ExcelWriter(com.vartech.common.io.writer.ExcelWriter) IOUtils(com.vartech.common.utils.IOUtils) SQLWriter(com.varsql.core.data.writer.SQLWriter) ValidateUtils(com.varsql.web.util.ValidateUtils) ResultSetConvertException(com.varsql.core.exception.ResultSetConvertException) JdbcUtils(com.varsql.core.sql.util.JdbcUtils) CommonServiceImpl(com.varsql.web.common.service.CommonServiceImpl) HashMap(java.util.HashMap) ResponseResult(com.vartech.common.app.beans.ResponseResult) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) FileUtils(com.vartech.common.utils.FileUtils) ArrayList(java.util.ArrayList) SecurityUtil(com.varsql.core.common.util.SecurityUtil) LinkedHashMap(java.util.LinkedHashMap) SQLDataType(com.varsql.core.sql.type.SQLDataType) GridColumnInfo(com.varsql.core.sql.beans.GridColumnInfo) VartechUtils(com.vartech.common.utils.VartechUtils) SQLException(java.sql.SQLException) HttpServletRequest(javax.servlet.http.HttpServletRequest) UploadFileType(com.varsql.web.constants.UploadFileType) AbstractWriter(com.vartech.common.io.writer.AbstractWriter) Service(org.springframework.stereotype.Service) SqlSourceResultVO(com.varsql.core.sql.builder.SqlSourceResultVO) JsonToken(com.fasterxml.jackson.core.JsonToken) LinkedList(java.util.LinkedList) DateUtils(com.vartech.common.utils.DateUtils) OutputStream(java.io.OutputStream) SQLParamUtils(com.varsql.core.sql.util.SQLParamUtils) Logger(org.slf4j.Logger) DataDownloadException(com.varsql.web.exception.DataDownloadException) JsonParser(com.fasterxml.jackson.core.JsonParser) GridUtils(com.varsql.core.common.util.GridUtils) FileServiceUtils(com.varsql.web.util.FileServiceUtils) ParameterMapping(com.varsql.core.sql.mapping.ParameterMapping) StringUtils(com.vartech.common.utils.StringUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) VarsqlConstants(com.varsql.core.common.constants.VarsqlConstants) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SqlLogInfoDTO(com.varsql.web.dto.sql.SqlLogInfoDTO) File(java.io.File) SqlDataConstants(com.varsql.core.common.constants.SqlDataConstants) JsonFactory(com.fasterxml.jackson.core.JsonFactory) EscapeType(com.vartech.common.utils.StringUtils.EscapeType) VarsqlCommandType(com.varsql.core.sql.builder.VarsqlCommandType) SQLHandlerParameter(com.varsql.core.sql.executor.handler.SQLHandlerParameter) Statement(java.sql.Statement) DBType(com.varsql.core.db.DBType) SqlExecuteDTO(com.varsql.web.dto.sql.SqlExecuteDTO) VarsqlAppCode(com.varsql.core.common.code.VarsqlAppCode) VarsqlStatementType(com.varsql.core.sql.builder.VarsqlStatementType) SelectExecutor(com.varsql.core.sql.executor.SelectExecutor) AbstractSQLExecutorHandler(com.varsql.core.sql.executor.handler.AbstractSQLExecutorHandler) DataDownloadException(com.varsql.web.exception.DataDownloadException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ExcelWriter(com.vartech.common.io.writer.ExcelWriter) SQLHandlerParameter(com.varsql.core.sql.executor.handler.SQLHandlerParameter) WriteMetadataInfo(com.vartech.common.io.writer.WriteMetadataInfo) CSVWriter(com.vartech.common.io.writer.CSVWriter) AbstractWriter(com.vartech.common.io.writer.AbstractWriter) XMLWriter(com.vartech.common.io.writer.XMLWriter) VarsqlFileType(com.varsql.core.common.code.VarsqlFileType) AbstractSQLExecutorHandler(com.varsql.core.sql.executor.handler.AbstractSQLExecutorHandler) ExportColumnInfo(com.varsql.core.sql.beans.ExportColumnInfo) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) VarsqlAppException(com.varsql.web.exception.VarsqlAppException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) VarsqlAppException(com.varsql.web.exception.VarsqlAppException) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) ResultSetConvertException(com.varsql.core.exception.ResultSetConvertException) SQLException(java.sql.SQLException) DataDownloadException(com.varsql.web.exception.DataDownloadException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) SQLWriter(com.varsql.core.data.writer.SQLWriter) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) File(java.io.File)

Aggregations

SQLExecuteResult (com.varsql.core.sql.executor.SQLExecuteResult)6 SqlStatementInfo (com.varsql.core.db.valueobject.SqlStatementInfo)3 AbstractSQLExecutorHandler (com.varsql.core.sql.executor.handler.AbstractSQLExecutorHandler)3 SQLHandlerParameter (com.varsql.core.sql.executor.handler.SQLHandlerParameter)3 ResponseResult (com.vartech.common.app.beans.ResponseResult)3 IOException (java.io.IOException)3 VarsqlFileType (com.varsql.core.common.code.VarsqlFileType)2 VarsqlConstants (com.varsql.core.common.constants.VarsqlConstants)2 SecurityUtil (com.varsql.core.common.util.SecurityUtil)2 SQLWriter (com.varsql.core.data.writer.SQLWriter)2 DBType (com.varsql.core.db.DBType)2 DatabaseInfo (com.varsql.core.db.valueobject.DatabaseInfo)2 ExportColumnInfo (com.varsql.core.sql.beans.ExportColumnInfo)2 SelectExecutor (com.varsql.core.sql.executor.SelectExecutor)2 UploadFileType (com.varsql.web.constants.UploadFileType)2 SqlExecuteDTO (com.varsql.web.dto.sql.SqlExecuteDTO)2 FileServiceUtils (com.varsql.web.util.FileServiceUtils)2 ValidateUtils (com.varsql.web.util.ValidateUtils)2 VarsqlUtils (com.varsql.web.util.VarsqlUtils)2 AbstractWriter (com.vartech.common.io.writer.AbstractWriter)2