mirror of
https://github.com/dataease/dataease.git
synced 2026-05-14 21:12:33 +08:00
feat(X-Pack): [数据填报]表单绑定MySQL与PostgreSQL的已有表,支持使用其自增主键
This commit is contained in:
@@ -542,7 +542,7 @@ public class CalciteProvider extends Provider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate(DatasourceRequest datasourceRequest) throws DEException {
|
||||
public ExecuteResult executeUpdate(DatasourceRequest datasourceRequest, boolean autoIncrement) throws DEException {
|
||||
DatasourceSchemaDTO value = datasourceRequest.getDsList().entrySet().iterator().next().getValue();
|
||||
datasourceRequest.setDatasource(value);
|
||||
DatasourceConfiguration datasourceConfiguration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), DatasourceConfiguration.class);
|
||||
@@ -553,6 +553,7 @@ public class CalciteProvider extends Provider {
|
||||
statement.executeUpdate("ALTER SESSION SET CURRENT_SCHEMA = " + datasourceConfiguration.getSchema());
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
if (CollectionUtils.isNotEmpty(datasourceRequest.getTableFieldWithValues())) {
|
||||
LogUtil.info("execWithPreparedStatement sql: " + datasourceRequest.getQuery());
|
||||
for (int i = 0; i < datasourceRequest.getTableFieldWithValues().size(); i++) {
|
||||
@@ -563,11 +564,24 @@ public class CalciteProvider extends Provider {
|
||||
throw new SQLException(e.getMessage() + ". VALUE: " + datasourceRequest.getTableFieldWithValues().get(i).getValue().toString() + " , TARGET TYPE: " + datasourceRequest.getTableFieldWithValues().get(i).getColumnTypeName());
|
||||
}
|
||||
}
|
||||
return ((PreparedStatement) statement).executeUpdate();
|
||||
count = ((PreparedStatement) statement).executeUpdate();
|
||||
} else {
|
||||
return statement.executeUpdate(datasourceRequest.getQuery());
|
||||
count = statement.executeUpdate(datasourceRequest.getQuery());
|
||||
}
|
||||
|
||||
ExecuteResult result = new ExecuteResult();
|
||||
result.setCount(count);
|
||||
|
||||
if (autoIncrement) {
|
||||
List<String> generatedKeys = new ArrayList<>();
|
||||
ResultSet keys = statement.getGeneratedKeys();
|
||||
while (keys.next()) {
|
||||
generatedKeys.add(keys.getObject(1).toString());
|
||||
}
|
||||
result.setGeneratedKeys(generatedKeys);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (SQLException e) {
|
||||
DEException.throwException("SQL ERROR: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
@@ -582,7 +596,7 @@ public class CalciteProvider extends Provider {
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return new ExecuteResult();
|
||||
}
|
||||
|
||||
private List<TableField> getField(ResultSet rs, DatasourceRequest datasourceRequest) throws Exception {
|
||||
@@ -761,6 +775,10 @@ public class CalciteProvider extends Provider {
|
||||
tableField.setPrimary(resultSet.getInt(4) > 0);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
tableField.setAutoIncrement(resultSet.getInt(5) > 0);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
tableField.setTypeNumber(tableTypeMap.get(StringUtils.lowerCase(tableField.getOriginName())));
|
||||
} catch (Exception e) {
|
||||
@@ -1069,7 +1087,7 @@ public class CalciteProvider extends Provider {
|
||||
if (database.contains(".")) {
|
||||
sql = "select * from " + datasourceRequest.getTable() + " limit 0 offset 0 ";
|
||||
} else {
|
||||
sql = String.format("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", database, datasourceRequest.getTable());
|
||||
sql = String.format("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,IF(COLUMN_KEY='PRI',1,0),IF(EXTRA LIKE '%%auto_increment%%',1,0) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", database, datasourceRequest.getTable());
|
||||
}
|
||||
break;
|
||||
case mysql:
|
||||
@@ -1086,21 +1104,21 @@ public class CalciteProvider extends Provider {
|
||||
String[] databasePrams = matcher.group(3).split("\\?");
|
||||
database = databasePrams[0];
|
||||
}
|
||||
sql = String.format("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,IF(COLUMN_KEY='PRI',1,0) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", database, datasourceRequest.getTable());
|
||||
sql = String.format("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,IF(COLUMN_KEY='PRI',1,0),IF(EXTRA LIKE '%%auto_increment%%',1,0) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", database, datasourceRequest.getTable());
|
||||
break;
|
||||
case oracle:
|
||||
configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Oracle.class);
|
||||
if (StringUtils.isEmpty(configuration.getSchema())) {
|
||||
DEException.throwException(Translator.get("i18n_schema_is_empty"));
|
||||
}
|
||||
sql = String.format("SELECT a.COLUMN_NAME , a.DATA_TYPE , b.COMMENTS ,0 FROM all_tab_columns a LEFT JOIN all_col_comments b ON a.owner = b.owner AND a.table_name = b.table_name AND a.column_name = b.column_name WHERE a.owner = '%s' AND a.table_name = '%s' ORDER BY a.table_name, a.column_id", configuration.getSchema(), datasourceRequest.getTable());
|
||||
sql = String.format("SELECT a.COLUMN_NAME, a.DATA_TYPE, b.COMMENTS , 0, 0 FROM all_tab_columns a LEFT JOIN all_col_comments b ON a.owner = b.owner AND a.table_name = b.table_name AND a.column_name = b.column_name WHERE a.owner = '%s' AND a.table_name = '%s' ORDER BY a.table_name, a.column_id", configuration.getSchema(), datasourceRequest.getTable());
|
||||
break;
|
||||
case db2:
|
||||
configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Db2.class);
|
||||
if (StringUtils.isEmpty(configuration.getSchema())) {
|
||||
DEException.throwException(Translator.get("i18n_schema_is_empty"));
|
||||
}
|
||||
sql = String.format("SELECT COLNAME , TYPENAME , REMARKS FROM SYSCAT.COLUMNS WHERE TABSCHEMA = '%s' AND TABNAME = '%s' ", configuration.getSchema(), datasourceRequest.getTable());
|
||||
sql = String.format("SELECT COLNAME, TYPENAME, REMARKS, 0, 0 FROM SYSCAT.COLUMNS WHERE TABSCHEMA = '%s' AND TABNAME = '%s' ", configuration.getSchema(), datasourceRequest.getTable());
|
||||
break;
|
||||
case sqlServer:
|
||||
configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Sqlserver.class);
|
||||
@@ -1108,7 +1126,7 @@ public class CalciteProvider extends Provider {
|
||||
DEException.throwException(Translator.get("i18n_schema_is_empty"));
|
||||
}
|
||||
|
||||
sql = String.format("SELECT \n" + " c.name ,t.name ,ep.value, 0 \n" + "FROM \n" + " sys.columns AS c\n" + "LEFT JOIN sys.extended_properties AS ep ON c.object_id = ep.major_id AND c.column_id = ep.minor_id\n" + "LEFT JOIN sys.types AS t ON c.user_type_id = t.user_type_id\n" + "LEFT JOIN sys.objects AS o ON c.object_id = o.object_id\n" + "WHERE o.name = '%s'", datasourceRequest.getTable());
|
||||
sql = String.format("SELECT \n" + " c.name ,t.name ,ep.value, 0, 0 \n" + "FROM \n" + " sys.columns AS c\n" + "LEFT JOIN sys.extended_properties AS ep ON c.object_id = ep.major_id AND c.column_id = ep.minor_id\n" + "LEFT JOIN sys.types AS t ON c.user_type_id = t.user_type_id\n" + "LEFT JOIN sys.objects AS o ON c.object_id = o.object_id\n" + "WHERE o.name = '%s'", datasourceRequest.getTable());
|
||||
break;
|
||||
case pg:
|
||||
configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), Pg.class);
|
||||
@@ -1120,12 +1138,17 @@ public class CalciteProvider extends Provider {
|
||||
" t.typname,\n" +
|
||||
" b.description AS ColumnDescription,\n" +
|
||||
" CASE\n" +
|
||||
" WHEN d.indisprimary THEN 1\n" +
|
||||
" WHEN d.indisprimary THEN 1\n" +
|
||||
" ELSE 0\n" +
|
||||
" END,\n" +
|
||||
" CASE\n" +
|
||||
" WHEN pg_get_expr(ad.adbin, ad.adrelid) LIKE 'nextval%%' THEN 1\n" +
|
||||
" ELSE 0\n" +
|
||||
" END\n" +
|
||||
"FROM\n" +
|
||||
" pg_class c\n" +
|
||||
" JOIN pg_attribute a ON a.attrelid = c.oid\n" +
|
||||
" LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum\n" +
|
||||
" LEFT JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid\n" +
|
||||
" JOIN pg_type t ON a.atttypid = t.oid\n" +
|
||||
" LEFT JOIN pg_index d ON d.indrelid = a.attrelid AND d.indisprimary AND a.attnum = ANY(d.indkey)\n" +
|
||||
@@ -1139,7 +1162,7 @@ public class CalciteProvider extends Provider {
|
||||
break;
|
||||
case redshift:
|
||||
configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), CK.class);
|
||||
sql = String.format("SELECT\n" + " a.attname AS ColumnName,\n" + " t.typname,\n" + " b.description AS ColumnDescription,\n" + " 0\n" + "FROM\n" + " pg_class c\n" + " JOIN pg_attribute a ON a.attrelid = c.oid\n" + " LEFT JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid\n" + " JOIN pg_type t ON a.atttypid = t.oid\n" + "WHERE\n" + " c.relname = '%s'\n" + " AND a.attnum > 0\n" + " AND NOT a.attisdropped\n" + "ORDER BY\n" + " a.attnum\n" + " ", datasourceRequest.getTable());
|
||||
sql = String.format("SELECT\n" + " a.attname AS ColumnName,\n" + " t.typname,\n" + " b.description AS ColumnDescription,\n" + " 0, 0\n" + "FROM\n" + " pg_class c\n" + " JOIN pg_attribute a ON a.attrelid = c.oid\n" + " LEFT JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid\n" + " JOIN pg_type t ON a.atttypid = t.oid\n" + "WHERE\n" + " c.relname = '%s'\n" + " AND a.attnum > 0\n" + " AND NOT a.attisdropped\n" + "ORDER BY\n" + " a.attnum\n" + " ", datasourceRequest.getTable());
|
||||
break;
|
||||
case ck:
|
||||
configuration = JsonUtil.parseObject(datasourceRequest.getDatasource().getConfiguration(), CK.class);
|
||||
@@ -1153,13 +1176,13 @@ public class CalciteProvider extends Provider {
|
||||
String[] databasePrams = matcher.group(3).split("\\?");
|
||||
database = databasePrams[0];
|
||||
}
|
||||
sql = String.format(" SELECT\n" + " name,\n" + " type,\n" + " comment,\n" + " 0\n" + "FROM\n" + " system.columns\n" + "WHERE\n" + " database = '%s' \n" + " AND table = '%s' ", database, datasourceRequest.getTable());
|
||||
sql = String.format(" SELECT\n" + " name,\n" + " type,\n" + " comment,\n" + " 0, 0\n" + "FROM\n" + " system.columns\n" + "WHERE\n" + " database = '%s' \n" + " AND table = '%s' ", database, datasourceRequest.getTable());
|
||||
break;
|
||||
case impala:
|
||||
sql = String.format("DESCRIBE `%s`", datasourceRequest.getTable());
|
||||
break;
|
||||
case h2:
|
||||
sql = String.format("SELECT COLUMN_NAME, DATA_TYPE, REMARKS FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s'", datasourceRequest.getTable());
|
||||
sql = String.format("SELECT COLUMN_NAME, DATA_TYPE, REMARKS, 0, 0 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s'", datasourceRequest.getTable());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -1345,7 +1368,7 @@ public class CalciteProvider extends Provider {
|
||||
if (CollectionUtils.isNotEmpty(values)) {
|
||||
PreparedStatement stat = null;
|
||||
try {
|
||||
stat = connection.prepareStatement(sql);
|
||||
stat = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stat.setQueryTimeout(queryTimeout);
|
||||
} catch (Exception e) {
|
||||
DEException.throwException(e.getMessage());
|
||||
|
||||
@@ -130,6 +130,7 @@ i18n_df_gt_check=Value needs to be greater than %s: %s
|
||||
i18n_df_le_check=Value needs to be less than or equal to %s: %s
|
||||
i18n_df_ge_check=Value needs to be greater than or equal to %s: %s
|
||||
i18n_df_column_exists=The column: %s exists
|
||||
i18n_df_table_primary_key_not_exists=Primary key does not exist in this table
|
||||
|
||||
i18n_wrong_email=Email format is incorrect
|
||||
i18n_wrong_tel=Phone number format is incorrect
|
||||
|
||||
@@ -129,6 +129,7 @@ i18n_df_gt_check=\u503C\u9700\u8981\u5927\u4E8E %s: %s
|
||||
i18n_df_le_check=\u503C\u9700\u8981\u5C0F\u4E8E\u7B49\u4E8E %s: %s
|
||||
i18n_df_ge_check=\u503C\u9700\u8981\u5927\u4E8E\u7B49\u4E8E %s: %s
|
||||
i18n_df_column_exists=\u5B57\u6BB5: %s \u5DF2\u5B58\u5728
|
||||
i18n_df_table_primary_key_not_exists=\u6B64\u8868\u4E0D\u5B58\u5728\u4E3B\u952E\uFF0C\u4E0D\u652F\u6301\u5173\u8054
|
||||
|
||||
i18n_wrong_email=\u90AE\u7BB1\u683C\u5F0F\u9519\u8BEF
|
||||
i18n_wrong_tel=\u624B\u673A\u53F7\u7801\u683C\u5F0F\u9519\u8BEF
|
||||
@@ -203,9 +204,9 @@ i18n_user_pwd_same_error=\u65B0\u65E7\u5BC6\u7801\u4E0D\u80FD\u76F8\u540C
|
||||
|
||||
i18n_copilot_ds=\u5F53\u524D\u4EC5\u652F\u6301MySQL\u6570\u636E\u6E90
|
||||
|
||||
i18n_file_download_failed=\u4e0b\u8f7d\u6587\u4ef6\u5931\u8d25\uff01
|
||||
i18n_unsupported_file_format=\u4e0d\u652f\u6301\u7684\u6587\u4ef6\u683c\u5f0f\uff01
|
||||
i18n_invalid_address=\u65e0\u6548\u7684\u5730\u5740\uff01
|
||||
i18n_unsupported_protocol=\u4e0d\u652f\u6301\u7684\u534f\u8bae\uff01
|
||||
i18n_excel_error_first_row=\u9996\u884c\u884c\u4e2d\u4e0d\u5141\u8bb8\u6709\u7a7a\u5355\u5143\u683c\uff01
|
||||
i18n_app_error_no_api=\u5f53\u524d\u4e0d\u652f\u6301API\u6570\u636e\u6e90\u3002
|
||||
i18n_file_download_failed=\u4E0B\u8F7D\u6587\u4EF6\u5931\u8D25\uFF01
|
||||
i18n_unsupported_file_format=\u4E0D\u652F\u6301\u7684\u6587\u4EF6\u683C\u5F0F\uFF01
|
||||
i18n_invalid_address=\u65E0\u6548\u7684\u5730\u5740\uFF01
|
||||
i18n_unsupported_protocol=\u4E0D\u652F\u6301\u7684\u534F\u8BAE\uFF01
|
||||
i18n_excel_error_first_row=\u9996\u884C\u884C\u4E2D\u4E0D\u5141\u8BB8\u6709\u7A7A\u5355\u5143\u683C\uFF01
|
||||
i18n_app_error_no_api=\u5F53\u524D\u4E0D\u652F\u6301API\u6570\u636E\u6E90\u3002
|
||||
|
||||
@@ -129,6 +129,7 @@ i18n_df_gt_check=\u503C\u9700\u8981\u5927\u65BC %s: %s
|
||||
i18n_df_le_check=\u503C\u9700\u8981\u5C0F\u65BC\u7B49\u65BC %s: %s
|
||||
i18n_df_ge_check=\u503C\u9700\u8981\u5927\u65BC\u7B49\u65BC %s: %s
|
||||
i18n_df_column_exists=\u5B57\u6BB5: %s \u5DF2\u5B58\u5728
|
||||
i18n_df_table_primary_key_not_exists=\u6B64\u8868\u4E0D\u5B58\u5728\u4E3B\u9375\uFF0C\u4E0D\u652F\u6301\u95DC\u806F
|
||||
|
||||
i18n_wrong_email=\u90F5\u7BB1\u683C\u5F0F\u932F\u8AA4
|
||||
i18n_wrong_tel=\u624B\u6A5F\u865F\u78BC\u683C\u5F0F\u932F\u8AA4
|
||||
@@ -203,9 +204,9 @@ i18n_user_pwd_same_error=\u65B0\u820A\u5BC6\u78BC\u4E0D\u80FD\u76F8\u540C
|
||||
|
||||
i18n_copilot_ds=\u7576\u524D\u50C5\u652F\u6301MySQL\u6578\u64DA\u6E90
|
||||
|
||||
i18n_file_download_failed=\u6587\u4ef6\u4e0b\u8f09\u5931\u6557\uff01
|
||||
i18n_unsupported_file_format=\u4e0d\u652f\u63f4\u7684\u6587\u4ef6\u683c\u5f0f\uff01
|
||||
i18n_invalid_address=\u7121\u6548\u7684\u5730\u5740\uff01
|
||||
i18n_unsupported_protocol=\u4e0d\u652f\u63f4\u7684\u5354\u8b70\uff01
|
||||
i18n_excel_error_first_row=\u9996\u884c\u884c\u4e2d\u4e0d\u5141\u8a31\u6709\u7a7a\u5132\u5b58\u683c\uff01
|
||||
i18n_app_error_no_api=\u7576\u524d\u4e0d\u652f\u63f4API\u6578\u64da\u6e90\u3002
|
||||
i18n_file_download_failed=\u6587\u4EF6\u4E0B\u8F09\u5931\u6557\uFF01
|
||||
i18n_unsupported_file_format=\u4E0D\u652F\u63F4\u7684\u6587\u4EF6\u683C\u5F0F\uFF01
|
||||
i18n_invalid_address=\u7121\u6548\u7684\u5730\u5740\uFF01
|
||||
i18n_unsupported_protocol=\u4E0D\u652F\u63F4\u7684\u5354\u8B70\uFF01
|
||||
i18n_excel_error_first_row=\u9996\u884C\u884C\u4E2D\u4E0D\u5141\u8A31\u6709\u7A7A\u5132\u5B58\u683C\uFF01
|
||||
i18n_app_error_no_api=\u7576\u524D\u4E0D\u652F\u63F4API\u6578\u64DA\u6E90\u3002
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.dataease.extensions.datasource.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ExecuteResult {
|
||||
|
||||
private int count;
|
||||
|
||||
private List<String> generatedKeys;
|
||||
}
|
||||
@@ -23,6 +23,7 @@ public class TableField implements Serializable {
|
||||
private int extField;
|
||||
private String jsonPath;
|
||||
private boolean primary;
|
||||
private boolean autoIncrement;
|
||||
List<Object> value;
|
||||
|
||||
private int inCount;
|
||||
|
||||
@@ -21,6 +21,7 @@ public class TableFieldWithValue implements Serializable {
|
||||
private String typeName;
|
||||
private Integer type;
|
||||
private String columnTypeName;
|
||||
private boolean autoIncrement;
|
||||
|
||||
private String isDateTime;
|
||||
private String dateFormat;
|
||||
@@ -30,6 +31,7 @@ public class TableFieldWithValue implements Serializable {
|
||||
public static TableFieldWithValue copy(TableFieldWithValue tableFieldWithValue) {
|
||||
return new TableFieldWithValue()
|
||||
.setValue(tableFieldWithValue.getValue())
|
||||
.setAutoIncrement(tableFieldWithValue.isAutoIncrement())
|
||||
.setFiledName(tableFieldWithValue.getFiledName())
|
||||
.setTypeName(tableFieldWithValue.getTypeName())
|
||||
.setType(tableFieldWithValue.getType())
|
||||
|
||||
@@ -97,8 +97,8 @@ public abstract class Provider {
|
||||
|
||||
}
|
||||
|
||||
public int executeUpdate(DatasourceRequest datasourceRequest) {
|
||||
return 0;
|
||||
public ExecuteResult executeUpdate(DatasourceRequest datasourceRequest, boolean autoIncrement) {
|
||||
return new ExecuteResult();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user