i use propel for database interaction. Now i have to create a query like
SELECT data FROM values WHERE a=1 AND (vis=1 or (vis=0 AND userID=5));
I create a propel object from the table "values".
$p = new ValuesQuery()::create
->filterByA(1)
->filterByVis(1)
->_or()
->filterByVis(0)
->filterByUserId(5)
->findOne();
Propel generates the following SQL-Query which mostly makes sense:
SELECT data FROM values WHERE a=1 AND (vis=1 or vis=0) AND userID=5;
How can i fix this? Is it possible to say propel what it should put in brackets?
Thanks for all answers!
As #halfer said, read the following documentation on how to combine several conditions: http://www.propelorm.org/reference/model-criteria.html#combining_several_conditions
Playing with next tool may not only answer your particular question, but also help you understand, how complicated conditions are built.
http://propel.jondh.me.uk/criteria/analyse
Related
I have a Keyword based JPA query I need to modify in order to exclude records with a particular status. Currently, I have the following:
findAllByLatestVersion_Entity_DataFieldGreaterThanEqualAndLatestVersion_AnotherFieldNull(datefield: Istant, pageable: Pageable)
I do not want to parameterise, therefore I would like to have the query to work as there was a WHERE clause stating that the status IS NOT C, for example. I am struggling to find clear documentation on how to go about. Is it possible to write something along these lines:
findAllByLatestVersion_Entity_DataFieldGreaterThanEqualAndLatestVersion_AnotherFieldNullAndLatestVersion_StatusCNot(datefield: Istant, pageable: Pageable)
Thank you
No this is not possible with query derivation, i.e. the feature you are using here. And even if it were possible you shouldn't do it.
Query derivation is intended for simple queries where the name of the repository method that you would choose anyway perfectly expresses everything one needs to know about the query to generate it.
It is not intended as a replacement for JPQL or SQL.
It should never be used when the resulting method name isn't a good method name.
So just formulate the query as a JPQL query and use a #Query annotation to specify it.
When building ORM query, I want to see what the actual (raw) query is that is executed.
For example in Rails we can do like this:
User.where(name: 'Oscar').to_sql
# => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
This feature present in Bookshelfjs? or any other way to get this?
Thanks in advance
Looks like there is no direct way to get SQL statement like Rails produce..
You can use .query() .toSQL() or .query() .toString(). But achieving exact result as in Rails is a bit more complicated as queries may be not complete. The cause is that many statements get applied just before performing the query in Bookshelf. For example Bookshelf relations behave so. Also many plugins use events to apply query statements. If you want to debug the queries then I would suggest you to use Knex#debug instead. For example
model.query(function(qb) {
qb.debug(true);
}).fetch()
It prints the debug info in the console.
Source: from officials bookshelfjs github
Suppose I have Posts and posts' Comments. I want to filter all the Posts that have more than 10 comments. I began writing something like Posts.includes(:comments).group("post.id").count("comments.id"), to obtain a hash of posts and their counts, and I can extract the information from there, but I want some one-line straightforward way to do that
Sure I can use some pure sql syntax statements, but I want it in a pure rails way. Any idea ?
Assuming the models are named in the more typical singular form of Post and Comment and have the usual association relationship, then the following should work:
Post.joins(:comments).group('posts.id').having('count(comments.id) > 10')
Any idea how to create this function in t-sql?
Pseudo-code:
function( #table, #table_column )
{
update #table
set #table_column = replace(#table_column,',','')
where #table_column like '%,%'
}
Ideas I've tried:
Procedures: only take readonly tables (http://technet.microsoft.com/en-us/library/ms187926.aspx)
Functions: cannot do updates...
Any suggestions? Thanks everyone!
Update: I had a database with about 40 tables, each with columns that I needed to remove special characters (i.e., ","). Although it would be nice to create a function/procedure where I could give it the name and fix the column, I decided instead (based on the comments) to just write each update out. Perhaps I was just looking for too fancy of a solution to a relatively simple problem.
The only way to do this is dynamic SQL. Unless you are writing database tools, you rarely need to do this kind of thing. Are you sure your database is designed correctly? What is the problem you are trying to solve?
Why do you need a function?
Usually functions are applied to a column inside the select list, such as SELECT MYFUNC(COL1) FROM TAB1;
This can definitely be done in a Stored Procedure with dynamic SQL. You can even look at a return value for the number of rows updated.
I guess the main question is what is your business requirements??
I want to use a table-valued database function in the where clause of a query I am building using LLBLGen Pro 2.6 (self-servicing).
SELECT * FROM [dbo].[Users]
WHERE [dbo].[Users].[UserID] IN (
SELECT UserID FROM [dbo].[GetScopedUsers] (#ScopedUserID)
)
I am looking into the FieldCompareSetPredicate class, but can't for the life of me figure out what the exact signature would be. Any help would be greatly appreciated.
ADDITION -
A better question would be "How do can you interact with a table-valued function via LLBLGen Pro?" I do not see how to generate files/classes for it.
Yes. Use DbFunctioncallExpression, to formulate an expression with a DbFunctionCall and then use a FieldCompareExpression predicate to use it. See 'Calling a Database Function' in ... the manual! :)
http://www.llblgen.com/documentation/3.0/LLBLGen%20Pro%20RTF/hh_goto.htm#Using%20the%20generated%20code/gencode_dbfunctioncall.htm
Please post questions on our forums, it's easier to track them down :)