How to use database data on Event OnBeforeCreate - google-cloud-sql

I want to get the last data created on my database, more specifically the field called: "Saldo Atual" and after this get the form data field "Valor" and make a sum between this fields (the Old one "Saldo Atual" and the new "Valor") before create a new record and add this "Valor" and the sum of "Saldo Atual"+"Valor".
I don't know how to get the database last record
var saldoAtual =
record.saldoAtual = saldoAtual + record.valor;
And I want to know if the second line it's correct to save the sum of "Saldo Atual"+"Valor"

Based on my comment, I think that your records will need to have a Date_Created field. Then in your onBeforeCreate event the following script should work (this is untested, so you need to do your own testing):
var query = app.models.YourModelTheSameAsYourEventModel.newQuery();
query.sorting.Date_Created._descending();
query.limit = 1;
var lastrecord = query.run();
record.saldoAtual = lastrecord.saldoAtual + record.valor; //You may need to use lastrecord[0].saldoAtual
record.Created_Date = new Date();
Try that and do some testing to see if this yields your desired output.

Related

NHibernate error system.object[] when retrieving multiple entities in one query

I have a HQL query like this:
string QueryString = select client, transporter
from BaseClient as client, BaseTrans as transporter
where client.tr = transporter.Id and transporter.badge = 1
order by transporter.date;
But when I use this hql I receive the following error :
The value "System.Object[]" is not of type "xxx" and cannot be used in this generic collection.
Parameter name: value
Just like Example but, when I omit the Transporter entity in my select it works.
Like :
string QueryString = select client
from BaseClient as client, BaseTrans as transporter
where client.tr = transporter.Id and transporter.badge = 1
order by transporter.date;
But I need transporter in my select, because I use order by.
By the way, I have 2 hbm Client.hbm.xml and Transporter.hbm.xml. Each hbm have their own Class and Table.
i call it with :
IQuery requete = this.CreateQuery(QueryString);
IList<Client> executions = requete.List<Client>();
it hang on this line, when hibernate try to convert to the list
This happens because your result-set will likely be a multi-dimensional array where the first column represents a Client and the 2nd column contains a Transporter.
What happens if you change your code like this:
IQuery requete = this.CreateQuery(QueryString);
var result = requete.List();
var clients = result[0] as IEnumerable<Client>;
(I have no NHibernate installed on this system so I cannot just test something out quickly without creating and setting up a new project. :)

Ionic 2 Storage - Adding Records

I am new to the NoSQL world and since Ionic 2 by default supports simple key-value DB, I was to some help here.
My app has a very large form. How do I go about saving new records? How do I retrieve those particular records?
Currently, to save a new record, I am doing something like this:
save(data){
let newData = JSON.stringify(data);
this.storage.set('reports', newData);
}
The problem with this is it overwrites the record instead of inserting a new record.
I am retrieving records like this:
getData() {
return this.storage.get('reports');
}
How do I go about fetching a particular record using certain values in the stored JSON?
Thanks.
What you would have to do is make reports as an array and set it to the storage.
everytime you need to insert a new record, do a
function(newData){
var some_variable = storage.get('reports'); //get Existing Table
some_variable.push(newData); //Inserts the new record to array
storage.set('reports', some_variable); //Saves report with updated data
}
For getting a particular report alone, I hope you have some id or a unique attribute y which you can distinguish a report. Assuming you have the report json as below :
var report {id : "UniqueID", name : "A sample report json"}
Then to get the report,
function(reportId){
var reports = this.storage.get('reports');//fetches your reports Array
var wantedReport = {};//Variable to store the wanted report
reports.forEach(function(r){ //Looping the array.You can use a forloop as well
if(r.id === reportId){ //filtering for the wanted reportId
wantedReport = r; // storing the report to variable
}
})
return wantedReport; //Returning the report to the caller.
}
Alternatively, If you are used to Sql and want a Sql-like way of storing these data then you can install the Sqlite cordova plugin and store your data in a Sqlite DB.

Rally: how to pull latest testresult from a specific testset

I am trying to use the following code to get a pie chart for the latest testresult from a particular testset.
pieconfig = {
type: 'TestCaseResult',
attribute: 'Verdict',
query: rally.sdk.util.Query.and(['TestSet = "' + testsetDropdown.getValue() + '"','Date < "2012-11-01"'])
};
var pieChart = new rally.sdk.ui.PieChart(pieconfig,rallyDataSource);
pieChart.display("pieChartDiv");
In this code I've put today's date manually, but I want to make this query as of a generic type which should pull the latest testresult from a specific testset. Any hints...? Thank you.
Rally has a nice DateTime format that formats dates to whatever you want. Here's the reference you can use: http://developer.rallydev.com/help/datetime.
If you always want the current day in the year-month-day format, you can do this:
var dateQuery = 'Date < ' + rally.sdk.util.DateTime.format(new Date(), "yyyy-MM-dd");
Then your query will look something like this:
query: rally.sdk.util.Query.and(['TestSet = "' + testsetDropdown.getValue() + '"', dateQuery])
EDIT: Now I see what you mean by grabbing the latest TestCaseResult. You don't actually need the Date conditional in your query. What you need to add is a couple of options to your pie config.
var pieconfig = {
type: 'TestCaseResult',
attribute: 'Verdict',
query: '(Test Set = "' + testsetDropdown.getValue() + '")',
order: 'CreationDate desc',
pagesize: 1
};
This will sort by the Creation Date in descending order, which means the most recently made TestCaseResult will be on top. Setting a pagesize to 1 means you'll always get the most recent result.
This also solves the issue of not getting test case results of the current day. The previous query will get all TestCaseResults from today at 12:00AM backward. So any test case results made in the current day will not be included.
Let me know if that doesn't work out for you. I'm pretty new to the Rally SDK1 query system.

Creating optimal query of oracle db using EF

I have an Oracle database where I am storing information about customers.
One of the fields is user number.
My UserNumber column is type of text.
A user sends a number in various formats:
+44777XXXXXXX
777XXXXXXX
0777XXXXXXX
So far I have:
var list = context.UserDetails.Where(x => x.UserNumber == number).ToList();
I can also do this with:
var strippedNumber = ConvertNumberToBasic(number); // this will return me number as 777XXXXXXX
now
var list = context.UserDetails.Where(x => x.UserNumber.Contains(number)).ToList();
Is there a more optimal way for me to do this?
You really need to ask the customer what they need before you can write any code for this...

NumericRangeQuery in NHibernate.Search

I am creating a search, where the user can both choose an interval and search on a term in the same go.
This is however giving me trouble, since I have up until have only used the usual text query.
I am wondering how I am to go about using both a NumericRangeQuery and a regular term query. Usually I would use a query below:
var parser = new MultiFieldQueryParser(
new[] { "FromPrice", "ToPrice", "Description"}, new SimpleAnalyzer());
Query query = parser.Parse(searchQuery.ToString());
IFullTextSession session = Search.CreateFullTextSession(this.Session);
IQuery fullTextQuery = session.CreateFullTextQuery(query, new[] { typeof(MyObject) });
IList<MyObject> results = fullTextQuery.List<MyObject>();
But if I was to e.g. search the range FromPrice <-> ToPrice and also the description, how should I do this, since session.CreateFullTextQuery only takes one Query object?
you can create a single query that is a BooleanQuery combining all the conditions you want to be met.
For the ranges, heres a link to the synthax using the QueryParser:
http://lucene.apache.org/core/old_versioned_docs/versions/2_9_2/queryparsersyntax.html#Range Searches