How to force update a DB grid? - database

I may have been too clever for my own good :-/
I have a table which holds some pressure measurements. These are always stored as PSI, but the user can select a radio group button to toggle between PSI and BAR.
In order to keep the code clean and and push work onto the database, I created a second table for configuration items, with a single row. One column psi_bar_conversion will take the value either 1 or 14.5 as the user toggles the radio group.
In Delphi, my query which ties to my DB grid is set up with statements like
SELECT ROUND(inlet_waterPressure_psi /
(SELECT psi_bar_conversion FROM configuration),
(SELECT float_precision FROM configuration))
AS inlet_waterPressure,
FROM measurements
All of which works just fine (and perhaps I am explaining too much).
All that I am tring to do is add some code in the function which handles the radio button toggle to force my DB grid to refresh its contents becuase I have just updated the value of configuration.psi_bar_conversion (but no direct field of my query, nor of my datasource).
Should I invoke Refresh() or Invalidate() or SomeOtherFunction() - of the DB grid, the query, the datasrouce? That's what is confusing me.
Thanks in advance for any help ....

You need to close and then reopen the query to have the change in psi_bar_conversion and float_precision to take effect. The two sub-selects (for the values from configuration) only happen when the query is executed.

TDBGrid presentation depends on the connected TDataSet (via a TDataSource).
To update the Grid Values you have to refresh the data in TDataSet with the method TDataSet.Refresh.
To update a special Grid you can refresh the connected DataSet like this:
DBGrid1.DataSource.DataSet.Refresh;
But some TDataSet descendants will not refresh and that is documented by Embarcadero
TDataSet.Refresh
It depends on the components you use (i did a test with UniDAC and it works fine)
procedure TForm1.RadioGroup1Click( Sender : TObject );
var
LRate : Extended;
begin
case RadioGroup1.ItemIndex of
0 :
LRate := 1;
1 :
LRate := 14.5;
end;
UniConnection1.ExecSQL( 'UPDATE configuration SET psi_bar_conversion = :conversion', [LRate] );
DBGrid1.DataSource.DataSet.Refresh;
end;
If your components did not refetch the data on calling Refresh, then you have to close and reopen (also stated by documentation)
DBGrid1.DataSource.DataSet.Close;
DBGrid1.DataSource.DataSet.Open;
IMHO such components are not fully implemented, so this is just a workaround having side effects in calling some maybe unwanted events (BeforeClose, AfterClose, BeforeOpen, AfterOpen) which will not get fired using Refresh.
But at all it has nothing to do with SubSelects.

I'm afarid I can't help you with the Delphi side of things here.
On the datbase side of things....
Is there any reason that you can't just store both the bar and psi values in the database?
You could do the conversion when saving and then you are left just to do a simple select on the data when you want to view it. This could be done by the software that is performing the save or by a trigger in the database.
The reason I suggest this is that the psi-bar conversion ratio is not going to change so you are doing a bunch of processing everytime you view the data that is not required...

Related

How to reload child view object completely in ADF

I have a 2 ViewObjects A and B. each connected with a ViewLink. I have shuttled the ViewLink in the ApplicationModule. So B comes under A in DataControl.
I have a Sunburst graph component using this instance. My requirement is to change the ViewObject Query at runtime. This works fine. But the graph is not getting refreshed for the level 2. Only Level 1 getting refreshed.
ie; A is only getting refreshed.
How to refresh B (Or the view link) ?
This sounds weird since view link usually get refreshed together automatically.
Your requirement is change the VO query at runtime. I don't have much details on your implementation, how would you manage to do this? Through an ApplicationModule method ? or some VO manipulation. Anyway, after you update a query, you need to do an executeQuery() right?
A possible way to solve your problem is to call B.executeQuery() after you update A's query.

How do you use the SQLQuery component to run multiple queries?

