MYSQL "before insert" trigger synchronise

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
oleg_b
Posts: 3
Joined: Mon 23 Jan 2012 09:06

MYSQL "before insert" trigger synchronise

Post by oleg_b » Mon 23 Jan 2012 10:02

I've created a "before insert" trigger (with HeidiSQL) for a table to preset some fields on inserts. Is it possible to sync the data when I perform an insert in my application? I don't want to preset the fields in the AfterInsert event of my TUniQuery.

Thanks

AndreyZ

Post by AndreyZ » Mon 23 Jan 2012 17:05

Hello,

To solve the problem, you can set the TUniQuery.RefreshOptions property to [roAfterInsert] . In this case UniDAC refreshes record (using the RefreshRecord method) after its inserting.

oleg_b
Posts: 3
Joined: Mon 23 Jan 2012 09:06

Post by oleg_b » Tue 24 Jan 2012 08:29

Hello AndreyZ,
thanks for your reply. But that does not work for me. When I'm in insert state the value of the field is still NULL.
In my case I set a required field ("language") in a "before insert" trigger in mysql. In my application I've got a grid and draw a country flag image for every row in dependence which language is selected. When a user inserts a new row, I need to know the default language to change the position of a combobox where the user can select his language. But I don't get the value while I'm in insert state. After posting the data I get the correct value from my trigger, but that's too late.
I don't want to preset the default value in my application because I don't want to compile my application after changing the default value. And I've got a few other tables where I would like to preset the data directly in mysql and not in my application.
I could set the "required" property of my fields to false and perform an auto-post after insert and then change the stated to edit. But then I have to work with transactions to rollback the insert in case that the user cancels his action. I hope there is a better solution to solve my problem...
Thanks

AndreyZ

Post by AndreyZ » Tue 24 Jan 2012 12:38

If you set field values in a trigger, the only way to obtain these values on a client side is to read them again from the server (perform the refresh operation). To solve the problem, you can set the default value for the "language" field on the server and set the TUniQuery.Options.DefaultValues property to True. The DefaultValues property is used to request default values/expressions from a server and assign them to the DefaultExpression property of the fields. In this case you don't need to recompile your application every time you change default value on the server because TUniQuery will obtain default values directly from the server every time it is opened.

oleg_b
Posts: 3
Joined: Mon 23 Jan 2012 09:06

Post by oleg_b » Tue 24 Jan 2012 13:27

Thank you for the information.
I think currently it's the easiest sollution to set the default values in the fields on the server. I will have to post and refresh my data on the client side when I need to calculate something before inserts... But it's fine for my use at the moment. Thank you for the fast response.

AndreyZ

Post by AndreyZ » Tue 24 Jan 2012 14:27

If any other questions come up, please contact us.

Post Reply