diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java index fc96ece8c1..445372d339 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/FunctionTests.java @@ -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> 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 { diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 38706c1e91..3596d51844 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -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(); diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 33088099d3..d14dadcc67 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -6884,6 +6884,34 @@ public class DSL { return new Repeat(nullSafe(field), nullSafe(count)); } + /** + * Get the SQL Server specific SPACE() function. + *

+ * This function can be emulated using {@link #repeat(String, int)} in + * dialects that do not ship with a native SPACE() function. + * + * @see http://technet.microsoft.com/en-us/library/ms187950.aspx + */ + @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + public static Field space(int value) { + return space(val(value)); + } + + /** + * Get the SQL Server specific SPACE() function. + *

+ * This function can be emulated using {@link #repeat(String, int)} in + * dialects that do not ship with a native SPACE() function. + * + * @see http://technet.microsoft.com/en-us/library/ms187950.aspx + */ + @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + public static Field space(Field value) { + return new Space(nullSafe(value)); + } + /** * Get the reverse(field) function. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/Space.java b/jOOQ/src/main/java/org/jooq/impl/Space.java new file mode 100644 index 0000000000..92f7af1b81 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Space.java @@ -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 { + + /** + * Generated UID + */ + private static final long serialVersionUID = -4239524454814412161L; + + private final Field count; + + Space(Field 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); + } + } + +}