[#7885] Add Table.rowid()

This commit is contained in:
Lukas Eder 2018-09-24 09:29:22 +02:00
parent 6164cedbd1
commit 5e892a8afa
3 changed files with 125 additions and 3 deletions

View File

@ -292,6 +292,32 @@ public interface Table<R extends Record> extends TableLike<R>, Named {
@Support
QualifiedAsterisk asterisk();
/**
* Get a <code>table.rowid</code> reference from this table.
* <p>
* A rowid value describes the physical location of a row on the disk, which
* can be used as a replacement for a primary key in some situations -
* especially within a query, e.g. to self-join a table:
* <p>
* <code><pre>
* -- Emulating this MySQL statement...
* DELETE FROM x ORDER BY x.y LIMIT 1
*
* -- ... in other databases
* DELETE FROM x
* WHERE x.rowid IN (
* SELECT x.rowid FROM x ORDER BY x.a LIMIT 1
* )
* </pre></code>
* <p>
* It is <em>not</em> recommended to use <code>rowid</code> values in client
* applications as actual row identifiers as the database system may move a
* row to a different physical location at any time, thus changing the rowid
* value. In general, use primary keys, instead.
*/
@Support({ H2, POSTGRES, SQLITE })
Field<?> rowid();
// -------------------------------------------------------------------------
// XXX: Aliasing clauses
// -------------------------------------------------------------------------

View File

@ -171,15 +171,24 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
return record.into(this);
}
// ------------------------------------------------------------------------
// XXX: TableLike API
// ------------------------------------------------------------------------
// -------------------------------------------------------------------------
// XXX: Expressions based on this table
// -------------------------------------------------------------------------
@Override
public final QualifiedAsterisk asterisk() {
return new QualifiedAsteriskImpl(this);
}
@Override
public final Field<?> rowid() {
return new Rowid(this);
}
// ------------------------------------------------------------------------
// XXX: TableLike API
// ------------------------------------------------------------------------
/**
* Subclasses should override this method to provide the set of fields
* contained in the concrete table implementation. For example, a

View File

@ -0,0 +1,87 @@
/*
* 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.unquotedName;
import org.jooq.Context;
import org.jooq.Table;
/**
* @author Lukas Eder
*/
final class Rowid extends AbstractField<Object> {
/**
* Generated UID
*/
private static final long serialVersionUID = 1514220224208896376L;
private final Table<?> table;
Rowid(Table<?> table) {
super(table.getQualifiedName().append(unquotedName("rowid")), SQLDataType.OTHER);
this.table = table;
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case H2:
ctx.visit(table.getQualifiedName().append(unquotedName("_rowid_")));
break;
case POSTGRES:
ctx.visit(table.getQualifiedName().append(unquotedName("ctid")));
break;
case SQLITE:
default:
ctx.visit(getQualifiedName());
break;
}
}
}