Add CountRecords0 function

This commit is contained in:
root
2020-07-26 14:54:59 +00:00
parent 8b03c5f795
commit 98154ded88
2 changed files with 33 additions and 6 deletions

View File

@@ -114,9 +114,12 @@ func (dbobj dbcon) getAuditEvents(userTOKEN string, offset int32, limit int32) (
}
func (dbobj dbcon) getAdminAuditEvents(offset int32, limit int32) ([]byte, int64, error) {
count := int64(1000)
count, err := dbobj.store.CountRecords0(storage.TblName.Audit)
if err != nil {
return nil, 0, err
}
if count == 0 {
return []byte("[]"), 0, nil
return []byte("[]"), 0, err
}
var results []bson.M
records, err := dbobj.store.GetList0(storage.TblName.Audit, offset, limit, "when")

View File

@@ -361,10 +361,10 @@ func (dbobj DBStorage) CreateRecord(t Tbl, data interface{}) (int, error) {
return dbobj.CreateRecordInTable(tbl, data)
}
// CountRecords returns number of records that match filter
func (dbobj DBStorage) CountRecords(t Tbl, keyName string, keyValue string) (int64, error) {
// CountRecords returns number of records in table
func (dbobj DBStorage) CountRecords0(t Tbl) (int64, error) {
tbl := getTable(t)
q := "select count(*) from " + tbl + " WHERE " + escapeName(keyName) + "=$1"
q := "select count(*) from " + tbl
fmt.Printf("q: %s\n", q)
tx, err := dbobj.db.Begin()
@@ -372,7 +372,7 @@ func (dbobj DBStorage) CountRecords(t Tbl, keyName string, keyValue string) (int
return 0, err
}
defer tx.Rollback()
row := tx.QueryRow(q, keyValue)
row := tx.QueryRow(q)
// Columns
var count int
err = row.Scan(&count)
@@ -385,6 +385,30 @@ func (dbobj DBStorage) CountRecords(t Tbl, keyName string, keyValue string) (int
return int64(count), nil
}
// CountRecords returns number of records that match filter
func (dbobj DBStorage) CountRecords(t Tbl, keyName string, keyValue string) (int64, error) {
tbl := getTable(t)
q := "select count(*) from " + tbl + " WHERE " + escapeName(keyName) + "=$1"
fmt.Printf("q: %s\n", q)
tx, err := dbobj.db.Begin()
if err != nil {
return 0, err
}
defer tx.Rollback()
row := tx.QueryRow(q, keyValue)
// Columns
var count int
err = row.Scan(&count)
if err != nil {
return 0, err
}
if err = tx.Commit(); err != nil {
return 0, err
}
return int64(count), nil
}
// UpdateRecord updates database record
func (dbobj DBStorage) UpdateRecord(t Tbl, keyName string, keyValue string, bdoc *bson.M) (int64, error) {
table := getTable(t)