replace all single-row SelectContext calls with GetContext calls
This commit is contained in:
+9
-25
@@ -605,16 +605,11 @@ func AmGetCommunity(ctx context.Context, id int32) (*Community, error) {
|
|||||||
defer getCommunityMutex.Unlock()
|
defer getCommunityMutex.Unlock()
|
||||||
rc, ok := communityCache.Get(id)
|
rc, ok := communityCache.Get(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []Community
|
var newcomm Community
|
||||||
if err := amdb.SelectContext(ctx, &dbdata, "SELECT * from communities WHERE commid = ?", id); err != nil {
|
if err := amdb.GetContext(ctx, &newcomm, "SELECT * from communities WHERE commid = ?", id); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
rc = &newcomm
|
||||||
return nil, fmt.Errorf("community with ID %d not found", id)
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetCommunity(%d): too many responses(%d)", id, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
communityCache.Add(id, rc)
|
communityCache.Add(id, rc)
|
||||||
}
|
}
|
||||||
return rc.(*Community), nil
|
return rc.(*Community), nil
|
||||||
@@ -634,16 +629,11 @@ func AmGetCommunityTx(ctx context.Context, tx *sqlx.Tx, id int32) (*Community, e
|
|||||||
defer getCommunityMutex.Unlock()
|
defer getCommunityMutex.Unlock()
|
||||||
rc, ok := communityCache.Get(id)
|
rc, ok := communityCache.Get(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []Community
|
var newcomm Community
|
||||||
if err := tx.SelectContext(ctx, &dbdata, "SELECT * from communities WHERE commid = ?", id); err != nil {
|
if err := tx.GetContext(ctx, &newcomm, "SELECT * from communities WHERE commid = ?", id); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
rc = &newcomm
|
||||||
return nil, fmt.Errorf("community with ID %d not found", id)
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetCommunity(%d): too many responses(%d)", id, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
communityCache.Add(id, rc)
|
communityCache.Add(id, rc)
|
||||||
}
|
}
|
||||||
return rc.(*Community), nil
|
return rc.(*Community), nil
|
||||||
@@ -809,17 +799,11 @@ func internalGetCommProp(ctx context.Context, cid int32, ndx int32) (*CommunityP
|
|||||||
defer getCommunityPropMutex.Unlock()
|
defer getCommunityPropMutex.Unlock()
|
||||||
rc, ok := communityPropCache.Get(key)
|
rc, ok := communityPropCache.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []CommunityProperties
|
var prop CommunityProperties
|
||||||
if err = amdb.SelectContext(ctx, &dbdata, "SELECT * from propcomm WHERE cid = ? AND ndx = ?", cid, ndx); err != nil {
|
if err = amdb.GetContext(ctx, &prop, "SELECT * from propcomm WHERE cid = ? AND ndx = ?", cid, ndx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
rc = &prop
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetCommunityProperty(%d): too many responses(%d)", cid, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
communityPropCache.Add(key, rc)
|
communityPropCache.Add(key, rc)
|
||||||
}
|
}
|
||||||
return rc.(*CommunityProperties), nil
|
return rc.(*CommunityProperties), nil
|
||||||
|
|||||||
+46
-65
@@ -415,11 +415,13 @@ func (c *Conference) SaveFlags(ctx context.Context, f *util.OptionSet) error {
|
|||||||
|
|
||||||
// Settings returns the settings for a user.
|
// Settings returns the settings for a user.
|
||||||
func (c *Conference) Settings(ctx context.Context, u *User) (*ConferenceSettings, error) {
|
func (c *Conference) Settings(ctx context.Context, u *User) (*ConferenceSettings, error) {
|
||||||
var dbdata []ConferenceSettings
|
var settings ConferenceSettings
|
||||||
if err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM confsettings WHERE confid = ? AND uid = ?", c.ConfId, u.Uid); err != nil {
|
err := amdb.GetContext(ctx, &settings, "SELECT * FROM confsettings WHERE confid = ? AND uid = ?", c.ConfId, u.Uid)
|
||||||
return nil, err
|
switch err {
|
||||||
}
|
case nil:
|
||||||
if len(dbdata) == 0 {
|
settings.newflag = false
|
||||||
|
return &settings, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
settings := ConferenceSettings{
|
settings := ConferenceSettings{
|
||||||
ConfId: c.ConfId,
|
ConfId: c.ConfId,
|
||||||
Uid: u.Uid,
|
Uid: u.Uid,
|
||||||
@@ -430,11 +432,7 @@ func (c *Conference) Settings(ctx context.Context, u *User) (*ConferenceSettings
|
|||||||
}
|
}
|
||||||
return &settings, nil
|
return &settings, nil
|
||||||
}
|
}
|
||||||
if len(dbdata) > 1 {
|
return nil, err
|
||||||
return nil, fmt.Errorf("conference.Settings(c=%d,u=%d): too many results (%d)", c.ConfId, u.Uid, len(dbdata))
|
|
||||||
}
|
|
||||||
dbdata[0].newflag = false
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Link returns a link string to this conference.
|
// Link returns a link string to this conference.
|
||||||
@@ -465,36 +463,32 @@ 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,
|
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)
|
create_lvl, hide_lvl, nuke_lvl, change_lvl, delete_lvl, c.ConfId)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var tmp []Conference
|
var tmp Conference
|
||||||
err := amdb.SelectContext(ctx, &tmp, "SELECT * FROM confs WHERE confid = ?", c.ConfId)
|
err := amdb.GetContext(ctx, &tmp, "SELECT * FROM confs WHERE confid = ?", c.ConfId)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(tmp) != 1 {
|
if c.Name != tmp.Name {
|
||||||
err = errors.New("internal error rereading conference")
|
AmStoreAudit(AmNewCommAudit(AuditConferenceName, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", c.ConfId), fmt.Sprintf("name='%s'", tmp.Name)))
|
||||||
} else {
|
|
||||||
if c.Name != tmp[0].Name {
|
|
||||||
AmStoreAudit(AmNewCommAudit(AuditConferenceName, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", c.ConfId), fmt.Sprintf("name='%s'", tmp[0].Name)))
|
|
||||||
}
|
|
||||||
deltaSecurity := false
|
|
||||||
if (c.ReadLevel != tmp[0].ReadLevel) || (c.PostLevel != tmp[0].PostLevel) || (c.CreateLevel != tmp[0].CreateLevel) || (c.HideLevel != tmp[0].HideLevel) {
|
|
||||||
deltaSecurity = true
|
|
||||||
}
|
|
||||||
if (c.NukeLevel != tmp[0].NukeLevel) || (c.ChangeLevel != tmp[0].ChangeLevel) || (c.DeleteLevel != tmp[0].DeleteLevel) {
|
|
||||||
deltaSecurity = true
|
|
||||||
}
|
|
||||||
if deltaSecurity {
|
|
||||||
AmStoreAudit(AmNewCommAudit(AuditConferenceSecurity, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", c.ConfId)))
|
|
||||||
}
|
|
||||||
c.Name = tmp[0].Name
|
|
||||||
c.Description = tmp[0].Description
|
|
||||||
c.ReadLevel = tmp[0].ReadLevel
|
|
||||||
c.PostLevel = tmp[0].PostLevel
|
|
||||||
c.CreateLevel = tmp[0].CreateLevel
|
|
||||||
c.HideLevel = tmp[0].HideLevel
|
|
||||||
c.NukeLevel = tmp[0].NukeLevel
|
|
||||||
c.ChangeLevel = tmp[0].ChangeLevel
|
|
||||||
c.DeleteLevel = tmp[0].DeleteLevel
|
|
||||||
c.LastUpdate = tmp[0].LastUpdate
|
|
||||||
}
|
}
|
||||||
|
deltaSecurity := false
|
||||||
|
if (c.ReadLevel != tmp.ReadLevel) || (c.PostLevel != tmp.PostLevel) || (c.CreateLevel != tmp.CreateLevel) || (c.HideLevel != tmp.HideLevel) {
|
||||||
|
deltaSecurity = true
|
||||||
|
}
|
||||||
|
if (c.NukeLevel != tmp.NukeLevel) || (c.ChangeLevel != tmp.ChangeLevel) || (c.DeleteLevel != tmp.DeleteLevel) {
|
||||||
|
deltaSecurity = true
|
||||||
|
}
|
||||||
|
if deltaSecurity {
|
||||||
|
AmStoreAudit(AmNewCommAudit(AuditConferenceSecurity, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", c.ConfId)))
|
||||||
|
}
|
||||||
|
c.Name = tmp.Name
|
||||||
|
c.Description = tmp.Description
|
||||||
|
c.ReadLevel = tmp.ReadLevel
|
||||||
|
c.PostLevel = tmp.PostLevel
|
||||||
|
c.CreateLevel = tmp.CreateLevel
|
||||||
|
c.HideLevel = tmp.HideLevel
|
||||||
|
c.NukeLevel = tmp.NukeLevel
|
||||||
|
c.ChangeLevel = tmp.ChangeLevel
|
||||||
|
c.DeleteLevel = tmp.DeleteLevel
|
||||||
|
c.LastUpdate = tmp.LastUpdate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@@ -1022,16 +1016,11 @@ func AmGetConference(ctx context.Context, id int32) (*Conference, error) {
|
|||||||
defer getConferenceMutex.Unlock()
|
defer getConferenceMutex.Unlock()
|
||||||
rc, ok := conferenceCache.Get(id)
|
rc, ok := conferenceCache.Get(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []Conference
|
var conf Conference
|
||||||
if err = amdb.SelectContext(ctx, &dbdata, "SELECT * from confs where confid = ?", id); err != nil {
|
if err = amdb.GetContext(ctx, &conf, "SELECT * from confs where confid = ?"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
rc = &conf
|
||||||
return nil, fmt.Errorf("conference with ID %d not found", id)
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetConference(%d): too many responses(%d)", id, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
conferenceCache.Add(id, rc)
|
conferenceCache.Add(id, rc)
|
||||||
}
|
}
|
||||||
return rc.(*Conference), err
|
return rc.(*Conference), err
|
||||||
@@ -1157,17 +1146,11 @@ func internalGetConfProp(ctx context.Context, confid int32, ndx int32) (*Confere
|
|||||||
defer getConferencePropMutex.Unlock()
|
defer getConferencePropMutex.Unlock()
|
||||||
rc, ok := conferencePropCache.Get(key)
|
rc, ok := conferencePropCache.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []ConferenceProperties
|
var prop ConferenceProperties
|
||||||
if err = amdb.SelectContext(ctx, &dbdata, "SELECT * from propconf WHERE confid = ? AND ndx = ?", confid, ndx); err != nil {
|
if err = amdb.GetContext(ctx, &prop, "SELECT * from propconf WHERE confid = ? AND ndx = ?", confid, ndx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
rc = &prop
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetConferenceProperty(%d): too many responses(%d)", confid, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
conferencePropCache.Add(key, rc)
|
conferencePropCache.Add(key, rc)
|
||||||
}
|
}
|
||||||
return rc.(*ConferenceProperties), nil
|
return rc.(*ConferenceProperties), nil
|
||||||
@@ -1304,16 +1287,14 @@ func AmCreateConference(ctx context.Context, comm *Community, name, alias, descr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var rc []Conference
|
var rc Conference
|
||||||
err = tx.SelectContext(ctx, &rc, "SELECT * FROM confs WHERE confid = ?", int32(newId))
|
err = tx.GetContext(ctx, &rc, "SELECT * FROM confs WHERE confid = ?", int32(newId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if len(rc) != 1 {
|
|
||||||
return nil, errors.New("internal error reading back conference")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach the alias to the conference.
|
// Attach the alias to the conference.
|
||||||
_, err = tx.ExecContext(ctx, "INSERT INTO confalias (confid, alias) VALUES (?, ?)", rc[0].ConfId, alias)
|
_, err = tx.ExecContext(ctx, "INSERT INTO confalias (confid, alias) VALUES (?, ?)", rc.ConfId, alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1327,14 +1308,14 @@ func AmCreateConference(ctx context.Context, comm *Community, name, alias, descr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Link the conference into the community, and set the hide flag.
|
// Link the conference into the community, and set the hide flag.
|
||||||
_, err = tx.ExecContext(ctx, "INSERT INTO commtoconf (commid, confid, sequence, hide_list) VALUES (?, ?, ?, ?)", comm.Id, rc[0].ConfId,
|
_, err = tx.ExecContext(ctx, "INSERT INTO commtoconf (commid, confid, sequence, hide_list) VALUES (?, ?, ?, ?)", comm.Id, rc.ConfId,
|
||||||
int16(seq+COMMTOCONF_SEQ_SPACING), hide_list)
|
int16(seq+COMMTOCONF_SEQ_SPACING), hide_list)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the specified user the first host of the conference.
|
// Make the specified user the first host of the conference.
|
||||||
_, err = tx.ExecContext(ctx, "INSERT INTO confmember (confid, uid, granted_lvl) VALUES (?, ?, ?)", rc[0].ConfId, u.Uid, AmDefaultRole("Conference.NewHost").Level())
|
_, err = tx.ExecContext(ctx, "INSERT INTO confmember (confid, uid, granted_lvl) VALUES (?, ?, ?)", rc.ConfId, u.Uid, AmDefaultRole("Conference.NewHost").Level())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1344,7 +1325,7 @@ func AmCreateConference(ctx context.Context, comm *Community, name, alias, descr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the new conference to the cache, and create our audit record.
|
// Add the new conference to the cache, and create our audit record.
|
||||||
conferenceCache.Add(rc[0].ConfId, &(rc[0]))
|
conferenceCache.Add(rc.ConfId, &rc)
|
||||||
AmStoreAudit(AmNewCommAudit(AuditConferenceCreate, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", rc[0].ConfId), fmt.Sprintf("name=%s", name), fmt.Sprintf("alias=%s", alias)))
|
AmStoreAudit(AmNewCommAudit(AuditConferenceCreate, u.Uid, comm.Id, ipaddr, fmt.Sprintf("confid=%d", rc.ConfId), fmt.Sprintf("name=%s", name), fmt.Sprintf("alias=%s", alias)))
|
||||||
return &(rc[0]), nil
|
return &rc, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -250,16 +250,10 @@ func setupContactsCache() {
|
|||||||
|
|
||||||
// internalContactInfo retrieves the contact info from the database.
|
// internalContactInfo retrieves the contact info from the database.
|
||||||
func internalContactInfo(ctx context.Context, id int32) (*ContactInfo, error) {
|
func internalContactInfo(ctx context.Context, id int32) (*ContactInfo, error) {
|
||||||
var dbdata []ContactInfo
|
var cinf ContactInfo
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * from contacts WHERE contactid = ?", id)
|
err := amdb.GetContext(ctx, &cinf, "SELECT * from contacts WHERE contactid = ?", id)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(dbdata) > 1 {
|
return &cinf, nil
|
||||||
err = fmt.Errorf("internalContactInfo(%d): Too many responses (%d)", id, len(dbdata))
|
|
||||||
} else if len(dbdata) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
} else {
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-7
@@ -12,7 +12,6 @@ package database
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.erbosoft.com/amy/amsterdam/util"
|
"git.erbosoft.com/amy/amsterdam/util"
|
||||||
@@ -108,15 +107,12 @@ func AmGlobals(ctx context.Context) (*Globals, error) {
|
|||||||
globalsMutex.Lock()
|
globalsMutex.Lock()
|
||||||
defer globalsMutex.Unlock()
|
defer globalsMutex.Unlock()
|
||||||
if theGlobals == nil {
|
if theGlobals == nil {
|
||||||
var dbdata []Globals
|
var g Globals
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM globals")
|
err := amdb.GetContext(ctx, &g, "SELECT * FROM globals")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) > 1 {
|
theGlobals = &g
|
||||||
return nil, errors.New("should only be one globals record")
|
|
||||||
}
|
|
||||||
theGlobals = &(dbdata[0])
|
|
||||||
}
|
}
|
||||||
return theGlobals, nil
|
return theGlobals, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ package database
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageStore is the structure for the image store table.
|
// ImageStore is the structure for the image store table.
|
||||||
@@ -61,17 +60,12 @@ func (img *ImageStore) Save(ctx context.Context) error {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func AmLoadImage(ctx context.Context, id int32) (*ImageStore, error) {
|
func AmLoadImage(ctx context.Context, id int32) (*ImageStore, error) {
|
||||||
var dbdata []ImageStore
|
var imgdata ImageStore
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM imagestore WHERE imgid = ?", id)
|
err := amdb.GetContext(ctx, &imgdata, "SELECT * FROM imagestore WHERE imgid = ?", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
return &imgdata, nil
|
||||||
return nil, fmt.Errorf("image ID %d not found", id)
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("image ID %d too many images (%d)", id, len(dbdata))
|
|
||||||
}
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* AmStoreImage stores an image in the database, overwriting one with the same type code and owner if it exists.
|
/* AmStoreImage stores an image in the database, overwriting one with the same type code and owner if it exists.
|
||||||
|
|||||||
+3
-8
@@ -272,17 +272,12 @@ func AmListIPBans(ctx context.Context) ([]IPBanEntry, error) {
|
|||||||
|
|
||||||
// AmGetIPBan returns a single IP address ban structure.
|
// AmGetIPBan returns a single IP address ban structure.
|
||||||
func AmGetIPBan(ctx context.Context, id int32) (*IPBanEntry, error) {
|
func AmGetIPBan(ctx context.Context, id int32) (*IPBanEntry, error) {
|
||||||
var dbdata []IPBanEntry
|
var ban IPBanEntry
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM ipban WHERE id = ?", id)
|
err := amdb.GetContext(ctx, &ban, "SELECT * FROM ipban WHERE id = ?", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
return &ban, nil
|
||||||
return nil, errors.New("not found")
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
return nil, errors.New("internal error, too many returns")
|
|
||||||
}
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AmAddIPBan adds a new IP address ban.
|
// AmAddIPBan adds a new IP address ban.
|
||||||
|
|||||||
+13
-25
@@ -236,17 +236,17 @@ func (p *PostHeader) PruneAttachment(ctx context.Context, u *User, comm *Communi
|
|||||||
|
|
||||||
// Text returns the text associated with a post.
|
// Text returns the text associated with a post.
|
||||||
func (p *PostHeader) Text(ctx context.Context) (string, error) {
|
func (p *PostHeader) Text(ctx context.Context) (string, error) {
|
||||||
var dbdata []PostData
|
var pd PostData
|
||||||
if err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM postdata WHERE postid = ?", p.PostId); err != nil {
|
if err := amdb.GetContext(ctx, &pd, "SELECT * FROM postdata WHERE postid = ?", p.PostId); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return "", ErrNoPostData
|
||||||
|
}
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if len(dbdata) > 1 {
|
if pd.Data == nil {
|
||||||
return "", fmt.Errorf("too many data records (%d) for post #%d", len(dbdata), p.PostId)
|
|
||||||
}
|
|
||||||
if len(dbdata) == 0 || dbdata[0].Data == nil {
|
|
||||||
return "", ErrNoPostData
|
return "", ErrNoPostData
|
||||||
}
|
}
|
||||||
return *dbdata[0].Data, nil
|
return *pd.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Link returns a link string to this post.
|
// Link returns a link string to this post.
|
||||||
@@ -514,17 +514,11 @@ func (p *PostHeader) MoveTo(ctx context.Context, target *Topic, u *User, comm *C
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func AmGetPost(ctx context.Context, postId int64) (*PostHeader, error) {
|
func AmGetPost(ctx context.Context, postId int64) (*PostHeader, error) {
|
||||||
var dbdata []PostHeader
|
var pd PostHeader
|
||||||
if err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM posts WHERE postid = ?", postId); err != nil {
|
if err := amdb.GetContext(ctx, &pd, "SELECT * FROM posts WHERE postid = ?", postId); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
return &pd, nil
|
||||||
return nil, errors.New("post not found")
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetPost: too many entries (%d) for post ID %d", len(dbdata), postId)
|
|
||||||
}
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* AmGetPostRange gets a range of posts from a topic by post numbers.
|
/* AmGetPostRange gets a range of posts from a topic by post numbers.
|
||||||
@@ -580,17 +574,11 @@ func AmNewPost(ctx context.Context, conf *Conference, topic *Topic, user *User,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read back the post header.
|
// Read back the post header.
|
||||||
var dbdata []PostHeader
|
var pd PostHeader
|
||||||
if err := tx.SelectContext(ctx, &dbdata, "SELECT * FROM posts WHERE postid = ?", xid); err != nil {
|
if err := tx.GetContext(ctx, &pd, "SELECT * FROM posts WHERE postid = ?", xid); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
hdr := &pd
|
||||||
return nil, errors.New("AmNewPost: new post not found")
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmNewPost: too many entries (%d) for post ID %d", len(dbdata), xid)
|
|
||||||
}
|
|
||||||
hdr := &(dbdata[0])
|
|
||||||
|
|
||||||
// Add the post data.
|
// Add the post data.
|
||||||
_, err = tx.ExecContext(ctx, "INSERT INTO postdata (postid, data) VALUES (?, ?)", hdr.PostId, post)
|
_, err = tx.ExecContext(ctx, "INSERT INTO postdata (postid, data) VALUES (?, ?)", hdr.PostId, post)
|
||||||
|
|||||||
+12
-36
@@ -65,16 +65,10 @@ func (t *Topic) GetPost(ctx context.Context, num int32) (*PostHeader, error) {
|
|||||||
if num > t.TopMessage {
|
if num > t.TopMessage {
|
||||||
return nil, fmt.Errorf("no post %d in topic %d", num, t.TopicId)
|
return nil, fmt.Errorf("no post %d in topic %d", num, t.TopicId)
|
||||||
}
|
}
|
||||||
var dbdata []PostHeader
|
var pd PostHeader
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM posts WHERE topicid = ? AND num = ?", t.TopicId, num)
|
err := amdb.GetContext(ctx, &pd, "SELECT * FROM posts WHERE topicid = ? AND num = ?", t.TopicId, num)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(dbdata) == 0 {
|
return &pd, nil
|
||||||
err = fmt.Errorf("no post %d in topic %d", num, t.TopicId)
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
err = fmt.Errorf("topic.GetPost: too many entries (%d) for post %d in topic %d", len(dbdata), num, t.TopicId)
|
|
||||||
} else {
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -498,17 +492,11 @@ type TopicSummary struct {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func AmGetTopic(ctx context.Context, topicId int32) (*Topic, error) {
|
func AmGetTopic(ctx context.Context, topicId int32) (*Topic, error) {
|
||||||
var dbdata []Topic
|
var top Topic
|
||||||
if err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil {
|
if err := amdb.GetContext(ctx, &top, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
return &top, nil
|
||||||
return nil, fmt.Errorf("topic %d not found", topicId)
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetTopic(%d): too many responses (%d)", topicId, len(dbdata))
|
|
||||||
}
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* AmGetTopicTx retrieves a topic by ID, in a transaction.
|
/* AmGetTopicTx retrieves a topic by ID, in a transaction.
|
||||||
@@ -521,17 +509,11 @@ func AmGetTopic(ctx context.Context, topicId int32) (*Topic, error) {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func AmGetTopicTx(ctx context.Context, tx *sqlx.Tx, topicId int32) (*Topic, error) {
|
func AmGetTopicTx(ctx context.Context, tx *sqlx.Tx, topicId int32) (*Topic, error) {
|
||||||
var dbdata []Topic
|
var top Topic
|
||||||
if err := tx.SelectContext(ctx, &dbdata, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil {
|
if err := tx.GetContext(ctx, &top, "SELECT * FROM topics WHERE topicid = ?", topicId); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
return &top, nil
|
||||||
return nil, fmt.Errorf("topic %d not found", topicId)
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetTopic(%d): too many responses (%d)", topicId, len(dbdata))
|
|
||||||
}
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* AmGetTopicByNumber retrieves a topic by conference and sequence number.
|
/* AmGetTopicByNumber retrieves a topic by conference and sequence number.
|
||||||
@@ -544,16 +526,10 @@ func AmGetTopicTx(ctx context.Context, tx *sqlx.Tx, topicId int32) (*Topic, erro
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func AmGetTopicByNumber(ctx context.Context, conf *Conference, topicNum int16) (*Topic, error) {
|
func AmGetTopicByNumber(ctx context.Context, conf *Conference, topicNum int16) (*Topic, error) {
|
||||||
var dbdata []Topic
|
var top Topic
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM topics WHERE confid = ? AND num = ?", conf.ConfId, topicNum)
|
err := amdb.GetContext(ctx, &top, "SELECT * FROM topics WHERE confid = ? AND num = ?", conf.ConfId, topicNum)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(dbdata) == 0 {
|
return &top, nil
|
||||||
err = fmt.Errorf("no topic numbered %d in conference %s (#%d)", topicNum, conf.Name, conf.ConfId)
|
|
||||||
} else if len(dbdata) > 1 {
|
|
||||||
err = fmt.Errorf("AmGetTopicByNumber: too many entries (%d) for topic #%d in conference %s (#%d)", len(dbdata), topicNum, conf.Name, conf.ConfId)
|
|
||||||
} else {
|
|
||||||
return &(dbdata[0]), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-43
@@ -62,14 +62,12 @@ func (p *UserPrefs) Save(ctx context.Context, u, setter *User, ipaddr string) er
|
|||||||
}
|
}
|
||||||
var old *UserPrefs
|
var old *UserPrefs
|
||||||
if setter.Uid != u.Uid {
|
if setter.Uid != u.Uid {
|
||||||
var dbdata []UserPrefs
|
var pref UserPrefs
|
||||||
err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM userprefs WHERE uid = ?", u.Uid)
|
err := amdb.GetContext(ctx, &pref, "SELECT * FROM userprefs WHERE uid = ?", u.Uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(dbdata) != 1 {
|
|
||||||
return errors.New("unable to take snapshot")
|
|
||||||
}
|
}
|
||||||
old = &(dbdata[0])
|
old = &pref
|
||||||
}
|
}
|
||||||
_, err := amdb.NamedExecContext(ctx, "UPDATE userprefs SET localeid = :localeid, tzid = :tzid WHERE uid = :uid", p)
|
_, err := amdb.NamedExecContext(ctx, "UPDATE userprefs SET localeid = :localeid, tzid = :tzid WHERE uid = :uid", p)
|
||||||
if err == nil && u != nil {
|
if err == nil && u != nil {
|
||||||
@@ -349,14 +347,11 @@ func (u *User) Prefs(ctx context.Context) (*UserPrefs, error) {
|
|||||||
u.Mutex.Lock()
|
u.Mutex.Lock()
|
||||||
defer u.Mutex.Unlock()
|
defer u.Mutex.Unlock()
|
||||||
if u.prefs == nil {
|
if u.prefs == nil {
|
||||||
var dbdata []UserPrefs
|
var pref UserPrefs
|
||||||
if err := amdb.SelectContext(ctx, &dbdata, "SELECT * FROM userprefs WHERE uid = ?", u.Uid); err != nil {
|
if err := amdb.GetContext(ctx, &pref, "SELECT * FROM userprefs WHERE uid = ?", u.Uid); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) != 1 {
|
u.prefs = &pref
|
||||||
return nil, fmt.Errorf("invalid preferences records for user %d", u.Uid)
|
|
||||||
}
|
|
||||||
u.prefs = &(dbdata[0])
|
|
||||||
}
|
}
|
||||||
return u.prefs, nil
|
return u.prefs, nil
|
||||||
}
|
}
|
||||||
@@ -444,14 +439,11 @@ func AmGetUser(ctx context.Context, uid int32) (*User, error) {
|
|||||||
defer getUserMutex.Unlock()
|
defer getUserMutex.Unlock()
|
||||||
rc, ok := userCache.Get(uid)
|
rc, ok := userCache.Get(uid)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []User
|
var user User
|
||||||
if err = amdb.SelectContext(ctx, &dbdata, "SELECT * from users WHERE uid = ?", uid); err != nil {
|
if err = amdb.GetContext(ctx, &user, "SELECT * from users WHERE uid = ?", uid); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) > 1 {
|
rc = &user
|
||||||
return nil, fmt.Errorf("AmGetUser(%d): too many responses(%d)", uid, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
userCache.Add(uid, rc)
|
userCache.Add(uid, rc)
|
||||||
}
|
}
|
||||||
return rc.(*User), err
|
return rc.(*User), err
|
||||||
@@ -472,14 +464,11 @@ func AmGetUserTx(ctx context.Context, tx *sqlx.Tx, uid int32) (*User, error) {
|
|||||||
defer getUserMutex.Unlock()
|
defer getUserMutex.Unlock()
|
||||||
rc, ok := userCache.Get(uid)
|
rc, ok := userCache.Get(uid)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []User
|
var user User
|
||||||
if err = tx.SelectContext(ctx, &dbdata, "SELECT * from users WHERE uid = ?", uid); err != nil {
|
if err = tx.GetContext(ctx, &user, "SELECT * from users WHERE uid = ?", uid); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) > 1 {
|
rc = &user
|
||||||
return nil, fmt.Errorf("AmGetUser(%d): too many responses(%d)", uid, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
userCache.Add(uid, rc)
|
userCache.Add(uid, rc)
|
||||||
}
|
}
|
||||||
return rc.(*User), err
|
return rc.(*User), err
|
||||||
@@ -495,26 +484,21 @@ func AmGetUserTx(ctx context.Context, tx *sqlx.Tx, uid int32) (*User, error) {
|
|||||||
* Standard Go error status
|
* Standard Go error status
|
||||||
*/
|
*/
|
||||||
func AmGetUserByName(ctx context.Context, name string, tx *sqlx.Tx) (*User, error) {
|
func AmGetUserByName(ctx context.Context, name string, tx *sqlx.Tx) (*User, error) {
|
||||||
var dbdata []User
|
var user User
|
||||||
var err error
|
var err error
|
||||||
if tx != nil {
|
if tx != nil {
|
||||||
err = tx.SelectContext(ctx, &dbdata, "SELECT * FROM users WHERE username = ?", name)
|
err = tx.GetContext(ctx, &user, "SELECT * FROM users WHERE username = ?", name)
|
||||||
} else {
|
} else {
|
||||||
err = amdb.SelectContext(ctx, &dbdata, "SELECT * FROM users WHERE username = ?", name)
|
err = amdb.GetContext(ctx, &user, "SELECT * FROM users WHERE username = ?", name)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetUserByName(\"%s\"): too many responses(%d)", name, len(dbdata))
|
|
||||||
} else if len(dbdata) == 0 {
|
|
||||||
return nil, errors.New("user not found")
|
|
||||||
}
|
|
||||||
getUserMutex.Lock()
|
getUserMutex.Lock()
|
||||||
rc, ok := userCache.Get(dbdata[0].Uid)
|
rc, ok := userCache.Get(user.Uid)
|
||||||
if !ok {
|
if !ok {
|
||||||
rc = &(dbdata[0])
|
rc = &user
|
||||||
userCache.Add(dbdata[0].Uid, rc)
|
userCache.Add(user.Uid, rc)
|
||||||
}
|
}
|
||||||
getUserMutex.Unlock()
|
getUserMutex.Unlock()
|
||||||
return rc.(*User), nil
|
return rc.(*User), nil
|
||||||
@@ -797,17 +781,11 @@ func internalGetProp(ctx context.Context, uid int32, ndx int32) (*UserProperties
|
|||||||
defer getUserPropMutex.Unlock()
|
defer getUserPropMutex.Unlock()
|
||||||
rc, ok := userPropCache.Get(key)
|
rc, ok := userPropCache.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []UserProperties
|
var prop UserProperties
|
||||||
if err = amdb.SelectContext(ctx, &dbdata, "SELECT * from propuser WHERE uid = ? AND ndx = ?", uid, ndx); err != nil {
|
if err = amdb.GetContext(ctx, &prop, "SELECT * from propuser WHERE uid = ? AND ndx = ?", uid, ndx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(dbdata) == 0 {
|
rc = &prop
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
if len(dbdata) > 1 {
|
|
||||||
return nil, fmt.Errorf("AmGetUserProperty(%d): too many responses(%d)", uid, len(dbdata))
|
|
||||||
}
|
|
||||||
rc = &(dbdata[0])
|
|
||||||
userPropCache.Add(key, rc)
|
userPropCache.Add(key, rc)
|
||||||
}
|
}
|
||||||
return rc.(*UserProperties), nil
|
return rc.(*UserProperties), nil
|
||||||
|
|||||||
Reference in New Issue
Block a user