I'm trying to do a join on a table in an attached SQLite database (https://sqlite.org/lang_attach.html) and I'm having trouble getting jOOQ to format the field names correctly. The query that jOOQ is generating is quoting the name of the attached database, but this causes the query to fail:
select "attached.MemberTransactions".time, "attached.MemberTransactions".tableName, "attached.MemberTransactions".id
from MemberTransactions as curr
full outer join attached.MemberTransactions
on (curr.time, curr.tableName, curr.id) = ("attached.MemberTransactions".time, "attached.MemberTransactions".tableName, "attached.MemberTransactions".id)
where curr.id is null
order by "attached.MemberTransactions".time asc
Removing the quotes fixes the query, i.e.:
select attached.MemberTransactions.time, attached.MemberTransactions.tableName, attached.MemberTransactions.id
from MemberTransactions as curr
full outer join attached.MemberTransactions
on (curr.time, curr.tableName, curr.id) = (attached.MemberTransactions.time, attached.MemberTransactions.tableName, attached.MemberTransactions.id)
where curr.id is null
order by attached.MemberTransactions.time asc
This is how I'm generating the query:
try(Connection connection = DatabaseConnector.createConnection(baseFileName))
{
connection.prepareStatement(String.format("ATTACH DATABASE \"%s\" AS %s", attachFileName, ATTACHED_DB_NAME)).execute();
DSLContext context = DSL.using(connection, SQLDialect.SQLITE);
// ...
String otherTableName = String.format("%s.%s", DatabaseConnector.ATTACHED_DB_NAME, Membertransactions.MEMBERTRANSACTIONS.getName());
Membertransactions otherTable = Membertransactions.MEMBERTRANSACTIONS.as(otherTableName);
Membertransactions currTable = Membertransactions.MEMBERTRANSACTIONS.as("curr");
return context.select(otherTable.TIME, otherTable.TABLENAME, otherTable.ID)
.from(currTable.fullOuterJoin(otherTableName).on(currTable.eq(otherTable)).asTable())
.where(currTable.ID.isNull())
.fetchInto(MembertransactionsRecord.class);
I've tried various workarounds, but I haven't found anything that fixes all of the field names. I can pass raw strings to fullOuterJoin() or from(), but select() can't take raw strings. I need to specifically select otherTable's fields because otherwise, fetchInto() uses the null-valued side of the join; this means I can't just use selectFrom() with a raw string and drop the orderBy() call as a workaround. I can pass an unquoted qualified name from DSL.unquotedName() to the as() call that I'm assigning to otherTable, but then the field names in the query stop being qualified for some reason (i.e. instead of "\"attached.MemberTransactions\".*", I get "MemberTransactions.*").
Is there any way I can force the field names to be inserted into the query as fully-qualified and unquoted? Or is there any official support for the SQLite attach statement that I missed which would handle this case correctly?