Commit beac5827 by inrgihc

补充对象成员抽取

parent be465b5c
package com.gitee.sqlrest.core.dto;
import com.gitee.sqlrest.common.enums.ProductTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class DataSourceBaseRequest {
@NotBlank(message = "name不能为空")
@ApiModelProperty("名称")
private String name;
@NotNull(message = "type不能为null")
@ApiModelProperty("类型")
private ProductTypeEnum type;
@NotBlank(message = "version不能为空")
@ApiModelProperty("驱动版本")
private String version;
@NotBlank(message = "driver不能为空")
@ApiModelProperty("驱动类型")
private String driver;
@NotBlank(message = "url不能为空")
@ApiModelProperty("连接JDBC-URL")
private String url;
@NotBlank(message = "username不能为空")
@ApiModelProperty("账号")
private String username;
@ApiModelProperty("密码")
private String password;
}
package com.gitee.sqlrest.core.dto;
import com.gitee.sqlrest.common.enums.ProductTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@ApiModel("数据源保存")
public class DataSourceSaveRequest {
public class DataSourceSaveRequest extends DataSourceBaseRequest {
@ApiModelProperty("ID编号")
private Long id;
@NotBlank(message = "name不能为空")
@ApiModelProperty("名称")
private String name;
@NotNull(message = "type不能为null")
@ApiModelProperty("类型")
private ProductTypeEnum type;
@NotBlank(message = "version不能为空")
@ApiModelProperty("驱动版本")
private String version;
@NotBlank(message = "driver不能为空")
@ApiModelProperty("驱动类型")
private String driver;
@NotBlank(message = "url不能为空")
@ApiModelProperty("连接JDBC-URL")
private String url;
@NotBlank(message = "username不能为空")
@ApiModelProperty("账号")
private String username;
@ApiModelProperty("密码")
private String password;
}
......@@ -8,6 +8,7 @@ import com.gitee.sqlrest.common.exception.CommonException;
import com.gitee.sqlrest.common.exception.ResponseErrorCode;
import com.gitee.sqlrest.common.util.JdbcUrlUtils;
import com.gitee.sqlrest.core.driver.DriverLoadService;
import com.gitee.sqlrest.core.dto.DataSourceBaseRequest;
import com.gitee.sqlrest.core.dto.DataSourceSaveRequest;
import com.gitee.sqlrest.core.dto.DatabaseTypeDetailResponse;
import com.gitee.sqlrest.core.dto.DatasourceDetailResponse;
......@@ -74,21 +75,57 @@ public class DataSourceService {
return response;
}
private void testConnection(HikariDataSource ds, ProductTypeEnum type) {
try (Connection connection = ds.getConnection()) {
if (StringUtils.isNotBlank(type.getSql())) {
try (Statement statement = connection.createStatement()) {
statement.execute(type.getSql());
}
} else {
if (!connection.isValid(2)) {
throw new RuntimeException("Connection is invalid!");
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void preTest(DataSourceBaseRequest request) {
DataSourceEntity dataSourceEntity = DataSourceEntity.builder()
.name(request.getName())
.type(request.getType())
.driver(request.getDriver())
.version(request.getVersion())
.url(request.getUrl())
.username(request.getUsername())
.password(request.getPassword())
.build();
File driverPathFile = SpringUtil.getBean(DriverLoadService.class)
.getVersionDriverFile(dataSourceEntity.getType(),
dataSourceEntity.getVersion());
String driverPath = driverPathFile.getAbsolutePath();
HikariDataSource ds = DataSourceUtils.createDataSource(dataSourceEntity, driverPath);
try {
testConnection(ds, request.getType());
} finally {
DataSourceUtils.closeHikariDataSource(ds);
}
}
public void testDataSource(Long id) {
DataSourceEntity dataSourceEntity = dataSourceDao.getById(id);
if (null == dataSourceEntity) {
throw new CommonException(ResponseErrorCode.ERROR_RESOURCE_NOT_EXISTS, "id=" + id);
}
File driverPathFile = SpringUtil.getBean(DriverLoadService.class)
.getVersionDriverFile(dataSourceEntity.getType(),
dataSourceEntity.getVersion());
String driverPath = driverPathFile.getAbsolutePath();
HikariDataSource ds = DataSourceUtils.getHikariDataSource(dataSourceEntity, driverPath);
if (StringUtils.isNotBlank(dataSourceEntity.getType().getSql())) {
try (Connection connection = ds.getConnection()) {
Statement statement = connection.createStatement();
statement.execute(dataSourceEntity.getType().getSql());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
testConnection(ds, dataSourceEntity.getType());
}
public void createDataSource(DataSourceSaveRequest request) {
......
......@@ -8,6 +8,7 @@ import com.gitee.sqlrest.persistence.entity.DataSourceEntity;
import com.zaxxer.hikari.HikariDataSource;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
......@@ -31,7 +32,17 @@ public final class DataSourceUtils {
if (!datasourceMap.containsKey(entity.getId())) {
HikariDataSource ds = createDataSource(entity, driverPath);
try (Connection connection = ds.getConnection()) {
connection.isValid(2);
if (StringUtils.isNotBlank(entity.getType().getSql())) {
try (Statement statement = connection.createStatement()) {
statement.execute(entity.getType().getSql());
}
} else {
if (!connection.isValid(2)) {
throw new RuntimeException("Connection is invalid!");
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -48,15 +59,19 @@ public final class DataSourceUtils {
public static void dropHikariDataSource(Long dataSourceId) {
Pair<DataSourceEntity, HikariDataSource> dsPair = datasourceMap.remove(dataSourceId);
if (null != dsPair) {
try {
dsPair.getRight().close();
} catch (Exception e) {
log.warn("Error when close HikariDataSource:{}", e.getMessage());
}
closeHikariDataSource(dsPair.getRight());
}
}
public static void closeHikariDataSource(HikariDataSource ds) {
try {
ds.close();
} catch (Exception e) {
log.warn("Error when close HikariDataSource:{}", e.getMessage());
}
}
private static HikariDataSource createDataSource(DataSourceEntity properties, String driverPath) {
public static HikariDataSource createDataSource(DataSourceEntity properties, String driverPath) {
Properties parameters = new Properties();
HikariDataSource ds = new HikariDataSource();
ds.setPoolName("The_JDBC_Connection");
......
......@@ -235,9 +235,12 @@
</el-form>
<div slot="footer"
class="dialog-footer">
<el-button @click="createFormVisible = false">取 消</el-button>
<el-button type="success"
@click="handlePreTest(createform,'createform')">测试</el-button>
<el-button type="primary"
@click="handleCreate">确 定</el-button>
<el-button type="info"
@click="createFormVisible = false">取 消</el-button>
</div>
</el-dialog>
......@@ -314,9 +317,12 @@
</el-form>
<div slot="footer"
class="dialog-footer">
<el-button @click="updateFormVisible = false">取 消</el-button>
<el-button type="success"
@click="handlePreTest(updateform,'updateform')">测试</el-button>
<el-button type="primary"
@click="handleSave">确 定</el-button>
<el-button type="info"
@click="updateFormVisible = false">取 消</el-button>
</div>
</el-dialog>
</el-card>
......@@ -523,6 +529,61 @@ export default {
this.createFormVisible = true;
this.createform = {};
},
handlePreTest: function (form, refName,) {
let driverClass = "";
if (this.databaseType.length > 0) {
for (let i = 0; i < this.databaseType.length; i++) {
if (this.databaseType[i].type == form.type) {
driverClass = this.databaseType[i].driver;
break;
}
}
}
this.$refs[refName].validate(valid => {
if (valid) {
this.$http({
method: "POST",
headers: {
'Content-Type': 'application/json'
},
url: "/sqlrest/manager/api/v1/datasource/preTest",
data: JSON.stringify({
name: form.name,
type: form.type,
version: form.version,
driver: driverClass,
url: form.url,
username: form.username,
password: form.password
})
}).then(res => {
if (0 === res.data.code) {
this.$alert("测试连接信息成功", "测试操作成功",
{
confirmButtonText: "确定",
type: "info"
}
);
} else {
this.$alert(res.data.message, "测试操作失败",
{
confirmButtonText: "确定",
type: "error"
}
);
}
});
} else {
this.$alert("请检查输入", "提示信息",
{
confirmButtonText: "确定",
type: "info"
}
);
}
});
},
handleCreate: function () {
let driverClass = "";
if (this.databaseType.length > 0) {
......
......@@ -5,6 +5,7 @@ import com.gitee.sqlrest.common.dto.PageResult;
import com.gitee.sqlrest.common.dto.ResultEntity;
import com.gitee.sqlrest.common.enums.ProductTypeEnum;
import com.gitee.sqlrest.core.driver.DriverLoadService;
import com.gitee.sqlrest.core.dto.DataSourceBaseRequest;
import com.gitee.sqlrest.core.dto.DataSourceSaveRequest;
import com.gitee.sqlrest.core.dto.DatasourceDetailResponse;
import com.gitee.sqlrest.core.dto.EntityIdNameResponse;
......@@ -46,6 +47,13 @@ public class DataSourceController {
return ResultEntity.success(driverLoadService.getDrivers(type));
}
@ApiOperation(value = "连接预测试")
@PostMapping(value = "/preTest", produces = MediaType.APPLICATION_JSON_VALUE)
public ResultEntity preTest(@Valid @RequestBody DataSourceBaseRequest request) {
datasourceService.preTest(request);
return ResultEntity.success();
}
@ApiOperation(value = "数据源列表")
@PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
public PageResult<DatasourceDetailResponse> getConnections(@RequestBody EntitySearchRequest request) {
......
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>SQLREST工具</title><link href=/static/css/app.f2a15a0e03018d8441776c4304ddc3b9.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.d057b7936f48fb6e72f1.js></script><script type=text/javascript src=/static/js/vendor.6bde4750a07bb5a2f647.js></script><script type=text/javascript src=/static/js/app.5b13ca32f61140a99ff5.js></script></body></html>
\ No newline at end of file
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>SQLREST工具</title><link href=/static/css/app.072d7e806ffea2dbe3ce273489ed669e.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.075ed85199204f860c2e.js></script><script type=text/javascript src=/static/js/vendor.6bde4750a07bb5a2f647.js></script><script type=text/javascript src=/static/js/app.5b13ca32f61140a99ff5.js></script></body></html>
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
!function(e){var n=window.webpackJsonp;window.webpackJsonp=function(r,o,c){for(var f,d,i,u=0,b=[];u<r.length;u++)d=r[u],t[d]&&b.push(t[d][0]),t[d]=0;for(f in o)Object.prototype.hasOwnProperty.call(o,f)&&(e[f]=o[f]);for(n&&n(r,o,c);b.length;)b.shift()();if(c)for(u=0;u<c.length;u++)i=a(a.s=c[u]);return i};var r={},t={24:0};function a(n){if(r[n])return r[n].exports;var t=r[n]={i:n,l:!1,exports:{}};return e[n].call(t.exports,t,t.exports,a),t.l=!0,t.exports}a.e=function(e){var n=t[e];if(0===n)return new Promise(function(e){e()});if(n)return n[2];var r=new Promise(function(r,a){n=t[e]=[r,a]});n[2]=r;var o=document.getElementsByTagName("head")[0],c=document.createElement("script");c.type="text/javascript",c.charset="utf-8",c.async=!0,c.timeout=12e4,a.nc&&c.setAttribute("nonce",a.nc),c.src=a.p+"static/js/"+e+"."+{0:"e9559647b804220c4683",1:"b17200cccd46e216dcb3",2:"140338f6a5528feea1a3",3:"776d791724a8de12ff9e",4:"f8494b8dd039413f79c8",5:"530a43b5ad6055214539",6:"8f85de06573e2a5f9562",7:"061807fe4716131f26f8",8:"d1391c270de5a9f111c5",9:"cbdb7fa4f5180acfbb03",10:"7eeaa94fd42d34a86b92",11:"096c0f0eaf2850056b7e",12:"cea57d271a961f0b44ad",13:"4d2138ee1bee3ad573f4",14:"429592868e75adc95933",15:"3b3f0c03ff4fed9903cc",16:"9616cfe0a4f7517b0841",17:"b4bc5fa31e227bee8651",18:"5e7f065a8d031847e833",19:"3936346cb7e30aa279e2",20:"5ef9c751035ee9a08f94",21:"d8007e7169c085e13dab"}[e]+".js";var f=setTimeout(d,12e4);function d(){c.onerror=c.onload=null,clearTimeout(f);var n=t[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),t[e]=void 0)}return c.onerror=c.onload=d,o.appendChild(c),r},a.m=e,a.c=r,a.d=function(e,n,r){a.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},a.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(n,"a",n),n},a.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},a.p="/",a.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=manifest.075ed85199204f860c2e.js.map
\ No newline at end of file
!function(e){var n=window.webpackJsonp;window.webpackJsonp=function(r,f,a){for(var c,d,i,u=0,b=[];u<r.length;u++)d=r[u],t[d]&&b.push(t[d][0]),t[d]=0;for(c in f)Object.prototype.hasOwnProperty.call(f,c)&&(e[c]=f[c]);for(n&&n(r,f,a);b.length;)b.shift()();if(a)for(u=0;u<a.length;u++)i=o(o.s=a[u]);return i};var r={},t={24:0};function o(n){if(r[n])return r[n].exports;var t=r[n]={i:n,l:!1,exports:{}};return e[n].call(t.exports,t,t.exports,o),t.l=!0,t.exports}o.e=function(e){var n=t[e];if(0===n)return new Promise(function(e){e()});if(n)return n[2];var r=new Promise(function(r,o){n=t[e]=[r,o]});n[2]=r;var f=document.getElementsByTagName("head")[0],a=document.createElement("script");a.type="text/javascript",a.charset="utf-8",a.async=!0,a.timeout=12e4,o.nc&&a.setAttribute("nonce",o.nc),a.src=o.p+"static/js/"+e+"."+{0:"e9559647b804220c4683",1:"b17200cccd46e216dcb3",2:"140338f6a5528feea1a3",3:"776d791724a8de12ff9e",4:"f8494b8dd039413f79c8",5:"8fcbb35b45285576e78f",6:"8f85de06573e2a5f9562",7:"061807fe4716131f26f8",8:"d1391c270de5a9f111c5",9:"cbdb7fa4f5180acfbb03",10:"7eeaa94fd42d34a86b92",11:"096c0f0eaf2850056b7e",12:"da6814989fe7ae4e141f",13:"4d2138ee1bee3ad573f4",14:"429592868e75adc95933",15:"3b3f0c03ff4fed9903cc",16:"9616cfe0a4f7517b0841",17:"b4bc5fa31e227bee8651",18:"5e7f065a8d031847e833",19:"3936346cb7e30aa279e2",20:"5ef9c751035ee9a08f94",21:"d8007e7169c085e13dab"}[e]+".js";var c=setTimeout(d,12e4);function d(){a.onerror=a.onload=null,clearTimeout(c);var n=t[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),t[e]=void 0)}return a.onerror=a.onload=d,f.appendChild(a),r},o.m=e,o.c=r,o.d=function(e,n,r){o.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},o.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(n,"a",n),n},o.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},o.p="/",o.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=manifest.d057b7936f48fb6e72f1.js.map
\ No newline at end of file
......@@ -3,6 +3,7 @@ package com.gitee.sqlrest.template;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.builder.BaseBuilder;
......@@ -198,6 +199,19 @@ public class XmlScriptBuilder extends BaseBuilder {
inputParams.put(collection, true);
// extract object item
Map<String, Boolean> objectItems = new LinkedHashMap<>();
storeParameter(nodeToHandle.getStringBody(), objectItems);
for (Map.Entry<String, Boolean> entry : objectItems.entrySet()) {
String param = entry.getKey();
if (null != param && param.length() > 0) {
String name = param.replaceFirst(item, collection);
if (!collection.equals(name)) {
inputParams.put(name, false);
}
}
}
ForEachSqlNode forEachSqlNode = new ForEachSqlNode(configuration, mixedSqlNode, collection, nullable, index, item,
open, close, separator);
targetContents.add(forEachSqlNode);
......
......@@ -62,16 +62,29 @@ public class XmlSqlTemplate {
if (idx > 0) {
subName = name;
name = name.substring(0, idx);
}
if (entry.getValue()) {
names.put(name, true);
if (null != subName) {
names.put(subName, true);
if (entry.getValue()) {
names.put(name, false);
if (null != subName) {
names.put(subName, true);
}
} else {
names.putIfAbsent(name, entry.getValue());
if (null != subName) {
names.putIfAbsent(subName, entry.getValue());
}
}
continue;
} else {
names.putIfAbsent(name, entry.getValue());
if (null != subName) {
names.putIfAbsent(subName, entry.getValue());
if (entry.getValue()) {
names.put(name, true);
if (null != subName) {
names.put(subName, true);
}
} else {
names.putIfAbsent(name, entry.getValue());
if (null != subName) {
names.putIfAbsent(subName, entry.getValue());
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment