How to query html node using class attribute? - yql

I am trying to query html data using YQL.
I wanted to know how to perform query of a html node based on class attribute when the node has multiple classes, e.g:
<tr class='class1 class2'></tr>
This query works fine:
select * from html where url="http://soccernet.espn.go.com/scores" and
xpath='//tr[#class="class1"]'
But when I try querying for class2, I've got empty result:
select * from html where url="http://soccernet.espn.go.com/scores" and
xpath='//tr[#class="class2"]'
So basically I want to know how to query html nodes using YQL when the node has multiple class attribute.

There may be a better way - but this is how I tackle this problem when I come across it:
select * from html where url="http://soccernet.espn.go.com/scores" and xpath='//tr[contains(#class,"class2")]'

Related

GRAILS / GORM: Dynamic multiple joins

I've got a complicated query which I can solve using SQL but I can't find a proper GORM way to do the same.
I have Story objects. Each Story object refers to many Tag objects. It's a unidirectional OneToMany relation:
class Story {
...
Collection tags
static hasMany = [
tags: Tag
]
...
}
Several Tags are considered as being "aliased" (technically using a separate "AliasGroup" table, which is not the problem here).
Now, I want to search for multiple tags. Returned stories have to be tagged with ALL of these tags ("Match All tags"). But also, for each of those tags, an 'aliased' tag should also be accepted.
In SQL I solve that by using a dynmically composed sequence of joins. For example, if there are two tags given, both having several "aliases", the resulting SQL statement looks like this (using the actual SQL row names):
select s.id from story
join story_tag st1 on s.id = st1.story_id
join story_tag st2 on s.id = st2.story_id
where
st1.tag_id in (<list of ids for tag1 and all its aliases>) and
st2.tag_id in (<list of ids for tag2 and all its aliases>)
It's important to understand that this cannot be written as a single join, because then the SAME tag would have to be in both groups of tag lists, which is not what I want.
So, this statement is working fine. But I want to achieve the same thing using grails directly. The GORM documentation on joins is rather terse, but I figured that the feature using an association query and 'and' them together would give the correct result. So I tried:
def c = Story.createCriteria()
def stories = c.list {
and {
srchTags.each { tag ->
def ids = []
tag.aliasGroup.aliases.each { alias ->
ids << alias.id
}
tags {
inList('id', ids)
}
}
}
}
This works fine if I just use one single tag as input. I.e. the list of aliased tags ids is properly resolved and the statement is working in principle.
But if I use two or more tags, it doesn't work correctly. The code runs, but GORM seems to just create the wrong SQL query, it seems that it again tries to match each tag with all of the match-lists which cannot work.
I realize that this problem is not easy to understand, and it's even more difficult to describe properly.
Does anyone have a solution how to create a proper Grails query for this problem?
In a project at work now I had several sql queries that I could not translate to gorm(criteria, where). With the need to move forward I used native sql queries in grails and everything goes well.
Please have a look at the article Using Hibernate Native SQL Queries for an explanation of the implementation method.
I know you can not always decide which method to use but having this facility at hand is always useful.
I hope it's useful for you. regards

How to execute a LINQ to Entities query, pulling values from an existing resultset variable instead of against the DB

