From 5e892a8afa63a35d86e8cb38375072e621bb3997 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 24 Sep 2018 09:29:22 +0200 Subject: [PATCH] [#7885] Add Table.rowid() --- jOOQ/src/main/java/org/jooq/Table.java | 26 ++++++ .../java/org/jooq/impl/AbstractTable.java | 15 +++- jOOQ/src/main/java/org/jooq/impl/Rowid.java | 87 +++++++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Rowid.java diff --git a/jOOQ/src/main/java/org/jooq/Table.java b/jOOQ/src/main/java/org/jooq/Table.java index 7036eecf5e..1e8d1ec897 100644 --- a/jOOQ/src/main/java/org/jooq/Table.java +++ b/jOOQ/src/main/java/org/jooq/Table.java @@ -292,6 +292,32 @@ public interface Table extends TableLike, Named { @Support QualifiedAsterisk asterisk(); + /** + * Get a table.rowid reference from this table. + *

+ * 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: + *

+ *

+     * -- 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
+     * )
+     * 
+ *

+ * It is not recommended to use rowid 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 // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java b/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java index 34da0f7c6a..fdeeef1182 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java @@ -171,15 +171,24 @@ abstract class AbstractTable 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 diff --git a/jOOQ/src/main/java/org/jooq/impl/Rowid.java b/jOOQ/src/main/java/org/jooq/impl/Rowid.java new file mode 100644 index 0000000000..05b57f96a5 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Rowid.java @@ -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 { + + /** + * 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; + } + } +}