62 lines
2.4 KiB
Go
62 lines
2.4 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// QueryHistory almacena el historial de consultas ejecutadas por los usuarios.
|
|
type QueryHistory struct {
|
|
gorm.Model
|
|
ConxDbID uint `json:"conx_db_id" gorm:"column:conx_db_id;index"`
|
|
ConxDb ConxDb `json:"conx_db" gorm:"foreignKey:ConxDbID"`
|
|
UserID uint `json:"user_id" gorm:"column:user_id;index;default:0"`
|
|
SQL string `json:"sql" gorm:"column:sql;type:text"`
|
|
Status string `json:"status" gorm:"column:status"` // ok | error
|
|
ErrorMsg string `json:"error_msg" gorm:"column:error_msg;type:text"`
|
|
RowsAffect int64 `json:"rows_affect" gorm:"column:rows_affect"`
|
|
DurationMs int64 `json:"duration_ms" gorm:"column:duration_ms"`
|
|
ExecutedAt time.Time `json:"executed_at" gorm:"column:executed_at;autoCreateTime"`
|
|
}
|
|
|
|
func (QueryHistory) TableName() string {
|
|
return "query_history"
|
|
}
|
|
|
|
// SaveQueryHistory guarda una entrada en el historial.
|
|
func SaveQueryHistory(h QueryHistory) error {
|
|
return app.Http.Database.DB.Create(&h).Error
|
|
}
|
|
|
|
// GetQueryHistory devuelve el historial de una conexión con paginación, filtrado por usuario.
|
|
func GetQueryHistory(conxDbID, userID uint, limit, offset int) ([]QueryHistory, int64, error) {
|
|
var items []QueryHistory
|
|
var total int64
|
|
db := app.Http.Database.DB.Model(&QueryHistory{}).Where("conx_db_id = ? AND user_id = ?", conxDbID, userID)
|
|
db.Count(&total)
|
|
err := db.Order("executed_at DESC").Limit(limit).Offset(offset).Find(&items).Error
|
|
return items, total, err
|
|
}
|
|
|
|
// DeleteQueryHistory elimina el historial de una conexión para un usuario.
|
|
func DeleteQueryHistory(conxDbID, userID uint) error {
|
|
return app.Http.Database.DB.Where("conx_db_id = ? AND user_id = ?", conxDbID, userID).Delete(&QueryHistory{}).Error
|
|
}
|
|
|
|
// GetQueryHistoryAdmin devuelve todo el historial de una conexión (solo admin).
|
|
func GetQueryHistoryAdmin(conxDbID uint, limit, offset int) ([]QueryHistory, int64, error) {
|
|
var items []QueryHistory
|
|
var total int64
|
|
db := app.Http.Database.DB.Model(&QueryHistory{}).Where("conx_db_id = ?", conxDbID)
|
|
db.Count(&total)
|
|
err := db.Order("executed_at DESC").Limit(limit).Offset(offset).Find(&items).Error
|
|
return items, total, err
|
|
}
|
|
|
|
// DeleteQueryHistoryAdmin elimina todo el historial de una conexión (solo admin).
|
|
func DeleteQueryHistoryAdmin(conxDbID uint) error {
|
|
return app.Http.Database.DB.Where("conx_db_id = ?", conxDbID).Delete(&QueryHistory{}).Error
|
|
}
|