# Class MSSQLEngine (0.3.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [0.3.0 (latest)](https://docs.cloud.google.com/python/docs/reference/langchain-google-cloud-sql-mssql/latest/langchain_google_cloud_sql_mssql.engine.MSSQLEngine)
- [0.2.1](https://docs.cloud.google.com/python/docs/reference/langchain-google-cloud-sql-mssql/0.2.1/langchain_google_cloud_sql_mssql.engine.MSSQLEngine)

    MSSQLEngine(engine: sqlalchemy.engine.base.Engine)

A class for managing connections to a Cloud SQL for MSSQL database.

## Methods

### _create_connector_engine

    _create_connector_engine(
        instance_connection_name: str, database: str, user: str, password: str
    ) -> sqlalchemy.engine.base.Engine

Create a SQLAlchemy engine using the Cloud SQL Python Connector.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `instance_connection_name` | `str` The instance connection name of the Cloud SQL instance to establish a connection to. (ex. "project-id:instance-region:instance-name") |
| `database` | `str` The name of the database to connect to on the Cloud SQL instance. |
| `user` | `str` The username to use for authentication. |
| `password` | `str` The password to use for authentication. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `(sqlalchemy.engine.Engine)` | Engine configured using the Cloud SQL Python Connector. |

### _load_document_table

    _load_document_table(table_name: str) -> sqlalchemy.sql.schema.Table

Load table schema from existing table in MSSQL database.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The MSSQL database table name. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `(sqlalchemy.Table)` | The loaded table. |

### connect

    connect() -> sqlalchemy.engine.base.Connection

Create a connection from SQLAlchemy connection pool.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `(sqlalchemy.engine.Connection)` | a single DBAPI connection checked out from the connection pool. |

### from_instance

    from_instance(
        project_id: str, region: str, instance: str, database: str, user: str, password: str
    ) -> langchain_google_cloud_sql_mssql.engine.MSSQLEngine

Create an instance of MSSQLEngine from Cloud SQL instance
details.

This method uses the Cloud SQL Python Connector to connect to Cloud SQL
MSSQL instance using the given database credentials.

More details can be found at
<https://github.com/GoogleCloudPlatform/cloud-sql-python-connector#credentials>

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `project_id` | `str` Project ID of the Google Cloud Project where the Cloud SQL instance is located. |
| `region` | `str` Region where the Cloud SQL instance is located. |
| `instance` | `str` The name of the Cloud SQL instance. |
| `database` | `str` The name of the database to connect to on the Cloud SQL instance. |
| `db_user` | `str` The username to use for authentication. |
| `db_password` | `str` The password to use for authentication. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `(MSSQLEngine)` | The engine configured to connect to a Cloud SQL instance database. |

### init_chat_history_table

    init_chat_history_table(table_name: str) -> None

Create table with schema required for MSSQLChatMessageHistory class.

Required schema is as follows:

::

    CREATE TABLE {table_name} (
        id INT IDENTITY(1,1) PRIMARY KEY,
        session_id NVARCHAR(MAX) NOT NULL,
        data NVARCHAR(MAX) NOT NULL,
        type NVARCHAR(MAX) NOT NULL
    )

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` Name of database table to create for storing chat message history. |

### init_document_table

    init_document_table(
        table_name: str,
        metadata_columns: typing.List[sqlalchemy.sql.schema.Column] = [],
        content_column: str = "page_content",
        metadata_json_column: typing.Optional[str] = "langchain_metadata",
        overwrite_existing: bool = False,
    ) -> None

Create a table for saving of langchain documents.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The MSSQL database table name. |
| `metadata_columns` | `List[sqlalchemy.Column]` A list of SQLAlchemy Columns to create for custom metadata. Optional. |
| `content_column` | `str` The column to store document content. Deafult: `page_content`. |
| `metadata_json_column` | `Optional[str]` The column to store extra metadata in JSON format. Default: `langchain_metadata`. Optional. |
| `overwrite_existing` | `bool` Whether to drop existing table. Default: False. |