Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: date type variable support format #1024

Merged
merged 3 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/main/java/datart/core/common/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package datart.core.common;

import org.apache.commons.lang3.StringUtils;

public class DateUtils {

private static final String[] FMT = {"y", "M", "d", "H", "m", "s", "S"};
Expand Down Expand Up @@ -46,4 +48,12 @@ public static String inferDateFormat(String src) {
return stringBuilder.toString();
}

public static boolean isDateFormat(String format) {
return StringUtils.isNotBlank(format) && "yyyy-MM-dd".equalsIgnoreCase(format.trim());
}

public static boolean isDateTimeFormat(String format) {
return StringUtils.isNotBlank(format) && "yyyy-MM-dd HH:mm:ss".equalsIgnoreCase(format.trim());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class ScriptVariable extends TypedValue {
// Permission variable valid flag, which is false when executed by the organization owner
private boolean disabled;

private String format;

@Override
public String toString() {
if (values == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class SingleTypedValue extends TypedValue {

private Object value;

private String format;

public SingleTypedValue(Object value, ValueType valueType) {
this.value = value;
this.valueType = valueType;
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/datart/core/entity/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ public class Variable extends BaseEntity {

private String viewId;

private String sourceId;

private String name;

private String type;

private String valueType;

private String format;

private Boolean encrypt;

private String label;
Expand Down
19 changes: 13 additions & 6 deletions core/src/main/java/datart/core/mappers/VariableMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ public interface VariableMapper extends CRUDMapper {

@Insert({
"insert into variable (id, org_id, ",
"view_id, `name`, `type`, ",
"value_type, permission, ",
"view_id, source_id, ",
"`name`, `type`, value_type, ",
"format, permission, ",
"encrypt, `label`, ",
"default_value, expression, ",
"create_time, create_by, ",
"update_time, update_by)",
"values (#{id,jdbcType=VARCHAR}, #{orgId,jdbcType=VARCHAR}, ",
"#{viewId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, ",
"#{valueType,jdbcType=VARCHAR}, #{permission,jdbcType=INTEGER}, ",
"#{viewId,jdbcType=VARCHAR}, #{sourceId,jdbcType=VARCHAR}, ",
"#{name,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{valueType,jdbcType=VARCHAR}, ",
"#{format,jdbcType=VARCHAR}, #{permission,jdbcType=INTEGER}, ",
"#{encrypt,jdbcType=TINYINT}, #{label,jdbcType=VARCHAR}, ",
"#{defaultValue,jdbcType=VARCHAR}, #{expression,jdbcType=TINYINT}, ",
"#{createTime,jdbcType=TIMESTAMP}, #{createBy,jdbcType=VARCHAR}, ",
Expand All @@ -42,18 +44,21 @@ public interface VariableMapper extends CRUDMapper {

@Select({
"select",
"id, org_id, view_id, `name`, `type`, value_type, permission, encrypt, `label`, ",
"default_value, expression, create_time, create_by, update_time, update_by",
"id, org_id, view_id, source_id, `name`, `type`, value_type, format, permission, ",
"encrypt, `label`, default_value, expression, create_time, create_by, update_time, ",
"update_by",
"from variable",
"where id = #{id,jdbcType=VARCHAR}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
@Result(column="org_id", property="orgId", jdbcType=JdbcType.VARCHAR),
@Result(column="view_id", property="viewId", jdbcType=JdbcType.VARCHAR),
@Result(column="source_id", property="sourceId", jdbcType=JdbcType.VARCHAR),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
@Result(column="value_type", property="valueType", jdbcType=JdbcType.VARCHAR),
@Result(column="format", property="format", jdbcType=JdbcType.VARCHAR),
@Result(column="permission", property="permission", jdbcType=JdbcType.INTEGER),
@Result(column="encrypt", property="encrypt", jdbcType=JdbcType.TINYINT),
@Result(column="label", property="label", jdbcType=JdbcType.VARCHAR),
Expand All @@ -73,9 +78,11 @@ public interface VariableMapper extends CRUDMapper {
"update variable",
"set org_id = #{orgId,jdbcType=VARCHAR},",
"view_id = #{viewId,jdbcType=VARCHAR},",
"source_id = #{sourceId,jdbcType=VARCHAR},",
"`name` = #{name,jdbcType=VARCHAR},",
"`type` = #{type,jdbcType=VARCHAR},",
"value_type = #{valueType,jdbcType=VARCHAR},",
"format = #{format,jdbcType=VARCHAR},",
"permission = #{permission,jdbcType=INTEGER},",
"encrypt = #{encrypt,jdbcType=TINYINT},",
"`label` = #{label,jdbcType=VARCHAR},",
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/datart/core/mappers/VariableSqlProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public String insertSelective(Variable record) {
sql.VALUES("view_id", "#{viewId,jdbcType=VARCHAR}");
}

if (record.getSourceId() != null) {
sql.VALUES("source_id", "#{sourceId,jdbcType=VARCHAR}");
}

if (record.getName() != null) {
sql.VALUES("`name`", "#{name,jdbcType=VARCHAR}");
}
Expand All @@ -32,6 +36,10 @@ public String insertSelective(Variable record) {
sql.VALUES("value_type", "#{valueType,jdbcType=VARCHAR}");
}

if (record.getFormat() != null) {
sql.VALUES("format", "#{format,jdbcType=VARCHAR}");
}

if (record.getPermission() != null) {
sql.VALUES("permission", "#{permission,jdbcType=INTEGER}");
}
Expand Down Expand Up @@ -83,6 +91,10 @@ public String updateByPrimaryKeySelective(Variable record) {
sql.SET("view_id = #{viewId,jdbcType=VARCHAR}");
}

if (record.getSourceId() != null) {
sql.SET("source_id = #{sourceId,jdbcType=VARCHAR}");
}

if (record.getName() != null) {
sql.SET("`name` = #{name,jdbcType=VARCHAR}");
}
Expand All @@ -95,6 +107,10 @@ public String updateByPrimaryKeySelective(Variable record) {
sql.SET("value_type = #{valueType,jdbcType=VARCHAR}");
}

if (record.getFormat() != null) {
sql.SET("format = #{format,jdbcType=VARCHAR}");
}

if (record.getPermission() != null) {
sql.SET("permission = #{permission,jdbcType=INTEGER}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
package datart.data.provider.calcite;

import datart.core.base.exception.Exceptions;
import datart.core.common.DateUtils;
import datart.core.data.provider.ScriptVariable;
import datart.core.data.provider.SingleTypedValue;
import datart.data.provider.calcite.custom.SqlSimpleStringLiteral;
import org.apache.calcite.sql.*;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.DateString;
import org.apache.calcite.util.TimestampString;
import org.apache.commons.collections4.CollectionUtils;

Expand Down Expand Up @@ -75,11 +77,9 @@ public static SqlNode toSingleSqlLiteral(ScriptVariable variable, SqlParserPos s
}

public static List<SqlNode> createSqlNodes(ScriptVariable variable, SqlParserPos sqlParserPos) {

if (CollectionUtils.isEmpty(variable.getValues())) {
return Collections.singletonList(SqlLiteral.createNull(sqlParserPos));
}

switch (variable.getValueType()) {
case STRING:
return variable.getValues().stream()
Expand All @@ -93,7 +93,7 @@ public static List<SqlNode> createSqlNodes(ScriptVariable variable, SqlParserPos
SqlLiteral.createBoolean(Boolean.parseBoolean(v), sqlParserPos)).collect(Collectors.toList());
case DATE:
return variable.getValues().stream().map(v ->
SqlLiteral.createTimestamp(new TimestampString(v), 0, sqlParserPos))
createDateSqlNode(v, variable.getFormat()))
.collect(Collectors.toList());
case FRAGMENT:
return variable.getValues().stream().map(SqlFragment::new).collect(Collectors.toList());
Expand All @@ -112,7 +112,7 @@ public static SqlNode createSqlNode(SingleTypedValue value, String... names) {
case BOOLEAN:
return SqlLiteral.createBoolean(Boolean.parseBoolean(value.getValue().toString()), SqlParserPos.ZERO);
case DATE:
return SqlLiteral.createTimestamp(new TimestampString(value.getValue().toString()), 0, SqlParserPos.ZERO);
return createDateSqlNode(value.getValue().toString(), value.getFormat());
case FRAGMENT:
return new SqlFragment(value.getValue().toString());
case IDENTIFIER:
Expand All @@ -127,6 +127,15 @@ public static SqlNode createSqlNode(SingleTypedValue value) {
return createSqlNode(value, null);
}

private static SqlNode createDateSqlNode(String value, String format) {
if (DateUtils.isDateFormat(format)) {
return SqlLiteral.createDate(new DateString(value), SqlParserPos.ZERO);
} else if (DateUtils.isDateTimeFormat(format)) {
return SqlLiteral.createTimestamp(new TimestampString(value), 0, SqlParserPos.ZERO);
} else {
return new SqlSimpleStringLiteral(value);
}
}

/**
* SQL 输出时,字段名称要默认加上引号,否则对于特殊字段名称无法处理,以及pg数据库无法正常执行等问题。
Expand Down
28 changes: 28 additions & 0 deletions server/src/main/java/datart/server/base/ExternalRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Datart
* <p>
* Copyright 2021
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package datart.server.base;

public class ExternalRegister {

private ExternalRegisterType type;

private String name;


}
28 changes: 28 additions & 0 deletions server/src/main/java/datart/server/base/ExternalRegisterType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Datart
* <p>
* Copyright 2021
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package datart.server.base;

public enum ExternalRegisterType {

OAUTH2,

LDAP,

SAML

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class VariableValue {

private String viewId;

private String sourceId;

private String format;

private String name;

private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ public class VariableCreateParam extends BaseCreateParam {

private String viewId;

private String sourceId;

private String name;

private String type;

private String format;

private String valueType;

private String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public class VariableUpdateParam extends BaseUpdateParam {

private String viewId;

private String sourceId;

private String name;

private String type;

private String valueType;

private String format;

private String label;

private String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void configure(WebSecurity web) throws Exception {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.headers().frameOptions().disable();
if (this.oAuth2ClientProperties != null) {
http
.authorizeRequests()
Expand Down
Loading