I have a ListBox showing all the records for a single field (first) in a database. When users click the ListBox item I'd like the value for a different field in the db (last), same record, to be displayed. My code ListBox OnClick event code is:
SQLQuery2.SQL.Text:='SELECT * FROM Names WHERE first= :FIRST';
SQLQuery2.Params.ParamByName('FIRST').AsString := ListBox1.Items[ListBox1.ItemIndex];
SQLQuery2.Open;
ShowMessage('You selected '+SQLQuery2.FieldByName('last').AsString);
When you click an item, the expected field comes up in the MessageBox. However, if you then click a different item, nothing changes--the MessageBox shows the original field.
I don't know enough about SQLQuery components and how they interact with the underlying db to know what's happening.
[P.S. The db is sqlite3, if that matters, and I'm using Lazarus rather than Delphi, if that matters.]
You need to close your query before opening it again. If you look at the code for Open on TDataSet it sets the Active property to true. The setting code for Active first checks that the value is different before doing any work:
procedure TDataSet.SetActive(Value: Boolean);
begin
..
if Active <> Value then
begin
end;
end;
So in your case, the active property is already true and the code just exits.

Oracle ADF web browser refresh button gets old page

i'm developing an ADF application. I have a problem. My Jdev version 11.1.2.3.0.
So, For example i have an URL like this "http://www.hohoho.com/view/index.xhtml?_adf.ctrl-state=ju9lnu5ld_3"
In this page i have a table gets value from DB. For example again, i changed some rows in DB browser, and clicked the web browser's refresh button. But new results doesnt get!. For example i remove the "?_adf.ctrl-state=ju9lnu5ld_3" and enter url, this gets new results. How can handle this situation. I need that when an user clicked the refresh button, last result must be fetched. I think it is based on ADF state. How can handle this situation.
Solution
Thanks Andread, mysql's autocommit property default value is true, not false :) but i was using it false. I've solved the problem like yours, but my own solution so cool :) I've only overried the clearCache() method. And it solved the problem.
public void clearCache() {
getDBTransaction().commit(); // added
super.clearCache();
}
The results from the query are cached in the middle tier. This is the reason why updates to the database which are applied through a different channel than through your application are not reflected when the page reloads.
I am using JDeveloper 11.1.1.7. Setting the "CacheResults" property on the Iterator to false solved this issue (go to the "Bindings" tab of your page or page fragment which contains the table, select the Iterator Executable for your table data, and in the Property Inspector at the "Advanced" section, set "CacheResults" to "false").
In terms of XML, the iterator definition in the PageDef.xml file should look like
<iterator id="TestIterator" Binds="TestView1"
DataControl="AppModuleDataControl" RangeSize="25"
CacheResults="false"/>
There seem to be some additional approaches to solve this, probably this was necessary in earlier JDeveloper versions:
http://technology.amis.nl/2012/06/18/notifying-adf-applications-of-database-changes-fast-and-lean-using-database-query-result-change-notification-part-one/
http://radio-weblogs.com/0118231/stories/2005/06/16/whyIsntRefreshingTheBrowserPageEnoughToRefreshTheDataDisplayed.html
ADDENDUM 03-DEC-2012
OP uses MySQL. With MySQL, setting the CacheResults property is required, but not sufficient. By default, MySQL runs with autocommit=false which has the side effect of using the isolation level REPEATABLE READ. A SELECT implicitly opens a transaction, and subsequent SELECTs return the same result. The Oracle RDBMS uses READ COMMITTED by default, so that data inserted and committed in one session is returned by SELECT in a different session.
One solution to get around this in ADF is to create an implementation class for the View Object, override executeQueryForCollection() and commit the transaction before executing the query:
protected void executeQueryForCollection(Object object, Object[] object2, int i) {
getApplicationModule().getTransaction().commit();
super.executeQueryForCollection(object, object2, i);
}
Please use this carefully and review your actual isolation level requirements to make sure that you do not unintentionally commit data by a browser refresh. Another drawback of this solution is that it is not portable between Oracle RDBMS and MySQL.
See https://github.com/afester/StackOverflow/tree/master/AdfRefresh for an SSCCE.

Updating a grid after changes have been made

Right, this is probably going to be pretty simple. I have a form that contains a grid, if you select one of the records and press 'Edit' you get taken to another form where you can edit the data attached to that record - this all works.
However upon saving it will write it to the database but will not update the aforementioned grid until I come out of my program all together and then go back in.
All I want to do is get the grid to update when exiting the other form.
I have tried opening/closing the table components, calling the 'refresh' against the query and the table. I am connecting to the database using BDE and writing this in Rad Studio XE2.
Any help would be appreciated.
Hope this solves your problem
Table1.Active := False;
Table1.Active := True;
for a TTable and
TQuery1.Active := False;
TQuery1.active := True;
for a TQuery

How can I retrieve latest from database using NHibernate after an update?

Here is the scenario:
I have a winforms application using NHibernate. When launched, I populate a DataGridView with the results of a NHibernate query. This part works fine. If I update a record in that list and flush the session, the update takes in the database. Upon closing the form after the update, I call a method to retrieve a list of objects to populate the DataGridView again to pick up the change and also get any other changes that may have occurred by somebody else. The problem is that the record that got updated, NHibernate doesn't reflect the change in the list it gives me. When I insert or delete a record, everything works fine. It is just when I update, that I get this behavior. I narrowed it down to NHibernate with their caching mechanism. I cannot figure out a way to make NHibernate retrieve from the database instead of using the cache after an update occurs. I posted on the NHibernate forums, but the suggestions they gave me didn't work. I stated this and nobody replied back. I am not going to state what I have tried in case I didn't do it right. If you answer with something that I tried exactly, I will state it in the comments of your answer.
This is the code that I use to retrieve the list:
public IList<WorkOrder> FindBy(string fromDate, string toDate)
{
IQuery query = _currentSession.CreateQuery("from WorkOrder wo where wo.Date >= ? and wo.Date <= ?");
query.SetParameter(0, fromDate);
query.SetParameter(1, toDate);
return query.List<WorkOrder>();
}
The session is passed to the class when it is constructed. I can post my mapping file also, but I am not sure if there is anything wrong with it, since everything else works. Anybody seen this before? This is the first project that I have used NHibernate, thanks for the help.
After your update, Evict the object from the first level cache.
Session.Update(obj);
Session.Evict(obj);
You may want to commit and/or flush first.
what about refresh? - see 9.2. Loading an object of the docs:
"sess.Save(cat);
sess.Flush(); //force the SQL INSERT
sess.Refresh(cat); //re-read the state (after the trigger executes)
"

Resources