feat: 获取表的colume属性

This commit is contained in:
taojinlong 2021-02-22 18:06:37 +08:00
parent 99a617e1be
commit a0146d9cfe
5 changed files with 112 additions and 36 deletions

View File

@ -0,0 +1,15 @@
package io.dataease.datasource.dto;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class TableFiled {
private String fieldName;
private String remarks;
private String fieldType;
private int fieldSize;
}

View File

@ -1,7 +1,9 @@
package io.dataease.datasource.provider;
import io.dataease.base.domain.Datasource;
import io.dataease.datasource.dto.TableFiled;
import java.util.ArrayList;
import java.util.List;
public abstract class DatasourceProvider {
@ -31,6 +33,10 @@ public abstract class DatasourceProvider {
abstract public List<String> getTables() throws Exception;
public List<TableFiled> getTableFileds(String table) throws Exception{
return new ArrayList<>();
};
public void test() throws Exception {
getData();
}

View File

@ -3,15 +3,14 @@ package io.dataease.datasource.provider;
import com.google.gson.Gson;
import io.dataease.datasource.constants.DatasourceTypes;
import io.dataease.datasource.dto.MysqlConfigrationDTO;
import io.dataease.datasource.dto.TableFiled;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import io.dataease.datasource.constants.DatasourceTypes.*;
import java.util.Properties;
@Service("jdbc")
@ -42,7 +41,9 @@ public class JdbcProvider extends DatasourceProvider{
}
list.add(row);
}
} catch (Exception e) {
} catch (SQLException e){
throw new Exception("ERROR:" + e.getMessage(), e);
}catch (Exception e) {
throw new Exception("ERROR:" + e.getMessage(), e);
}
return list;
@ -64,6 +65,39 @@ public class JdbcProvider extends DatasourceProvider{
return tables;
}
@Override
public List<TableFiled> getTableFileds(String table) throws Exception{
List<TableFiled> list = new LinkedList<>();
try (
Connection connection = getConnection();
) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getColumns(null, "%", table.toUpperCase(), "%");
while (resultSet.next()) {
String tableName=resultSet.getString("TABLE_NAME");
String database = resultSet.getString("TABLE_CAT");
if(tableName.equals(table) && database.equalsIgnoreCase(getDatabase())){
TableFiled tableFiled = new TableFiled();
String colName = resultSet.getString("COLUMN_NAME");
tableFiled.setFieldName(colName);
String remarks = resultSet.getString("REMARKS");
if(remarks == null || remarks.equals("")){
remarks = colName;
}
tableFiled.setRemarks(remarks);
String dbType = resultSet.getString("TYPE_NAME");
tableFiled.setFieldType(dbType);
list.add(tableFiled);
}
}
} catch (SQLException e){
throw new Exception("ERROR:" + e.getMessage(), e);
}catch (Exception e) {
throw new Exception("ERROR:" + e.getMessage(), e);
}
return list;
};
@Override
public void test() throws Exception {
String queryStr = "show tables";
@ -74,7 +108,6 @@ public class JdbcProvider extends DatasourceProvider{
}
}
private Connection getConnection() throws Exception {
String username = null;
String password = null;
@ -102,4 +135,51 @@ public class JdbcProvider extends DatasourceProvider{
return DriverManager.getConnection(jdbcurl, props);
}
private String getDatabase(){
DatasourceTypes datasourceType = DatasourceTypes.valueOf(getDatasource().getType());
switch (datasourceType) {
case mysql:
MysqlConfigrationDTO mysqlConfigrationDTO = new Gson().fromJson(getDatasource().getConfiguration(), MysqlConfigrationDTO.class);
return mysqlConfigrationDTO.getDataBase();
default:
return null;
}
}
private static String getSchema(Connection conn) throws Exception {
String schema;
schema = conn.getMetaData().getUserName();
System.out.println(schema);
if ((schema == null) || (schema.length() == 0)) {
throw new Exception("ORACLE数据库模式不允许为空");
}
return schema.toUpperCase().toString();
}
private static String changeDbType(String dbType) {
dbType = dbType.toUpperCase();
switch(dbType){
case "VARCHAR":
case "VARCHAR2":
case "CHAR":
return "1";
case "NUMBER":
case "DECIMAL":
return "4";
case "INT":
case "SMALLINT":
case "INTEGER":
return "2";
case "BIGINT":
return "6";
case "DATETIME":
case "TIMESTAMP":
case "DATE":
return "7";
default:
return "1";
}
}
}

View File

@ -21,34 +21,6 @@
:total="total"/>
</el-card>
<!-- dialog of organization member -->
<el-dialog :close-on-click-modal="false" :visible.sync="dialogDatasourceMemberVisible" width="70%" :destroy-on-close="true" @close="closeFunc"
class="dialog-css">
<ms-table-header :condition.sync="dialogCondition" @create="addMember" @search="dialogSearch"
:create-tip="$t('member.create')" :title="$t('commons.member')"/>
<!-- organization member table -->
<el-table :border="true" class="adjust-table" :data="memberLineData" style="width: 100%;margin-top:5px;">
<el-table-column prop="id" label="ID"/>
<el-table-column prop="name" :label="$t('commons.username')"/>
<el-table-column prop="email" :label="$t('commons.email')"/>
<el-table-column prop="phone" :label="$t('commons.phone')"/>
<el-table-column :label="$t('commons.role')" width="140">
<template v-slot:default="scope">
<ms-roles-tag :roles="scope.row.roles"/>
</template>
</el-table-column>
<el-table-column :label="$t('commons.operating')">
<template v-slot:default="scope">
<ms-table-operator :tip2="$t('commons.remove')" @editClick="editMember(scope.row)"
@deleteClick="delMember(scope.row)"/>
</template>
</el-table-column>
</el-table>
<ms-table-pagination :change="dialogSearch" :current-page.sync="dialogCurrentPage"
:page-size.sync="dialogPageSize"
:total="dialogTotal"/>
</el-dialog>
<!-- add datasource form -->
<el-dialog :close-on-click-modal="false" :title="$t('datasource.create')" :visible.sync="dialogDatasourceAddVisible" width="30%" @closed="closeFunc"
:destroy-on-close="true">
@ -90,7 +62,8 @@
</el-form>
<template v-slot:footer>
<ms-dialog-footer is-show-validate="true"
<ms-dialog-footer
:isShowValidate="true"
@cancel="dialogDatasourceAddVisible = false"
@validate="validaDatasource('createDatasource')"
@confirm="createDatasource('createDatasource')"/>
@ -128,7 +101,8 @@
</el-form-item>
</el-form>
<template v-slot:footer>
<ms-dialog-footer is-show-validate="true"
<ms-dialog-footer
:isShowValidate="true"
@cancel="dialogDatasourceUpdateVisible = false"
@validate="validaDatasource('updateDatasourceForm')"
@confirm="updateDatasource('updateDatasourceForm')"/>

View File

@ -1558,6 +1558,7 @@ export default {
please_input_host: '请输入主机',
please_input_port: '请输入端口',
modify: '修改组织',
validate_success: '校验成功',
delete: '删除组织',
delete_confirm: '删除该组织会关联删除该组织下的所有资源(如:相关工作空间,项目,测试用例等),确定要删除吗?',
input_name: '请输入名称',