[#2971] Add DSL.space() to support the SQL Server specific SPACE() function

This commit is contained in:
Lukas Eder 2014-01-22 11:40:10 +01:00
parent 9305e305d3
commit 755eaa28ea
4 changed files with 154 additions and 0 deletions

View File

@ -120,6 +120,7 @@ import static org.jooq.impl.DSL.shr;
import static org.jooq.impl.DSL.sign;
import static org.jooq.impl.DSL.sin;
import static org.jooq.impl.DSL.sinh;
import static org.jooq.impl.DSL.space;
import static org.jooq.impl.DSL.sqrt;
import static org.jooq.impl.DSL.substring;
import static org.jooq.impl.DSL.tan;
@ -608,6 +609,32 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
}
@Test
public void testFunctionsOnStrings_SPACE() throws Exception {
switch (dialect()) {
case DERBY:
log.info("SKIPPING", "SPACE function");
break;
default: {
Result<Record2<String, String>> result =
create().select(
space(1),
space(TAuthor_ID().mul(2)))
.from(TAuthor())
.orderBy(TAuthor_ID())
.fetch();
assertEquals(" ", result.get(0).value1());
assertEquals(" ", result.get(0).value2());
assertEquals(" ", result.get(1).value1());
assertEquals(" ", result.get(1).value2());
break;
}
}
}
@Test
public void testFunctionsOnStrings_REVERSE() throws Exception {

View File

@ -2191,6 +2191,11 @@ public abstract class jOOQAbstractTest<
new FunctionTests(this).testFunctionsOnStrings_REPEAT();
}
@Test
public void testFunctionsOnStrings_SPACE() throws Exception {
new FunctionTests(this).testFunctionsOnStrings_SPACE();
}
@Test
public void testFunctionsOnStrings_REVERSE() throws Exception {
new FunctionTests(this).testFunctionsOnStrings_REVERSE();

View File

@ -6884,6 +6884,34 @@ public class DSL {
return new Repeat(nullSafe(field), nullSafe(count));
}
/**
* Get the SQL Server specific <code>SPACE()</code> function.
* <p>
* This function can be emulated using {@link #repeat(String, int)} in
* dialects that do not ship with a native <code>SPACE()</code> function.
*
* @see <a
* href="http://technet.microsoft.com/en-us/library/ms187950.aspx">http://technet.microsoft.com/en-us/library/ms187950.aspx</a>
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
public static Field<String> space(int value) {
return space(val(value));
}
/**
* Get the SQL Server specific <code>SPACE()</code> function.
* <p>
* This function can be emulated using {@link #repeat(String, int)} in
* dialects that do not ship with a native <code>SPACE()</code> function.
*
* @see <a
* href="http://technet.microsoft.com/en-us/library/ms187950.aspx">http://technet.microsoft.com/en-us/library/ms187950.aspx</a>
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
public static Field<String> space(Field<Integer> value) {
return new Space(nullSafe(value));
}
/**
* Get the <code>reverse(field)</code> function.
*/

View File

@ -0,0 +1,94 @@
/**
* Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.impl;
import org.jooq.Configuration;
import org.jooq.Field;
import org.jooq.QueryPart;
class Space extends AbstractFunction<String> {
/**
* Generated UID
*/
private static final long serialVersionUID = -4239524454814412161L;
private final Field<Integer> count;
Space(Field<Integer> count) {
super("space", SQLDataType.VARCHAR, count);
this.count = count;
}
@Override
final QueryPart getFunction0(Configuration configuration) {
switch (configuration.dialect().family()) {
/* [pro] xx
xxxx xxxx
xxxx xxxxxxx
xxxx xxxx
xxxx xxxxxxxxxx
xxxx xxxxxxx
xx [/pro] */
case CUBRID:
case MARIADB:
case MYSQL:
case H2:
return DSL.field("{space}({0})", getDataType(), count);
/* [pro] xx
xxxx xxxxxxx
xxxx xxxxxxx
xx [/pro] */
case DERBY:
case FIREBIRD:
case HSQLDB:
case POSTGRES:
case SQLITE:
default:
return DSL.repeat(DSL.inline(" "), count);
}
}
}