I have a SQL request like:
select X from "myTable" where (cond1 AND cond2) OR (cond3 AND cond 4)...
How many (cond AND cond) can I have in my clause where? Because it makes a stackOverFlow error with my 24578 conditions.
final List update = xService.getCountUpdate(couple);
couple is a list build by that way (every line in my file this is done ):
dcIssn = new ArrayList<String>();
dcIssn.add(0, row.getCell(dc).getStringCellValue());
dcIssn.add(1, row.getCell(issn).getStringCellValue());
couple.add(dcIssn);
I solve my problem.
The problem was not my query, just my StringBuilder which was too short for the query.
Thank you for helping me =)
There is no limit to the number of predicates that can be included in a search condition. For more information about search conditions and predicates.
see this:-
https://technet.microsoft.com/en-us/library/ms189575(v=sql.105).aspx
Related
Using Jooq, I am trying to fetch from a table by id first, if no matches found, then fetch by handle again.
And I want all fields of the returned rows, not just one.
Field<?> firstMatch = DSL.select(Tables.MY_TABLE.fields())
.from(Tables.MY_TABLE.fields())
.where(Tables.MY_TABLE.ID.eq(id))
.asfield(); // This is wrong, because it supports only one field, but above we selected Tables.MY_TABLE.fields(), which is plural.
Field<?> secondMatch = DSL.select(Tables.MY_TABLE.fields())
.from(Tables.MY_TABLE.fields())
.where(Tables.MY_TABLE.HANDLE.eq(handle))
.asfield(); // Same as above.
dslContext.select(DSL.coalesce(firstMatch, secondMatch))
.fetchInto(MyClass.class);
Due to the mistake mentioned above in the code, the following error occurs:
Can only use single-column ResultProviderQuery as a field
I am wondering how to make firstMatch and secondMatch two lists of fields, instead of two fields?
I tried
Field<?>[] secondMatch = DSL.select(Tables.MY_TABLE.fields())
.from(Tables.MY_TABLE.fields())
.where(Tables.MY_TABLE.HANDLE.eq(handle))
.fields();
but the following error occurred in the line containing DSL.coalesce
Type interface org.jooq.Field is not supported in dialect DEFAULT
Thanks in advance!
This sounds much more like something you'd do with a simple OR?
dslContext.selectFrom(MY_TABLE)
.where(MY_TABLE.ID.eq(id))
// The ne(id) part might not be required...
.or(MY_TABLE.ID.ne(id).and(MY_TABLE.HANDLE.eq(handle))
.fetchInto(MyClass.class);
If the two result sets should be completely exclusive, then you can do this:
dslContext.selectFrom(MY_TABLE)
.where(MY_TABLE.ID.eq(id))
.or(MY_TABLE.HANDLE.eq(handle).and(notExists(
selectFrom(MY_TABLE).where(MY_TABLE.ID.eq(id))
)))
.fetchInto(MyClass.class);
If on your database product, a query using OR doesn't perform well, you can write an equivalent query with UNION ALL, which might perform better.
I am stuck trying to get a query (QueryDSL) to work that gives me a count of distinct categories.
For example, what I am trying to achieve:
categoryA -> 10 entries
categoryB -> 20 entries
This is what i have so far:
query().from(application)
.transform(groupBy(application.category).as(list(application)));
However, this gives me for each category a list of all whole entries, I just want to get a count of this.
I tried messing around with count() but no luck.
Anybody know how to do this?
Note that as of Querydsl 4.x, the accepted answer by Timo Westkämper is no longer valid. Breaking changes in Querydsl 4.0 have removed the query.list() method.
A solution working with Querydsl 4.0+ would now be:
query.from(application)
.groupBy(application.category)
.select(application.category, application.category.count())
.fetch();
transform combined groupBy is meant to construct tree structures out of flat result sets. What you need can be easier expressed as
query.from(application)
.groupBy(application.category)
.list(application.category, application.category.count())
If using 4.1.4, you may need to use a JPAQueryFactory instead of JPAQuery
If using JPAQuery, there is no select() or fetch() on the return from groupBy.
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
queryFactory
.select(application.category, application.category.count())
.from(application)
.groupBy(application.category)
.fetch();
In QueryDSL 4.4.0:
query.from(application)
.groupBy(application.category)
.transform(GroupBy.groupBy(application.category)).as(application.category.count()))
Learn more on: https://www.tabnine.com/code/java/methods/com.querydsl.jpa.JPQLQuery/transform
I have the following code in java.
List<UserHelper> users=List<UserHelper>)session.getNamedQuery("PkUser.loadHelperUsers").list();,
I think it does not matter what the "UserHelper" class is that's why I do not write it, not to overload my question. This is my namedQuery mentioned above.
#NamedQuery(name = "PkUser.loadHelperUsers", query = "SELECT new ge.tec.pto.ext.helpers.UserHelper(u) from PkUser u order by u.pkUserId desc"),
The problem is that the hql selects too many rows, I think the same number of rows that is in database in pk_user table.If anyone knows how to fix this please inform me. It will be very nice if the solution will not require to alter my "NamedQuery", It will be graet if I will have to change only my Query creation, But any solutions will be helpful, Thank you
Multiple selects when using Key word “`new`” in `hql`
There is no problem with your code and with NEW keyword .
Your query will return all the Rows in the UserHelper related Table
You should use a WHERE clause to get the required rows .
EX :
query = "SELECT new ge.tec.pto.ext.helpers.UserHelper(u) from PkUser u where username=:passedparamer order by u.pkUserId desc"
I have a problem - I create my SQL queries dynamically and basing on user input options. So the user has 5 parameters (actually it's more) and he can choose to use some of them (all if he wants) or none and specify their value in the query. So I construct my query String (basic the WHERE conditions) by checking if a parameter was selected and if a value was provided. However now there is the problem of special characters like '. I could try to use replaceAll("'", "\\") but this is quite dull and I know that preparedStatement.setString() does the job better. However for me I would need than to check again if the parameter was provided and if the previous one were also (to specify the poison of ? and connect it to the right parameter). This causes a lot of combinations and does not look elegant.
So my question is - can I somehow receive the string preparedStatement.setString() produces? Or is there a similar function that would do the same job and give me the String so I can put it in the query manually.
Maybe the intro was too long but someone might have a better idea and I wanted to explain why I need it.
What you can do is construct the basic, unparameterized SQL query based on whether the parameters were specified, and then use the prepared statement to fill in the parameters.
It could look something like this (rough sketch):
Map<String, Object> parameterValues = /*from user*/;
List<String> parameterNames = Arrays.asList("field1", "field2", "field3");
List<Object> valueList = new ArrayList<Object>();
StringBuilder statementBuilder = new StringBuilder("select * from table where ");
for ( String parameterName : parameterNames ) {
if ( parameterValues.containsKey(parameterName) ) {
statementBuilder.append(parameterName + " = ? AND");
valueList.add(parameterValues.get(parameterName));
}
}
PreparedStatement st = conn.prepareStatement(statementBuilder.toString(),
valueList);
//set each parameter here.
It's only hard the first time; then you can make it generic. That said there are probably query builders that abstract all of this away for you. I use QueryDSL but that does not have bindings for pure JDBC but rather JPA and JDO, etc.
On another forum I was given a different, simpler and cleaner approach that work perfectly.
Here are some links for others with the same problem:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1669972300346534908
http://www.akadia.com/services/dyn_modify_where_clause.html
I want to build Select fields and Where clause dynamically with OR condition using squiggle-sql api.
Please take more than two fields as an example.
Select field1,filed2,filed3,field4,.....
from t1,t2,t3
where t1.field1 = t2.field1 and t1.field1 = t3.field1
where t1.field=? OR t2.field3=? OR t3.field2=?
Please suggest.
I have just discovered Squiggle recently. It seems to be very similar to jOOQ (of which I am the developer). In jOOQ, you could write (I'm sure Squiggle offers similar functionality)
List<Field<?>> fields = new ArrayList<Field<?>>();
fields.add(field1);
fields.add(field2);
// ... add more fields here
Condition condition = T1.field.equal(...);
condition = condition.or(T2.field3.equal(...));
condition = condition.or(T3.field2.equal(...));
// ... connect more conditions here
DSL.using(configuration)
.select(fields)
.from(t1, t2, t3)
.where(t1.field1.equal(t2.field1))
.and(t2.field1.equal(t3.field1))
.and(condition);
For more information, see http://www.jooq.org
Two WHERE at the same SELECT will produce an error. Do you mean this?
Select field1,filed2,filed3,field4,.....
from t1,t2,t3
where t1.field1 = t2.field1 and t1.field1 = t3.field1
AND (t1.field=? OR t2.field3=? OR t3.field2=?)