DatabaseHandler
class.
By providing the implementation for some or all of the methods contained in the DatabaseHandler
class, you can connect with the database of your choice.
__init__()
method, there are seven core methods that must be implemented. We recommend checking actual examples in the codebase to get an idea of what goes into each of these methods, as they can change a bit depending on the nature of the system being integrated.
Let’s review the purpose of each method.
Method | Purpose |
---|---|
connect() | It performs the necessary steps to connect to the underlying system. |
disconnect() | It gracefully closes connections established in the connect() method. |
check_connection() | It evaluates if the connection is alive and healthy. This method is called frequently. |
native_query() | It parses any native statement string and acts upon it (for example, raw SQL commands). |
query() | It takes a parsed SQL command in the form of an abstract syntax tree and executes it. |
get_tables() | It lists and returns all the available tables. Each handler decides what a table means for the underlying system when interacting with it from the data layer. Typically, these are actual tables. |
get_columns() | It returns columns of a table registered in the handler with the respective data type. |
mindsdb.integrations.libs.utils
library, contributors can find various methods that may be useful while implementing new handlers.Also, there are wrapper classes for the DatabaseHandler
instances called HandlerResponse and HandlerStatusResponse. You should use them to ensure proper output formatting.DatabaseHandler
class.
Here is a step-by-step guide:
name
class property:
MindsDB uses it internally as the name of the handler.
For example, the CREATE DATABASE
statement uses the handler’s name.
__init__()
method:
This method initializes the handler. The connection_data
argument contains the PARAMETERS
from the CREATE DATABASE
statement, such as user
, password
, etc.
connect()
method:
The connect()
method sets up the connection.
disconnect()
method:
The disconnect()
method closes the existing connection.
check_connection()
method:
The check_connection()
method performs the health check for the connection.
native_query()
method:
The native_query()
method runs commands of the native database language.
query()
method:
The query method runs parsed SQL commands.
get_tables()
method:
The get_tables()
method lists all the available tables.
get_columns()
method:
The get_columns()
method lists all columns of a specified table.
connection_args
Dictionaryconnection_args
dictionary contains all of the arguments used to establish the connection along with their descriptions, types, labels, and whether they are required or not.
Here is an example of the connection_args
dictionary from the MySQL handler.
connection_args_example
Dictionaryconnection_args_example
dictionary contains an example of all required arguments to establish the connection.
Here is an example of the connection_args_example
dictionary from the MySQL handler.
__init__.py
file of the handler:
Handler
class.version
of the handler.name
of the handler.type
of the handler, either DATA
handler or ML
handler.icon_path
to the file with the database icon.title
of the handler or a short description.description
of the handler.connection_args
dictionary with the connection arguments.connection_args_example
dictionary with an example of the connection arguments.import_error
message that is used if the import of the Handler
class fails.__about__.py
. This file is imported into the __init__.py
file.
Here is an example of the __init__.py
file for the MySQL handler.
__about__.py
file for the same MySQL handler contains the following variables: