Следующие четыре варианта темы могут использоваться для разделения строки ESQL.
Вместо того, чтобы добавлять множество параметров и использовать довольно сложную внутреннюю логику, я выбрал вариант использования имен функций вместо флагов.
SplitString
Не добавляет пустые строки, но добавляет строки с более чем одним пробелом.
CREATE PROCEDURE SplitString(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR CompositeString;
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
DECLARE Element CHAR SUBSTRING(Remainder BEFORE Delimiter);
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = SUBSTRING(Remainder AFTER Delimiter);
ELSE
DECLARE Element CHAR Remainder;
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = '';
END IF;
END WHILE;
END;
SplitStringTrim
Обрезает начальные и конечные пробелы из строк элемента.
Не добавляет пустые или пустые строки.
CREATE PROCEDURE SplitStringTrim(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR TRIM(CompositeString);
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
DECLARE Element CHAR TRIM(SUBSTRING(Remainder BEFORE Delimiter));
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = SUBSTRING(Remainder AFTER Delimiter;
ELSE
DECLARE Element CHAR TRIM(Remainder);
IF LENGTH(Element) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Element;
END IF;
SET Remainder = '';
END IF;
END WHILE;
END;
SplitStringAddEmpty
Добавляет пустые элементы в SplitterArray, обеспечивая наличие хотя бы одного элемента.
Пробелы сохраняются.
CREATE PROCEDURE SplitStringAddEmpty(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR CompositeString;
DECLARE EndsWithDelimiter BOOLEAN ENDSWITH(Remainder, Delimiter);
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
IF LENGTH(Remainder) = 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
ELSE
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE SUBSTRING(Remainder BEFORE Delimiter);
SET Remainder = SUBSTRING(Remainder AFTER Delimiter);
ELSE
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE Remainder;
SET Remainder = '';
END IF;
END WHILE;
IF EndsWithDelimiter THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
END IF;
END IF;
END;
SplitStringAddEmptyTrim
Добавить пустые элементы в SplitterArray, обеспечив наличие хотя бы одного элемента.
Обрезать начальные и конечные пробелы в строках Element.
CREATE PROCEDURE SplitStringAddEmptyTrim(
IN CompositeString CHAR, -- Composite string that needs to be split
IN Delimiter CHAR, -- Delimiter to be used when splitting the string
IN ArrayName CHAR, -- Name of the array for the results of the function
IN NewArray BOOLEAN, -- Use TRUE to clear a pre-existing array, FALSE appends new element
IN EnvRef REFERENCE -- Reference to Environment tree
)
BEGIN
IF NewArray THEN
DELETE FIELD EnvRef.SplitterArrays.{ArrayName};
END IF;
DECLARE Element CHAR;
DECLARE Remainder CHAR TRIM(CompositeString);
DECLARE EndsWithDelimiter BOOLEAN ENDSWITH(Remainder, Delimiter);
DECLARE SplitterArrayRef REFERENCE TO EnvRef.SplitterArrays.{ArrayName};
IF NOT LASTMOVE(SplitterArrayRef) THEN
CREATE LASTCHILD OF EnvRef.SplitterArrays AS SplitterArrayRef NAME ArrayName;
END IF;
IF LENGTH(Remainder) = 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
ELSE
WHILE LENGTH(Remainder) <> 0 DO
IF POSITION(Delimiter IN Remainder) > 0 THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE TRIM(SUBSTRING(Remainder BEFORE Delimiter));
SET Remainder = SUBSTRING(Remainder AFTER Delimiter);
ELSE
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE TRIM(Remainder);
SET Remainder = '';
END IF;
END WHILE;
IF EndsWithDelimiter THEN
CREATE LASTCHILD OF SplitterArrayRef NAME 'Element' VALUE '';
END IF;
END IF;
END;