The provided code is a method named `exportRecordSheet` that retrieves...
The provided code is a method named exportRecordSheet
that retrieves data from a database table and exports it to a spreadsheet (using the Apache POI library). Here's what the code does, step-by-step:
Key Functionality:
The purpose of this method is to create a new sheet in an Excel workbook (Workbook wb
) and populate it with data retrieved from a database table (tbl
), filtered by specified key-value conditions (keys
).
Step-by-Step Explanation:
-
Create a new sheet in the workbook:
- A new sheet is created using the
Workbook.createSheet
method, named after the sanitized value oftbl
(wheresanitize(tbl)
is presumably a method to ensure the table name is safe and valid for a sheet name).
- A new sheet is created using the
-
Early exit for empty keys:
- If the
keys
map is empty, the method writes a message to the first cell of the sheet (indicating no keys are available for the table) and then exits.
- If the
-
Retrieve column names from the database:
- A database metadata query (
getColumns
) is used to fetch all column names for the specified table (tbl
). - The column names are added to a list called
cols
.
- A database metadata query (
-
Filter the keys to match columns:
- For each key provided in the
keys
map, the method checks if it matches any of the columns (case-insensitive). - If a matching column is found, the key-value pair is added to a new
actual
map (used as filtering criteria). - If a key is provided that doesn't match any column, a warning is printed to the console.
- For each key provided in the
-
Early exit for no matching columns:
- If none of the provided keys match the table's columns, a message is written to the first cell of the sheet (indicating no matching columns) and the method exits.
-
Construct and log the SQL query:
- A
SELECT
query is constructed based on the table name and the filtered columns (actual
map). - Parameters in the SQL query are represented as
?
, and the query is logged to the console along with the parameter values.
- A
-
Prepare the SQL query with parameters:
- A
PreparedStatement
is created, and the values in theactual
map are bound to the query's parameters. - A
Map
of column data types (columnTypes
) is created by querying database metadata again, ensuring correct type-specific binding for each parameter:- Numeric types (
INTEGER
,BIGINT
,DOUBLE
, etc.) are parsed and bound accordingly. - Date and Timestamp types are expected in specific formats (
yyyy-mm-dd
for dates andyyyy-mm-dd hh:mm:ss
for timestamps). - If a timestamp is in ISO 8601 format (e.g.,
yyyy-mm-ddThh:mm:ssZ
), adjustments are made to convert it to SQL's expected format. - Defaults to binding the value as a string for other data types.
- Numeric types (
- A
-
Execute the query and populate the spreadsheet:
- The
PreparedStatement
is executed, and the resultingResultSet
(containing rows from the database) is iterated over:- Column names are written to column 0 of each row in the Excel sheet.
- Row data is added to the corresponding columns (one cell per value).
- If the result set is empty (no rows found), a message is written to the first cell of the sheet (indicating no data was found).
- The
-
Handle exceptions:
- The method declares
throws Exception
, meaning it will propagate any exceptions that occur (e.g., SQL errors, number format issues, etc.).
- The method declares
Summary:
This method queries data from a database table (tbl
) based on filtering criteria provided as key-value pairs (keys
). It validates the keys, fetches matching rows, and writes the columns and their values to an Excel sheet in a POI Workbook
. If no matching keys or rows are found, appropriate messages are written to the Excel sheet instead. The code also handles type-sensitive SQL parameter binding based on column metadata.