#!/bin/bash
# Flower Database Quick Query Tool
# 快速查询工具 - 用于执行单个SQL查询

set -e

# SSH连接参数
SSH_HOST="8.148.215.233"
SSH_PORT="18762"
SSH_USER="root"

# 数据库连接参数（在远程服务器上）
DB_HOST="127.0.0.1"
DB_PORT="5432"
DB_NAME="flower"
DB_USER="postgres"
DB_PASS="postgres"

# 使用方法
usage() {
    echo "用法: $0 'SQL查询语句'"
    echo
    echo "示例:"
    echo "  $0 'SELECT * FROM business_salesorder LIMIT 10'"
    echo "  $0 'SELECT COUNT(*) FROM printing_plateorder'"
    echo
    echo "或者通过管道传递SQL:"
    echo "  echo 'SELECT * FROM basic_info_customer LIMIT 5' | $0"
    exit 1
}

# 检查参数
if [ -t 0 ]; then
    # 从命令行参数读取
    if [ $# -eq 0 ]; then
        usage
    fi
    QUERY="$*"
else
    # 从标准输入读取
    QUERY=$(cat)
fi

# 执行查询
ssh -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" \
    "PGPASSWORD=$DB_PASS psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c \"$QUERY\""
