mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
add convert method; close #I781LP
This commit is contained in:
parent
3eae0c6bea
commit
e6c896f14a
@ -65,6 +65,11 @@ public class QueryMethods {
|
||||
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) {
|
||||
return new StringQueryColumn(column);
|
||||
}
|
||||
|
||||
@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -306,6 +306,22 @@ public class AccountSqlTester {
|
||||
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
|
||||
public void testLimitOffset() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user