From 55c5c88c95e03e69aca24f5e27f5ec5196b5aec0 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Thu, 5 Mar 2026 15:47:23 -0700 Subject: [PATCH] some database code cleanups --- database/adverts.go | 8 ++-- database/audit.go | 12 +++--- database/community.go | 82 ++++++++++++++++++++--------------------- database/conference.go | 36 +++++++++--------- database/contactinfo.go | 20 +++++----- database/globals.go | 10 ++--- database/imagestore.go | 6 +-- database/ipban.go | 6 +-- database/passchange.go | 2 +- database/post.go | 14 +++---- database/sidebox.go | 6 +-- database/topic.go | 28 +++++++------- database/user.go | 44 +++++++++++----------- 13 files changed, 137 insertions(+), 137 deletions(-) diff --git a/database/adverts.go b/database/adverts.go index 6796832..5278df3 100644 --- a/database/adverts.go +++ b/database/adverts.go @@ -64,13 +64,13 @@ func AmGetAd(ctx context.Context, adid int32) (*Advert, error) { if ok { return rc.(*Advert), nil } - var theAd Advert - err := amdb.GetContext(ctx, &theAd, "SELECT * FROM adverts WHERE adid = ?", adid) + theAd := new(Advert) + err := amdb.GetContext(ctx, theAd, "SELECT * FROM adverts WHERE adid = ?", adid) if err != nil { return nil, err } - adCache.Add(adid, &theAd) - return &theAd, nil + adCache.Add(adid, theAd) + return theAd, nil } // AmGetRandomAd gets a random ad from the diff --git a/database/audit.go b/database/audit.go index 68b0b38..7f5b46d 100644 --- a/database/audit.go +++ b/database/audit.go @@ -174,8 +174,8 @@ func (ar *AuditRecord) Store(ctx context.Context) error { * Returns: * The audit record pointer. */ -func AmNewAudit(rectype int32, uid int32, ip string, data ...string) *AuditRecord { - rc := AuditRecord{Event: rectype, Uid: uid, CommId: 0} +func AmNewAudit(rectype, uid int32, ip string, data ...string) *AuditRecord { + rc := &AuditRecord{Event: rectype, Uid: uid, CommId: 0} if len(ip) > 0 { rc.IP = &ip } @@ -194,7 +194,7 @@ func AmNewAudit(rectype int32, uid int32, ip string, data ...string) *AuditRecor rc.Data4 = &(data[3]) } } - return &rc + return rc } /* AmNewCommAudit creates a new audit record tied to a community. @@ -207,8 +207,8 @@ func AmNewAudit(rectype int32, uid int32, ip string, data ...string) *AuditRecor * Returns: * The audit record pointer. */ -func AmNewCommAudit(rectype int32, uid int32, commid int32, ip string, data ...string) *AuditRecord { - rc := AuditRecord{Event: rectype, Uid: uid, CommId: commid} +func AmNewCommAudit(rectype, uid, commid int32, ip string, data ...string) *AuditRecord { + rc := &AuditRecord{Event: rectype, Uid: uid, CommId: commid} if len(ip) > 0 { rc.IP = &ip } @@ -227,7 +227,7 @@ func AmNewCommAudit(rectype int32, uid int32, commid int32, ip string, data ...s rc.Data4 = &(data[3]) } } - return &rc + return rc } // AmStoreAudit stores the audit record in the background. diff --git a/database/community.go b/database/community.go index 9664d9f..0f87c33 100644 --- a/database/community.go +++ b/database/community.go @@ -127,7 +127,7 @@ var memberCache *lru.Cache = nil var memberMutex sync.Mutex // stuffMembership stuffs a membership record into the cache. -func stuffMembership(cid int32, uid int32, member bool, locked bool, level uint16) { +func stuffMembership(cid int32, uid int32, member, locked bool, level uint16) { key := fmt.Sprintf("%d:%d", cid, uid) memberMutex.Lock() memberCache.Add(key, &memberCacheData{isMember: member, locked: locked, level: level}) @@ -253,13 +253,13 @@ func (c *Community) MemberCount(ctx context.Context, hidden bool) (int, error) { * ListMembersOperRegex - The specified field matches the regular expression in "term". * term - The search term, as specified above. * offset - Number of members to skip at beginning of list. - * max - Maximum number of members to return. + * maxCount - Maximum number of members to return. * Returns: * Array of User pointers representing the return elements. * The total number of members matching this query (could be greater than max) * Standard Go error status. */ -func (c *Community) ListMembers(ctx context.Context, field int, oper int, term string, offset int, max int, showHidden bool) ([]*User, int, error) { +func (c *Community) ListMembers(ctx context.Context, field, oper int, term string, offset, maxCount int, showHidden bool) ([]*User, int, error) { var query strings.Builder if field != ListMembersFieldNone && oper != ListMembersOperNone { query.WriteString(" AND ") @@ -303,16 +303,16 @@ func (c *Community) ListMembers(ctx context.Context, field int, oper int, term s AND u.contactid = c.contactid`+q, c.Id); err == nil { if offset > 0 { rs, err = amdb.QueryContext(ctx, `SELECT m.uid FROM commmember m, users u, contacts c WHERE m.commid = ? AND m.uid = u.uid - AND u.contactid = c.contactid`+q+" ORDER BY u.username LIMIT ? OFFSET ?", c.Id, max, offset) + AND u.contactid = c.contactid`+q+" ORDER BY u.username LIMIT ? OFFSET ?", c.Id, maxCount, offset) } else { rs, err = amdb.QueryContext(ctx, `SELECT m.uid FROM commmember m, users u, contacts c WHERE m.commid = ? AND m.uid = u.uid - AND u.contactid = c.contactid`+q+" ORDER BY u.username LIMIT ?", c.Id, max) + AND u.contactid = c.contactid`+q+" ORDER BY u.username LIMIT ?", c.Id, maxCount) } } if err != nil { return nil, total, err } - rc := make([]*User, 0, min(max, 10000)) + rc := make([]*User, 0, min(maxCount, 10000)) for rs.Next() { var uid int32 if err = rs.Scan(&uid); err == nil { @@ -650,12 +650,12 @@ func AmGetCommunity(ctx context.Context, id int32) (*Community, error) { if rc, ok := communityCache.Get(id); ok { return rc.(*Community), nil } - var newcomm Community - err := amdb.GetContext(ctx, &newcomm, "SELECT * from communities WHERE commid = ?", id) + newcomm := new(Community) + err := amdb.GetContext(ctx, newcomm, "SELECT * from communities WHERE commid = ?", id) switch err { case nil: - communityCache.Add(id, &newcomm) - return &newcomm, nil + communityCache.Add(id, newcomm) + return newcomm, nil case sql.ErrNoRows: return nil, ErrNoCommunity } @@ -677,12 +677,12 @@ func AmGetCommunityTx(ctx context.Context, tx *sqlx.Tx, id int32) (*Community, e if rc, ok := communityCache.Get(id); ok { return rc.(*Community), nil } - var newcomm Community - err := tx.GetContext(ctx, &newcomm, "SELECT * from communities WHERE commid = ?", id) + newcomm := new(Community) + err := tx.GetContext(ctx, newcomm, "SELECT * from communities WHERE commid = ?", id) switch err { case nil: - communityCache.Add(id, &newcomm) - return &newcomm, nil + communityCache.Add(id, newcomm) + return newcomm, nil case sql.ErrNoRows: return nil, ErrNoCommunity } @@ -791,7 +791,7 @@ func AmGetCommunitiesForUser(ctx context.Context, uid int32) ([]*Community, erro * Access level within the community, or 0 if the user is not a member. * Standard Go error status. */ -func AmGetCommunityAccessLevel(ctx context.Context, uid int32, commid int32) (uint16, error) { +func AmGetCommunityAccessLevel(ctx context.Context, uid, commid int32) (uint16, error) { var rc uint16 = 0 rows, err := amdb.QueryxContext(ctx, `SELECT GREATEST(m.granted_lvl, u.base_lvl) AS level FROM users u, commmember m WHERE u.uid = m.uid AND m.uid = ? AND m.commid = ?`, uid, commid) @@ -844,19 +844,19 @@ func AmAutoJoinCommunities(ctx context.Context, user *User) error { } // internalGetCommProp is a helper used by the community property functions. -func internalGetCommProp(ctx context.Context, cid int32, ndx int32) (*CommunityProperties, error) { +func internalGetCommProp(ctx context.Context, cid, ndx int32) (*CommunityProperties, error) { key := fmt.Sprintf("%d:%d", cid, ndx) getCommunityPropMutex.Lock() defer getCommunityPropMutex.Unlock() if rc, ok := communityPropCache.Get(key); ok { return rc.(*CommunityProperties), nil } - var prop CommunityProperties - err := amdb.GetContext(ctx, &prop, "SELECT * from propcomm WHERE cid = ? AND ndx = ?", cid, ndx) + prop := new(CommunityProperties) + err := amdb.GetContext(ctx, prop, "SELECT * from propcomm WHERE cid = ? AND ndx = ?", cid, ndx) switch err { case nil: - communityPropCache.Add(key, &prop) - return &prop, nil + communityPropCache.Add(key, prop) + return prop, nil case sql.ErrNoRows: return nil, nil } @@ -872,7 +872,7 @@ func internalGetCommProp(ctx context.Context, cid int32, ndx int32) (*CommunityP * Value of the property string. * Standard Go error status. */ -func AmGetCommunityProperty(ctx context.Context, cid int32, ndx int32) (*string, error) { +func AmGetCommunityProperty(ctx context.Context, cid, ndx int32) (*string, error) { p, err := internalGetCommProp(ctx, cid, ndx) if err != nil { return nil, err @@ -891,7 +891,7 @@ func AmGetCommunityProperty(ctx context.Context, cid int32, ndx int32) (*string, * Returns: * Standard Go error status. */ -func AmSetCommunityProperty(ctx context.Context, cid int32, ndx int32, val *string) error { +func AmSetCommunityProperty(ctx context.Context, cid, ndx int32, val *string) error { p, err := internalGetCommProp(ctx, cid, ndx) if err != nil { return err @@ -930,8 +930,8 @@ func AmSetCommunityProperty(ctx context.Context, cid int32, ndx int32, val *stri * Pointer to new Community record, or nil. * Standard Go error status. */ -func AmCreateCommunity(ctx context.Context, name string, alias string, hostUid int32, language *string, synopsis *string, - rules *string, joinkey *string, hideDirectory bool, hideSearch bool, remoteIP string) (*Community, error) { +func AmCreateCommunity(ctx context.Context, name, alias string, hostUid int32, language, synopsis, rules, joinkey *string, + hideDirectory, hideSearch bool, remoteIP string) (*Community, error) { tx, commit, rollback := transaction(ctx) defer rollback() @@ -1012,14 +1012,14 @@ func AmCreateCommunity(ctx context.Context, name string, alias string, hostUid i * ctx - Standard Go context value. * catid - Category ID to search for. * offset - Number of communities to skip at beginning of list. - * max - Maximum number of communities to return. + * maxCount - Maximum number of communities to return. * showAll - Include communities that are "hidden in directory." * Returns: * Array of Community pointers representing the return elements. * The total number of communities matching this query (could be greater than max) * Standard Go error status. */ -func AmGetCommunitiesForCategory(ctx context.Context, catid int32, offset int, max int, showAll bool) ([]*Community, int, error) { +func AmGetCommunitiesForCategory(ctx context.Context, catid int32, offset, maxCount int, showAll bool) ([]*Community, int, error) { var err error var total int if showAll { @@ -1034,22 +1034,22 @@ func AmGetCommunitiesForCategory(ctx context.Context, catid int32, offset int, m if showAll { if offset > 0 { rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities WHERE catid = ? ORDER BY commname LIMIT ? OFFSET ?", - catid, max, offset) + catid, maxCount, offset) } else { - rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities WHERE catid = ? ORDER BY commname LIMIT ?", catid, max) + rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities WHERE catid = ? ORDER BY commname LIMIT ?", catid, maxCount) } } else { if offset > 0 { rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities WHERE catid = ? AND hide_dir = 0 ORDER BY commname LIMIT ? OFFSET ?", - catid, max, offset) + catid, maxCount, offset) } else { - rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities WHERE catid = ? AND hide_dir = 0 ORDER BY commname LIMIT ?", catid, max) + rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities WHERE catid = ? AND hide_dir = 0 ORDER BY commname LIMIT ?", catid, maxCount) } } if err != nil { return nil, total, err } - rc := make([]*Community, 0, min(max, 10000)) + rc := make([]*Community, 0, min(maxCount, 10000)) for rs.Next() { var commid int32 if err = rs.Scan(&commid); err == nil { @@ -1074,14 +1074,14 @@ func AmGetCommunitiesForCategory(ctx context.Context, catid int32, offset int, m * SearchCommOperRegex - The specified field matches the regular expression in "term". * term - The search term, as specified above. * offset - Number of communities to skip at beginning of list. - * max - Maximum number of communities to return. + * maxCount - Maximum number of communities to return. * showAll - Include communities that are "hidden in search." * Returns: * Array of Community pointers representing the return elements. * The total number of communities matching this query (could be greater than max) * Standard Go error status. */ -func AmSearchCommunities(ctx context.Context, field int, oper int, term string, offset int, max int, showAll bool) ([]*Community, int, error) { +func AmSearchCommunities(ctx context.Context, field, oper int, term string, offset, maxCount int, showAll bool) ([]*Community, int, error) { var queryPortion strings.Builder queryPortion.WriteString("WHERE ") switch field { @@ -1119,14 +1119,14 @@ func AmSearchCommunities(ctx context.Context, field int, oper int, term string, } var rs *sql.Rows if offset > 0 { - rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities "+q+" ORDER BY commname LIMIT ? OFFSET ?", max, offset) + rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities "+q+" ORDER BY commname LIMIT ? OFFSET ?", maxCount, offset) } else { - rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities "+q+" ORDER BY commname LIMIT ?", max) + rs, err = amdb.QueryContext(ctx, "SELECT commid FROM communities "+q+" ORDER BY commname LIMIT ?", maxCount) } if err != nil { return nil, total, err } - rc := make([]*Community, 0, min(max, 10000)) + rc := make([]*Community, 0, min(maxCount, 10000)) for rs.Next() { var commid int32 if err = rs.Scan(&commid); err == nil { @@ -1154,13 +1154,13 @@ func AmSearchCommunities(ctx context.Context, field int, oper int, term string, * SearchUserOperRegex - The specified field matches the regular expression in "term". * term - The search term, as specified above. * offset - Number of users to skip at beginning of list. - * max - Maximum number of users to return. + * maxCount - Maximum number of users to return. * Returns: * Array of User pointers representing the return elements. * The total number of users matching this query (could be greater than max) * Standard Go error status. */ -func AmSearchCommunityMembers(ctx context.Context, c *Community, field int, oper int, term string, offset int, max int) ([]*User, int, error) { +func AmSearchCommunityMembers(ctx context.Context, c *Community, field, oper int, term string, offset, maxCount int) ([]*User, int, error) { var queryPortion strings.Builder switch field { case SearchUserFieldName: @@ -1203,15 +1203,15 @@ func AmSearchCommunityMembers(ctx context.Context, c *Community, field int, oper var rs *sql.Rows if offset > 0 { rs, err = amdb.QueryContext(ctx, `SELECT u.uid FROM users u, contacts c, commmember m WHERE u.contactid = c.contactid AND u.uid = m.uid - AND m.commid = ? AND u.is_anon = 0 AND `+q+" ORDER BY u.username LIMIT ? OFFSET ?", c.Id, max, offset) + AND m.commid = ? AND u.is_anon = 0 AND `+q+" ORDER BY u.username LIMIT ? OFFSET ?", c.Id, maxCount, offset) } else { rs, err = amdb.QueryContext(ctx, `SELECT u.uid FROM users u, contacts c, commmember m WHERE u.contactid = c.contactid AND u.uid = m.uid - AND m.commid = ? AND u.is_anon = 0 AND `+q+" ORDER BY u.username LIMIT ?", c.Id, max) + AND m.commid = ? AND u.is_anon = 0 AND `+q+" ORDER BY u.username LIMIT ?", c.Id, maxCount) } if err != nil { return nil, total, err } - rc := make([]*User, 0, min(max, 10000)) + rc := make([]*User, 0, min(maxCount, 10000)) for rs.Next() { var uid int32 if err = rs.Scan(&uid); err == nil { diff --git a/database/conference.go b/database/conference.go index cfbd819..a69f0e8 100644 --- a/database/conference.go +++ b/database/conference.go @@ -408,14 +408,14 @@ func (c *Conference) SaveFlags(ctx context.Context, f *util.OptionSet) error { // Settings returns the settings for a user. func (c *Conference) Settings(ctx context.Context, u *User) (*ConferenceSettings, error) { - var settings ConferenceSettings - err := amdb.GetContext(ctx, &settings, "SELECT * FROM confsettings WHERE confid = ? AND uid = ?", c.ConfId, u.Uid) + settings := new(ConferenceSettings) + err := amdb.GetContext(ctx, settings, "SELECT * FROM confsettings WHERE confid = ? AND uid = ?", c.ConfId, u.Uid) switch err { case nil: settings.newflag = false - return &settings, nil + return settings, nil case sql.ErrNoRows: - settings := ConferenceSettings{ + settings := &ConferenceSettings{ ConfId: c.ConfId, Uid: u.Uid, DefaultPseud: nil, @@ -423,7 +423,7 @@ func (c *Conference) Settings(ctx context.Context, u *User) (*ConferenceSettings LastPost: nil, newflag: true, } - return &settings, nil + return settings, nil } return nil, err } @@ -456,8 +456,8 @@ func (c *Conference) SetInfo(ctx context.Context, name, descr string, read_lvl, hide_lvl = ?, nuke_lvl = ?, change_lvl = ?, delete_lvl = ?, lastupdate = NOW() WHERE confid = ?`, name, descr, read_lvl, post_lvl, create_lvl, hide_lvl, nuke_lvl, change_lvl, delete_lvl, c.ConfId) if err == nil { - var tmp Conference - if err = amdb.GetContext(ctx, &tmp, "SELECT * FROM confs WHERE confid = ?", c.ConfId); err == nil { + tmp := new(Conference) + if err = amdb.GetContext(ctx, tmp, "SELECT * FROM confs WHERE confid = ?", c.ConfId); err == nil { if c.Name != tmp.Name { AmStoreAudit(AmNewCommAudit(AuditConferenceName, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", c.ConfId), fmt.Sprintf("name='%s'", tmp.Name))) } @@ -1016,12 +1016,12 @@ func AmGetConference(ctx context.Context, id int32) (*Conference, error) { if rc, ok := conferenceCache.Get(id); ok { return rc.(*Conference), nil } - var conf Conference - err := amdb.GetContext(ctx, &conf, "SELECT * from confs where confid = ?", id) + conf := new(Conference) + err := amdb.GetContext(ctx, conf, "SELECT * from confs where confid = ?", id) switch err { case nil: - conferenceCache.Add(id, &conf) - return &conf, nil + conferenceCache.Add(id, conf) + return conf, nil case sql.ErrNoRows: return nil, ErrNoConference } @@ -1137,19 +1137,19 @@ func AmListConferences(ctx context.Context, cid int32, showHidden bool) ([]*Conf } // internalGetConfProp is a helper used by the conference property functions. -func internalGetConfProp(ctx context.Context, confid int32, ndx int32) (*ConferenceProperties, error) { +func internalGetConfProp(ctx context.Context, confid, ndx int32) (*ConferenceProperties, error) { key := fmt.Sprintf("%d:%d", confid, ndx) getConferencePropMutex.Lock() defer getConferencePropMutex.Unlock() if rc, ok := conferencePropCache.Get(key); ok { return rc.(*ConferenceProperties), nil } - var prop ConferenceProperties - err := amdb.GetContext(ctx, &prop, "SELECT * from propconf WHERE confid = ? AND ndx = ?", confid, ndx) + prop := new(ConferenceProperties) + err := amdb.GetContext(ctx, prop, "SELECT * from propconf WHERE confid = ? AND ndx = ?", confid, ndx) switch err { case nil: - conferencePropCache.Add(key, &prop) - return &prop, nil + conferencePropCache.Add(key, prop) + return prop, nil case sql.ErrNoRows: return nil, nil } @@ -1165,7 +1165,7 @@ func internalGetConfProp(ctx context.Context, confid int32, ndx int32) (*Confere * Value of the property string. * Standard Go error status. */ -func AmGetConferenceProperty(ctx context.Context, confid int32, ndx int32) (*string, error) { +func AmGetConferenceProperty(ctx context.Context, confid, ndx int32) (*string, error) { p, err := internalGetConfProp(ctx, confid, ndx) if err != nil { return nil, err @@ -1184,7 +1184,7 @@ func AmGetConferenceProperty(ctx context.Context, confid int32, ndx int32) (*str * Returns: * Standard Go error status. */ -func AmSetConferenceProperty(ctx context.Context, confid int32, ndx int32, val *string) error { +func AmSetConferenceProperty(ctx context.Context, confid, ndx int32, val *string) error { p, err := internalGetConfProp(ctx, confid, ndx) if err != nil { return err diff --git a/database/contactinfo.go b/database/contactinfo.go index 93db6d7..8ccef2a 100644 --- a/database/contactinfo.go +++ b/database/contactinfo.go @@ -197,7 +197,7 @@ func (ci *ContactInfo) Save(ctx context.Context, changer *User, ipaddr string) ( // Clone makes a copy of the ContactInfo. func (ci *ContactInfo) Clone() *ContactInfo { - newstr := ContactInfo{ + newstr := &ContactInfo{ ContactId: ci.ContactId, GivenName: ci.GivenName, FamilyName: ci.FamilyName, @@ -225,7 +225,7 @@ func (ci *ContactInfo) Clone() *ContactInfo { URL: ci.URL, LastUpdate: ci.LastUpdate, } - return &newstr + return newstr } // contactCache is the cache for ContactInfo objects. @@ -245,11 +245,11 @@ func setupContactsCache() { // internalContactInfo retrieves the contact info from the database. func internalContactInfo(ctx context.Context, id int32) (*ContactInfo, error) { - var cinf ContactInfo - if err := amdb.GetContext(ctx, &cinf, "SELECT * from contacts WHERE contactid = ?", id); err != nil { + cinf := new(ContactInfo) + if err := amdb.GetContext(ctx, cinf, "SELECT * from contacts WHERE contactid = ?", id); err != nil { return nil, err } - return &cinf, nil + return cinf, nil } /* AmGetContactInfo retrieves the contact info for a given identifier. @@ -304,8 +304,8 @@ func AmGetContactInfoForUser(ctx context.Context, uid int32) (*ContactInfo, erro * New ContactInfo structure. */ func AmNewUserContactInfo(uid int32) *ContactInfo { - rc := ContactInfo{OwnerUid: uid, OwnerCommId: -1} - return &rc + rc := &ContactInfo{OwnerUid: uid, OwnerCommId: -1} + return rc } /* AmNewCommunityContactInfo creates a new contact info record for the community. @@ -315,7 +315,7 @@ func AmNewUserContactInfo(uid int32) *ContactInfo { * Returns: * New ContactInfo structure. */ -func AmNewCommunityContactInfo(uid int32, cid int32) *ContactInfo { - rc := ContactInfo{OwnerUid: uid, OwnerCommId: cid} - return &rc +func AmNewCommunityContactInfo(uid, cid int32) *ContactInfo { + rc := &ContactInfo{OwnerUid: uid, OwnerCommId: cid} + return rc } diff --git a/database/globals.go b/database/globals.go index 35d4147..c8544e1 100644 --- a/database/globals.go +++ b/database/globals.go @@ -62,7 +62,7 @@ var globalPropMutex sync.Mutex // Clone clones the entire global state. func (g *Globals) Clone() *Globals { - rc := Globals{ + rc := &Globals{ PostsPerPage: g.PostsPerPage, OldPostsAtTop: g.OldPostsAtTop, MaxSearchPage: g.MaxSearchPage, @@ -73,7 +73,7 @@ func (g *Globals) Clone() *Globals { CommunityCreateLevel: g.CommunityCreateLevel, flags: nil, } - return &rc + return rc } // Flags returns the global flags. @@ -107,11 +107,11 @@ func AmGlobals(ctx context.Context) (*Globals, error) { globalsMutex.Lock() defer globalsMutex.Unlock() if theGlobals == nil { - var g Globals - if err := amdb.GetContext(ctx, &g, "SELECT * FROM globals"); err != nil { + g := new(Globals) + if err := amdb.GetContext(ctx, g, "SELECT * FROM globals"); err != nil { return nil, err } - theGlobals = &g + theGlobals = g } return theGlobals, nil } diff --git a/database/imagestore.go b/database/imagestore.go index b2ff4be..4010fa6 100644 --- a/database/imagestore.go +++ b/database/imagestore.go @@ -60,11 +60,11 @@ func (img *ImageStore) Save(ctx context.Context) error { * Standard Go error status. */ func AmLoadImage(ctx context.Context, id int32) (*ImageStore, error) { - var imgdata ImageStore - if err := amdb.GetContext(ctx, &imgdata, "SELECT * FROM imagestore WHERE imgid = ?", id); err != nil { + imgdata := new(ImageStore) + if err := amdb.GetContext(ctx, imgdata, "SELECT * FROM imagestore WHERE imgid = ?", id); err != nil { return nil, err } - return &imgdata, nil + return imgdata, nil } /* AmStoreImage stores an image in the database, overwriting one with the same type code and owner if it exists. diff --git a/database/ipban.go b/database/ipban.go index 5ae5ce1..9293423 100644 --- a/database/ipban.go +++ b/database/ipban.go @@ -272,11 +272,11 @@ func AmListIPBans(ctx context.Context) ([]IPBanEntry, error) { // AmGetIPBan returns a single IP address ban structure. func AmGetIPBan(ctx context.Context, id int32) (*IPBanEntry, error) { - var ban IPBanEntry - if err := amdb.GetContext(ctx, &ban, "SELECT * FROM ipban WHERE id = ?", id); err != nil { + ban := new(IPBanEntry) + if err := amdb.GetContext(ctx, ban, "SELECT * FROM ipban WHERE id = ?", id); err != nil { return nil, err } - return &ban, nil + return ban, nil } // AmAddIPBan adds a new IP address ban. diff --git a/database/passchange.go b/database/passchange.go index 19c46bd..2d7621f 100644 --- a/database/passchange.go +++ b/database/passchange.go @@ -35,7 +35,7 @@ var passwordRequests map[int32]*PasswordChangeRequest = make(map[int32]*Password * Returns: * Pointer to the new PasswordChangeRequest. */ -func AmNewPasswordChangeRequest(uid int32, username string, email string) *PasswordChangeRequest { +func AmNewPasswordChangeRequest(uid int32, username, email string) *PasswordChangeRequest { rc := PasswordChangeRequest{Uid: uid, Username: username, Email: email, Authentication: util.GenerateRandomConfirmationNumber(), Expires: time.Now().Add(time.Hour)} passwordRequests[uid] = &rc diff --git a/database/post.go b/database/post.go index ac61637..d955d42 100644 --- a/database/post.go +++ b/database/post.go @@ -88,11 +88,11 @@ func (p *PostHeader) AttachmentInfo(ctx context.Context) (*PostAttachInfo, error return nil, errors.New("no attachment data for scribbled post") } row := amdb.QueryRowContext(ctx, "SELECT filename, mimetype, datalen FROM postattach WHERE postid = ?", p.PostId) - var rc PostAttachInfo + rc := new(PostAttachInfo) err := row.Scan(&(rc.Filename), &(rc.MIMEType), &(rc.Length)) switch err { case nil: - return &rc, nil + return rc, nil case sql.ErrNoRows: return nil, nil } @@ -235,8 +235,8 @@ func (p *PostHeader) PruneAttachment(ctx context.Context, u *User, comm *Communi // Text returns the text associated with a post. func (p *PostHeader) Text(ctx context.Context) (string, error) { - var pd PostData - err := amdb.GetContext(ctx, &pd, "SELECT * FROM postdata WHERE postid = ?", p.PostId) + pd := new(PostData) + err := amdb.GetContext(ctx, pd, "SELECT * FROM postdata WHERE postid = ?", p.PostId) switch err { case nil: if pd.Data == nil { @@ -519,11 +519,11 @@ func (p *PostHeader) ImportFix(ctx context.Context, parent int64, dateStamp time * Standard Go error status. */ func AmGetPost(ctx context.Context, postId int64) (*PostHeader, error) { - var pd PostHeader - if err := amdb.GetContext(ctx, &pd, "SELECT * FROM posts WHERE postid = ?", postId); err != nil { + pd := new(PostHeader) + if err := amdb.GetContext(ctx, pd, "SELECT * FROM posts WHERE postid = ?", postId); err != nil { return nil, err } - return &pd, nil + return pd, nil } /* AmGetPostRange gets a range of posts from a topic by post numbers. diff --git a/database/sidebox.go b/database/sidebox.go index 3a17fec..4739428 100644 --- a/database/sidebox.go +++ b/database/sidebox.go @@ -65,7 +65,7 @@ func AmGetSideboxes(ctx context.Context, uid int32) ([]*Sidebox, error) { } // AmReorderSideboxes changes the position of two sideboxes on the user's list. -func AmReorderSideboxes(ctx context.Context, uid int32, seq1, seq2 int32) error { +func AmReorderSideboxes(ctx context.Context, uid, seq1, seq2 int32) error { tx, commit, rollback := transaction(ctx) defer rollback() @@ -86,7 +86,7 @@ func AmReorderSideboxes(ctx context.Context, uid int32, seq1, seq2 int32) error } // AmRemoveSidebox removes a sidebox from the user configuration. -func AmRemoveSidebox(ctx context.Context, uid int32, boxid int32) error { +func AmRemoveSidebox(ctx context.Context, uid, boxid int32) error { tx, commit, rollback := transaction(ctx) defer rollback() @@ -113,7 +113,7 @@ func AmRemoveSidebox(ctx context.Context, uid int32, boxid int32) error { } // AmAppendSidebox appends a new sidebox to the existing user's configuration. -func AmAppendSidebox(ctx context.Context, uid int32, boxid int32, param *string) error { +func AmAppendSidebox(ctx context.Context, uid, boxid int32, param *string) error { tx, commit, rollback := transaction(ctx) defer rollback() diff --git a/database/topic.go b/database/topic.go index e772c3e..4158c7c 100644 --- a/database/topic.go +++ b/database/topic.go @@ -488,14 +488,14 @@ type TopicSummary struct { * Standard Go error status. */ func AmGetTopic(ctx context.Context, topicId int32) (*Topic, error) { - var top Topic - if err := amdb.GetContext(ctx, &top, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil { + top := new(Topic) + if err := amdb.GetContext(ctx, top, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil { if err == sql.ErrNoRows { err = ErrNoTopic } return nil, err } - return &top, nil + return top, nil } /* AmGetTopicTx retrieves a topic by ID, in a transaction. @@ -508,14 +508,14 @@ func AmGetTopic(ctx context.Context, topicId int32) (*Topic, error) { * Standard Go error status. */ func AmGetTopicTx(ctx context.Context, tx *sqlx.Tx, topicId int32) (*Topic, error) { - var top Topic - if err := tx.GetContext(ctx, &top, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil { + top := new(Topic) + if err := tx.GetContext(ctx, top, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil { if err == sql.ErrNoRows { err = ErrNoTopic } return nil, err } - return &top, nil + return top, nil } /* AmGetTopicByNumber retrieves a topic by conference and sequence number. @@ -528,10 +528,10 @@ func AmGetTopicTx(ctx context.Context, tx *sqlx.Tx, topicId int32) (*Topic, erro * Standard Go error status. */ func AmGetTopicByNumber(ctx context.Context, conf *Conference, topicNum int16) (*Topic, error) { - var top Topic - err := amdb.GetContext(ctx, &top, "SELECT * FROM topics WHERE confid = ? AND num = ?", conf.ConfId, topicNum) + top := new(Topic) + err := amdb.GetContext(ctx, top, "SELECT * FROM topics WHERE confid = ? AND num = ?", conf.ConfId, topicNum) if err == nil { - return &top, nil + return top, nil } if err == sql.ErrNoRows { err = ErrNoTopic @@ -549,10 +549,10 @@ func AmGetTopicByNumber(ctx context.Context, conf *Conference, topicNum int16) ( * Standard Go error status. */ func AmGetTopicByName(ctx context.Context, conf *Conference, name string) (*Topic, error) { - var top Topic - err := amdb.GetContext(ctx, &top, "SELECT * FROM topics WHERE confid = ? AND name = ?", conf.ConfId, name) + top := new(Topic) + err := amdb.GetContext(ctx, top, "SELECT * FROM topics WHERE confid = ? AND name = ?", conf.ConfId, name) if err == nil { - return &top, nil + return top, nil } if err == sql.ErrNoRows { err = ErrNoTopic @@ -723,8 +723,8 @@ func AmListTopics(ctx context.Context, confid int32, uid int32, viewOption int, * Pointer to the new Topic data structure. * Standard Go error status. */ -func AmNewTopic(ctx context.Context, conf *Conference, user *User, title string, zeroPostPseud string, zeroPost string, - zeroPostLines int32, comm *Community, ipaddr string) (*Topic, error) { +func AmNewTopic(ctx context.Context, conf *Conference, user *User, title, zeroPostPseud, zeroPost string, zeroPostLines int32, + comm *Community, ipaddr string) (*Topic, error) { tx, commit, rollback := transaction(ctx) defer rollback() diff --git a/database/user.go b/database/user.go index 1086f39..08f9c51 100644 --- a/database/user.go +++ b/database/user.go @@ -473,12 +473,12 @@ func AmGetUser(ctx context.Context, uid int32) (*User, error) { if rc, ok := userCache.Get(uid); ok { return rc.(*User), nil } - var user User - err := amdb.GetContext(ctx, &user, "SELECT * from users WHERE uid = ?", uid) + user := new(User) + err := amdb.GetContext(ctx, user, "SELECT * from users WHERE uid = ?", uid) switch err { case nil: - userCache.Add(uid, &user) - return &user, nil + userCache.Add(uid, user) + return user, nil case sql.ErrNoRows: return nil, ErrNoUser } @@ -500,12 +500,12 @@ func AmGetUserTx(ctx context.Context, tx *sqlx.Tx, uid int32) (*User, error) { if rc, ok := userCache.Get(uid); ok { return rc.(*User), nil } - var user User - err := tx.GetContext(ctx, &user, "SELECT * from users WHERE uid = ?", uid) + user := new(User) + err := tx.GetContext(ctx, user, "SELECT * from users WHERE uid = ?", uid) switch err { case nil: - userCache.Add(uid, &user) - return &user, nil + userCache.Add(uid, user) + return user, nil case sql.ErrNoRows: return nil, ErrNoUser } @@ -522,12 +522,12 @@ func AmGetUserTx(ctx context.Context, tx *sqlx.Tx, uid int32) (*User, error) { * Standard Go error status */ func AmGetUserByName(ctx context.Context, name string, tx *sqlx.Tx) (*User, error) { - var user User var err error + user := new(User) if tx != nil { - err = tx.GetContext(ctx, &user, "SELECT * FROM users WHERE username = ?", name) + err = tx.GetContext(ctx, user, "SELECT * FROM users WHERE username = ?", name) } else { - err = amdb.GetContext(ctx, &user, "SELECT * FROM users WHERE username = ?", name) + err = amdb.GetContext(ctx, user, "SELECT * FROM users WHERE username = ?", name) } switch err { case nil: @@ -536,9 +536,9 @@ func AmGetUserByName(ctx context.Context, name string, tx *sqlx.Tx) (*User, erro if rc, ok := userCache.Get(user.Uid); ok { return rc.(*User), nil } else { - userCache.Add(user.Uid, &user) + userCache.Add(user.Uid, user) } - return &user, nil + return user, nil case sql.ErrNoRows: return nil, ErrNoUser } @@ -821,12 +821,12 @@ func internalGetProp(ctx context.Context, uid int32, ndx int32) (*UserProperties if rc, ok := userPropCache.Get(key); ok { return rc.(*UserProperties), nil } - var prop UserProperties - if err := amdb.GetContext(ctx, &prop, "SELECT * from propuser WHERE uid = ? AND ndx = ?", uid, ndx); err != nil { + prop := new(UserProperties) + if err := amdb.GetContext(ctx, prop, "SELECT * from propuser WHERE uid = ? AND ndx = ?", uid, ndx); err != nil { return nil, err } - userPropCache.Add(key, &prop) - return &prop, nil + userPropCache.Add(key, prop) + return prop, nil } /* AmGetUserProperty retrieves the value of a user property. @@ -891,13 +891,13 @@ func AmSetUserProperty(ctx context.Context, uid int32, ndx int32, val *string) e * SearchUserOperRegex - The specified field matches the regular expression in "term". * term - The search term, as specified above. * offset - Number of users to skip at beginning of list. - * max - Maximum number of users to return. + * maxCount - Maximum number of users to return. * Returns: * Array of User pointers representing the return elements. * The total number of users matching this query (could be greater than max) * Standard Go error status. */ -func AmSearchUsers(ctx context.Context, field int, oper int, term string, offset int, max int) ([]*User, int, error) { +func AmSearchUsers(ctx context.Context, field, oper int, term string, offset, maxCount int) ([]*User, int, error) { var queryPortion strings.Builder switch field { case SearchUserFieldName: @@ -939,15 +939,15 @@ func AmSearchUsers(ctx context.Context, field int, oper int, term string, offset var rs *sql.Rows if offset > 0 { rs, err = amdb.QueryContext(ctx, "SELECT u.uid FROM users u, contacts c WHERE u.contactid = c.contactid AND u.is_anon = 0 AND "+q+ - " ORDER BY u.username LIMIT ? OFFSET ?", max, offset) + " ORDER BY u.username LIMIT ? OFFSET ?", maxCount, offset) } else { rs, err = amdb.QueryContext(ctx, "SELECT u.uid FROM users u, contacts c WHERE u.contactid = c.contactid AND u.is_anon = 0 AND "+q+ - " ORDER BY u.username LIMIT ?", max) + " ORDER BY u.username LIMIT ?", maxCount) } if err != nil { return nil, total, err } - rc := make([]*User, 0, min(max, 10000)) + rc := make([]*User, 0, min(maxCount, 10000)) for rs.Next() { var uid int32 if err = rs.Scan(&uid); err == nil {