CouchDB View with Multiple Parameters - view

I have a database that has an id column and a ts column. I need to be able to pass in the id and a start time and end time to retrieve all the values during the specified period. Can I do this with a view, or do I need the view to return all of the values that match the id? My concern is that I will be returning and parsing a lot more data than I really care about. Here is the format of my DB from my current view, which simply returns everything that matches the id...
{"id":"62db2aa3472dce80b1f2193fc21d52fd","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d52fd","_rev":"1-6aadd58f4f5dabacf6f4f638396246d0","id":"A-Meter-KW","ts":1437969600000,"tz":"New_York","val":"191kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d5100","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d5100","_rev":"1-71155153c0f03c49b02850bee5535e22","id":"A-Meter-KW","ts":1437968700000,"tz":"New_York","val":"190kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d45d7","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d45d7","_rev":"1-661511616958d45fdff3307600d2a9ed","id":"A-Meter-KW","ts":1437967800000,"tz":"New_York","val":"189kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d3c23","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d3c23","_rev":"1-4e97cfc6cb97ddc65f04efd9043b3abd","id":"A-Meter-KW","ts":1437966900000,"tz":"New_York","val":"188kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d2e35","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d2e35","_rev":"1-120298e95c9d2b4b9cdf438836b6c0c0","id":"A-Meter-KW","ts":1437966000000,"tz":"New_York","val":"187kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d22b0","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d22b0","_rev":"1-61e55d02bd8f0c601274b904f46c9f34","id":"A-Meter-KW","ts":1437965100000,"tz":"New_York","val":"186kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1ce2","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1ce2","_rev":"1-b4fe80563c70a40981e293af9c6a87b3","id":"A-Meter-KW","ts":1437964200000,"tz":"New_York","val":"185kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1ccc","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1ccc","_rev":"1-bdf1881c4270e68e7a7ed90a1d945228","id":"A-Meter-KW","ts":1437963300000,"tz":"New_York","val":"184kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1303","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1303","_rev":"1-404d5934fc882aa36e6d355d9a3485ae","id":"A-Meter-KW","ts":1437962400000,"tz":"New_York","val":"183kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d0941","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d0941","_rev":"1-64288d1c98e9b93aa6c546acb1e02078","id":"A-Meter-KW","ts":1437961500000,"tz":"New_York","val":"182kW"}}
...
... my current query is http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true. I'd like to passing a start and end time as well, something like http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true&start=1437963300000&end=1437966000000 but cannot figure out how to do this.

EDITED:
In order for Couch to match your query all the data (eg:A-Meter-KW and date) must be in the key, emitted by the view. So I think you might do something like:
emit([key,year,month,day],doc._id)
Then you can use the parameters startkey and endkey to filter the results properly.
Reference: http://guide.couchdb.org/draft/views.html#many
Side consideration: I would not use an "id" property inside my documents, because it could get easily confused with the "_id" (compulsory) one.

Related

Laravel - find offset of record in mysql results

I have a MySQL table of records with several fields.
These records are shown and updated live in the browser. They are displayed in an order choosen by the user, with optional filters also choosen by the user.
Sometimes a change is made to one of the records, and it may affect the order for a given user.
In order to position the message correctly in the list, I need to find where its new offset falls after the change to the record. Basically, I need to get the "id" for the record that now comes before it in the MySQL results, so that I can use Javascript on the client side to reposition the record on the screen.
In raw SQL, I'd do something like this:
SET #rank=0;
SELECT rank
FROM
(SELECT #rank:=#rank+1 AS rank,
subQuery.id AS innerQuery,
FROM ...{rest of custom query here}... as subQuery)
AS outerQuery WHERE outerQuery.innerQuery={ID TO FIND};
Then I can just subtract 1 from the resulting rank, and find the ID of the question that comes before the record in question.
Is this kind of query possible with Laravel's query builder? Or is there a better strategy than what I've come up with here to accomplish the same task?
EDIT: There are a lot of records. So if possible I'd like to avoid loading all the records into memory to find the offset of the record. They are originally loaded on the screen in an "infinite scroll" type method, since it would be too much to load all of them at once.

How to allow user to add information for every fields in forms

