rewrote contactinfo.Save() to use NamedExec

This commit is contained in:
2025-10-05 23:08:49 -06:00
parent 347e96d81d
commit 33c2fcc471
+15 -12
View File
@@ -52,7 +52,7 @@ type ContactInfo struct {
// lookupCommunityContact looks up the ID of a contact for a community. // lookupCommunityContact looks up the ID of a contact for a community.
func lookupCommunityContact(id int32) (int32, error) { func lookupCommunityContact(id int32) (int32, error) {
var rc int32 = -1 var rc int32 = -1
rs, err := amdb.Query("SELECT contactid FROM contacts WHERE onwer_commid = ?", id) rs, err := amdb.Query("SELECT contactid FROM contacts WHERE owner_commid = ?", id)
if err == nil { if err == nil {
if rs.Next() { if rs.Next() {
rs.Scan(&rc) rs.Scan(&rc)
@@ -81,6 +81,8 @@ func lookupUserContact(uid int32) (int32, error) {
func (ci *ContactInfo) Save() (bool, error) { func (ci *ContactInfo) Save() (bool, error) {
ci.Mutex.Lock() ci.Mutex.Lock()
defer ci.Mutex.Unlock() defer ci.Mutex.Unlock()
// Determine whether we're doing an UPDATE or an INSERT, and whether the E-mail address is changing.
updateMode := false updateMode := false
emailChange := true emailChange := true
if ci.ContactId <= 0 { if ci.ContactId <= 0 {
@@ -104,6 +106,7 @@ func (ci *ContactInfo) Save() (bool, error) {
emailChange = false emailChange = false
} }
if !emailChange { if !emailChange {
// we don't THINK the E-mail address is changing, but we could be wrong...
rs, err := amdb.Query("SELECT contactid FROM contacts WHERE contactid = ? AND email = ?", ci.ContactId, ci.Email) rs, err := amdb.Query("SELECT contactid FROM contacts WHERE contactid = ? AND email = ?", ci.ContactId, ci.Email)
if err != nil { if err != nil {
return false, err return false, err
@@ -112,24 +115,23 @@ func (ci *ContactInfo) Save() (bool, error) {
emailChange = true emailChange = true
} }
} }
// Handle the database heavy lifting.
if updateMode { if updateMode {
_, err := amdb.Exec(`UPDATE contacts SET given_name = ?, family_name = ?, middle_init = ?, prefix = ?, suffix = ?, company = ?, _, err := amdb.NamedExec(`UPDATE contacts SET given_name = :given_name, family_name = :family_name, middle_init = :middle_init,
addr1 = ?, addr2 = ?, locality = ?, region = ?, pcode = ?, country = ?, phone = ?, fax = ?, mobile = ?, email = ?, prefix = :prefix, suffix = :suffix, company = :company, addr1 = :addr1, addr2 = :addr2, locality = :locality, region = :region,
pvt_addr = ?, pvt_phone = ?, pvt_fax = ?, pvt_email = ?, photo_url = ?, url = ?, lastupdate = NOW() pcode = :pcode, country = :country, phone = :phone, fax = :fax, mobile = :mobile, email = :email, pvt_addr = :pvt_addr,
WHERE contactid = ?`, ci.GivenName, ci.FamilyName, ci.MiddleInit, ci.Prefix, ci.Suffix, ci.Company, pvt_phone = :pvt_phone, pvt_fax = :pvt_fax, pvt_email = :pvt_email, photo_url = :photo_url, url = :url, lastupdate = NOW()
ci.Addr1, ci.Addr2, ci.Locality, ci.Region, ci.PostalCode, ci.Country, ci.Phone, ci.Fax, ci.Mobile, ci.Email, WHERE contactid = :contactid`, ci)
ci.PrivateAddr, ci.PrivatePhone, ci.PrivateFax, ci.PrivateEmail, ci.PhotoURL, ci.URL, ci.ContactId)
if err != nil { if err != nil {
return false, err return false, err
} }
} else { } else {
res, err := amdb.Exec(`INSERT INTO contacts (given_name, family_name, middle_init, prefix, suffix, company, addr1, res, err := amdb.NamedExec(`INSERT INTO contacts (given_name, family_name, middle_init, prefix, suffix, company, addr1,
addr2, locality, region, pcode, country, phone, fax, mobile, email, pvt_addr, pvt_phone, pvt_fax, addr2, locality, region, pcode, country, phone, fax, mobile, email, pvt_addr, pvt_phone, pvt_fax,
pvt_email, owner_uid, owner_commid, photo_url, url, lastupdate) pvt_email, owner_uid, owner_commid, photo_url, url, lastupdate)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())`, VALUES (:given_name, :family_name, :middle_init, :prefix, :suffix, :company, :addr1, :addr2, :locality,
ci.GivenName, ci.FamilyName, ci.MiddleInit, ci.Prefix, ci.Suffix, ci.Company, ci.Addr1, ci.Addr2, :region:, :pcode, :country, :phone, :fax, :mobile, :email, :pvt_addr, :pvt_phone, :pvt_fax, :pvt_email,
ci.Locality, ci.Region, ci.PostalCode, ci.Country, ci.Phone, ci.Fax, ci.Mobile, ci.Email, :owner_uid, :owner_commid, :photo_url, :url, NOW())`, ci)
ci.PrivateAddr, ci.PrivatePhone, ci.PrivateFax, ci.PrivateEmail, ci.OwnerUid, ci.OwnerCommId, ci.PhotoURL, ci.URL)
if err != nil { if err != nil {
return false, err return false, err
} }
@@ -137,6 +139,7 @@ func (ci *ContactInfo) Save() (bool, error) {
ci.ContactId = int32(lii) ci.ContactId = int32(lii)
contactCache.Add(ci.ContactId, ci) contactCache.Add(ci.ContactId, ci)
} }
// Refresh the last update date.
rs, err := amdb.Query("SELECT lastupdate FROM contacts WHERE contactid = ?", ci.ContactId) rs, err := amdb.Query("SELECT lastupdate FROM contacts WHERE contactid = ?", ci.ContactId)
if err != nil { if err != nil {
return false, err return false, err