Data Handler Class

The class has a few helper methods to aid in sending data between Microsoft SQL and Pandas data frames. The class uses the PandasSqlHandler class.

Methods

__init__

Assign the PanadaSqlHandler class.

_adjust_dataframe_types

Loop through the columns and set the convert the pandas data type to a data type that Microsoft SQL can support.

__sql_to_pandas_dtype

Convert Microsoft SQL datatypes to applicable Pandas data frame datatype.

get_table_columns

Filter columns are stored in the config / sql_column_config.yaml file. We may only want to show certain columns to the user.

Next we query the database and read the column meta data. Then we drop any rows that are not in the filter columns list.

Lastly we return a dictionary with the column name and the converted the data_type from Microsoft SQL data type to a Pandas datatype.

add_row

We check that the data is a Pandas data frame and there is only one data frame. If there is an issue we raise an error. The method does allow to insert multiple rows at the same time.

We use the first row to create a dictionary variable called row_dict. It will be used to create the placeholders.

Next we grab the list of column names and store them in the columns variable.

Next we create the placeholders, and join them together as a string. The placeholders are the values to insert into the table.

Next we will format the query string, inserting the table name, columns, and placeholders.

Lastly will execute the query.

update_row

Similiar to the add_row method, we will prepare the query and merge into a string and execute.

First create a dictionary update_data for all the columns and their applicable values to be updated.

Next due a conditional conversion on row_id so that it is converted to int 32 if an int 64 value.

Next create the string that will hold the column names and the updated value.

Next merge the update_data dictionary and row_id value into one dictionary that can be passed to the execute_query method.

Lastly execute the query.

delete_row

Create a query string to indicate which row to delete from the table.

First create the string merging the table_name and the row_id.

Execute the sql query using the execute_query method.

Next page will talk about the PandasSQLHandler Class