Class MySQLEngine (0.2.2)

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

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

Methods

_create_connector_engine

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

Create a SQLAlchemy engine using the Cloud SQL Python Connector.

Defaults to use "pymysql" driver and to connect using automatic IAM database authentication with the IAM principal associated with the environment's Google Application Default Credentials. If user and password arguments are given, basic database authentication will be used for database login.

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, optional

Database user to use for basic database authentication and login. Defaults to None.

password str, optional

Database password for 'user' to use for basic database authentication and login. Defaults to None.

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

_execute

_execute(query: str, params: typing.Optional[dict] = None) -> None

Executes a SQL query within a transaction.

_execute_outside_tx

_execute_outside_tx(query: str, params: typing.Optional[dict] = None) -> None

Executes a SQL query with autocommit (outside of transaction).

_fetch

_fetch(query: str, params: typing.Optional[dict] = None)

Fetch results from a SQL query.

_fetch_rows

_fetch_rows(query: str, params: typing.Optional[dict] = None)

Fetch results from a SQL query as rows.

_load_document_table

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

Load table schema from existing table in MySQL database.

Parameter
Name Description
table_name str

The MySQL 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: typing.Optional[str] = None,
    password: typing.Optional[str] = None,
) -> langchain_google_cloud_sql_mysql.engine.MySQLEngine

Create an instance of MySQLEngine from Cloud SQL instance details.

This method uses the Cloud SQL Python Connector to connect to Cloud SQL using automatic IAM database authentication with the Google ADC credentials sourced from the environment by default. If user and password arguments are given, basic database authentication will be used for database login.

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.

user str, optional

Database user to use for basic database authentication and login. Defaults to None.

password str, optional

Database password for 'user' to use for basic database authentication and login. Defaults to None.

Returns
Type Description
(MySQLEngine) 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 MySQLChatMessageHistory class.

Required schema is as follows:

::

CREATE TABLE {table_name} (
    id INT AUTO_INCREMENT PRIMARY KEY,
    session_id TEXT NOT NULL,
    data JSON NOT NULL,
    type TEXT NOT NULL
)
Parameter
Name Description
table_name str

The name of the table to create.

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 MySQL 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.

init_vectorstore_table

init_vectorstore_table(
    table_name: str,
    vector_size: int,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[langchain_google_cloud_sql_mysql.engine.Column] = [],
    metadata_json_column: str = "langchain_metadata",
    id_column: str = "langchain_id",
    overwrite_existing: bool = False,
    store_metadata: bool = True,
) -> None

Create a table for saving of vectors to be used with MySQLVectorStore.

Parameters
Name Description
table_name str

The MySQL database table name.

vector_size int

Vector size for the embedding model to be used.

content_column str

Name of the column to store document content. Default: page_content.

embedding_column str

Name of the column to store vector embeddings. Default: embedding.

metadata_columns List[Column]

A list of Columns to create for custom metadata. Default: []. Optional.

metadata_json_column str

The column to store extra metadata in JSON format. Default: langchain_metadata. Optional.

id_column str

Name of the column to store ids. Default: langchain_id. Optional,

overwrite_existing bool

Whether to drop existing table. Default: False.

store_metadata bool

Whether to store metadata in the table. Default: True.