package com.agent.management.model.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * SQL 查询历史
 */
@Data
@Entity
@Table(name = "kb_sql_query_history", indexes = {
        @Index(name = "idx_sql_history_ds", columnList = "datasource_id"),
        @Index(name = "idx_sql_history_time", columnList = "executed_at")
})
public class SqlQueryHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "datasource_id", nullable = false)
    private Long datasourceId;

    /**
     * 执行的 SQL（已截断到 4000 字符以内）
     */
    @Column(columnDefinition = "TEXT", nullable = false)
    private String sql;

    /**
     * SUCCESS / FAILED
     */
    @Column(nullable = false)
    private String status;

    @Column(name = "row_count")
    private Integer rowCount;

    /**
     * 执行耗时（毫秒）
     */
    @Column(name = "execution_time_ms")
    private Long executionTimeMs;

    @Column(name = "error_message")
    private String errorMessage;

    @Column(name = "executed_at")
    private LocalDateTime executedAt;

    @PrePersist
    protected void onCreate() {
        if (executedAt == null) executedAt = LocalDateTime.now();
    }
}
