I want to use following query in fluent nHibernate:
_dataContext.Products
.Where(filterExpression)
.OrderBy(sortExpression + " " + sortDirection)
.Skip(pageIndex * pageSize)
.Take(pageSize);
in both Criteria and HQL use
.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize)
LinqToNhibernate:
session.Query<Product>()
.Where(filterExpression)
.OrderBy(sortExpression + " " + sortDirection)
.Skip(pageIndex * pageSize)
.Take(pageSize);
Related
I am using this query to retrieve latest record based on runId, but this query is giving any result.
#Query("select vr1 from VendorResult vr1 left join VendorResult vr2 on (vr1.applicationId = vr2.applicationId "
+ "and vr1.productType=vr2.productType and vr1.runId<vr2.runId) "
+ "where vr1.applicationId=?1 and vr1.overallStatus=?2 and vr2.applicationId is null")
List<VendorResult> getVendorResults(String applicationId,String overallStatus);
Tried this also
#Query(value = "select vs from VendorResult vs where vs.applicationId=?1 and vs.overallSatatus=?2 and vs.runId = (select max(v.runId) from VendorResult v where v.applicationId = vs.applicationId)",nativeQuery = true)
List<VendorResult> getVendorResults(String applicationId,String overallStatus);
Can you please help me?
a paginated select with a page long 1 and order by id desc will get you what you need
#Query("select vr1 from VendorResult vr1 where vr1.applicationId=?1 and vr1.overallStatus=?2 order by vr1.runId DESC")
List<VendorResult> getVendorResults(String applicationId,String overallStatus, Pageable pageable);
invoke with
repository.getVendorResults(applicationId, overallStatus, PagePageRequest.of(0, 1));
I do search function In raw SQL like this
//test condition variable
if ($param_type != "") {
if($param_type =='SAS'){
$str .= " AND pos.type IN('SAS1','SAS2') ";
}
else if($param_type =='SME'){
$str .= " AND pos.type IN('SME1','SME2') ";
}
else if($param_type =='PT'){
$str .= " AND pos.type IN('PT','PT0') ";
}else{
$str .= " AND pos.type ='$param_type'";
}
}
//SQL
$query =" SELECT * FROM tbl_user AS u INNER JOIN tbl_position AS pos
ON u.user_id = pos.user_id where u.levels IN ('7','8','9') .$str ;" ;
How I can write SQL like this in laravel ?
You can use raw query.
$query = DB::select(SELECT * FROM tbl_user AS u INNER JOIN tbl_position AS pos
ON u.user_id = pos.user_id where u.levels IN ('7','8','9') .$str ;");
Of course then you need to add at the top in your controller use DB;
Anyway Raw queries may be dangerous and it's better to use query builder.
Simple example:
$query = DB::table('tbl_user')
->join('tbl_position','tbl_user.user_id','=','tbl_position.user_id')
->where('something','=','something')
->get();
Query builder: query builder
Reply to my question for who meet problem like me:
Example like this
$query = DB::table('user');
if ($param_type =='PT')
$query->where('pos.type', '=', 1);
}
$result = $query->get();
Have to convert below working query into hql query. product and car is the domain.
Product {
long id
hasmany car: Car
string code
}
Car {
int no
int value
}
select p.id from products p
inner join car c on c.id=p.id and p.code='car'
inner join car cr on cr.id=p.id and p.code='car'
where c.no=1 and c.value>=10 and c.no=2 and c.value<=30
Looking at the definition of your model it should be enough to do the following query
var query = session.CreateQuery("select p.id from Product p join p.car c" +
" where c.no=1 " +
" and c.value>=10 " +
" and c.no=2 " +
" and c.value<=30 " +
" and p.code='car' ");
var results = query.List<int>();
with session being instance of ISession interface.
I have Java method which call Native SQL (Oracle) via Hibernate:
public List<Location> getLocationsAround(double latitude, double longitude, double radius, long retailerId) {
List<Location> locationList = (List<Location>) SessionManager.getSession().createSQLQuery(
"SELECT loc.*, distance(ci.coord1, ci.coord2, :latitude, :longitude) as dist " +
"FROM location loc " +
"join rl_retailer_location rrl on rrl.location_id = loc.location_id " +
"join contact_info ci on ci.contact_info_id=loc.contact_info_id " +
"WHERE rrl.retailer_id=:retailerId " +
"and NVL(distance(ci.coord1, ci.coord2, :latitude, :longitude), :limit) <= :radius " +
"ORDER BY dist ASC"
).addEntity("loc", DAO.getInstance().getMappedClass(Location.class))
.setLong("retailerId", retailerId)
.setDouble("latitude", latitude)
.setDouble("longitude", longitude)
.setDouble("radius", radius)
.setDouble("limit", radius + 1.)
.list();
return locationList;
}
For dist calculation is used FUNCTION (stored procedure) distance which has 4 parameters (numbers): latitude1, longitude1, latitude2, longitude2 and returns NUMBER(18,6) or null (if parameter invalid). dist is used in ORDER BY for result set sorting.
This version works as expected.
Question: How can I rewrite query for reuse part dist in WHERE?
Goal: eliminate 2x calculation distance(ci.coord1, ci.coord2, :latitude, :longitude)
In Oracle you can use a query as table in your FROM clause, so you can do something like:
SELECT *
FROM (
SELECT loc.*
, distance(ci.coord1, ci.coord2, :latitude, :longitude) as dist
FROM location loc
JOIN rl_retailer_location rrl on rrl.location_id = loc.location_id
JOIN contact_info ci on ci.contact_info_id=loc.contact_info_id
WHERE rrl.retailer_id=:retailerId
) loc
WHERE dist <= :radius
ORDER BY dist ASC
Given are movies and actors in an m:n relation. What I want to do is retrieve a list of actors, ordered by the number of movies they played in.
class Movie
include DataMapper::Resource
property :id, Serial
property :title, String
has n, :actors, through: Resource
end
class Actor
include DataMapper::Resource
property :name, String, key: true
has n, :movies, through: Resource
end
In pseudo-DM what I want is this:
Actor.all order: [ :movies.count ]
I found another question about sorting by a single attribute of an association but this approach only worked for real properties. Any usable solution would be helpful. Thx!
Taking the answer by Sean Larkin as a starting point I ended up with something like this:
actors = repository(:default).adapter.select(
"SELECT actors.name, count(actor_movies.actor_name) AS count " +
"FROM actors " +
"JOIN actor_movies WHERE actors.name = actor_movies.actor_name " +
"GROUP BY actors.name " +
"ORDER BY count(actor_movies.actor_name) desc " +
"LIMIT 5;"
)
=> [
#<struct name="Samuel L. Jackson", count=66>,
#<struct name="Michael Caine", count=64>,
#<struct name="Robert De Niro", count=59>,
#<struct name="Harvey Keitel", count=58>,
#<struct name="Gene Hackman", count=57>
]
The documentation for DataMapper is little outdated and I struggled trying to accomplish the same thing that you were doing.
I instead used a direct MySQL query:
records = repository(:default).adapter.select(“SELECT * FROM actor ORDER BY count(movies) desc;”)
It is important to note that when you use the direct MySQL query, that a struct is returned rather than just a hash of the data. You will have to convert it into a hash manually if, say you are returning this data as JSON.
You could convert a struct to hash in Ruby 1.8-1.9 via:
actors = repository(:default).adapter.select( "SELECT actors.name, count(actor_movies.actor_name) AS count " + "FROM actors " + "JOIN actor_movies WHERE actors.name = actor_movies.actor_name " + "GROUP BY actors.name " + "ORDER BY count(actor_movies.actor_name) desc " + "LIMIT 5;" ).map{|struct| {:name => struct.name, :count => struct.count}}
In Ruby 2.0, they added to_h so you can use this:
actors = repository(:default).adapter.select( "SELECT actors.name, count(actor_movies.actor_name) AS count " + "FROM actors " + "JOIN actor_movies WHERE actors.name = actor_movies.actor_name " + "GROUP BY actors.name " + "ORDER BY count(actor_movies.actor_name) desc " + "LIMIT 5;" ).map(&:to_h)