i have a strange user request: they want to be able to say for every fields in every forms from where they get information used to compile it.
value must be selected from a list
essentially iam looking a way to avoid to double the fields.
one way can be add a M:M table where link field-name and information-source
but using this for retrive from real data table only fields derived from an info-source can be really bad
EDIT
to be more clear the solution i want to avoid is to have a data table like this:
fieldname1
fieldname1Origin
fieldname2
fieldname2Origin
fieldname3
fieldname3Origin
...
but i do the link table in this way:
FieldName
OriginId
i have to map oll field names and to search in it with string name when i need to query data table i have to do something like this
select
case when (exists select 1 from LinkTable where FieldName='fieldname1' AND
OriginId=SelectedID) then fieldname1
else NULL
end
for every fields
What about mixing several controls, such as a combobox with all lines, that would trigger the display of all columns of that line.
Something like that (I am currenlty working on that project):

How to stop Doctrine from returning a primary key for every query

I am kind of annoyed at Doctrine for returning primary keys in each and every query even though I don't want it to. Is there anyway to stop this ? coz I don't really want those damn primary keys along with my doctrine query results.
A query for instance that I have is:
$getAllDatesForUserQuery = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_ARRAY) ;
In this situation, it retrieves all the datenames as it should, but also happily returns the primary key column value. I DON"T WANT IT.
Is it me? or is it Doctrine ?
In a case like this where you want a simple array and only have a single field being selected, the answer is the Single Scalar Hydration mode. Use it like this:
$q = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_SINGLE_SCALAR);
You should find that the query will return a simple one-dimensional array containing only the value(s) you wanted.

How can I get the IQueryable object used by LinqDataSource?

Is there a way to get the IQueryable object that the LinqDataSource has used to retrieve data? I thought that it might be possible from the selected event, but it doesn't appear to be.
Each row in my table has a category field, and I want to determine how many rows there are per category in the results.
I should also note that I'm using a DataPager, so not all of the rows are being returned. That's why I want to get the IQueryable, so that I can do something like
int count = query.Where(i => i.Category == "Category1").Count();
Use the QueryCreated event. QueryCreatedEventArgs has a Query property that contains the IQueryable.
The event is raised after the original LINQ query is created, and contains the query expression before to it is sent to the database, without the ordering and paging parameters.
There's no "Selected" event in IQueryable. Furthermore, if you're filtering your data on the server, there'd be no way you can access it, even if the API exposed it, but to answer a part of the question, let's say you have category -> product where each category has many products and you want the count of the products in each category. It'd be a simple LINQ query:
var query = GetListOfCategories();
var categoryCount = query.Select(c => c.Products).Count();
Again, depending on the type of object GetListOfCategories return, you might end up having correct value for all the entries, or just the ones that are loaded and are in memory, but that's the different between Linq-to-Objects (in memory) and Linq-to-other data sources (lazy loaded).

Generate reports in birt using user input. When input is null, everything should be fetched otherwise corresponding data should be shown

I have to create a birt report with user input parameters. It is something like when the para
meter is left blank it should fetch all values from a table otherwise when the user inputs the students roll no.,the corresponding data should be fetched. Can this be done through Birt report? If yes, then please suggest a way.
Thanks!
Yes, you can do that. If the parameter is optional you can't use the Dataset Parameter (with a ? in your query), because it will be null. Instead you have to modify your query using JavaScript.
Create a Report Parameter like usual, in this case 'stud_no'. Then add a comment in your SQL that you are reasonably sure is unique, I use something like --$stud_no$, wherever you want your clause inserted.
Then add a script like this to your Data Set, in beforeOpen:
if (params["stud_no"].value){
this.queryText = this.queryText.replace("--$stud_no$", "and stud_no = " + params["stud_no"]);
}
This replaces the comment with the clause when the parameter has a value. You can use regex in the search string, and then you can also insert it multiple places if you want.
Create your paremater using a like statement
where students_roll_no like ?
Create your report paramater using as a text box, with a defualt value of %
Because the percent '%' is the SQL wildcard, it returns all values. If the user enters a Student Roll number, it returns only that record. Additionally the user can enter 0500% and get all the records that begin 0500.

Resources