Skip to content

FunctionTranslate

Yong Zhu edited this page Jan 20, 2018 · 1 revision

Function translate

Use translate method:

System.out.println(Dialect.MySQL55Dialect.translate("select concat('a','b','c'), current_time() from user_tb"));
System.out.println(Dialect.Oracle12cDialect.translate("select concat('a','b','c'), current_time() from user_tb"));
System.out.println(Dialect.SQLServer2008Dialect.translate("select concat('a','b','c'), current_time() from user_tb"));

Result:

select concat('a', 'b', 'c'), current_time from user_tb
select 'a'||'b'||'c', current_timestamp from user_tb
select ('a'+'b'+'c'), getdate() from user_tb

For more detail of databases supported functions, can see DatabaseDialects.xls file.

Function Prefix

Sometime if only want translate functions with a prefix, can use Dialect.setSqlFunctionPrefix("xxx") method,like:

Dialect.setSqlFunctionPrefix("#");
System.out.println(Dialect.Oracle12cDialect.translate("select #concat('a','b','c'), current_time()  from user_tb"));

Result:
select 'a'||'b'||'c', current_time() from user_tb

Use Dialect.setSqlFunctionPrefix(null) to cancel prefix, note setSqlFunctionPrefix method is a static global method, only need call one time at program starting.

Pagination + function translate

Use paginAndTrans method:

System.out.println(Dialect.Oracle12cDialect.paginAndTrans(3, 10,
                "select concat('a','b','c'), current_time() from user_tb"));

result:
select 'a'||'b'||'c', current_timestamp from user_tb offset 20 rows fetch next 10 rows only