use of com.varsql.core.db.valueobject.DatabaseInfo in project varsql by varsqlinfo.
the class BaseExecutorTest method testSelect.
@Test
// @Ignore
void testSelect() {
ConnectionInfo connInfo = getBaseConnection();
try {
DatabaseInfo di = getDatabaseInfo(connInfo);
SqlStatementInfo sqi = new SqlStatementInfo();
sqi.setSchema("PUBLIC");
sqi.setConuid(connInfo.getConnid(), TEST_CON_UID, di);
SQLExecutor baseExecutor = new SelectExecutor();
sqi.setSqlParam("{}");
sqi.setSql("select * from test2222");
SQLWriter writer = new SQLWriter(new FileOutputStream(new File("c:/zzz/exportInsertQuery.sql")), DBType.getDBType(sqi.getDbType()), "test2222");
AbstractSQLExecutorHandler handler = new AbstractSQLExecutorHandler() {
private boolean firstFlag = true;
@Override
public boolean handle(SQLHandlerParameter handleParam) {
if (firstFlag) {
writer.setColumnInfos(handleParam.getColumnInfoList());
firstFlag = false;
}
try {
writer.addRow(handleParam.getRowObject());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
};
SQLExecuteResult ser = baseExecutor.execute(sqi, handler);
writer.write();
System.out.println("testUpdate ser.getTotalCount(): " + ser.getTotalCount());
System.out.println("testSelect ser.getResult(): " + ser.getResult());
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.varsql.core.db.valueobject.DatabaseInfo in project varsql by varsqlinfo.
the class ExportServiceImpl method downloadTableData2.
public ResponseResult downloadTableData2(PreferencesRequestDTO preferencesInfo, HttpServletRequest req, HttpServletResponse res) throws Exception {
DataDownloadInfo ddi = VartechUtils.jsonStringToObject(preferencesInfo.getPrefVal(), DataDownloadInfo.class, true);
DatabaseInfo databaseInfo = SecurityUtil.loginInfo().getDatabaseInfo().get(ddi.getConuid());
logger.debug("downloadTableData : {}", preferencesInfo.getPrefVal());
String prefixSchema = StringUtils.isBlank(ddi.getSchema()) ? "" : ddi.getSchema() + ".";
String charset = StringUtils.isBlank(ddi.getCharset()) ? VarsqlConstants.CHAR_SET : ddi.getCharset();
String exportFileName = ValidateUtils.getValidFileName(ddi.getFileName());
String fileId = VartechUtils.generateUUID();
Path fileExportPath = FileServiceUtils.getSavePath(UploadFileType.EXPORT);
File zipFile = new File(FileUtils.pathConcat(fileExportPath.toAbsolutePath().toString(), VarsqlFileType.ZIP.concatExtension(fileId)));
int bufferSize = 2048;
final VarsqlFileType exportType = ddi.getExportType();
DBType dbType = DBType.getDBType(databaseInfo.getType());
String requid = ddi.getRequid();
ProgressInfo progressInfo = new ProgressInfo();
progressInfo.setTotalItemSize(ddi.getItems().size());
String sessAttrKey = HttpSessionConstants.progressKey(requid);
System.out.println("downloadTableData2 sessAttrKey : " + sessAttrKey);
HttpSession session = req.getSession();
session.setAttribute(sessAttrKey, progressInfo);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile), Charset.forName(charset))) {
int idx = 0;
for (DownloadItemInfo item : ddi.getItems()) {
++idx;
SqlExecuteDTO seDto = new SqlExecuteDTO();
seDto.setLimit(ddi.getLimit());
seDto.setConuid(ddi.getConuid(), exportFileName, databaseInfo);
String objectName = prefixSchema + item.getName();
seDto.setSqlParam("{}");
seDto.setSql(SQLUtils.generateSelectQuery(objectName, item.getCondition(), dbType));
String downloadFilePath = FileUtils.pathConcat(fileExportPath.toAbsolutePath().toString(), exportType.concatExtension(VartechUtils.generateUUID()));
progressInfo.setName(item.getName());
progressInfo.setItemIdx(idx);
AbstractWriter writer = null;
try (OutputStream outstream = new FileOutputStream(downloadFilePath)) {
if (VarsqlFileType.CSV.equals(exportType)) {
writer = new CSVWriter(outstream, ',', charset);
} else if (VarsqlFileType.JSON.equals(exportType)) {
writer = new JSONWriter(outstream, "row", charset);
} else if (VarsqlFileType.XML.equals(exportType)) {
writer = new XMLWriter(outstream, "row", charset);
} else if (VarsqlFileType.EXCEL.equals(exportType)) {
writer = new ExcelWriter(outstream);
} else {
writer = new SQLWriter(outstream, dbType, objectName, charset);
}
logger.debug("data export downloadFilePath :{} , query : {}", downloadFilePath, seDto.getSql());
final String tableName = item.getName();
SQLExecuteResult ser = (new SelectExecutor()).execute(seDto, new AbstractSQLExecutorHandler(writer) {
private boolean firstFlag = true;
private int rowIdx = 0;
public boolean handle(SQLHandlerParameter handleParam) {
if (this.firstFlag) {
WriteMetadataInfo whi = new WriteMetadataInfo("exportInfo");
List<ExportColumnInfo> columns = new LinkedList<>();
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(new WriteMetadataInfo[] { whi });
if (VarsqlFileType.SQL.equals(exportType)) {
((SQLWriter) getWriter()).setColumnInfos(handleParam.getColumnInfoList());
}
this.firstFlag = false;
}
progressInfo.setProgressContentLength(++rowIdx);
try {
getWriter().addRow(handleParam.getRowObject());
} catch (IOException e) {
logger.error(e.getMessage(), e);
return false;
}
return true;
}
});
writer.writeAndClose();
String zipFileName = exportType.concatExtension(item.getName());
if (ser.getResultCode() != null) {
VarsqlUtils.textDownload(new FileOutputStream(downloadFilePath), ser.getMessage());
zipFileName = item.getName() + "-export-error.txt";
}
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(downloadFilePath))) {
ZipEntry zentry = new ZipEntry(zipFileName);
zentry.setSize(new File(downloadFilePath).length());
zos.putNextEntry(zentry);
byte[] buffer = new byte[bufferSize];
int cnt = 0;
while ((cnt = bis.read(buffer, 0, bufferSize)) != -1) {
zos.write(buffer, 0, cnt);
}
zos.closeEntry();
}
new File(downloadFilePath).delete();
} catch (Exception e) {
throw e;
} finally {
IOUtils.close(writer);
}
}
IOUtils.close(zos);
}
FileInfoEntity fie = FileInfoEntity.builder().fileId(fileId).fileContId(fileId).contGroupId(databaseInfo.getVconnid()).fileDiv(UploadFileType.EXPORT.getDiv()).fileFieldName("downloadZipFile").fileName(VarsqlFileType.ZIP.concatExtension(exportFileName)).fileSize(zipFile.length()).fileExt(VarsqlFileType.ZIP.getExtension()).filePath(FileUtils.pathConcat(FileServiceUtils.getSaveRelativePath(UploadFileType.EXPORT), zipFile.getName())).build();
fileInfoEntityRepository.save(fie);
session.setAttribute(sessAttrKey, "complete");
return VarsqlUtils.getResponseResultItemOne(fie);
}
use of com.varsql.core.db.valueobject.DatabaseInfo in project varsql by varsqlinfo.
the class UserMainController method connectionInfo.
/**
* @Method Name : connectionInfo
* @Method 설명 : 사용자 권한 있는 db 정보.
* @작성자 : ytkim
* @작성일 : 2019. 8. 16.
* @변경이력 :
* @param req
* @return
* @throws Exception
*/
@RequestMapping(value = "/connectionInfo", method = RequestMethod.POST)
@ResponseBody
public ResponseResult connectionInfo(HttpServletRequest req) throws Exception {
ResponseResult resultObject = new ResponseResult();
DatabaseUtils.reloadUserDatabaseInfo();
Collection<DatabaseInfo> dataBaseInfo = SecurityUtil.loginInfo(req).getDatabaseInfo().values();
List<HashMap<String, String>> databaseList = new ArrayList<>();
dataBaseInfo.forEach(item -> {
HashMap<String, String> addMap = new HashMap<>();
addMap.put("uuid", item.getConnUUID());
addMap.put("type", item.getType());
addMap.put("name", item.getName());
databaseList.add(addMap);
});
resultObject.setItemList(databaseList);
return resultObject;
}
use of com.varsql.core.db.valueobject.DatabaseInfo in project varsql by varsqlinfo.
the class DbDiffServiceImpl method getDatabaseParamInfo.
private DatabaseParamInfo getDatabaseParamInfo(DBConnectionEntity vtConnRVO) {
DatabaseParamInfo dpi = new DatabaseParamInfo();
dpi.setConuid(null, SecurityUtil.loginInfo().getViewid(), new DatabaseInfo(vtConnRVO.getVconnid(), null, vtConnRVO.getDbTypeDriverProvider().getDbType(), vtConnRVO.getVname(), vtConnRVO.getVdbschema(), vtConnRVO.getBasetableYn(), vtConnRVO.getLazyloadYn(), ConvertUtils.longValueOf(vtConnRVO.getVdbversion()), vtConnRVO.getSchemaViewYn(), ConvertUtils.intValue(vtConnRVO.getMaxSelectCount()), vtConnRVO.getUseColumnLabel()));
return dpi;
}
use of com.varsql.core.db.valueobject.DatabaseInfo in project varsql by varsqlinfo.
the class SQLManagerTest method test.
@Ignore
@Test
void test() {
ConnectionInfo connInfo = getBaseConnection();
try {
DatabaseInfo di = getDatabaseInfo(connInfo);
DatabaseParamInfo dpi = new DatabaseParamInfo();
dpi.setSchema("PUBLIC");
dpi.setConuid(connInfo.getConnid(), TEST_CON_UID, di);
for (int i = 0; i < 10; i++) {
// System.out.println(MetaControlFactory.getDbInstanceFactory(DBType.H2).getSchemas(dpi) + " :: " +MetaControlFactory.getDbInstanceFactory(DBType.OTHER).getSchemas(dpi));
List<TableInfo> tblList = MetaControlFactory.getDbInstanceFactory(DBType.OTHER).getDBObjectMeta("table", dpi);
System.out.println(i + " : tblList.size() : " + tblList.size());
if (i % 1000 == 0) {
System.out.println("111111111111");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations