[#915] Add <T1, T2, ..., T[N]> Table<Record[N]<T1, T2, ..., T[N]>>

Factory.values(Row[N]<T1, T2, ..., T[N]>...), to create ad-hoc tables
from data
This commit is contained in:
Lukas Eder 2012-10-31 21:58:00 +01:00
parent 36c3f04a00
commit b32cf31f5b
4 changed files with 154 additions and 0 deletions

View File

@ -441,4 +441,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
.where(val(1).eq(val(0))))
));
}
@Test
public void testRowValueExpressionValuesConstructor() throws Exception {
// [#915] TODO: How to properly alias things in order to be able to select them?
// create().select()
// .from(Factory.values(row(1, 2), row(3, 4)))
// .fetch();
}
}

View File

@ -1057,6 +1057,11 @@ public abstract class jOOQAbstractTest<
new RowValueExpressionTests(this).testRowValueExpressionTableRecords();
}
@Test
public void testRowValueExpressionValuesConstructor() throws Exception {
new RowValueExpressionTests(this).testRowValueExpressionValuesConstructor();
}
@Test
public void testIgnoreCase() throws Exception {
new PredicateTests(this).testIgnoreCase();

View File

@ -5905,6 +5905,46 @@ public class Factory {
return new RowImpl(values);
}
// -------------------------------------------------------------------------
// [#915] TODO: These are experimental VALUES() table constructors
// -------------------------------------------------------------------------
static <T1> Table<Record1<T1>> values(Row1<T1>... rows) {
return new Values<Record1<T1>>(rows);
}
static <T1, T2> Table<Record2<T1, T2>> values(Row2<T1, T2>... rows) {
return new Values<Record2<T1, T2>>(rows);
}
static <T1, T2, T3> Table<Record3<T1, T2, T3>> values(Row3<T1, T2, T3>... rows) {
return new Values<Record3<T1, T2, T3>>(rows);
}
static <T1, T2, T3, T4> Table<Record4<T1, T2, T3, T4>> values(Row4<T1, T2, T3, T4>... rows) {
return new Values<Record4<T1, T2, T3, T4>>(rows);
}
static <T1, T2, T3, T4, T5> Table<Record5<T1, T2, T3, T4, T5>> values(Row5<T1, T2, T3, T4, T5>... rows) {
return new Values<Record5<T1, T2, T3, T4, T5>>(rows);
}
static <T1, T2, T3, T4, T5, T6> Table<Record6<T1, T2, T3, T4, T5, T6>> values(Row6<T1, T2, T3, T4, T5, T6>... rows) {
return new Values<Record6<T1, T2, T3, T4, T5, T6>>(rows);
}
static <T1, T2, T3, T4, T5, T6, T7> Table<Record7<T1, T2, T3, T4, T5, T6, T7>> values(Row7<T1, T2, T3, T4, T5, T6, T7>... rows) {
return new Values<Record7<T1, T2, T3, T4, T5, T6, T7>>(rows);
}
static <T1, T2, T3, T4, T5, T6, T7, T8> Table<Record8<T1, T2, T3, T4, T5, T6, T7, T8>> values(Row8<T1, T2, T3, T4, T5, T6, T7, T8>... rows) {
return new Values<Record8<T1, T2, T3, T4, T5, T6, T7, T8>>(rows);
}
static Table<Record> values(RowN... rows) {
return new Values<Record>(rows);
}
// -------------------------------------------------------------------------
// XXX Literals
// -------------------------------------------------------------------------

View File

@ -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 <code>VALUES(...)</code> table constructor
*
* @author Lukas Eder
*/
class Values<R extends Record> extends AbstractTable<R> {
/**
* 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<? extends R> getRecordType() {
return (Class<? extends R>) RecordImpl.class;
}
@Override
@Support
public final Table<R> as(String alias) {
return new TableAlias<R>(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());
}
}