46 lines
1.6 KiB
Go
46 lines
1.6 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"`
|
|
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.
|
|
func GetQueryHistory(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
|
|
}
|
|
|
|
// DeleteQueryHistory elimina todo el historial de una conexión.
|
|
func DeleteQueryHistory(conxDbID uint) error {
|
|
return app.Http.Database.DB.Where("conx_db_id = ?", conxDbID).Delete(&QueryHistory{}).Error
|
|
}
|