add convert method; close #I781LP

This commit is contained in:
开源海哥 2023-05-25 11:35:06 +08:00
parent 3eae0c6bea
commit e6c896f14a
3 changed files with 106 additions and 2 deletions

View File

@ -65,6 +65,11 @@ public class QueryMethods {
return new DistinctQueryColumn(columns); return new DistinctQueryColumn(columns);
} }
//CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
public static StringFunctionQueryColumn convert(String... params) {
return new StringFunctionQueryColumn("CONVERT", params);
}
public static StringQueryColumn column(String column) { public static StringQueryColumn column(String column) {
return new StringQueryColumn(column); return new StringQueryColumn(column);
} }
@ -85,7 +90,7 @@ public class QueryMethods {
return new OperatorQueryCondition(" NOT ", childCondition); return new OperatorQueryCondition(" NOT ", childCondition);
} }
public static QueryCondition noCondition(){ public static QueryCondition noCondition() {
return QueryCondition.createEmpty(); return QueryCondition.createEmpty();
} }
@ -110,7 +115,7 @@ public class QueryMethods {
return select(count("1")); return select(count("1"));
} }
public static RawValue raw(String raw){ public static RawValue raw(String raw) {
return new RawValue(raw); return new RawValue(raw);
} }

View File

@ -0,0 +1,83 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <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 com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.util.SqlUtil;
import com.mybatisflex.core.util.StringUtil;
import java.util.Arrays;
import java.util.List;
/**
* 数据库 聚合函数例如 CONVERT(NVARCHAR(30), GETDATE(), 126) 等等
*/
public class StringFunctionQueryColumn extends QueryColumn {
protected String fnName;
protected List<String> params;
public StringFunctionQueryColumn(String fnName, String ...params) {
SqlUtil.keepColumnSafely(fnName);
this.fnName = fnName;
this.params = Arrays.asList(params);
}
public String getFnName() {
return fnName;
}
public void setFnName(String fnName) {
this.fnName = fnName;
}
public List<String> getParams() {
return params;
}
public void setParams(List<String> params) {
this.params = params;
}
@Override
public String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = StringUtil.join(", ",params);
return StringUtil.isBlank(sql) ? "" : fnName + "(" + sql + ")" + WrapperUtil.buildAsAlias(alias);
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = StringUtil.join(", ",params);
return StringUtil.isBlank(sql) ? "" : fnName + "(" + sql + ")";
}
@Override
public QueryColumn as(String alias) {
SqlUtil.keepColumnSafely(alias);
this.alias = alias;
return this;
}
@Override
public String toString() {
return "StringFunctionQueryColumn{" +
"fnName='" + fnName + '\'' +
", params=" + params +
'}';
}
}

View File

@ -306,6 +306,22 @@ public class AccountSqlTester {
System.out.println(sql); System.out.println(sql);
} }
@Test
public void testConvert() {
IDialect dialect = new CommonsDialectImpl();
QueryWrapper queryWrapper = QueryWrapper.create()
.select(ACCOUNT.ALL_COLUMNS, convert("NVARCHAR(30)", "GETDATE()", "126").as("result"))
.from(ACCOUNT)
.and(ACCOUNT.USER_NAME.like("michael"))
.and(convert("NVARCHAR(30)", "GETDATE()", "126").in(
select(ACCOUNT.ID).from(ACCOUNT).where(ACCOUNT.ID.ge(100)))
);
String sql = dialect.forSelectByQuery(queryWrapper);
System.out.println(sql);
}
@Test @Test
public void testLimitOffset() { public void testLimitOffset() {