AW: [gentle-devel] Updating Records
Brought to you by:
mnmr
From: <Gr...@mi...> - 2005-09-19 16:31:04
|
You should follow OO guidelines. =20 In my case, I have a table "Contact", which has several rows with = contact information, like first name, last name, etc., and a field which = contains the last modified date, which gets updated by a SQL trigger and = is very important, because I need to set the HTTP-headers properly so = the caching subsystem works right. =20 I also have fields with password, last login date and failed login = count. Now the problem was, that a login shouldnt trigger the last = modified date to be updated, because the "real" contact information = didnt change. So I've rewritten the trigger to only update if changes = occured on the contact information, but not on the login information. = However, Gentle updated all rows, no matter if they were changed or if = they are null. The easiest way was using a nested class approach: =20 [TableName("Contacts"), XmlInclude(typeof(Contact))] public class Contact { [TableColumn("FirstName", NotNull=3Dfalse), XmlAttribute] public string FirstName; =20 [TableColumn("LastName", NotNull=3Dfalse), XmlAttribute] public string LastName; =20 [TableColumn("LastModified", NotNull=3Dfalse, IsReadOnly=3Dtrue, = IsUpdateAfterWrite=3Dtrue), XmlAttribute] public DateTime LastModified; =20 [TableName("Contacts")] private class ContactLogin { [TableColumn("LastLogin", NotNull=3Dfalse)] public DateTime LastLogin; =20 [TableColumn("Logins", NotNull=3Dtrue)] public int Logins; =20 [TableColumn("FailedCount", NotNull=3Dtrue)] public int FailedCount; } =20 private ContactLogin _contactLogin; =20 public void PersistLogin() { if (_contactLogin!=3Dnull) { _contactLogin.IsPersisted=3Dtrue; _contactLogin.UUID=3Dthis.UUID; _contactLogin.Persist(); } } =20 [XmlAttribute, XmlIgnore] public int Logins { get { if (_contactLogin =3D=3D null) _contactLogin=3DContactLogin.Retrieve(UUID); =20 if (_contactLogin !=3D null) return _contactLogin.Logins; else return 0; } set { if (_contactLogin =3D=3D null) _contactLogin=3DContactLogin.Retrieve(UUID); =20 if (_contactLogin =3D=3D null) _contactLogin=3Dnew ContactLogin(UUID, null, null, = DateTime.MinValue, value, 0); else _contactLogin.Logins=3Dvalue; } } ... =20 } =20 Seems a little bit complicated in the inside, but from the outside, you = can simply do this: =20 Contact contact=3DContact.Retrieve("te...@te..."); contact.Logins=3D3; contact.PersistLogin(); // This tells the object to only update the = login-part =20 You can also go one step further and implement the cols as properties = instead of fields, update two seperate dirty-flags, and simply override = the Persist()-method, where the object will then decide itself if it = needs to update all fields, or only the login-subpart. You shouldnt = think of the objects as interfaces to your table rows, but instead see = the objects as if they were your table rows. Wouldnt it be nice if your = table rows decide for their selves which columns they need to update? =20 One thing: *Never* use inheritance. This will confuse the Gentle caching = subsystem, because two different objects can reference the same table = row. And without the caching, the perfomance doesnt scale very well with = the number of users. =20 Regards, Alex ________________________________ Von: gop...@li... = [mailto:gop...@li...] Im Auftrag von David = Cumps Gesendet: Montag, 19. September 2005 13:33 An: gop...@li... Betreff: RE: [gentle-devel] Updating Records =09 =09 Ouch, that's an ugly solution imho :/ =20 If there was only a way to simply discard the parameters which have a = null value from the query, everything would be solved :p =20 Anything planned to make this easier Morton? =20 Greets =20 David ________________________________ Van: gop...@li... = [mailto:gop...@li...] Namens Birgir = Kristmannsson Verzonden: maandag 19 september 2005 13:20 Aan: gop...@li... Onderwerp: RE: [gentle-devel] Updating Records =09 =09 For this you need to have two (or more) classes. =20 class BaseClass : Persistent { // Define the properties that you want to update } =20 class FullClass : BaseClass { // Define the rest of the properties } =20 And for your update scenario, you would work exclusively with BaseClass =20 AFAIK that is the only way to do it in Gentle.NET - but it works like a = charm =20 Biggi =09 ________________________________ From: gop...@li... = [mailto:gop...@li...] On Behalf Of David Cumps Sent: 19. september 2005 07:11 To: gop...@li... Subject: [gentle-devel] Updating Records =20 Hello, =20 I have been working with Gentle for some time now, and starting to help = myself just enough to make everything work, until now :( =20 I'm looking for a way to create an update query, which does *not* set = all the fields. =20 I know how to insert, update, delete, select a single record. But for = multiple updates, I can't get it to work. =20 I'm looking to create something like: "UPDATE *table* SET = *a_field_i_want*=3D*value*, *maybe another field* WHERE = *yet_another_field*=3D*value*" =20 The problems I encountered are that it wants to put every column in the = SET clause. =20 Any suggestions? =20 Thanks =20 Best Regards, =20 David |