All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooq.impl.ArrayTable Maven / Gradle / Ivy

Go to download

A Java implementation of a KillBill Payment Plugin that uses Authorize.Net as a payment gateway

There is a newer version: 2.8.196
Show newest version
/**
 * Copyright (c) 2009-2015, 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.impl;

import static org.jooq.impl.DSL.name;

import java.util.ArrayList;
import java.util.List;

// ...
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Param;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.UDTRecord;
import org.jooq.exception.DataTypeException;
import org.jooq.util.h2.H2DataType;

/**
 * An unnested array
 *
 * @author Lukas Eder
 */
class ArrayTable extends AbstractTable {

    /**
     * Generated UID
     */
    private static final long    serialVersionUID = 2380426377794577041L;

    private final Field       array;
    private final Fields field;
    private final String         alias;

    ArrayTable(Field array) {
        this(array, "array_table");
    }

    @SuppressWarnings({ "unchecked" })
    ArrayTable(Field array, String alias) {
        super(alias);

        Class arrayType;

        // TODO [#523] Solve this in a more object-oriented way...
        if (array.getDataType().getType().isArray()) {
            arrayType = array.getDataType().getType().getComponentType();
        }

        /* [pro] xx
        xx xxxxxxx xxxx xxxxx xx xxxxxxx xxxx xxxxxxxxxxx xx xxxxxx xxxxxx x xxxxx xxxxx
        xxxx xx xxxxxx xxxxxxxxxx xxxxxxxxxxxxxx x
            xxxxxxxxx x xxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        x

        xx xxxxxxx xxxx xxxxx xx xxxxxxx xxxx xxxxxxxxxxx xx xxxxxx
        xx xxxxxx x xxxxx xxxxx xxxxxxxx xxxx xxxxxxxxx
        xxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x
            xx xxxx xxxxxx xxxx xxxxxxxxxxx xxxxxx xx xxxxxxxxx xx xxxxx xxxxxxxxx
            xxxxxxxxxxxxxx xxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            xxxxxxxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        x

        xx [/pro] */
        // Is this case possible?
        else {
            arrayType = Object.class;
        }

        this.array = array;
        this.alias = alias;
        this.field = init(alias, arrayType);

        init(alias, arrayType);
    }

    @SuppressWarnings("unused")
    ArrayTable(Field array, String alias, String[] fieldAliases) {
        super(alias);

        throw new UnsupportedOperationException("This constructor is not yet implemented");
    }

    private static final Fields init(String alias, Class arrayType) {
        List> result = new ArrayList>();

        // [#1114] VARRAY/TABLE of OBJECT have more than one field
        if (UDTRecord.class.isAssignableFrom(arrayType)) {
            try {
                UDTRecord record = (UDTRecord) arrayType.newInstance();
                for (Field f : record.fields()) {
                    result.add(DSL.field(name(alias, f.getName()), f.getDataType()));
                }
            }
            catch (Exception e) {
                throw new DataTypeException("Bad UDT Type : " + arrayType, e);
            }
        }

        // Simple array types have a synthetic field called "COLUMN_VALUE"
        else {
            result.add(DSL.field(name(alias, "COLUMN_VALUE"), DSL.getDataType(arrayType)));
        }

        return new Fields(result);
    }

    @Override
    public final Class getRecordType() {
        return RecordImpl.class;
    }

    @Override
    public final Table as(String as) {
        return new ArrayTable(array, as);
    }

    @Override
    public final Table as(String as, String... fieldAliases) {
        return new ArrayTable(array, as, fieldAliases);
    }

    @Override
    public final boolean declaresTables() {

        // Always true, because unnested tables are always aliased
        return true;
    }

    @Override
    public final void accept(Context ctx) {
        ctx.visit(table(ctx.configuration()));
    }

    private final Table table(Configuration configuration) {
        switch (configuration.family()) {
            /* [pro] xx
            xxxx xxxxxxx x
                xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x
                    xxxxxx xxxxxxxxxxxxxxxxxxxxx
                x
                xxxx x
                    xxxxxx xxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                x
            x

            xx [/pro] */
            case H2: {
                return new H2ArrayTable().as(alias);
            }

            // [#756] These dialects need special care when aliasing unnested
            // arrays
            case HSQLDB:
            case POSTGRES: {
                return new PostgresHSQLDBTable().as(alias);
            }

            // Other dialects can simulate unnested arrays using UNION ALL
            default: {
                if (array.getDataType().getType().isArray() && array instanceof Param) {
                    return simulate();
                }

                /* [pro] xx
                xx xxxxxxx xxxxxxxxxx xxx xxxxxxxxxx xx xxxxxxx xxxxxx xxxx
                xx xxxxxxx xxxxxx xxxxxxx xxxxxxxxxxx
                xxxx xx xxxxxx xxxxxxxxxx xxxxxxxxxxxxxx x
                    xxxxxx xxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                x
                xx [/pro] */

                else {
                    return simulate();
                }
            }
        }
    }

    private class PostgresHSQLDBTable extends DialectArrayTable {

        /**
         * Generated UID
         */
        private static final long serialVersionUID = 6989279597964488457L;

        @Override
        public final void accept(Context ctx) {
            ctx.sql('(').keyword("select").sql(" * ")
               .keyword("from").sql(' ').keyword("unnest").sql('(').visit(array).sql(") ")
               .keyword("as").sql(' ').literal(alias)
               .sql('(').literal("COLUMN_VALUE").sql("))");
        }
    }

    private class H2ArrayTable extends DialectArrayTable {

        /**
         * Generated UID
         */
        private static final long serialVersionUID = 8679404596822098711L;

        @Override
        public final void accept(Context ctx) {
            ctx.keyword("table(").sql("COLUMN_VALUE ");

            // If the array type is unknown (e.g. because it's returned from
            // a stored function
            // Then the best choice for arbitrary types is varchar
            if (array.getDataType().getType() == Object[].class) {
                ctx.keyword(H2DataType.VARCHAR.getTypeName());
            }
            else {
                ctx.keyword(array.getDataType().getTypeName());
            }

            ctx.sql(" = ").visit(array).sql(')');
        }
    }

    /* [pro] xx
    xxxxxxx xxxxx xxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxxxxxx x

        xxx
         x xxxxxxxxx xxx
         xx
        xxxxxxx xxxxxx xxxxx xxxx xxxxxxxxxxxxxxxx x xxxxxxxxxxxxxxxxxxxxx

        xxxxxxxxx
        xxxxxx xxxxx xxxx xxxxxxxxxxxxxxxxx xxxx x
            xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx
        x
    x

    xx [/pro] */
    private abstract class DialectArrayTable extends AbstractTable {

        /**
         * Generated UID
         */
        private static final long serialVersionUID = 2662639259338694177L;

        DialectArrayTable() {
            super(alias);
        }

        @Override
        public final Class getRecordType() {
            return RecordImpl.class;
        }

        @Override
        public final Table as(String as) {
            return new TableAlias(this, as);
        }

        @Override
        public final Table as(String as, String... fieldAliases) {
            return new TableAlias(this, as, fieldAliases);
        }

        @Override
        final Fields fields0() {
            return ArrayTable.this.fields0();
        }
    }

    @SuppressWarnings("unchecked")
    private final ArrayTableSimulation simulate() {
        return new ArrayTableSimulation(((Param) array).getValue(), alias);
    }

    @Override
    final Fields fields0() {
        return field;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy