org.jooq.util.postgres.PostgresDSL Maven / Gradle / Ivy
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.util.postgres;
import static org.jooq.SQLDialect.POSTGRES;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.SQLDialect;
import org.jooq.Select;
import org.jooq.Support;
import org.jooq.Table;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
/**
* The {@link SQLDialect#POSTGRES} specific DSL.
*
* @author Lukas Eder
*/
public class PostgresDSL extends DSL {
/**
* No instances
*/
protected PostgresDSL() {}
// -------------------------------------------------------------------------
// PostgreSQL-specific array functions
// -------------------------------------------------------------------------
/**
* The PostgreSQL array(select)
function.
*
* Example:
* {1, 2, 3} = array(select 1 union select 2 union select 3)
*
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Support({ POSTGRES })
public static Field array(Select extends Record1> select) {
return DSL.field("array({0})", (DataType) select.getSelect().get(0).getDataType().getArrayDataType(), select);
}
/**
* The PostgreSQL array_append(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_append(ARRAY[1, 2], 3)
*
*/
@Support({ POSTGRES })
public static Field arrayAppend(T[] array, T value) {
return arrayAppend0(val(array), val(value));
}
/**
* The PostgreSQL array_append(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_append(ARRAY[1, 2], 3)
*
*/
@Support({ POSTGRES })
public static Field arrayAppend(T[] array, Field value) {
return arrayAppend0(val(array), value);
}
/**
* The PostgreSQL array_append(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_append(ARRAY[1, 2], 3)
*
*/
@Support({ POSTGRES })
public static Field arrayAppend(Field array, T value) {
return arrayAppend0(array, val(value));
}
/**
* The PostgreSQL array_append(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_append(ARRAY[1, 2], 3)
*
*/
@Support({ POSTGRES })
public static Field arrayAppend(Field array, Field value) {
return arrayAppend0(array, value);
}
// Java 8 is stricter than Java 7 with respect to generics and overload
// resolution (http://stackoverflow.com/q/5361513/521799)
static Field arrayAppend0(Field array, Field value) {
return field("{array_append}({0}, {1})", nullSafe(array).getDataType(), nullSafe(array), nullSafe(value));
}
/**
* The PostgreSQL array_prepend(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_prepend(1, ARRAY[2, 3])
*
*/
@Support({ POSTGRES })
public static Field arrayPrepend(T value, T[] array) {
return arrayPrepend0(val(value), val(array));
}
/**
* The PostgreSQL array_prepend(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_prepend(1, ARRAY[2, 3])
*
*/
@Support({ POSTGRES })
public static Field arrayPrepend(Field value, T[] array) {
return arrayPrepend0(value, val(array));
}
/**
* The PostgreSQL array_prepend(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_prepend(1, ARRAY[2, 3])
*
*/
@Support({ POSTGRES })
public static Field arrayPrepend(T value, Field array) {
return arrayPrepend0(val(value), array);
}
/**
* The PostgreSQL array_prepend(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3} = array_prepend(1, ARRAY[2, 3])
*
*/
@Support({ POSTGRES })
public static Field arrayPrepend(Field value, Field array) {
return arrayPrepend0(value, array);
}
// Java 8 is stricter than Java 7 with respect to generics and overload
// resolution (http://stackoverflow.com/q/5361513/521799)
static Field arrayPrepend0(Field value, Field array) {
return field("{array_prepend}({0}, {1})", nullSafe(array).getDataType(), nullSafe(value), nullSafe(array));
}
/**
* The PostgreSQL array_cat(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3, 4, 5} = array_cat(ARRAY[1, 2], ARRAY[3, 4, 5])
*
*/
@Support({ POSTGRES })
public static Field arrayCat(T[] array1, T[] array2) {
return arrayCat(val(array1), val(array2));
}
/**
* The PostgreSQL array_cat(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3, 4, 5} = array_cat(ARRAY[1, 2], ARRAY[3, 4, 5])
*
*/
@Support({ POSTGRES })
public static Field arrayCat(T[] array1, Field array2) {
return arrayCat(val(array1), array2);
}
/**
* The PostgreSQL array_cat(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3, 4, 5} = array_cat(ARRAY[1, 2], ARRAY[3, 4, 5])
*
*/
@Support({ POSTGRES })
public static Field arrayCat(Field array1, T[] array2) {
return arrayCat(array1, val(array2));
}
/**
* The PostgreSQL array_cat(anyarray, anyelement)
function.
*
* Example:
* {1, 2, 3, 4, 5} = array_cat(ARRAY[1, 2], ARRAY[3, 4, 5])
*
*/
@Support({ POSTGRES })
public static Field arrayCat(Field array1, Field array2) {
return field("{array_cat}({0}, {1})", nullSafe(array1).getDataType(), nullSafe(array1), nullSafe(array2));
}
/**
* The PostgreSQL array_remove(anyarray, anyelement)
function.
*
* Example:
* {1,3} = array_remove(ARRAY[1,2,3,2], 2)
*
*/
@Support({ POSTGRES })
public static Field arrayRemove(T[] array, T element) {
return arrayRemove0(val(array), val(element));
}
/**
* The PostgreSQL array_remove(anyarray, anyelement)
function.
*
* Example:
* {1,3} = array_remove(ARRAY[1,2,3,2], 2)
*
*/
@Support({ POSTGRES })
public static Field arrayRemove(Field array, T element) {
return arrayRemove0(nullSafe(array), val(element));
}
/**
* The PostgreSQL array_remove(anyarray, anyelement)
function.
*
* Example:
* {1,3} = array_remove(ARRAY[1,2,3,2], 2)
*
*/
@Support({ POSTGRES })
public static Field arrayRemove(T[] array, Field element) {
return arrayRemove0(val(array), nullSafe(element));
}
/**
* The PostgreSQL array_remove(anyarray, anyelement)
function.
*
* Example:
* {1,3} = array_remove(ARRAY[1,2,3,2], 2)
*
*/
@Support({ POSTGRES })
public static Field arrayRemove(Field array, Field element) {
return arrayRemove0(array, element);
}
// Java 8 is stricter than Java 7 with respect to generics and overload
// resolution (http://stackoverflow.com/q/5361513/521799)
static Field arrayRemove0(Field array, Field element) {
return field("{array_remove}({0}, {1})", array.getDataType(), array, element);
}
/**
* The PostgreSQL
* array_replace(anyarray, anyelement, anyelement)
function.
*
* Example:
* {1,2,3,4} = array_replace(ARRAY[1,2,5,4], 5, 3)
*
*/
@Support({ POSTGRES })
public static Field arrayReplace(T[] array, T search, T replace) {
return arrayReplace0(val(array), val(search), val(replace));
}
/**
* The PostgreSQL
* array_replace(anyarray, anyelement, anyelement)
function.
*
* Example:
* {1,2,3,4} = array_replace(ARRAY[1,2,5,4], 5, 3)
*
*/
@Support({ POSTGRES })
public static Field arrayReplace(T[] array, Field search, Field replace) {
return arrayReplace0(val(array), nullSafe(search), nullSafe(replace));
}
/**
* The PostgreSQL
* array_replace(anyarray, anyelement, anyelement)
function.
*
* Example:
* {1,2,3,4} = array_replace(ARRAY[1,2,5,4], 5, 3)
*
*/
@Support({ POSTGRES })
public static Field arrayReplace(Field array, T search, T replace) {
return arrayReplace0(nullSafe(array), val(search), val(replace));
}
/**
* The PostgreSQL
* array_replace(anyarray, anyelement, anyelement)
function.
*
* Example:
* {1,2,3,4} = array_replace(ARRAY[1,2,5,4], 5, 3)
*
*/
@Support({ POSTGRES })
public static Field arrayReplace(Field array, Field search, Field replace) {
return arrayReplace0(array, search, replace);
}
// Java 8 is stricter than Java 7 with respect to generics and overload
// resolution (http://stackoverflow.com/q/5361513/521799)
static Field arrayReplace0(Field array, Field search, Field replace) {
return field("{array_replace}({0}, {1}, {2})", array.getDataType(), nullSafe(array), nullSafe(search), nullSafe(replace));
}
/**
* The PostgreSQL array_fill(anyelement, int[])
function.
*
* Example:
* {7,7,7} = array_fill(7, ARRAY[3])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(T value, Integer[] dimensions) {
return arrayFill(val(value), val(dimensions));
}
/**
* The PostgreSQL array_fill(anyelement, int[])
function.
*
* Example:
* {7,7,7} = array_fill(7, ARRAY[3])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(Field value, Integer[] dimensions) {
return arrayFill(nullSafe(value), val(dimensions));
}
/**
* The PostgreSQL array_fill(anyelement, int[])
function.
*
* Example:
* {7,7,7} = array_fill(7, ARRAY[3])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(T value, Field dimensions) {
return arrayFill(val(value), nullSafe(dimensions));
}
/**
* The PostgreSQL array_fill(anyelement, int[])
function.
*
* Example:
* {7,7,7} = array_fill(7, ARRAY[3])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(Field value, Field dimensions) {
return field("{array_fill}({0}, {1})", nullSafe(value).getDataType().getArrayDataType(), nullSafe(value), nullSafe(dimensions));
}
/**
* The PostgreSQL array_fill(anyelement, int[], int[])
function.
*
* Example:
* [2:4]={7,7,7} = array_fill(7, ARRAY[3], ARRAY[2])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(T value, Integer[] dimensions, Integer[] bounds) {
return arrayFill(val(value), val(dimensions), val(bounds));
}
/**
* The PostgreSQL array_fill(anyelement, int[], int[])
function.
*
* Example:
* [2:4]={7,7,7} = array_fill(7, ARRAY[3], ARRAY[2])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(Field value, Integer[] dimensions, Integer[] bounds) {
return arrayFill(nullSafe(value), val(dimensions), val(bounds));
}
/**
* The PostgreSQL array_fill(anyelement, int[], int[])
function.
*
* Example:
* [2:4]={7,7,7} = array_fill(7, ARRAY[3], ARRAY[2])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(T value, Field dimensions, Field bounds) {
return arrayFill(val(value), nullSafe(dimensions), nullSafe(bounds));
}
/**
* The PostgreSQL array_fill(anyelement, int[], int[])
function.
*
* Example:
* [2:4]={7,7,7} = array_fill(7, ARRAY[3], ARRAY[2])
*
*/
@Support({ POSTGRES })
public static Field arrayFill(Field value, Field dimensions, Field bounds) {
return field("{array_fill}({0}, {1})", nullSafe(value).getDataType().getArrayDataType(), nullSafe(value), nullSafe(dimensions), nullSafe(bounds));
}
/**
* The PostgreSQL array_length(anyarray, int)
function.
*
* jOOQ currently doesn't support multi-dimensional arrays, so the dimension
* will always be 1
.
*
* Example:
* 3 = array_length(array[1,2,3], 1)
*
*/
@Support({ POSTGRES })
public static Field arrayLength(Object[] array) {
return arrayLength(val(array));
}
/**
* The PostgreSQL array_length(anyarray, int)
function.
*
* jOOQ currently doesn't support multi-dimensional arrays, so the dimension
* will always be 1
.
*
* Example:
* 3 = array_length(array[1,2,3], 1)
*
*/
@Support({ POSTGRES })
public static Field arrayLength(Field extends Object[]> array) {
return field("{array_length}({0}, 1)", SQLDataType.INTEGER, array);
}
/**
* The PostgreSQL array_to_string(anyarray, delimiter)
function.
*
* Example:
* '1~^~2~^~3' = array_to_string(ARRAY[1, 2, 3], '~^~')
*
*/
@Support({ POSTGRES })
public static Field arrayToString(Object[] array, String delimiter) {
return arrayToString(val(array), val(delimiter, String.class));
}
/**
* The PostgreSQL array_to_string(anyarray, delimiter)
function.
*
* Example:
* '1~^~2~^~3' = array_to_string(ARRAY[1, 2, 3], '~^~')
*
*/
@Support({ POSTGRES })
public static Field arrayToString(Object[] array, Field delimiter) {
return arrayToString(val(array), delimiter);
}
/**
* The PostgreSQL array_to_string(anyarray, delimiter)
function.
*
* Example:
* '1~^~2~^~3' = array_to_string(ARRAY[1, 2, 3], '~^~')
*
*/
@Support({ POSTGRES })
public static Field arrayToString(Field extends Object[]> array, String delimiter) {
return arrayToString(array, val(delimiter, String.class));
}
/**
* The PostgreSQL array_to_string(anyarray, delimiter)
function.
*
* Example:
* '1~^~2~^~3' = array_to_string(ARRAY[1, 2, 3], '~^~')
*
*/
@Support({ POSTGRES })
public static Field arrayToString(Field extends Object[]> array, Field delimiter) {
return field("{array_to_string}({0}, {1})", SQLDataType.VARCHAR, nullSafe(array), nullSafe(delimiter));
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(String string, String delimiter) {
return stringToArray(val(string, String.class), val(delimiter, String.class));
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(String string, Field delimiter) {
return stringToArray(val(string, String.class), delimiter);
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(Field string, String delimiter) {
return stringToArray(string, val(delimiter, String.class));
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(Field string, Field delimiter) {
return field("{string_to_array}({0}, {1})", SQLDataType.VARCHAR.getArrayDataType(), nullSafe(string), nullSafe(delimiter));
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(String string, String delimiter, String nullString) {
return stringToArray(val(string, String.class), val(delimiter, String.class), val(nullString, String.class));
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(String string, Field delimiter, Field nullString) {
return stringToArray(val(string, String.class), delimiter, nullString);
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(Field string, String delimiter, String nullString) {
return stringToArray(string, val(delimiter, String.class), val(nullString, String.class));
}
/**
* The PostgreSQL string_to_array(anyarray, delimiter)
function.
*
* Example:
* {xx,NULL,zz} = string_to_array('xx~^~yy~^~zz', '~^~', 'yy')
*
*/
@Support({ POSTGRES })
public static Field stringToArray(Field string, Field delimiter, Field nullString) {
return field("{string_to_array}({0}, {1}, {2})", SQLDataType.VARCHAR.getArrayDataType(), nullSafe(string), nullSafe(delimiter), nullSafe(nullString));
}
// -------------------------------------------------------------------------
// Other PostgreSQL-specific functions / clauses
// -------------------------------------------------------------------------
/**
* Get the PostgreSQL-specific ONLY [table]
clause for use with
* table inheritance.
*
* Example:
* SELECT * FROM ONLY parent_table
*
*/
@Support({ POSTGRES })
public static Table only(Table> table) {
return table("{only} {0}", table);
}
/**
* Get the PostgreSQL-specific [table].oid
column from any table.
*/
@Support({ POSTGRES })
public static Field oid(Table> table) {
return field("{0}.oid", Long.class, table);
}
}