#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""数据库连接配置模型"""
from sqlalchemy import Boolean, Column, Index, Integer, JSON, String, Text

from app.base_model import BaseModel


class DatabaseConnection(BaseModel):
    """外部数据库连接配置"""

    __tablename__ = "core_database_connection"

    application_id = Column(String(21), nullable=True, index=True, comment="所属应用ID")
    code = Column(String(50), unique=True, nullable=False, index=True, comment="连接编码")
    name = Column(String(100), nullable=False, comment="连接名称")
    db_type = Column(String(20), nullable=False, comment="数据库类型: postgresql/mysql/sqlserver/oracle")
    host = Column(String(255), nullable=False, comment="主机")
    port = Column(Integer, nullable=False, default=5432, comment="端口")
    user = Column(String(100), nullable=False, default="", comment="用户名")
    password_enc = Column(Text, default="", comment="加密密码")
    default_database = Column(String(100), nullable=False, default="", comment="默认数据库名")
    description = Column(Text, default="", comment="描述")
    status = Column(Boolean, default=True, index=True, comment="是否启用")
    is_system = Column(Boolean, default=False, comment="是否系统内置连接")
    extra_options = Column(JSON, default=dict, comment="扩展选项")

    __table_args__ = (
        Index("ix_core_database_connection_app_status", "application_id", "status"),
    )
