#!/usr/bin/env bash
# onx-db-flush-query-cache — FLUSH QUERY CACHE
# Input:  {} (bos obje kabul edilir)
# Output: {"flushed":true,"operation":"FLUSH QUERY CACHE"}
# Not: MariaDB 10.3+ QUERY CACHE kaldirildi; RESET QUERY CACHE kullanilir.
# Her iki komut da denenecek; biri basarili olursa yeterli.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/_lib/common.sh"

require_root
require_cmd mysql
onx_json_input

onx_log "db-flush-query-cache: FLUSH/RESET QUERY CACHE (root creds)"

# MariaDB 10.5+ query_cache_type=NONE (kaldırıldı) — komut çalışmayabilir ama
# error vermez. MariaDB 10.5 öncesi FLUSH QUERY CACHE, sonrası RESET QUERY CACHE.
# RELOAD privilege gerekir → root creds şart.
if ! mysql_exec_root "" "FLUSH QUERY CACHE;" 2>/dev/null; then
    if ! mysql_exec_root "" "RESET QUERY CACHE;" 2>/dev/null; then
        # MariaDB 10.5+ query cache yok → idempotent kabul et
        onx_log "Query cache yok (MariaDB 10.5+ default) — atlandı"
        json_ok '{"flushed":true,"operation":"NOOP (query_cache_type=NONE)","note":"MariaDB 10.5+ query cache kaldırıldı"}'
        exit 0
    fi
fi

json_ok '{"flushed":true,"operation":"FLUSH QUERY CACHE"}'
