WITH statement in JOOQ? - java

I have query for MS-SQL and Oracle but I want to convert into Jooq .I also trying somthing like this DSL.connectByRoot(field) But I am unable to find this solution . The main issue is that in Oracle we use clause connect by prior but its not available in MS-SQL.
MS-SQL QUERY:
WITH tempTable(ppCode, pCode) AS (
SELECT DefaultProcessDependent.PriorProcessCode, ProcessCode
FROM DefaultProcessDependent
WHERE DefaultProcessDependent.ProcessCode = ?
AND DefaultProcessDependent.FolderType = ?
UNION ALL
SELECT nplus1.PriorProcessCode, nplus1.ProcessCode
FROM DefaultProcessDependent as nplus1, tempTable
WHERE tempTable.ppCode = nplus1.ProcessCode
)
SELECT ppCode FROM tempTable
ORACLE QUERY:
Select processCode
from DefaultProcessDependent
start with DefaultProcessDependent.ProcessCode = ?
connect by prior processCode = priorProcessCode
Anyone help me please...................
Thanks

Common table expressions will be supported with jOOQ 3.4. jOOQ will also emulate CONNECT BY for other databases, but that won't be available in jOOQ 3.4 yet.

Related

Unknown column sql error while using jooq

Query:
this.dslContext.select(
ROLE.asterisk(),
multiset(
select(PERMISSION.asterisk())
.from(ROLE_PERMISSION)
.innerJoin(PERMISSION)
.on(ROLE_PERMISSION.PERMISSION_ID.eq(PERMISSION.ID))
.where(ROLE_PERMISSION.ROLE_ID.eq(ROLE.ID))
).as("permissions")
).from(ROLE)
.where(ROLE.ID.eq(id))
.fetchOneInto(Role.class)
Error:
jOOQ; bad SQL grammar [set #t = ##group_concat_max_len; set ##group_concat_max_len = 4294967295; select `users`.`role`.*, (select coalesce(json_merge_preserve('[]', concat('[', group_concat(json_array(`v0`, `v1`) separator ','), ']')), json_array()) from (select `users`.`permission`.`id` as `v0`, `users`.`permission`.`name` as `v1` from `users`.`role_permission` join `users`.`permission` on `users`.`role_permission`.`permission_id` = `users`.`permission`.`id` where `users`.`role_permission`.`role_id` = `users`.`role`.`id`) as `t`) as `permissions` from `users`.`role` where `users`.`role`.`id` = ?; set ##group_concat_max_len = #t;]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'users.role.id' in 'where clause'.
Database: MYSQL, Database Name: 'users', JOOQ Version: 3.16.6
Correlating derived tables isn't supported in MySQL 5.7. Support has been added only in MySQL 8.0.14:
https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html
jOOQ currently can't work around this limitation, see:
https://github.com/jOOQ/jOOQ/issues/12045
The solution is either:
Upgrade your MySQL version
Use a MULTISET_AGG based approach instead

how to use pg_column_size in hibernate?

I am trying to get the size of a row in a postgresql table, I found that pg_column_size would do the trick, but its not working with hibernate :
#Query("SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name")
int getSize(#Param("name") String name);
intellij is giving this error :
< operator > or AS expected, got '('
I guess the problem is that hibernate doesnt support specific postgresql queries, it only supports the basic sql queries.
so is there a way around this ? if not is there a way to get/estimate the size of a postgresql row in java ?
In order to use a built-in postgres function , you have to declare you JPA query as nativeQuery
you should first change query to native (hibernate will directly execute the query instead of jpa -> sql generation )
#Query(value="SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name",nativeQuery=true)
int getSize(#Param("name") String name);
Also be sur of the TABLE name to be correct .
Add nativeQuery = true after the native query.
#Query("SELECT pg_column_size(t.*) as filesize FROM users as t where t.name=:name",nativeQuery = true)
use above Query, Hope This will work.

Select statement failing after h2 database jar upgrade in oracle mode

After upgrading H2 database version from 1.3.171 to 1.4.187, my select statement failing with the jdbc error code [90022-187]. I'm, using Spring 4 and Hibernate 4.3.5.
Error Trace :
Function "BUS_ENTY_GUID" not found; SQL statement:
select * from ( select consumerin0_.CNSMR_INTNT_SID as CNSMR_IN1_0_0_, busentity1_.BUS_ENTY_SID as BUS_ENTY1_4_1_, intentuser2_.USR_SID as USR_SID1_10_2_ where consumerin0_.OWNR_ENTY_NM=busentity1_.BUS_ENTY_GUID(+) and consumerin0_.USR_EXTRNL_ID=intentuser2_.MAG_GUID(+) and lower(consumerin0_.ORD_ID)=lower(?) and lower(intentuser2_.USR_LAST_NM)=lower(?) ) where rownum <= ? [90022-187]
Kindly help in resolving the issue
H2 no longer supports the "old style" Oracle outer join syntax. You will have to use "outer join" instead.
Thanks Thomas for you help!
I was using org.hibernate.dialect.OracleDialect, hence conversion of the query has older style joins. Now I have changed it to Oracle10gDialect which is working fine.
Use -Dh2.oldStyleOuterJoin=true to support older syntax.
Visit http://www.h2database.com/javadoc/org/h2/engine/SysProperties.html

Migrate data from old MySQL database

I recently rewrote a Java EE web application (running on a MySQL database) to Rails 3.1. The problem now is that the database model of the new application is not the same as the old one because I added, removed and renamed some attributes. The database table names are also different.
Is there a way of migrating this data? The only way I can imagine to do this is writing a stored procedure with many ALTER TABLE and CREATE TABLE statements to update the database to the new model.
Thanks in advanced.
Solution:
I finally used INSERT..SELECT statements in a mysql stored procedure to migrate the data. INSERT INTO new_schema.new_table SELECT FROM old_schema.old_table. I am now considering making a Rake task to call that procedure and doing other stuff.
The only way is to write a script that take the data from the old db and insert thme in the new db. Or you can in some way to connect to the two databases and then make some select and insert query, something like
insert into new_db.table as select old_db.table.field1, ....
or
insert into new_db.table (field_1, field_2) values (select old_db.table.field_1, ...)
In any way, is a manual process, also if can be automated to some extend with a script
Instead of a Store procedure you can try with rails and some sql within the rails console using the information_schema of mysql
sql = ActiveRecord::Base.connection()
old_tables = sql.execute "select table_name from information_schema.tables where table_schema = your_old_schema"
res.each do | old_table|
old_fields = sql.execute "select distinct column_name, data_type from information_schema.columns where table_name='#{old_table}' and table_schema='your_old_schema'"
new_fields = sql.execute "select distinct column_name, data_type from information_schema.columns where table_name='#{old_table}' and table_schema='your_new_schema'"
#compare fields and so on...
end

convert sql to hql

I am performing this via sql but i want to do this in hql, select statement in from ( select count(*)...) not works in hql, any sugestion and optimization would be appreciated
SELECT u.username,u.device_tocken,sr.count
from users u,
(select count(*) as count ,ssr.recepient as res from survey_recipient ssr where
(ssr.is_read is false and ssr.recepient in ('abc','xyz'))group by ssr.recepient ) sr
where
(u.username = sr.res and u.device_tocken is not null)
Hibernate does not support subselects in from clouse.
i tried many things and gave up when i found this jira issue.
see here https://hibernate.onjira.com/browse/HHH-3356
But if you have to use subselect you can create database views and use them in your sql as normal tables.

Categories

Resources