Given a query for example:
from Users u where u.Country = "US"
I have a web app with a custom grid.
How do I query NHibernate to bring back a specfic page results
given page size and index???
Malcolm
Use SetFirstResult and SetMaxResults on the query or criteria:
int pagesize = 10;
int page = 2;
query = session.createQuery("...")
.SetFirstResult(pagesize * page)
.SetMaxResults(pagesize);
Related
My document schema is as follows.
Post
Id
Title
Body
Privacy -> Values can only be ["Me", "Anyone", "Team"]
UserId
What I want to do is retrieve all documents, but exclude documents where
Privacy = "Me"
and
UserId != "My-User-Id"
The SQL equivalent code is
SELECT * from Posts
WHERE (Privacy = "Anyone" OR Privacy = "Team")
OR (Visibility = "Me" AND UserId = 'My-User-Id')
I want to know how I can convert that SQL to the Elastic Search equivalent.
Thanks, any help would be appreciated.
In rails 5, I am using pg(postgresql) for a back-end database. Now I want to query through rails and get the data. How can I use IN and ORDER(:created_at, :desc) conditions in a query.
In controller,
PAGE_LIMIT = 5
posts = Post.where("user_id IN (?)", [1,2,3]).order(created_at: :desc)
posts = posts.paginate(params[:page], PAGE_LIMIT)
I am writing a custom method like,
def paginate(page, limit = 5)
page = page ? page.to_i : 1
limit = limit.to_i
offset = (page - 1) * limit
self.offset(offset).limit(limit)
end
I am new to postgresql. Please help me to solve this issue?
suppose you have User model and you want to get user which has id in [1,2,3]
User.where("id IN (?)", [1,2,3]).order(created_at: :desc)
for more dynamic
x = [1,2,3]
name = "testing123"
User.where("name = ? AND id IN (?)", name, x).order(created_at: :desc)
for more details you can see Active Record Query
to make it working with pagination for array changes to be done here
modified answer
PAGE_LIMIT = 5
posts_arr = Post.where("user_id IN (?)", [1,2,3]).order(created_at: :desc)
posts = Post.where(id: posts_arr.map(&:id)) #here to get active records of posts to make it working for custom pagination
posts = posts.paginate(params[:page], PAGE_LIMIT)
I have sqlite database with article table that contains articleID and articleDescription.
I'm using expressjs as server:
app.get('/articleDetail/:id',function(req,res){
res.sendFile(path.join(__dirname+'/articleDetail.html'));
var id = req.params.id;
var query = "select * from article where articleID = " + id;
});
I want the to show the content of the article based on its id in the database.
An example: when you select article 5 on the home page, it take you to url ..../articleDetail/5 and the content is shown to the user.
Summary of the 2 questions I have:
How can I connect and id in my database to the id of the url/route?
How can I show the correct article content based on its id using express?
Thanks for any help.
Install module called sqlite3 into your project. It provides exactly what you want, here you can see detailed guide on how to install it and use it for connecting and querying database - http://www.sqlitetutorial.net/sqlite-nodejs/connect/
So steps should be like this:
Get article id from request or url params e.g let id = req.params.id
Execute query and get the item from database
pass the value (article) object to view and render there
app.get('/articleDetail/:id',function(req,res){
var id = req.params.id;
var query = "select * from article where articleID = " + id;
// execute the query here
res.sendFile(path.join(__dirname+'/articleDetail.html'));
});
I have a model, lets say Ad, which I plan to fetch 2 datasets from, then merge them into 1. Is this possible:
ads = Ad.where(etc).limit(5)
if ads.length < 5
merge = Ad.where(etc).limit(remainder)
// merge both here
ActiveRecord allows you to do chaining. Try this out, it will execute only one query.
ads = Ad.where(etc).limit(5)
ads = ads.where(etc).limit(remainder)
But in your case, you call the length method so it will execute two queries. However, you can still do chaining. User count method is better than length method because count will send a COUNT(*) to your database (faster). length will load all records based on the where conditions and do a count on array (slower).
ads = Ad.where(etc).limit(5)
if ads.count < 5
ads = ads.where(etc).limit(remainder)
end
When paging data, I want to not only return 10 results, but I also want to get the total number of items in all the pages.
How can I get the total count AND the results for the page in a single call?
My paged method is:
public IList GetByCategoryId(int categoryId, int firstResult, int maxResults)
{
IList<Article> articles = Session.CreateQuery(
"select a from Article as a join a.Categories c where c.ID = :ID")
.SetInt32("ID", categoryId)
.SetFirstResult(firstResult)
.SetMaxResults(maxResults)
.List<Article>();
return articles;
}
The truth is that you make two calls. But a count(*) call is very, very cheap in most databases and when you do it after the main call sometimes the query cache helps out.
Your counter call will often be a little different too, it doesn't actually need to use the inner joins to make sense. There are a few other little performance tweaks too but most of the time you don't need them.
I believe you actually can do what you ask. You can retieve the count and the page in one go in code but not in one SQL statement. Actually two queries are send to the database but in one round trip and the results are retrieved as an array of 2 elements. One of the elements is the total count as an integer and the second is an IList of your retrieved entities.
There are 2 ways to do that:
MultyQuery
MultiCriteria
Here is a sample taken from the links below:
IList results = s.CreateMultiQuery()
.Add("from Item i where i.Id > :id")
.Add("select count(*) from Item i where i.Id > :id")
.SetInt32("id", 50)
.List();
IList items = (IList)results[0];
long count = (long)((IList)results[1])[0];
Here is more info on how you can do that. It is really straight forward.
http://ayende.com/Blog/archive/2006/12/05/NHibernateMutliQuerySupport.aspx
http://ayende.com/Blog/archive/2007/05/20/NHibernate-Multi-Criteria.aspx
If you read the 2 articles above you will see that there is a performance gain of using this approach that adds value to the anyway more transparent and clear approach of doing paging with MultiQuery and MultiCriteria against the conventional way.
Please note that recent versions of NHibernate support the idea of futures, so you can do.
var items = s.CreateQuery("from Item i where i.Id > :id")
.SetInt32("id", 50)
.Future<Item>();
var count = s.CreateQuery("select count(*) from Item i where i.Id > :id")
.SetInt32("id", 50)
.FutureValue<long>();
This is a much more natural syntax, and it will still result in a single DB query.
You can read more about it here:
http://ayende.com/Blog/archive/2009/04/27/nhibernate-futures.aspx