From b32cf31f5b6b05f1d0f4b9f4ec61937b329a32da Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 31 Oct 2012 21:58:00 +0100 Subject: [PATCH] [#915] Add Table> Factory.values(Row[N]...), to create ad-hoc tables from data --- .../_/testcases/RowValueExpressionTests.java | 9 ++ .../src/org/jooq/test/jOOQAbstractTest.java | 5 + jOOQ/src/main/java/org/jooq/impl/Factory.java | 40 +++++++ jOOQ/src/main/java/org/jooq/impl/Values.java | 100 ++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Values.java diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/RowValueExpressionTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/RowValueExpressionTests.java index 9f42b7f14a..35dc2c667c 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/RowValueExpressionTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/RowValueExpressionTests.java @@ -441,4 +441,13 @@ extends BaseTest Table> values(Row1... rows) { + return new Values>(rows); + } + + static Table> values(Row2... rows) { + return new Values>(rows); + } + + static Table> values(Row3... rows) { + return new Values>(rows); + } + + static Table> values(Row4... rows) { + return new Values>(rows); + } + + static Table> values(Row5... rows) { + return new Values>(rows); + } + + static Table> values(Row6... rows) { + return new Values>(rows); + } + + static Table> values(Row7... rows) { + return new Values>(rows); + } + + static Table> values(Row8... rows) { + return new Values>(rows); + } + + static Table values(RowN... rows) { + return new Values(rows); + } + // ------------------------------------------------------------------------- // XXX Literals // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/Values.java b/jOOQ/src/main/java/org/jooq/impl/Values.java new file mode 100644 index 0000000000..3750f7d35e --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Values.java @@ -0,0 +1,100 @@ +/** + * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.impl; + +import org.jooq.BindContext; +import org.jooq.Record; +import org.jooq.RenderContext; +import org.jooq.Row; +import org.jooq.Support; +import org.jooq.Table; +import org.jooq.exception.DataAccessException; + +/** + * An implementation for the VALUES(...) table constructor + * + * @author Lukas Eder + */ +class Values extends AbstractTable { + + /** + * Generated UID + */ + private static final long serialVersionUID = -637982217747670311L; + + private final Row[] rows; + + Values(Row[] rows) { + super("values"); + + this.rows = rows; + } + + @SuppressWarnings("unchecked") + @Override + public final Class getRecordType() { + return (Class) RecordImpl.class; + } + + @Override + @Support + public final Table as(String alias) { + return new TableAlias(this, alias, true); + } + + @Override + public final void toSQL(RenderContext context) { + context.keyword("values"); + + String separator = ""; + for (Row row : rows) { + context.sql(separator) + .sql(row); + + separator = ", "; + } + } + + @Override + public final void bind(BindContext context) throws DataAccessException { + context.bind(rows); + } + + @Override + protected final FieldList getFieldList() { + return new FieldList(rows[0].getFields()); + } +}