diff --git a/api/src/main/java/org/openmrs/module/mambacore/api/MambaReportService.java b/api/src/main/java/org/openmrs/module/mambacore/api/MambaReportService.java index 0edb46d8..609113c5 100644 --- a/api/src/main/java/org/openmrs/module/mambacore/api/MambaReportService.java +++ b/api/src/main/java/org/openmrs/module/mambacore/api/MambaReportService.java @@ -39,4 +39,8 @@ public interface MambaReportService extends OpenmrsService { */ @Authorized({MambaReportsConstants.VIEW_MAMBA_REPORT}) List getMambaReportByCriteria(MambaReportCriteria criteria); + + + @Authorized({MambaReportsConstants.VIEW_MAMBA_REPORT}) + Integer getMambaReportSize(MambaReportCriteria criteria); } diff --git a/api/src/main/java/org/openmrs/module/mambacore/api/dao/MambaReportItemDao.java b/api/src/main/java/org/openmrs/module/mambacore/api/dao/MambaReportItemDao.java index 977f5a25..a192f00a 100644 --- a/api/src/main/java/org/openmrs/module/mambacore/api/dao/MambaReportItemDao.java +++ b/api/src/main/java/org/openmrs/module/mambacore/api/dao/MambaReportItemDao.java @@ -32,4 +32,11 @@ public interface MambaReportItemDao { */ List getMambaReport(MambaReportCriteria criteria); + /** + * Get the Total records (size) of this report + * + * @param criteria to follow + * @return total records + */ + Integer getMambaReportSize(MambaReportCriteria criteria); } diff --git a/api/src/main/java/org/openmrs/module/mambacore/api/dao/impl/JdbcMambaReportItemDao.java b/api/src/main/java/org/openmrs/module/mambacore/api/dao/impl/JdbcMambaReportItemDao.java index fe6dc925..cb203872 100644 --- a/api/src/main/java/org/openmrs/module/mambacore/api/dao/impl/JdbcMambaReportItemDao.java +++ b/api/src/main/java/org/openmrs/module/mambacore/api/dao/impl/JdbcMambaReportItemDao.java @@ -14,6 +14,7 @@ import org.openmrs.module.mambacore.api.model.MambaReportItem; import org.openmrs.module.mambacore.api.model.MambaReportItemColumn; import org.openmrs.module.mambacore.api.parameter.MambaReportCriteria; +import org.openmrs.module.mambacore.api.parameter.MambaReportSearchField; import org.openmrs.module.mambacore.db.ConnectionPoolManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,11 +35,21 @@ public List getMambaReport(String mambaReportId) { @Override public List getMambaReport(MambaReportCriteria criteria) { + + Integer pageNumber = criteria.getPageNumber(); + Integer pageSize = criteria.getPageSize(); + String argumentsJson = ""; try { + ObjectMapper objectMapper = new ObjectMapper(); + + criteria.getSearchFields().add(new MambaReportSearchField("page_number", String.valueOf(pageNumber))); + criteria.getSearchFields().add(new MambaReportSearchField("page_size", String.valueOf(pageSize))); + argumentsJson = objectMapper.writeValueAsString(criteria.getSearchFields()); log.debug("Query arguments {}", argumentsJson); + } catch (Exception exc) { log.error("Failed to get MambaReport", exc); } @@ -50,6 +61,7 @@ public List getMambaReport(MambaReportCriteria criteria) { .getInstance() .getEtlDataSource(); + try (Connection connection = dataSource.getConnection(); CallableStatement statement = connection.prepareCall("{CALL sp_mamba_get_report_column_names(?)}")) { @@ -72,6 +84,8 @@ public List getMambaReport(MambaReportCriteria criteria) { try (Connection connection = dataSource.getConnection(); CallableStatement statement = connection.prepareCall("{CALL sp_mamba_generate_report_wrapper(?, ?, ?)}")) { + System.out.println("Query arguments here: " + argumentsJson); + statement.setInt("generate_columns_flag", 0); statement.setString("report_identifier", criteria.getReportId()); statement.setString("parameter_list", argumentsJson); @@ -118,4 +132,43 @@ public List getMambaReport(MambaReportCriteria criteria) { } return mambaReportItems; } + + @Override + public Integer getMambaReportSize(MambaReportCriteria criteria) { + String argumentsJson = ""; + try { + + ObjectMapper objectMapper = new ObjectMapper(); + + argumentsJson = objectMapper.writeValueAsString(criteria.getSearchFields()); + log.debug("Query arguments {}", argumentsJson); + + } catch (Exception exc) { + log.error("Failed to get MambaReport", exc); + } + + DataSource dataSource = ConnectionPoolManager + .getInstance() + .getEtlDataSource(); + + try (Connection connection = dataSource.getConnection(); + CallableStatement statement = connection.prepareCall("{CALL sp_mamba_generate_report_size_sp_wrapper(?, ?)}")) { + + statement.setString("report_identifier", criteria.getReportId()); + statement.setString("parameter_list", argumentsJson); + + System.out.println("Query report_identifier: " + criteria.getReportId()); + System.out.println("Query arguments: " + argumentsJson); + + if (statement.execute()) { + ResultSet resultSet = statement.getResultSet(); + if (resultSet.next()) { // Move to the first row + return resultSet.getInt(1); + } + } + } catch (SQLException e) { + log.error("Failed to get MambaReport", e); + } + return 0; + } } diff --git a/api/src/main/java/org/openmrs/module/mambacore/api/impl/MambaReportServiceImpl.java b/api/src/main/java/org/openmrs/module/mambacore/api/impl/MambaReportServiceImpl.java index 3bbcf953..d4bb1cbd 100644 --- a/api/src/main/java/org/openmrs/module/mambacore/api/impl/MambaReportServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/mambacore/api/impl/MambaReportServiceImpl.java @@ -36,4 +36,9 @@ public List getMambaReport(String mambaReportId) { public List getMambaReportByCriteria(MambaReportCriteria criteria) { return dao.getMambaReport(criteria); } + + @Override + public Integer getMambaReportSize(MambaReportCriteria criteria) { + return dao.getMambaReportSize(criteria); + } } diff --git a/api/src/main/java/org/openmrs/module/mambacore/api/model/MambaReportPagination.java b/api/src/main/java/org/openmrs/module/mambacore/api/model/MambaReportPagination.java new file mode 100644 index 00000000..f8df7bbb --- /dev/null +++ b/api/src/main/java/org/openmrs/module/mambacore/api/model/MambaReportPagination.java @@ -0,0 +1,68 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + *

+ * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.mambacore.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; + +public class MambaReportPagination implements Serializable { + + private static final long serialVersionUID = -5805274582315599409L; + + @JsonProperty("page_number") + private Integer pageNumber; + + @JsonProperty("page_size") + private Integer pageSize; + + @JsonProperty("total_pages") + private Integer totalPages; + + @JsonProperty("total_records") + private Integer totalRecords; + + public MambaReportPagination() { + this.pageNumber = 1; + this.pageSize = 50; + } + + public Integer getPageNumber() { + return pageNumber; + } + + public void setPageNumber(Integer pageNumber) { + this.pageNumber = pageNumber; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + public Integer getTotalPages() { + return totalPages; + } + + public void setTotalPages(Integer totalPages) { + this.totalPages = totalPages; + } + + public Integer getTotalRecords() { + return totalRecords; + } + + public void setTotalRecords(Integer totalRecords) { + this.totalRecords = totalRecords; + } +} diff --git a/api/src/main/java/org/openmrs/module/mambacore/api/parameter/MambaReportCriteria.java b/api/src/main/java/org/openmrs/module/mambacore/api/parameter/MambaReportCriteria.java index 7bc23ad8..e8536914 100644 --- a/api/src/main/java/org/openmrs/module/mambacore/api/parameter/MambaReportCriteria.java +++ b/api/src/main/java/org/openmrs/module/mambacore/api/parameter/MambaReportCriteria.java @@ -17,11 +17,17 @@ public class MambaReportCriteria implements Serializable { - private static final long serialVersionUID = 6717202824335189575L; + private static final long serialVersionUID = 6727202844335189575L; @JsonProperty("report_id") private String reportId; + @JsonProperty("page_number") + private Integer pageNumber = 1; + + @JsonProperty("page_size") + private Integer pageSize = 50; + @JsonProperty("arguments") private List searchFields = new ArrayList<>(); @@ -48,4 +54,20 @@ public List getSearchFields() { public void setSearchFields(List searchFields) { this.searchFields = searchFields; } -} + + public Integer getPageNumber() { + return pageNumber; + } + + public void setPageNumber(Integer pageNumber) { + this.pageNumber = pageNumber; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } +} \ No newline at end of file diff --git a/api/src/main/java/org/openmrs/module/mambacore/web/resource/MambaReportResource.java b/api/src/main/java/org/openmrs/module/mambacore/web/resource/MambaReportResource.java index 7a6670c5..ee5a93a1 100644 --- a/api/src/main/java/org/openmrs/module/mambacore/web/resource/MambaReportResource.java +++ b/api/src/main/java/org/openmrs/module/mambacore/web/resource/MambaReportResource.java @@ -9,9 +9,12 @@ */ package org.openmrs.module.mambacore.web.resource; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.openmrs.api.context.Context; import org.openmrs.module.mambacore.api.MambaReportService; import org.openmrs.module.mambacore.api.model.MambaReportItem; +import org.openmrs.module.mambacore.api.model.MambaReportPagination; import org.openmrs.module.mambacore.api.parameter.MambaReportCriteria; import org.openmrs.module.mambacore.api.parameter.MambaReportSearchField; import org.openmrs.module.mambacore.web.controller.MambaReportRestController; @@ -46,6 +49,8 @@ public SimpleObject search(RequestContext context) throws ResponseException { context.setStartIndex(0); MambaReportCriteria searchCriteria = new MambaReportCriteria(); + Integer pageSize = searchCriteria.getPageSize(); //defaults + Integer pageNumber = searchCriteria.getPageNumber(); //defaults Enumeration parameterNames = context.getRequest().getParameterNames(); while (parameterNames.hasMoreElements()) { @@ -55,10 +60,21 @@ public SimpleObject search(RequestContext context) throws ResponseException { log.debug("search API hit with param: {} vale: {}", paramName, paramValue); - if (paramName.equals("report_id")) { - searchCriteria.setReportId(paramValue); - } else { - searchCriteria.getSearchFields().add(new MambaReportSearchField(paramName, paramValue)); + switch (paramName) { + case "report_id": + searchCriteria.setReportId(paramValue); + break; + case "page_number": + pageNumber = Integer.parseInt(paramValue); + searchCriteria.setPageNumber(pageNumber); + break; + case "page_size": + pageSize = Integer.parseInt(paramValue); + searchCriteria.setPageSize(pageSize); + break; + default: + searchCriteria.getSearchFields().add(new MambaReportSearchField(paramName, paramValue)); + break; } } @@ -66,8 +82,37 @@ public SimpleObject search(RequestContext context) throws ResponseException { return new EmptySearchResult().toSimpleObject(null); } - List mambaReportItems = getService().getMambaReportByCriteria(searchCriteria); - return new SimpleObject().add("results", mambaReportItems); + ObjectMapper objectMapper = new ObjectMapper(); + + //TODO: Delete after Testing + try { + String argumentsJson = objectMapper.writeValueAsString(searchCriteria); + log.debug("Mamba search criteria before passing it over: {}", argumentsJson); + System.out.println("Mamba search criteria before passing it over sysout: " + argumentsJson); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + + List mambaReportItems = getService() + .getMambaReportByCriteria(searchCriteria); + + Integer totalRecords = getService() + .getMambaReportSize(searchCriteria); + + System.out.println("Total records: " + totalRecords); + System.out.println("fetched records: " + mambaReportItems.size()); + + Integer totalPages = (int) Math.ceil((double) totalRecords / pageSize); + + MambaReportPagination pagination = new MambaReportPagination(); + pagination.setPageNumber(pageNumber); + pagination.setPageSize(pageSize); + pagination.setTotalRecords(totalRecords); + pagination.setTotalPages(totalPages); + + return new SimpleObject() + .add("results", mambaReportItems) + .add("pagination", pagination); } @Override diff --git a/api/src/main/resources/_core/compiler/linux/compile-mysql.sh b/api/src/main/resources/_core/compiler/linux/compile-mysql.sh index 38763360..e50164ba 100755 --- a/api/src/main/resources/_core/compiler/linux/compile-mysql.sh +++ b/api/src/main/resources/_core/compiler/linux/compile-mysql.sh @@ -332,40 +332,68 @@ function read_config_report_definition_metadata() { # Get the total number of report_definitions total_reports=$(jq '.report_definitions | length' <<< "$json_string") + # Clear the variable before the loop + create_report_procedure="" + # Iterate through each report_definition for ((i = 0; i < total_reports; i++)); do reportId=$(jq -r ".report_definitions[$i].report_id" <<< "$json_string") + paginate_flag=$(jq ".report_definitions[$i].report_sql.paginate" <<< "$json_string") # Check if true report_procedure_name="sp_mamba_report_${reportId}_query" report_columns_procedure_name="sp_mamba_report_${reportId}_columns_query" + report_size_procedure_name="sp_mamba_report_${reportId}_size_query" # Name for the count SP report_columns_table_name="mamba_report_$reportId" sql_query=$(jq -r ".report_definitions[$i].report_sql.sql_query" <<< "$json_string") # echo "SQL Query: $sql_query" - # Iterate through query_params and save values before printing - query_params=$(jq -c ".report_definitions[$i].report_sql.query_params[] | select(length > 0) | {name, type}" <<< "$json_string") - in_parameters="" + # --- Generate IN parameters strings --- + # 1. Parameters EXCLUDING pagination (for count query) + in_query_parameters="" + query_params_list=$(jq -c ".report_definitions[$i].report_sql.query_params[]? | select(length > 0) | {name, type}" <<< "$json_string") # Added ? for optional query_params while IFS= read -r entry; do queryName=$(jq -r '.name' <<< "$entry") queryType=$(jq -r '.type' <<< "$entry") - - # Check if queryName and queryType are not null or empty before concatenating if [[ -n "$queryName" && -n "$queryType" ]]; then - in_parameters+="IN $queryName $queryType, " + in_query_parameters+="IN $queryName $queryType, " fi - done <<< "$query_params" - - # Remove trailing comma - in_parameters="${in_parameters%, }" - - # Print concatenated pairs if there are any - #if [ -n "$in_parameters" ]; then - # echo "Query Params: $in_parameters" - #fi + done <<< "$query_params_list" + in_query_parameters="${in_query_parameters%, }" # Remove trailing comma + + # 2. Parameters INCLUDING pagination if needed (for main/columns query) + in_parameters_with_pagination="$in_query_parameters" + if [[ "$paginate_flag" == "true" ]]; then + if [[ -n "$in_parameters_with_pagination" ]]; then + in_parameters_with_pagination+=", " # Add comma if other params exist + fi + # Add the standard pagination parameters + in_parameters_with_pagination+="IN page_number INT, IN page_size INT" + fi + # --- End Parameter Generation --- + + # --- Generate Main Report SP Definition --- + main_sp_body="" + main_sp_params="" + + if [[ "$paginate_flag" == "true" ]]; then + # For paginated reports, add offset calculation and LIMIT/OFFSET clause + main_sp_params="$in_parameters_with_pagination" + main_sp_body=" + DECLARE offset_val INT; + SET offset_val = (page_number - 1) * page_size; + -- Original query modified with LIMIT/OFFSET using calculated variable + ${sql_query} LIMIT page_size OFFSET offset_val;" + else + # For non-paginated reports, use base query and non-pagination params + main_sp_params="$in_query_parameters" + main_sp_body=" + -- Original query (non-paginated) + $sql_query;" + fi -create_report_procedure+=" + create_report_procedure+=" -- --------------------------------------------------------------------------------------------- -- ---------------------- $report_procedure_name ---------------------------- @@ -375,18 +403,22 @@ DROP PROCEDURE IF EXISTS $report_procedure_name; DELIMITER // -CREATE PROCEDURE $report_procedure_name($in_parameters) +-- Parameters vary based on pagination flag +CREATE PROCEDURE $report_procedure_name($main_sp_params) BEGIN -$sql_query; +${main_sp_body} END // DELIMITER ; " + # --- End Main Report SP --- -create_report_procedure+=" + # --- Generate Columns Report SP Definition --- + # Columns SP *always* uses LIMIT 0 internally, but accepts pagination params if needed for signature consistency + create_report_procedure+=" -- --------------------------------------------------------------------------------------------- -- ---------------------- $report_columns_procedure_name ---------------------------- @@ -396,32 +428,94 @@ DROP PROCEDURE IF EXISTS $report_columns_procedure_name; DELIMITER // -CREATE PROCEDURE $report_columns_procedure_name($in_parameters) +-- Accepts pagination params if main report is paginated (for signature consistency) +CREATE PROCEDURE $report_columns_procedure_name($in_parameters_with_pagination) -- Uses the full param list including pagination if applicable BEGIN -- Create Table to store report column names with no rows DROP TABLE IF EXISTS $report_columns_table_name; + +-- Use original query but *always* force LIMIT 0 to get structure without data CREATE TABLE $report_columns_table_name AS -$sql_query +${sql_query} -- Base query from JSON LIMIT 0; --- Select report column names from Table +-- Select report column names from the temporary table structure +-- Using information_schema might be slightly more robust if CREATE TABLE AS has issues SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ', ') INTO @column_names FROM INFORMATION_SCHEMA.COLUMNS -WHERE TABLE_NAME = '$report_columns_table_name'; +WHERE TABLE_SCHEMA = DATABASE() -- Use current database + AND TABLE_NAME = '$report_columns_table_name'; --- Update Table with report column names -UPDATE mamba_dim_report_definition -SET result_column_names = @column_names -WHERE report_id='$reportId'; +-- Update Table definition with report column names (optional, depends on your usage) +-- UPDATE mamba_dim_report_definition +-- SET result_column_names = @column_names +-- WHERE report_id='$reportId'; + +-- Return the column names as a result set +SELECT @column_names AS column_names; + +-- Clean up the temporary table +DROP TABLE IF EXISTS $report_columns_table_name; END // DELIMITER ; " - done + # --- End Columns Report SP --- + + # --- Generate Count Query from Original SQL Query --- + # Start with the original query + count_query_temp="$sql_query" + + # Remove ORDER BY clause first (if it exists at the end, case-insensitive) + count_query_temp=$(echo "$count_query_temp" | sed -E 's/ORDER BY +.*$//I') + + # Remove LIMIT ... OFFSET ... clause (if it exists at the end, case-insensitive) + count_query_temp=$(echo "$count_query_temp" | sed -E 's/LIMIT +.* +OFFSET +.*$//I') + + # Remove LIMIT ... clause (if it exists at the end, case-insensitive) + count_query_temp=$(echo "$count_query_temp" | sed -E 's/LIMIT +.*$//I') + + # Trim trailing whitespace that might be left after removals + count_query_temp=$(echo "$count_query_temp" | sed -E 's/[[:space:]]*$//') + + # Replace the initial SELECT clause with SELECT COUNT(*) (case-insensitive, greedy) + # This assumes the structure SELECT ... FROM ... + count_query=$(echo "$count_query_temp" | sed -E 's/^SELECT.*FROM/SELECT COUNT(*) FROM/I') + # --- End Count Query Generation --- + + # --- Generate Size/Count Report SP Definition --- + # Only generate if paginate is true and the count query could be constructed + if [[ -n "$count_query" && "$paginate_flag" == "true" ]]; then + create_report_procedure+=" +-- --------------------------------------------------------------------------------------------- +-- ---------------------- $report_size_procedure_name ---------------------------- +-- --------------------------------------------------------------------------------------------- + +DROP PROCEDURE IF EXISTS $report_size_procedure_name; + +DELIMITER // + +-- Uses only non-pagination query parameters +CREATE PROCEDURE $report_size_procedure_name($in_query_parameters) +BEGIN + +-- This query counts the total records based on the main query's criteria, +-- excluding pagination (LIMIT/OFFSET) and ordering (ORDER BY). +$count_query; + +END // + +DELIMITER ; + +" + fi + # --- End Size/Count Report SP --- + + done # End of the loop iterating through report_definitions # Now Read in the contents for the Mysql Part - to insert into Tables if [ ! -z "$FILENAME" ] && [ -f "$FILENAME" ]; then diff --git a/api/src/main/resources/_core/database/mysql/sp_makefile b/api/src/main/resources/_core/database/mysql/sp_makefile index 14ba9fdc..29477c7b 100644 --- a/api/src/main/resources/_core/database/mysql/sp_makefile +++ b/api/src/main/resources/_core/database/mysql/sp_makefile @@ -61,6 +61,7 @@ stored-procedures/sp_mamba_locale_insert_helper.sql xf_system/etl_report/sp_mamba_extract_report_column_names.sql xf_system/etl_report/sp_mamba_extract_report_definition_metadata.sql xf_system/etl_report/sp_mamba_generate_report_wrapper.sql +xf_system/etl_report/sp_mamba_generate_report_size_sp_wrapper.sql xf_system/etl_report/sp_mamba_get_report_column_names.sql #xf_system/etl_report/p_mamba_generate_report_columns.sql diff --git a/api/src/main/resources/_core/database/mysql/xf_system/etl_report/report_definition/sp_mamba_dim_report_definition_create.sql b/api/src/main/resources/_core/database/mysql/xf_system/etl_report/report_definition/sp_mamba_dim_report_definition_create.sql index 788d4f96..2184179a 100644 --- a/api/src/main/resources/_core/database/mysql/xf_system/etl_report/report_definition/sp_mamba_dim_report_definition_create.sql +++ b/api/src/main/resources/_core/database/mysql/xf_system/etl_report/report_definition/sp_mamba_dim_report_definition_create.sql @@ -6,6 +6,7 @@ CREATE TABLE mamba_dim_report_definition report_id VARCHAR(255) NOT NULL UNIQUE, report_procedure_name VARCHAR(255) NOT NULL UNIQUE, -- should be derived from report_id?? report_columns_procedure_name VARCHAR(255) NOT NULL UNIQUE, + report_size_procedure_name VARCHAR(255) NULL UNIQUE, sql_query TEXT NOT NULL, table_name VARCHAR(255) NOT NULL, -- name of the table (will contain columns) of this query report_name VARCHAR(255) NULL, diff --git a/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_extract_report_definition_metadata.sql b/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_extract_report_definition_metadata.sql index 04050cff..e532bbb8 100644 --- a/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_extract_report_definition_metadata.sql +++ b/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_extract_report_definition_metadata.sql @@ -25,21 +25,27 @@ BEGIN SELECT JSON_EXTRACT(@report_array, CONCAT('$[', @report_count, ']')) INTO @report; SELECT JSON_UNQUOTE(JSON_EXTRACT(@report, '$.report_name')) INTO @report_name; SELECT JSON_UNQUOTE(JSON_EXTRACT(@report, '$.report_id')) INTO @report_id; + SELECT CONCAT('sp_mamba_report_', @report_id, '_query') INTO @report_procedure_name; SELECT CONCAT('sp_mamba_report_', @report_id, '_columns_query') INTO @report_columns_procedure_name; + SELECT CONCAT('sp_mamba_report_', @report_id, '_size_query') INTO @report_size_procedure_name; SELECT CONCAT('mamba_report_', @report_id) INTO @table_name; + SELECT JSON_UNQUOTE(JSON_EXTRACT(@report, CONCAT('$.report_sql.sql_query'))) INTO @sql_query; SELECT JSON_EXTRACT(@report, CONCAT('$.report_sql.query_params')) INTO @query_params_array; + SELECT JSON_EXTRACT(@report, CONCAT('$.report_sql.paginate')) INTO @paginate_flag; INSERT INTO mamba_dim_report_definition(report_id, report_procedure_name, report_columns_procedure_name, + report_size_procedure_name, sql_query, table_name, report_name) VALUES (@report_id, @report_procedure_name, @report_columns_procedure_name, + @report_size_procedure_name, @sql_query, @table_name, @report_name); @@ -68,50 +74,31 @@ BEGIN SET @param_count = @param_position; END WHILE; + -- Handle pagination parameters if paginate flag is true + IF @paginate_flag = TRUE OR @paginate_flag = 'true' THEN + -- Add page_number parameter + SET @page_number_position = @total_params + 1; + INSERT INTO mamba_dim_report_definition_parameters(report_id, + parameter_name, + parameter_type, + parameter_position) + VALUES (@report_id, + 'page_number', + 'INT', + @page_number_position); + + -- Add page_size parameter + SET @page_size_position = @total_params + 2; + INSERT INTO mamba_dim_report_definition_parameters(report_id, + parameter_name, + parameter_type, + parameter_position) + VALUES (@report_id, + 'page_size', + 'INT', + @page_size_position); + END IF; --- SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ', ') --- INTO @column_names --- FROM INFORMATION_SCHEMA.COLUMNS --- -- WHERE TABLE_SCHEMA = 'alive' TODO: add back after verifying schema name --- WHERE TABLE_NAME = @report_id; --- --- SET @drop_table = CONCAT('DROP TABLE IF EXISTS `', @report_id, '`'); --- --- SET @createtb = CONCAT('CREATE TEMP TABLE AS SELECT ', @report_id, ';', CHAR(10), --- 'CREATE PROCEDURE ', @report_procedure_name, '(', CHAR(10), --- @parameters, CHAR(10), --- ')', CHAR(10), --- 'BEGIN', CHAR(10), --- @sql_query, CHAR(10), --- 'END;', CHAR(10)); --- --- PREPARE deletetb FROM @drop_table; --- PREPARE createtb FROM @create_table; --- --- EXECUTE deletetb; --- EXECUTE createtb; --- --- DEALLOCATE PREPARE deletetb; --- DEALLOCATE PREPARE createtb; - - -- SELECT GROUP_CONCAT(CONCAT('IN ', parameter_name, ' ', parameter_type) SEPARATOR ', ') --- INTO @parameters --- FROM mamba_dim_report_definition_parameters --- WHERE report_id = @report_id --- ORDER BY parameter_position; --- --- SET @procedure_definition = CONCAT('DROP PROCEDURE IF EXISTS ', @report_procedure_name, ';', CHAR(10), --- 'CREATE PROCEDURE ', @report_procedure_name, '(', CHAR(10), --- @parameters, CHAR(10), --- ')', CHAR(10), --- 'BEGIN', CHAR(10), --- @sql_query, CHAR(10), --- 'END;', CHAR(10)); --- --- PREPARE CREATE_PROC FROM @procedure_definition; --- EXECUTE CREATE_PROC; --- DEALLOCATE PREPARE CREATE_PROC; --- SET @report_count = @report_count + 1; END WHILE; diff --git a/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_generate_report_size_sp_wrapper.sql b/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_generate_report_size_sp_wrapper.sql new file mode 100644 index 00000000..ce097cd2 --- /dev/null +++ b/api/src/main/resources/_core/database/mysql/xf_system/etl_report/sp_mamba_generate_report_size_sp_wrapper.sql @@ -0,0 +1,58 @@ +DROP PROCEDURE IF EXISTS sp_mamba_generate_report_size_sp_wrapper; + +DELIMITER // + +CREATE PROCEDURE sp_mamba_generate_report_size_sp_wrapper( + IN report_identifier VARCHAR(255), + IN parameter_list JSON) +BEGIN + + DECLARE proc_name VARCHAR(255); + DECLARE sql_args VARCHAR(1000); + DECLARE arg_name VARCHAR(50); + DECLARE arg_value VARCHAR(255); + DECLARE tester VARCHAR(255); + DECLARE done INT DEFAULT FALSE; + + DECLARE cursor_parameter_names CURSOR FOR + SELECT DISTINCT (p.parameter_name) + FROM mamba_dim_report_definition_parameters p + WHERE p.report_id = report_identifier + AND p.parameter_name NOT IN ('page_number', 'page_size'); + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + SET proc_name = (SELECT DISTINCT (rd.report_size_procedure_name) + FROM mamba_dim_report_definition rd + WHERE rd.report_id = report_identifier); + + OPEN cursor_parameter_names; + read_loop: + LOOP + FETCH cursor_parameter_names INTO arg_name; + + IF done THEN + LEAVE read_loop; + END IF; + + SET arg_value = IFNULL((JSON_EXTRACT(parameter_list, CONCAT('$[', ((SELECT p.parameter_position + FROM mamba_dim_report_definition_parameters p + WHERE p.parameter_name = arg_name + AND p.report_id = report_identifier) - 1), + '].value'))), 'NULL'); + SET tester = CONCAT_WS(', ', tester, arg_value); + SET sql_args = IFNULL(CONCAT_WS(', ', sql_args, arg_value), NULL); + + END LOOP; + + CLOSE cursor_parameter_names; + + SET @sql = CONCAT('CALL ', proc_name, '(', IFNULL(sql_args, ''), ')'); + + PREPARE stmt FROM @sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + +END // + +DELIMITER ; \ No newline at end of file