To provide some context, I'm using jqGrid to conduct a server side search on a datetime field, truncating the time portion server side, and then fetching results via a stored procedure. This is being done using System.Dynamic.Linq, which I've modified slightly as per this question here. I'm using EF6 Code First approach. The relevant portion of the code looks something like this:
//For matching date instead of datetime values
if (wc.Clause.Contains("Date"))
{
wc.Clause = wc.Clause.Replace("DeliveryDate", "DbFunctions.TruncateTime(DeliveryDate)");
}
results = dataFileDB.Database.SqlQuery<Document>("exec [dbo].[sp_getDocumentDetails]").AsQueryable().Where(wc.Clause, wc.FormatObjects);
The problem I'm having is that because the query is fetched using a stored procedure, I get an exception on the last line saying
System.NotSupportedException: This function can only be invoked from LINQ to Entities.
at System.Data.Entity.DbFunctions.TruncateTime(Nullable`1 dateValue)
In other places where I've used a lambda query to fetch directly from the entity itself and apply the where clause filter, the filtering is done correctly.
What I was trying to do (and I'm not sure if this is possible but...) was run an entity query against the results variable and apply the where clause to that variable but I've had no luck. Is this the right way to go or is there another approach I could take?
Thanks in advance!
Got it... I was overcomplicating things too much.
I just converted the DateTime field to a Date field when returning it via the stored procedure. Doing so no longer required me to use the DbFunctions.TruncateTime method, thereby resolving my issue.
Hope this helps someone else!

Supplying the model with varying amounts of data from database

Supposed I have an online store and a model User, which contains a whole bunch of parameters (First Name, Last Name, an Address object, ProductCategoryAccess object etc.)
The way it's currently set up is the constructor accepts an Id and populates the entire class with data from the database.
But what if I don't need all that data? For example, if I have a view that just lists users, I'd have to create a list of Users, with each of them needlessly querying the database for information that I don't need (Address, ProductCategoryAccess, etc).
What's the best way of handling this? Do I create a bunch of different classes, with only the parameters I need for various situations? Do I create one User class, but with different "setter" methods, which will use different queries to populate only certain parameters?
Are you using any ORM tools to get the data? If yes, then you could use lazy binding to retrieve only elements that you need.
If you are directly querying the db, then dividing it across different methods with queries should do the trick.
What are the different situations you need to account for?
If you only wish to select certain properties from your user database record then you can use projections to bind the records into a data transfer object that contains only the properties you need for any given page.
For instance, if you needed properties for an edit user page then your query (I assume you're using Entity Framework?) would look something like this:
this.userContext.Users.Where(y => y.UserId == userId).Select(x => new UserEditDto {
FirstName = x.FirstName,
Surname = x.Surname,
EmailAddress = x.EmailAddress
}).FirstOrDefault();
What this will do is build a LINQ query against the database that selects only the properties specified within the select part of the LINQ statements. In fact, if you were to remove the FirstOfDefault() method and stick a breakpoint in you'd be able to see the SQL generated by that statement would look a little something like:
SELECT FirstName, Surname, EmailAddress FROM users WHERE userId = 1;
It's only when you call .FirstOrDefault() does the LINQ provider execute the query against the database and return the result.
From here you're able to return your DTO to your service layer or your controller to send it to a view within a view model. You can then rinse and repeat this same type of query for other possible data transfer objects and views that require certain data.

symfony admin generator table_method

When configuring my admin generator I created a table_method for my list view in order to join the correct tables and so on.
However, in my edit post / create post sections I have a rather extensive dropdown that is not joined at the moment. Is there an equivalent to table_method that I can use for these situations to specify the method that should be used for retrieving the record?
Thanks in advance.
You need to modify the respective widget in the form classes. (SomeModelForm.class.php in lib/form/doctrine).
All of the Doctrine widgets accept a "query" option to allow you to pass a Doctrine query to over-ride the default query the form creates, or a "table_method" option that can return a query or a doctrine collection to over-ride the default.
As a reference, see: http://www.symfony-project.org/api/1_4/sfWidgetFormDoctrineChoice
To use query, something along the lines of:
$somedoctrinequery = Doctrine::getTable('ModelName')->createQuery('t')->leftJoin('t.Relation r');
$this->widgetSchema['field_name']->setOption('query', $somedoctrinequery);
Or to use table_method:
$this->widgetSchema['field_name']->setOption('table_method', 'myMethod');

NHibernate + ASP.Net MVC - how to order data in strongly typed manned according to user selected field

I'm presenting data for users in a grid (ExtJS) with remote sorting and paging. Let's say I have a grid with some Orders. Order entity looks like Order{OrderNumber, Customer, Date, Address{Street, City, PostCode}}. Customer is mapped by NH as relation, Address is mapped as component. Data presented in the grid are flattened to columns named like this: OrderNumber, Customer.Number, Customer.Name, Date, Address.Street, Address.City, Address.PostCode.
User selects a column which he'd like to sort by and the grid sends the field name to server. Now on server side I need to restore backwards what entity property belongs to grid field name and decide if it's just component or if it's relation and build Criteria with CreateAlias + AddOrder etc. This logic is full of code like:
if (gridField=="Customer.Name"){
cri = cri.createAlias("Customer", "customerAlias");
cri.AddOrder(Order.Asc("customerAlias.Name"));
}
This is much simplified, but it neccesarily looks like this at the moment. I'm looking for some generic smarter solution. Any thoughts? The problem I'm facing now is that I can have a convention for transforming entity properties (including nested components and relations), but than I need to have a method how to determine if the field is mapped like component or relation. This would be quite heavy....
It would be quite heavy. I don't see a simple solution, but if you are planning on re-using this a lot, or need something very robust, you could build a system based on reflection.
Another possibility would be to use some T4 templates, but that would only help the 'string' issue, not the association issue.
How about:
cri = cri.CreateAlias( "Customer", "CustomerAlias" );
string sortProperty = gridField.Replace("Customer.", "CustomerAlias.");
cri.AddOrder( Order.Asc(sortProperty) );

Resources