package com.highdatas.mdm.service.impl;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.highdatas.mdm.entity.SysAssemble;
import com.highdatas.mdm.entity.SysAssembleDb;
import com.highdatas.mdm.entity.SysAssembleDbdriver;
import com.highdatas.mdm.mapper.SysAssembleDbMapper;
import com.highdatas.mdm.pojo.kettle.UnBigDataDataSourceInfo;
import com.highdatas.mdm.service.ISysAssembleDbService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.highdatas.mdm.service.ISysAssembleDbdriverService;
import com.highdatas.mdm.service.ISysAssembleService;
import com.highdatas.mdm.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.pentaho.di.core.KettleClientEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author kimi
* @since 2020-02-20
*/
@Service
public class SysAssembleDbServiceImpl extends ServiceImpl implements ISysAssembleDbService {
@Autowired
ISysAssembleDbdriverService dbdriverService;
public Connection getConnection(String parentId) {
SysAssembleDb assemble = this.selectById(parentId);
if(assemble == null){
return null;
}
return getConnection(assemble);
}
private Connection getConnection(SysAssembleDb assemble) {
Connection connection = null;
try {
SysAssembleDbdriver sysAssembleDbdriver = dbdriverService.selectOne(new EntityWrapper().eq(Constant.TYPE, assemble.getDatasourceType()));
if (sysAssembleDbdriver != null) {
String driver = sysAssembleDbdriver.getDriver();
Class.forName(driver).newInstance();
}
// Boolean bigdata = assemble.getBigdata();
// if (bigdata) {
// //HBASE
// }else {
// dataMetaOutput = new DatabaseMeta("Output", unBigDataDataSourceInfo.getDbType(), "Native", unBigDataDataSourceInfo.getDbHostName(), unBigDataDataSourceInfo.getDbName(), unBigDataDataSourceInfo.getDbPort(),
// unBigDataDataSourceInfo.getUsername(), unBigDataDataSourceInfo.getPassword());
// }
connection = DriverManager.getConnection(assemble.getDatasourceUrl(), assemble.getDatasourceUser(), assemble.getDatasourcePass());
return connection;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public List getFieldsByTableName(String parentId, String tableName) {
SysAssembleDb assemble = this.selectById(parentId);
if(assemble == null){
return null;
}
Connection connection = null;
try {
connection = getConnection(parentId);
SysAssembleDbdriver sysAssembleDbdriver = dbdriverService.selectOne(new EntityWrapper().eq(Constant.TYPE, assemble.getDatasourceType()));
String fieldSql = "";
if(sysAssembleDbdriver != null) {
String firstTemplate = sysAssembleDbdriver.getFirstTemplate();
fieldSql = MessageFormat.format(firstTemplate, MessageFormat.format(Constant.selectFieldSqlMysqlTemplate, tableName));
}else {
fieldSql = MessageFormat.format(Constant.selectFieldSqlMysqlTemplate, tableName);
}
PreparedStatement preparedStatement = connection.prepareStatement(fieldSql);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList columList = new ArrayList<>();
for (int i = 1; i <= columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i);
columList.add(columnLabel);
}
return columList;
}catch (Exception e) {
e.printStackTrace();
return null;
}finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public boolean testConnection(SysAssembleDb sysAssembleDb) {
Connection connection = null;
try{
connection = getConnection(sysAssembleDb);
if (connection != null) {
return true;
}
return false;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public boolean testConnection(String parentId) {
Connection connection = null;
try{
connection = getConnection(parentId);
if (connection != null) {
return true;
}
return false;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public boolean testSql(String parentId, String sql) {
Connection connection = null;
if (StringUtils.isEmpty(sql)) {
return false;
}
try {
connection = getConnection(parentId);
if (connection == null) {
return false;
}
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeQuery();
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
finally {
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
}
@Override
public List getFieldsBySql(String parentId, String sql) {
Connection connection = null;
if (StringUtils.isEmpty(sql)) {
return null;
}
try {
connection = getConnection(parentId);
if (connection == null) {
return null;
}
SysAssembleDb assemble = this.selectById(parentId);
SysAssembleDbdriver dbdriver = dbdriverService.selectOne(new EntityWrapper().eq(Constant.TYPE, assemble.getDatasourceType()));
String firstTemplate = dbdriver.getFirstTemplate();
if (!StringUtils.isEmpty(firstTemplate)) {
//有sql模板的 可以只取第一条
sql = MessageFormat.format(firstTemplate, sql);
}
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList columList = new ArrayList<>();
for (int i = 1; i <= columnCount; i++) {
String columnLabel = metaData.getColumnLabel(i);
columList.add(columnLabel);
}
return columList;
}catch (Exception e) {
e.printStackTrace();
return null;
}
finally {
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}