I want to write a totally dynamic query method, which gets dinamically column names as parameters. Column names i.e : id, age, name, etc. I'm going to use criteria query, however I don't know how it's done exactly.
Some example says:
"Path<Long> idPath = personRoot.get( Person_.id );
Path<Integer> agePath = personRoot.get( Person_.age );
criteria.select( builder.array( idPath, agePath ) );"
My problem is the usage of "builder.array" part. How can I put together my (i.e:) path elements into a "Selection... selections" parameter in order to the "select" accept it and be valid my dynamic query?
Is there any possibility to write multiple group by according to my "Selection... selections" parameter in the "select" part?
Any hints appreciated, thank You in advance.
Selection... selections is just syntactic sugra for Selection[] selections. So you just need to create an array of selections, and pas this array as argument to the builder.array() method.
Related
I created a page to insert and modify data of an existing mysql- table.
But based on my requirements and the structure of the table I have to modify the sql for inserting data.
Because I am completly new on rapidclipse and java I need some hints/ examples how and where to modify this.
Looking all rapidclipse videos did not give the right hint.
I would like to insert three fields into a mysql-table
One of the fields I have to edit manualy.
The second field contains always the same value.
The third field contains a calculated value, which I have to fetch while runtime from the database.
As sql I would use following code:
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01)
VALUES ('T_supplier', (select * from (select max(cast(DMV_COL00 as
Integer)) +1 from OKM_DB_METADATA_VALUE as t2 where DMV_TABLE =
'T_supplier') as t3 ) , 'new suppliername');
The value for field DMV_Table will be always 'T_supplier'
The value for field DMV_COL00 is always the highest value in the col +1
The value for field DMV_COL01 will be always entered manually
(I am not able/ I don't want to modify/ use table form, -design and trigger, because it is a original table of OpenKM)
Thank you in advance!
best regards
OpaHeinz
Just a suggestion for sql code .. Your code could be refactored in a more SQL like code .. You could avoid the innner subquery .. and use a normal insert select
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01)
select 'T_supplier', max(cast(DMV_COL00 asInteger)) +1 , 'new suppliername'
from OKM_DB_METADATA_VALUE
where DMV_TABLE ='T_supplier'
The first step to solution
In the buttonClick event of save function
I set the value of DMV_Table field with:
... this.txtDmvTable.setValue("T_supplier");
The second step;
I created a view in the database wich delivers only the expected value:
`CREATE
OR REPLACE
VIEW `okmdb`.`V_suppliers_newID` AS
select
1 as "id",
max(cast(DMV_COL00 as Integer)) +1 as "newSupId"
from OKM_DB_METADATA_VALUE
where DMV_TABLE = 'T_supplier'; `
After that I created an entity in rapidclipse, read the value out of the view and assigned it to the other field DMV_COL00.
This was all.
i have a nasty SQl that i want transform in JOOQ
Here are the Query:
SELECT
SUM(dpr.dpr_bruttopraemie_prt + dpr.dpr_sofortrabatt_prt)
, MAX(TO_NUMBER(DP1.dp_wert))
FROM deckungen deck, deckungspraemien dpr,
(SELECT dp.dp_id, dp.dp_wert
FROM textbausteine txb, druckparameter dp
WHERE dp.dp_txb_id = txb.txb_id
) DP1
WHERE DP1.dp_id = :druckparameter_id;
As you can see, i need to make alias from a select with two Fields.
dp.dp_id, dp.dp_wert
that im going to used it on some other parts.
How i can i get it done?
i've seen
.asField()
Funktion but it only make alias for one column.
PS: The actual Query are a lot more complicated. So i wrote a simpler one.
With hoping that it's satisfied the SQL ORACLE Dialect.
I'm assuming that you're using the code generator, so you have generated objects available for your tables like DECKUNGEN. I'm also assuming you're using these static imports:
import static org.jooq.impl.DSL.*; // The jOOQ API
import static com.example.myapp.generated.Tables.*; // Your generated tables
You can then write:
Deckungen deck = DECKUNGEN.as("deck");
Deckungspraemien dpr = DECKUNGSPRAEMIEN.as("dpr");
Textbausteine txb = TEXTBAUSTEINE.as("txb");
Druckparameter dp = DRUCKPARAMETER.as("dp");
Table<?> dp1 = table(
select(dp.DP_ID, dp.DP_WERT)
.from(txb, dp)
.where(dp.DP_TXB_ID.eq(txb.TXB_ID))
).as("dp1");
Record2<BigDecimal, BigDecimal> result =
using(configuration)
.select(
sum(dpr.DPR_BRUTTOPRAEMIE_PRT.plus(dpr.DPR_SOFORTRABATT_PRT)),
max(field("to_number({0})", BigDecimal.class, dp1.field(dp.DP_WERT))))
.from(deck, dpr, dp1)
.where(dp1.field(dp.DP_ID).eq(druckparameterId))
.fetchOne();
Some explanations
jOOQ currently doesn't have a built-in TO_NUMBER() function, but you can easily roll your own using DSL.field(String) and similar overloads. For more detail, refer to the manual's section about plain SQL
A derived table can be created most easily by using the DSL.table(Select) operator.
Columns from a derived table can be dereferenced using Table.field() methods, in particular the Table.field(Field), which tries to find a field by the same name as the argument field, retaining the argument field's type information.
Side-note
I don't think your query is correct as you're simply creating cartesian products between your tables deck, dpr, and dp1. Specifically, the SUM() is quite likely to be wrong, whereas MAX() is simply calculated inefficiently.
I'm using the statement below to update/insert some data to a table and, if I run it without parameters, it's fine. However, as soon as I try to execute it using parameters it throws:
SQL0418N - A statement contains a use of an untyped parameter marker, the DEFAULT keyword, or a null value that is not valid.
I've read the error information here, but I'm still struggling with why my statement won't execute.
--This statement works
MERGE Into AB.Testing_Table A
USING (VALUES('TEST', 'P')) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES ('TEST', 'P')
WHEN MATCHED THEN
UPDATE SET TEST_ID = 'TEST'
,"ACTION" = 'P';
--This statement fails with error SQL0418N
MERGE Into AB.Testing_Table A
USING (VALUES(#TEST, #ACTION)) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES (#TEST, #ACTION)
WHEN MATCHED THEN
UPDATE SET TEST_ID = #Test
,"ACTION" = #Action;
Thanks in advance for the help!
Basically, DB2 doesn't know what data types you're sending in on those parameters. I'm guessing you're either on an older version of DB2 (less than 9.7 on Linux/Unix/Windows, or on a Mainframe version older than 10.1), which doesn't do a whole lot of "automatic" type conversion. Or you're sending in NULL values (which still have to be "typed", strange as it sounds).
You can fix the problem by creating your parameter markers as typed parameters (I'm assuming data types here, use what would be appropriate):
MERGE INTO AB.TESTING_TABLE A
USING (VALUES (
CAST(#TEST AS CHAR(4))
,CAST(#ACTION AS CHAR(1))
)) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES (B.TEST_ID, B.ACTION)
WHEN MATCHED THEN
UPDATE SET "ACTION" = B.ACTION
Additionally, since you're using the MERGE, you don't have to use parameters in the UPDATE or INSERT parts, you can refer to the values in the USING table you passed in. Also, since you're matching on TEST_ID, you don't need to include that in your UPDATE statement, since it wouldn't be updated, anyway.
I'm trying to create a query using CriteriaBuilder where I need to have a predicate where the value of the predicate is like the value in the database.
Basically, I need to be able to do the following:
WHERE myTestValue LIKE columnValue
In native queries, it is an option to do that, but using the CriteriaBuilder or NamedQueries, it does not seem to work.
String myValue = "foo#bar.com";
cb.where(cb.like(myValue, root.get(Entity_.email));
Is there an option in JPA to do it like this? Or should I fall back to native queries?
EDIT
I need to be able to check if a given value matches a wildcard entry in database. So the database has a record %#bar.com%, and I need to check if my given value foo#bar.com matches to that record.
I think your params should be other way round:
cb.where(cb.like(root.get(Entity_.email),myValue);
Aditionally you may need to use add this to the second param:
cb.where(cb.like(root.get(Entity_.email),"%"+myValue+"%");
Chris found the answer. First I need to "generate" a parameter.
ParameterExpression<String> senderEmailParameter = cb.parameter(String.class, "senderEmailParameter");
Path<String> senderEmailPath = root.get(Entity_.senderEmail);
Predicate predEmail = cb.like(senderEmailParameter, senderEmailPath);
And then I need to fill the parameter in the query.
q.where(predEmail);
return em.createQuery(q).setParameter("senderEmailParameter", senderEmail).getSingleResult();
This works! Thanks Chris!
I have this query that I'm running in Java.
select book from com where genre=?;
I set the parameter for genre dynamically. Is there a way to sometimes set the parameter so that all data is selected?
The usual trick is to set a separate parameter for selecting everything:
SELECT book FROM com WHERE genre=? OR 1=?
When you set the second parameter to 0, filtering by genre is used, but when you set it to 1, everything is returned.
If you are willing to switch to using named JDBC parameters, you could rewrite with one parameter, and use null to mean "select everything":
SELECT book FROM com WHERE genre=:genre_param OR :genre_param is null
Do you mean you want to be able to use the same script and pass in a parameter that ignores your "Where"? Use this and pass in the string "AllData" to the second paramter to include all results and ignore your first parameter:
select book from com where (genre=? OR "AllData"=?);
Create a different query:
select book from com;
That will get the "book" column from all rows in "com".
Just use a wildcard.
Ex. SELECT book FROM com WHERE genre LIKE "%"+seachString+"%";
You can use CASE expression (SQL SQERVER):
select book from com where genre=CASE
WHEN ? IS NOT NULL THEN ?
ELSE genre
END