fix: historial por usuario (user_id en query_history)
This commit is contained in:
@@ -82,18 +82,22 @@ func openDynamicDB(c models.ConxDb) (*sql.DB, error) {
|
||||
|
||||
// ExecuteSQL ejecuta SQL arbitrario contra la conexión y devuelve QueryResult.
|
||||
// También guarda en query_history.
|
||||
func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
func ExecuteSQL(conx models.ConxDb, database, sqlText string, userID ...uint) QueryResult {
|
||||
uid := uint(0)
|
||||
if len(userID) > 0 {
|
||||
uid = userID[0]
|
||||
}
|
||||
if isMongoDriver(strings.ToLower(conx.TipoDb.Nombre)) {
|
||||
return mongoExecuteSQL(conx, database, sqlText)
|
||||
return mongoExecuteSQL(conx, database, sqlText, uid)
|
||||
}
|
||||
if isRedisDriver(strings.ToLower(conx.TipoDb.Nombre)) {
|
||||
return redisExecuteCommand(conx, database, sqlText)
|
||||
return redisExecuteCommand(conx, database, sqlText, uid)
|
||||
}
|
||||
start := time.Now()
|
||||
|
||||
db, err := openDynamicDB(conx)
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, sqlText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
defer db.Close()
|
||||
@@ -110,7 +114,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
}
|
||||
} else {
|
||||
if _, err2 := db.Exec("USE " + quoteIdentifier(database, conx.TipoDb.Nombre)); err2 != nil {
|
||||
saveHistory(conx.ID, sqlText, "error", err2.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err2.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err2.Error()}
|
||||
}
|
||||
}
|
||||
@@ -126,7 +130,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
rows, err := db.Query(trimmed)
|
||||
if err != nil {
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
saveHistory(conx.ID, sqlText, "error", err.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err.Error(), 0, elapsed)
|
||||
return QueryResult{Error: err.Error(), IsSelect: true}
|
||||
}
|
||||
defer rows.Close()
|
||||
@@ -170,7 +174,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
if len(preview) > 80 {
|
||||
preview = preview[:80] + "..."
|
||||
}
|
||||
saveHistory(conx.ID, sqlText, "error", execErr.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "error", execErr.Error(), 0, elapsed)
|
||||
return QueryResult{Error: fmt.Sprintf("[%s]: %s", preview, execErr.Error())}
|
||||
}
|
||||
if affected, err2 := res.RowsAffected(); err2 == nil {
|
||||
@@ -180,7 +184,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
result.AffectedRows = totalAffected
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, sqlText, "ok", "", totalAffected, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "ok", "", totalAffected, elapsed)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -188,18 +192,18 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
res, err := db.Exec(trimmed)
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, sqlText, "error", err.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err.Error(), 0, elapsed)
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
affected, _ := res.RowsAffected()
|
||||
result.AffectedRows = affected
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, sqlText, "ok", "", affected, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "ok", "", affected, elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
result.DurationMs = time.Since(start).Milliseconds()
|
||||
saveHistory(conx.ID, sqlText, "ok", "", int64(result.RowCount), result.DurationMs)
|
||||
saveHistory(conx.ID, uid, sqlText, "ok", "", int64(result.RowCount), result.DurationMs)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -559,9 +563,10 @@ func quoteIdentifier(name, driver string) string {
|
||||
return "`" + strings.ReplaceAll(name, "`", "``") + "`"
|
||||
}
|
||||
|
||||
func saveHistory(conxID uint, sqlText, status, errMsg string, rows, durationMs int64) {
|
||||
func saveHistory(conxID, userID uint, sqlText, status, errMsg string, rows, durationMs int64) {
|
||||
models.SaveQueryHistory(models.QueryHistory{
|
||||
ConxDbID: conxID,
|
||||
UserID: userID,
|
||||
SQL: sqlText,
|
||||
Status: status,
|
||||
ErrorMsg: errMsg,
|
||||
@@ -670,7 +675,11 @@ func redisListKeys(c models.ConxDb, database string) ([]string, error) {
|
||||
// redisExecuteCommand parsea y ejecuta un comando Redis.
|
||||
// Los comandos se escriben como en redis-cli: GET key / SET key value / etc.
|
||||
// Soporta múltiples líneas: cada línea no vacía es un comando independiente.
|
||||
func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResult {
|
||||
func redisExecuteCommand(conx models.ConxDb, database, cmdText string, userID ...uint) QueryResult {
|
||||
uid := uint(0)
|
||||
if len(userID) > 0 {
|
||||
uid = userID[0]
|
||||
}
|
||||
start := time.Now()
|
||||
|
||||
dbIndex := 0
|
||||
@@ -682,7 +691,7 @@ func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResu
|
||||
|
||||
client, err := redisConnect(conx, dbIndex)
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, cmdText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, cmdText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
defer client.Close()
|
||||
@@ -735,7 +744,7 @@ func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResu
|
||||
IsSelect: true,
|
||||
DurationMs: elapsed,
|
||||
}
|
||||
saveHistory(conx.ID, cmdText, "ok", "", int64(len(rows)), elapsed)
|
||||
saveHistory(conx.ID, uid, cmdText, "ok", "", int64(len(rows)), elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -753,13 +762,13 @@ func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResu
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
|
||||
if execErr != nil && execErr != goredis.Nil {
|
||||
saveHistory(conx.ID, cmdText, "error", execErr.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, cmdText, "error", execErr.Error(), 0, elapsed)
|
||||
return QueryResult{Error: execErr.Error(), DurationMs: elapsed}
|
||||
}
|
||||
|
||||
result := redisResultToQueryResult(val, lines[0])
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, cmdText, "ok", "", int64(result.RowCount), elapsed)
|
||||
saveHistory(conx.ID, uid, cmdText, "ok", "", int64(result.RowCount), elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1052,11 +1061,15 @@ func mongoSingleToDoubleQuotes(s string) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult {
|
||||
func mongoExecuteSQL(conx models.ConxDb, database, queryText string, userID ...uint) QueryResult {
|
||||
uid := uint(0)
|
||||
if len(userID) > 0 {
|
||||
uid = userID[0]
|
||||
}
|
||||
start := time.Now()
|
||||
client, err := mongoConnect(conx)
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, queryText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, queryText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
defer client.Disconnect(context.Background()) //nolint
|
||||
@@ -1066,11 +1079,11 @@ func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult
|
||||
result, err := mongoRunQuery(ctx, db, strings.TrimSpace(queryText))
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, queryText, "error", err.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, queryText, "error", err.Error(), 0, elapsed)
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, queryText, "ok", "", int64(result.RowCount)+result.AffectedRows, elapsed)
|
||||
saveHistory(conx.ID, uid, queryText, "ok", "", int64(result.RowCount)+result.AffectedRows, elapsed)
|
||||
return *result
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user