Query builder dbflow conditional query - orm

how can i build a query based on certain conditions .
i tried doing this
QueryBuilder builder = SQlite.Select().from(Table)
if(condition) {
builder.where(something)
}
Cursor c = builder.query;
but it is not permitted.
I have to query my database on conditions that i have saved in preferences . I googled and searched everywhere in thr docs but couldn't find a single example . do this feature exists in dbflow if yes then how if no is thr any other orm (like greenDAO) with this feature

You can create conditional queries in DBFlow. To query a table's columns you must append _Table to your class name, then access its property. These _Table classes are generated during build time.
The most simple query would be this one:
SQLite.select()
.from(YourTable.class)
.where(YourTable_Table.id.eq(someId)
.queryList();
You can also add new conditions by using .and and .or in your query:
SQLite.select()
.from(YourTable.class)
.where(YourTable_Table.id.eq(someId)
.and(YourTable_Table.name.eq(someName)
.queryList();
For a cleaner code, you can also group conditions into condition groups like this:
ConditionGroup conditionGroup = ConditionGroup.clause();
conditionGroup.and(YourTable_Table.id.eq(someId);
if (someCondition) {
conditionGroup.and(YourTable_Table.name.eq(someName);
}
return SQLite.select()
.from(YourTable.class)
.where(conditionGroup)
.queryList();

found two ways of achieving my problem
1.from #trevjonez(trevor jones)
Where<SomeModel> query = SQLite.select()
.from(SomeModel.class)
.where(SomeModel_Table.date_field.greaterThan(someValue));
if(someCondition) {
query = query.and(SomeModel_Table.other_field.eq(someOtherValue));
} else {
query = query.and(SomeModel_Table.another_field.isNotNull());
}
Cursor cursor = query.query();
//do stuff with cursor and close it
—
2.from #zshock using ConditionalGroup
ConditionGroup conditionGroup = ConditionGroup.clause();
conditionGroup.and(YourTable_Table.id.eq(someId);
if (someCondition) {
conditionGroup.and(YourTable_Table.name.eq(someName);
}
return SQLite.select()
.from(YourTable.class)
.where(conditionGroup)
.queryList();

Related

Entity Framework filter data by string sql

I am storing some filter data in my table. Let me make it more clear: I want to store some where clauses and their values in a database and use them when I want to retrieve data from a database.
For example, consider a people table (entity set) and some filters on it in another table:
"age" , "> 70"
"gender" , "= male"
Now when I retrieve data from the people table I want to get these filters to filter my data.
I know I can generate a SQL query as a string and execute that but is there any other better way in EF, LINQ?
One solution is to use Dynamic Linq Library , using this library you can have:
filterTable = //some code to retrive it
var whereClause = string.Join(" AND ", filterTable.Select(x=> x.Left + x.Right));
var result = context.People.Where(whereClause).ToList();
Assuming that filter table has columns Left and Right and you want to join filters by AND.
My suggestion is to include more details in the filter table, for example separate the operators from operands and add a column that determines the join is And or OR and a column that determines the other row which joins this one. You need a tree structure if you want to handle more complex queries like (A and B)Or(C and D).
Another solution is to build expression tree from filter table. Here is a simple example:
var arg = Expression.Parameter(typeof(People));
Expression whereClause;
for(var row in filterTable)
{
Expression rowClause;
var left = Expression.PropertyOrField(arg, row.PropertyName);
//here a type cast is needed for example
//var right = Expression.Constant(int.Parse(row.Right));
var right = Expression.Constant(row.Right, left.Member.MemberType);
switch(row.Operator)
{
case "=":
rowClause = Expression.Equal(left, right);
break;
case ">":
rowClause = Expression.GreaterThan(left, right);
break;
case ">=":
rowClause = Expression.GreaterThanOrEqual(left, right);
break;
}
if(whereClause == null)
{
whereClause = rowClause;
}
else
{
whereClause = Expression.AndAlso(whereClause, rowClause);
}
}
var lambda = Expression.Lambda<Func<People, bool>>(whereClause, arg);
context.People.Where(lambda);
this is very simplified example, you should do many validations type casting and ... in order to make this works for all kind of queries.
This is an interesting question. First off, make sure you're honest with yourself: you are creating a new query language, and this is not a trivial task (however trivial your expressions may seem).
If you're certain you're not underestimating the task, then you'll want to look at LINQ expression trees (reference documentation).
Unfortunately, it's quite a broad subject, I encourage you to learn the basics and ask more specific questions as they come up. Your goal is to interpret your filter expression records (fetched from your table) and create a LINQ expression tree for the predicate that they represent. You can then pass the tree to Where() calls as usual.
Without knowing what your UI looks like here is a simple example of what I was talking about in my comments regarding Serialize.Linq library
public void QuerySerializeDeserialize()
{
var exp = "(User.Age > 7 AND User.FirstName == \"Daniel\") OR User.Age < 10";
var user = Expression.Parameter(typeof (User), "User");
var parsExpression =
System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] {user}, null, exp);
//Convert the Expression to JSON
var query = e.ToJson();
//Deserialize JSON back to expression
var serializer = new ExpressionSerializer(new JsonSerializer());
var dExp = serializer.DeserializeText(query);
using (var context = new AppContext())
{
var set = context.Set<User>().Where((Expression<Func<User, bool>>) dExp);
}
}
You can probably get fancier using reflection and invoking your generic LINQ query based on the types coming in from the expression. This way you can avoid casting the expression like I did at the end of the example.

Linq strategy for complex set

Am developing a ViewModel/PresentationModel which is getting complex.
I want the Linq query to return an IQueryable<UserPresentationModel>
Using EntityFramework against MSSQL
Is it possible to do any sort of iteration over the set before returning it to the presentation layer ie
List<UserPresentationModel> list = new List<UserPresentationModel>();
foreach (var person in listOfPeople)
{
UserPresentationModel u = new UserPresentationModel();
int userUIStatus = GetColourStateOfPerson(person);
u.FirstName = person.FirstName;
u.UserUIStatus = userUIStatus;
list.Add(u);
}
return list
This feels like it would always be N+1, and I'd never get the advantages of deferred execution, composing of queries..
Or (and I think am answering my own question) do I need to think in a SQL set based manner.
First, we can convert your code to LINQ.
IEnumerable<UserPresentationModel> models =
from person in listOfPeople
select new UserPresentationModel
{
FirstName = person.FirstName,
UserUIStatus = GetColourStateOfPerson(person)
}
return models.ToList();
Now, if GetColourStateOfPerson is making a DB round-trip, you definitely want to pull that out.
IDictionary<int, int> colourStatesByPersonId = GetColourStatesOfPeople(listOfPeople);
IEnumerable<UserPresentationModel> models =
from person in listOfPeople
select new UserPresentationModel
{
FirstName = person.FirstName,
UserUIStatus = colourStatesByPersonId[person.PersonId]
}
return models.ToList();
You could probably manage to create a single LINQ query that grabs just the first names and colour states of the people you want in a single query, but you haven't provided enough information about your data context for me to help you with that.
I would personally avoid passing around an IQueryable, which could continue making database trips any time somebody touches it. Let your data layer get out all the data you're likely to need, compose it into a list, and return that.
use IEnumerable<T>.Aggregate() instead of looping.
return listOfPeople.Aggregate(new List<UserPresentationModel>(), person => {
return new UserPresentationModel {
FirstName = person.FirstName,
UserUIStatus = GetColourStateOfPerson(person)
};
}).AsQueryable();
return listOfPeople.AsEnumerable().Select(p =>
new UserPresentationModel
{
FirstName = p.FirstName,
UserUIStatus = GetColourStateOfPerson(p)
}).AsQueryable();
I'm assuming that listOfPeople is an IQueryable that will eventually execute against your database. If that is the case then AsEnumerable() is important because SQL Server won't know what to do with GetColourStateOfPerson(). AsEnumerable() will force the IQueryable's expression tree to execute, pull the resulting rows out of your database and then apply Select() transformation in code as oppose to in SQL Server.
If you can implement GetColourStateOfPerson() as a stored proc or database function then you can omit AsEnumerable() and AsQueryable() and allow execution to delay even longer.

SQL to Magento model understanding

Understanding Magento Models by reference of SQL:
select * from user_devices where user_id = 1
select * from user_devices where device_id = 3
How could I perform the same using my magento models? getModel("module/userdevice")
Also, how can I find the number of rows for each query
Following questions have been answered in this thread.
How to perform a where clause ?
How to retrieve the size of the result set ?
How to retrieve the first item in the result set ?
How to paginate the result set ? (limit)
How to name the model ?
You are referring to Collections
Some references for you:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
http://alanstorm.com/magento_collections
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
lib/varien/data/collection/db.php and lib/varien/data/collection.php
So, assuming your module is set up correctly, you would use a collection to retrieve multiple objects of your model type.
Syntax for this is:
$yourCollection = Mage::getModel('module/userdevice')->getCollection()
Magento has provided some great features for developers to use with collections. So your example above is very simple to achieve:
$yourCollection = Mage::getModel('module/userdevice')->getCollection()
->addFieldToFilter('user_id', 1)
->addFieldToFilter('device_id', 3);
You can get the number of objects returned:
$yourCollection->count() or simply count($yourCollection)
EDIT
To answer the question posed in the comment: "what If I do not require a collection but rather just a particular object"
This depends if you still require both conditions in the original question to be satisfied or if you know the id of the object you wish to load.
If you know the id of the object then simply:
Mage::getModel('module/userdevice')->load($objectId);
but if you wish to still load based on the two attributes:
user_id = 1
device_id = 3
then you would still use a collection but simply return the first object (assuming that only one object could only ever satisfy both conditions).
For reuse, wrap this logic in a method and place in your model:
public function loadByUserDevice($userId, $deviceId)
{
$collection = $this->getResourceCollection()
->addFieldToFilter('user_id', $userId)
->addFieldToFilter('device_id', $deviceId)
->setCurPage(1)
->setPageSize(1)
;
foreach ($collection as $obj) {
return $obj;
}
return false;
}
You would call this as follows:
$userId = 1;
$deviceId = 3;
Mage::getModel('module/userdevice')->loadByUserDevice($userId, $deviceId);
NOTE:
You could shorten the loadByUserDevice to the following, though you would not get the benefit of the false return value should no object be found:
public function loadByUserDevice($userId, $deviceId)
{
$collection = $this->getResourceCollection()
->addFieldToFilter('user_id', $userId)
->addFieldToFilter('device_id', $deviceId)
;
return $collection->getFirstItem();
}

Kohana ORM count_all() working but find_all() is not

I'm having an issue where I'm building an ORM query based on several conditions from a $_POST. The final query looks fine and returns records in a direct SQL query (phpmyadmin) but in my application does not return any records. here is the code...
$records = ORM::factory('record')->where(array('date >='=>$_POST['fromdate'],'date <='=>$_POST['todate']));
if ($_POST['agent'] != '0') $records->where(array('ccp_id'=>$_POST['agent']));
if ($_POST['supervisor'] != '0') {
$ccps = ORM::factory('employee')->where(array('supervisor_id'=>$_POST['supervisor'],'active'=>'1'))->find_all();
foreach ($ccps as $ccp) {
$agents[] = $ccp->id;
}
// echo kohana::debug($agents);
$records->in('ccp_id',$agents);
}
if ($_POST['lead'] != '0') $records->where(array('lead_id'=>$_POST['lead']));
if ($_POST['reasons'] != '[]') {
$reasons = explode(',',str_replace(array('[',']','"'),'',$_POST['reasons']));
$records->in('reason_id',$reasons);
}
$records->find_all();
$records->loaded is false. If I change out the find_all() with count_all() I get an accurate count.
With sample data in the $_POST I have this query in $records->last_query()
SELECT `records`.*
FROM (`records`)
WHERE `date` >= '2010-10-10'
AND `date` <= '2010-11-09'
AND `ccp_id` IN ('E128092','E128093','E124874','E124414','E129056','E137678','E078952','E112701','E084457','E098047','E099221','E001131','E120892')
AND `lead_id` = 'E110873'
AND `reason_id` IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
ORDER BY `records`.`id` ASC
this returns 4 records in phpmyadmin and (4) for count_all().
I do not understand why this is happening. Any insights would be helpful. Thank you.
In your last line you should have
$records = $records->find_all();
instead of
// this actually returns you the resultset and resets the query builder object
$records->find_all()
$records is a Database_Result and has no loaded property. Use count($records) or iterate it with foreach statement to get ORM objects.
Just a note: It's probably better not to wipe out the ORM object ( $results = $records->find_all() instead of $records = $records->find_all()) if you wish to use $records->count_all() or other calls later in your code. Just an issue I ran into.

hibernate - createCriteria or createAlias?

If I want to search those students who take class "Math" and "John" is his group:
shoud I use createCriteria or createAlias?
Criteria:
Criteria criteria = session.createCriteria(Student.class);
Criteria subquery1 = criteria.createCriteria("courses", course).add(Restrictions.eq(course.name, "Math"));
Criteria subquery2 = criteria.createCriteria("group", student).add(Restrictions.eq(student.name, "John"));
how to put subquery1 and subquery2 together with initial criteria?
Alias:
Criteria criteria = session.createCriteria(Student.class).
createAlias("courses", course).add(Restrictions.eq(course.name, "Math")).
createCriteria("group", student).add(Restrictions.eq(student.name, "John"));
When to use createCriteria and when createAlias? I think the boath are the same...
CreateAlias and CreateCriteria are identical in the current versions of Hibernate and NHibernate. The only difference being that CreateCriteria has 2 additional overloads without the alias parameter.
Presumably they were different in a older version, but any differences are long gone.
An alias can be defined in terms of another alias, so your first example can be written as:
// Java
Criteria criteria = session.createCriteria(Student.class)
.createAlias("courses", "course")
.createAlias("course.group", "student")
.add(Restrictions.eq("course.name", "Math"))
.add(Restrictions.eq("student.name", "John"));
// C#
ICriteria criteria = session.CreateCriteria<Student>()
.CreateAlias("Courses", "course")
.CreateAlias("course.Group", "student")
.Add(Restrictions.Eq("course.Name", "Math"))
.Add(Restrictions.Eq("student.Name", "John"));
Adding to xavierzhoa's answer:
There is actually quite a big difference between the two methods which you will notice if you chain Criteria methods. You will continue to work on the original Criteria object when using createAlias, whereas you work on a more nested scope when using createCriteria.
Consider this:
Criteria c = getSession()
.createCriteria(YourEntity.class)
.createCriteria("someMember", "s")
.add(Restrictions.eq("name", someArgument)); // checks YourEntity.someMember.name
versus
Criteria c = getSession()
.createCriteria(YourEntity.class)
.createAlias("someMember", "s")
.add(Restrictions.eq("name", someArgument)); // checks YourEntity.name
However, if you always assign and use an alias you will be able to work around the difference. Like:
Criteria c = getSession()
.createCriteria(YourEntity.class, "y")
.createAlias("someMember", "s")
.add(Restrictions.eq("y.name", someArgument)); // no more confusion
Please refer to the following source code from the Hibernate
public Criteria createCriteria(String associationPath, String alias, int joinType) {
return new Subcriteria( this, associationPath, alias, joinType );
}
public Criteria createAlias(String associationPath, String alias, int joinType) {
new Subcriteria( this, associationPath, alias, joinType );
return this;
}
Criteria criteria = (Criteria)sessionFactory.getCurrentSession().createCriteria(BillEntity.class)
.createAlias("worksEntity", "worksEntity" ,JoinType.LEFT_OUTER_JOIN)
instead of writing (Jointype) use the import file as
(org.hibernate.sql.JoinType..LEFT_OUTER_JOIN)