# coding: utf-8

"""
    Carrier API

    **Seaplane Global Messaging Service**  For information about authentication see [authentication](/docs/authentication)   # noqa: E501

    The version of the OpenAPI document: 1.1.0-beta
    Contact: support@seaplane.io
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""


from __future__ import annotations
import pprint
import re  # noqa: F401
import json


from typing import Optional
from pydantic import BaseModel, Field, StrictStr, validator


class SqlRaw(BaseModel):
    """
    Execute raw sql statement
    """

    driver: StrictStr = Field(..., description="A database driver to use.")
    dsn: StrictStr = Field(
        ...,
        description="DB connection string. `OUTPUT_DSN` Flow secret is the default.",
    )
    query: StrictStr = Field(
        ...,
        description="Query to execute with input, placeholders are database driver specific.",
    )
    args_mapping: Optional[StrictStr] = Field(
        None,
        description="Bloblang mapping which should evaluate to an array of values matching in size to the number of placeholder arguments in the field query.",
    )
    init_statement: Optional[StrictStr] = Field(
        None,
        description="An optional SQL statement to execute immediately upon the first connection to the target database. This is a useful way to initialise tables before processing data. Care should be taken to ensure that the statement is idempotent, and therefore would not cause issues when run multiple times after service restarts.",
    )
    __properties = ["driver", "dsn", "query", "args_mapping", "init_statement"]

    @validator("driver")
    def driver_validate_enum(cls, value):
        """Validates the enum"""
        if value not in (
            "mysql",
            "postgres",
            "clickhouse",
            "mssql",
            "sqlite",
            "oracle",
            "snowflake",
            "trino",
        ):
            raise ValueError(
                "must be one of enum values ('mysql', 'postgres', 'clickhouse', 'mssql', 'sqlite', 'oracle', 'snowflake', 'trino')"
            )
        return value

    class Config:
        """Pydantic configuration"""

        allow_population_by_field_name = True
        validate_assignment = True

    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.dict(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> SqlRaw:
        """Create an instance of SqlRaw from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self):
        """Returns the dictionary representation of the model using alias"""
        _dict = self.dict(by_alias=True, exclude={}, exclude_none=True)
        return _dict

    @classmethod
    def from_dict(cls, obj: dict) -> SqlRaw:
        """Create an instance of SqlRaw from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return SqlRaw.parse_obj(obj)

        _obj = SqlRaw.parse_obj(
            {
                "driver": obj.get("driver"),
                "dsn": obj.get("dsn"),
                "query": obj.get("query"),
                "args_mapping": obj.get("args_mapping"),
                "init_statement": obj.get("init_statement"),
            }
        )
        return _obj
