Project

General

Profile

QFQ Merge » build_dynamic_columns.sql

Enis Nuredini, 24.06.2024 14:07

 
1
DELIMITER $$
2
CREATE DEFINER=`medtool_dev`@`localhost` FUNCTION `build_dynamic_columns`(`columnList` TEXT, `tableName` VARCHAR(128), `dbName` VARCHAR(128), `outputType` VARCHAR(128), `stringIdVariable` VARCHAR(128)) RETURNS text CHARSET utf8mb4 COLLATE utf8mb4_general_ci
3
    DETERMINISTIC
4
BEGIN
5
    DECLARE finished INTEGER DEFAULT 0;
6
    DECLARE columnName TEXT DEFAULT '';
7
    DECLARE constructedString TEXT DEFAULT '';
8
    DECLARE delim TEXT DEFAULT ',';
9
    DECLARE columnListCursor TEXT DEFAULT columnList;
10
    DECLARE tableName1 VARCHAR(128) DEFAULT tableName;
11
    DECLARE tableSchema VARCHAR(128) DEFAULT dbName;
12
    DECLARE currentPosition INT DEFAULT 1;
13
    DECLARE nextCommaPosition INT;
14
    DECLARE conditionValue VARCHAR(128) DEFAULT outputType;
15
    DECLARE idVariable VARCHAR(128) DEFAULT stringIdVariable;
16
    DECLARE columnNames TEXT DEFAULT '';
17
    DECLARE cur CURSOR FOR SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME = tableName1 AND TABLE_SCHEMA = tableSchema;
18
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
19

    
20
    IF columnListCursor = '*' THEN
21
        OPEN cur;
22
        read_loop: LOOP
23
            FETCH cur INTO columnName;
24
            IF finished THEN
25
                LEAVE read_loop;
26
            END IF;
27
            SET columnNames = CONCAT(columnNames, columnName, ',');
28
        END LOOP;
29
        CLOSE cur;
30
        SET columnListCursor = TRIM(TRAILING ',' FROM columnNames);
31
        SET finished = 0;
32
    END IF;
33

    
34
    -- Handling the list without whitespaces
35
    SET columnListCursor = REPLACE(columnListCursor, ' ', '');
36

    
37
    -- Loop through the comma-separated list
38
    WHILE NOT finished DO
39
        -- Find the next comma (or end of string if no more commas)
40
        SET nextCommaPosition = LOCATE(delim, columnListCursor, currentPosition);
41

    
42
        -- Extract the next column name
43
        IF nextCommaPosition = 0 THEN
44
            SET columnName = SUBSTRING(columnListCursor, currentPosition);
45
            SET finished = 1;
46
        ELSE
47
            SET columnName = SUBSTRING(columnListCursor, currentPosition, nextCommaPosition - currentPosition);
48
            SET currentPosition = nextCommaPosition + 1;
49
        END IF;
50

    
51
        -- Build the output string
52
        IF LENGTH(columnName) > 0 THEN
53
            IF LENGTH(conditionValue) > 0 AND conditionValue = 'columnSearch' THEN
54
                SET constructedString = CONCAT(constructedString, "IF(", columnName, "='{{", idVariable, "}}', CONCAT('", columnName, "', ','), ''), ");
55
            ELSEIF LENGTH(conditionValue) > 0 AND conditionValue = 'setQuery' THEN
56
                SET constructedString = CONCAT(constructedString, "`", columnName, "`='{{primaryId:S0}}', ");
57
            ELSE
58
                SET constructedString = CONCAT(constructedString, "'<b>", columnName, "</b>: ', IF(ISNULL(`", columnName, "`), '', IF(LENGTH(`", columnName, "`) > 100, QMORE(strip_tags(`", columnName, "`), 100), `", columnName, "`)), '<br>', ");
59
            END IF;
60
        END IF;
61
    END WHILE;
62

    
63
    -- Remove the last comma and space and "<br>"
64
    IF LENGTH(conditionValue) > 0 AND (conditionValue = 'columnSearch' OR conditionValue = 'setQuery') THEN
65
        SET constructedString = TRIM(TRAILING ', ' FROM constructedString);
66
    ELSE
67
        SET constructedString = TRIM(TRAILING ", '<br>', " FROM constructedString);
68
    END IF;
69

    
70
    IF LENGTH(conditionValue) > 0 AND conditionValue = 'columnSearch' THEN
71
        SET constructedString = CONCAT("TRIM(TRAILING ',' FROM CONCAT(", constructedString, "))");
72
    END IF;
73

    
74
    RETURN constructedString;
75
END$$
76
DELIMITER ;
(1-1/12)