Skip to content
13 changes: 13 additions & 0 deletions linkis-dist/package/db/linkis_dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,19 @@ INSERT INTO `linkis_ps_dm_datasource_type` (`name`, `description`, `option`, `cl
INSERT INTO `linkis_ps_dm_datasource_type` (`name`, `description`, `option`, `classifier`, `icon`, `layers`) VALUES ('doris','doris数据库','doris','olap','',4);
INSERT INTO `linkis_ps_dm_datasource_type` (`name`, `description`, `option`, `classifier`, `icon`, `layers`) VALUES ('clickhouse','clickhouse数据库','clickhouse','olap','',4);
INSERT INTO `linkis_ps_dm_datasource_type` (`name`, `description`, `option`, `classifier`, `icon`, `layers`) VALUES ('starrocks','starrocks数据库','starrocks','olap','',4);
INSERT INTO `linkis_ps_dm_datasource_type` (`name`, `description`, `option`, `classifier`, `icon`, `layers`) VALUES ('oscar','神通数据库','oscar','关系型数据库','',3);

select @data_source_type_id := id from `linkis_ps_dm_datasource_type` where `name` = 'oscar';
INSERT INTO `linkis_ps_dm_datasource_type_key`
(`data_source_type_id`, `key`, `name`, `name_en`, `default_value`, `value_type`, `scope`, `require`, `description`, `description_en`, `value_regex`, `ref_id`, `ref_value`, `data_source`, `update_time`, `create_time`)
VALUES (@data_source_type_id, 'address', '地址', 'Address', NULL, 'TEXT', NULL, 0, '地址(host1:port1,host2:port2...)', 'Address(host1:port1,host2:port2...)', NULL, NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'host', '主机名(Host)', 'Host', NULL, 'TEXT', NULL, 1, '主机名(Host)', 'Host', NULL, NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'port', '端口号(Port)', 'Port', NULL, 'TEXT', NULL, 1, '端口号(Port)', 'Port', NULL, NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'driverClassName', '驱动类名(Driver class name)', 'Driver class name', 'com.oscar.Driver', 'TEXT', NULL, 1, '驱动类名(Driver class name)', 'Driver class name', NULL, NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'params', '连接参数(Connection params)', 'Connection params', NULL, 'TEXT', NULL, 0, '输入JSON格式(Input JSON format): {"param":"value"}', 'Input JSON format: {"param":"value"}', NULL, NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'username', '用户名(Username)', 'Username', NULL, 'TEXT', NULL, 1, '用户名(Username)', 'Username', '^[0-9A-Za-z_-]+$', NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'password', '密码(Password)', 'Password', NULL, 'PASSWORD', NULL, 1, '密码(Password)', 'Password', '', NULL, NULL, NULL, now(), now()),
(@data_source_type_id, 'instance', '实例名(instance)', 'Instance', NULL, 'TEXT', NULL, 1, '实例名(instance)', 'Instance', NULL, NULL, NULL, NULL, now(), now());

select @data_source_type_id := id from `linkis_ps_dm_datasource_type` where `name` = 'mongodb';
INSERT INTO `linkis_ps_dm_datasource_type_key` (`data_source_type_id`, `key`, `name`, `name_en`, `default_value`, `value_type`, `scope`, `require`, `description`, `description_en`, `value_regex`, `ref_id`, `ref_value`, `data_source`, `update_time`, `create_time`) VALUES (@data_source_type_id, 'username', '用户名', 'Username', NULL, 'TEXT', NULL, 1, '用户名', 'Username', '^[0-9A-Za-z_-]+$', NULL, '', NULL, now(), now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,53 @@ public Message getColumns(
}
}

@RequestMapping(
value = "/exists/{dataSourceId}/db/{database}/table/{table}",
method = RequestMethod.GET)
public Message existsTable(
@PathVariable("dataSourceId") String dataSourceId,
@PathVariable("database") String database,
@PathVariable("table") String table,
@RequestParam("system") String system,
HttpServletRequest request) {
try {
if (StringUtils.isBlank(system)) {
return Message.error("'system' is missing[缺少系统名]");
}
if (!MetadataUtils.nameRegexPattern.matcher(system).matches()) {
return Message.error("'system' is invalid[系统名错误]");
}
if (!MetadataUtils.nameRegexPattern.matcher(database).matches()) {
return Message.error("'database' is invalid[数据库名错误]");
}
if (!MetadataUtils.nameRegexPattern.matcher(table).matches()) {
return Message.error("'table' is invalid[表名错误]");
}
if (!MetadataUtils.nameRegexPattern.matcher(dataSourceId).matches()) {
return Message.error("'dataSourceId' is invalid[数据源错误]");
}

String userName =
ModuleUserUtils.getOperationUser(request, "existsTable, dataSourceId:" + dataSourceId);
boolean exists =
metadataAppService.existsTableByDsId(dataSourceId, database, table, system, userName);
return Message.ok().data("exists", exists);
} catch (Exception e) {
return errorToResponseMessage(
"Fail to check table existence[判断表是否存在失败], id:["
+ dataSourceId
+ "]"
+ ", system:["
+ system
+ "], database:["
+ database
+ "], table:["
+ table
+ "]",
e);
}
}

private Message errorToResponseMessage(String uiMessage, Exception e) {
if (e instanceof MetaMethodInvokeException) {
MetaMethodInvokeException invokeException = (MetaMethodInvokeException) e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,63 @@ public Message getColumns(
}
}

@ApiOperation(value = "existsTable", notes = "check if table exists", response = Message.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "dataSourceName", required = true, dataType = "String"),
@ApiImplicitParam(name = "envId", required = false, dataType = "String"),
@ApiImplicitParam(name = "system", required = true, dataType = "String"),
@ApiImplicitParam(name = "database", required = true, dataType = "String"),
@ApiImplicitParam(name = "table", required = true, dataType = "String")
})
@RequestMapping(value = "/existsTable", method = RequestMethod.GET)
public Message existsTable(
@RequestParam("dataSourceName") String dataSourceName,
@RequestParam(value = "envId", required = false) String envId,
@RequestParam("database") String database,
@RequestParam("table") String table,
@RequestParam("system") String system,
HttpServletRequest request) {
try {
if (StringUtils.isBlank(system)) {
return Message.error("'system' is missing[缺少系统名]");
}
if (!MetadataUtils.nameRegexPattern.matcher(system).matches()) {
return Message.error("'system' is invalid[系统名错误]");
}
if (!MetadataUtils.nameRegexPattern.matcher(database).matches()) {
return Message.error("'database' is invalid[数据库名错误]");
}
if (!MetadataUtils.nameRegexPattern.matcher(table).matches()) {
return Message.error("'table' is invalid[表名错误]");
}
if (!MetadataUtils.nameRegexPattern.matcher(dataSourceName).matches()) {
return Message.error("'dataSourceName' is invalid[数据源错误]");
}

String userName =
ModuleUserUtils.getOperationUser(
request, "existsTable, dataSourceName:" + dataSourceName);

boolean exists =
metadataQueryService.existsTableByDsNameAndEnvId(
dataSourceName, database, table, system, userName, envId);
return Message.ok().data("exists", exists);
} catch (Exception e) {
return errorToResponseMessage(
"Fail to check table existence[判断表是否存在失败], name:["
+ dataSourceName
+ "]"
+ ", system:["
+ system
+ "], database:["
+ database
+ "], table:["
+ table
+ "]",
e);
}
}

private Message errorToResponseMessage(String uiMessage, Exception e) {
if (e instanceof MetaMethodInvokeException) {
MetaMethodInvokeException invokeException = (MetaMethodInvokeException) e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ List<MetaColumnInfo> getColumnsByDsId(
String dataSourceId, String database, String table, String system, String userName)
throws ErrorException;

/**
* Check if the table exists by data source id
*
* @param dataSourceId data source id
* @param database database
* @param table table
* @param system system
* @param userName userName
* @return true if the table exists
* @throws ErrorException
*/
@Deprecated
boolean existsTableByDsId(
String dataSourceId, String database, String table, String system, String userName)
throws ErrorException;

/**
* Get connection information
*
Expand Down Expand Up @@ -224,6 +240,21 @@ List<MetaColumnInfo> getColumnsByDsName(
String dataSourceName, String database, String table, String system, String userName)
throws ErrorException;

/**
* Check if the table exists by data source name
*
* @param dataSourceName data source name
* @param database database
* @param table table
* @param system system
* @param userName userName
* @return true if the table exists
* @throws ErrorException
*/
boolean existsTableByDsName(
String dataSourceName, String database, String table, String system, String userName)
throws ErrorException;

/**
* @param dataSourceName
* @param database
Expand All @@ -242,4 +273,25 @@ List<MetaColumnInfo> getColumnsByDsNameAndEnvId(
String userName,
String envId)
throws ErrorException;

/**
* Check if the table exists by data source name and env id
*
* @param dataSourceName data source name
* @param database database
* @param table table
* @param system system
* @param userName userName
* @param envId env id
* @return true if the table exists
* @throws ErrorException
*/
boolean existsTableByDsNameAndEnvId(
String dataSourceName,
String database,
String table,
String system,
String userName,
String envId)
throws ErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,26 @@ public List<MetaColumnInfo> getColumnsByDsId(
return new ArrayList<>();
}

@Override
@Deprecated
public boolean existsTableByDsId(
String dataSourceId, String database, String table, String system, String userName)
throws ErrorException {
DsInfoResponse dsInfoResponse = reqToGetDataSourceInfo(dataSourceId, system, userName);
if (StringUtils.isNotBlank(dsInfoResponse.getDsType())) {
Boolean exists =
invokeMetaMethod(
dsInfoResponse.getDsType(),
"existsTable",
new Object[] {
dsInfoResponse.getCreator(), dsInfoResponse.getParams(), database, table
},
Boolean.class);
return Objects.nonNull(exists) && exists;
}
return false;
}

@Override
public List<String> getDatabasesByDsName(String dataSourceName, String system, String userName)
throws ErrorException {
Expand Down Expand Up @@ -341,6 +361,25 @@ public List<MetaColumnInfo> getColumnsByDsName(
return new ArrayList<>();
}

@Override
public boolean existsTableByDsName(
String dataSourceName, String database, String table, String system, String userName)
throws ErrorException {
DsInfoResponse dsInfoResponse = queryDataSourceInfoByName(dataSourceName, system, userName);
if (StringUtils.isNotBlank(dsInfoResponse.getDsType())) {
Boolean exists =
invokeMetaMethod(
dsInfoResponse.getDsType(),
"existsTable",
new Object[] {
dsInfoResponse.getCreator(), dsInfoResponse.getParams(), database, table
},
Boolean.class);
return Objects.nonNull(exists) && exists;
}
return false;
}

@Override
public List<MetaColumnInfo> getColumnsByDsNameAndEnvId(
String dataSourceName,
Expand All @@ -362,6 +401,31 @@ public List<MetaColumnInfo> getColumnsByDsNameAndEnvId(
return new ArrayList<>();
}

@Override
public boolean existsTableByDsNameAndEnvId(
String dataSourceName,
String database,
String table,
String system,
String userName,
String envId)
throws ErrorException {
DsInfoResponse dsInfoResponse =
queryDataSourceInfoByNameAndEnvId(dataSourceName, system, userName, envId);
if (StringUtils.isNotBlank(dsInfoResponse.getDsType())) {
Boolean exists =
invokeMetaMethod(
dsInfoResponse.getDsType(),
"existsTable",
new Object[] {
dsInfoResponse.getCreator(), dsInfoResponse.getParams(), database, table
},
Boolean.class);
return Objects.nonNull(exists) && exists;
}
return false;
}

/**
* Request to get data source information (type and connection parameters)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public class ElasticConnection implements Closeable {

private static final String DEFAULT_MAPPING_NAME = "mappings";
private static final String DEFAULT_INDEX_NAME = "index";
private static final String FIELD_PROPS = "properties";
/**
* Nested properties key in ElasticSearch mappings, used to descend into nested objects
* (ElasticSearch mappings 中嵌套对象的 properties 键,用于递归展开嵌套字段)
*/
public static final String FIELD_PROPS = "properties";

private RestClient restClient;

Expand Down
Loading
Loading