Pipo
Pipo

Reputation: 5093

Eiffel: multilines string formatting

Didn't find a better way to format a multiline string than this way... seems complicated. What would be the best way to format this type of code?

l_qry := "SELECT%
    %    * %
    %FROM %
    %    enumerate %
    %WHERE %
    %    " + {like item_prototype}.Primary_key_db_column_name + " = " + l_category_id + " %
    %UNION %
    %   SELECT %
    %       e.* %
    %   FROM %
    %       enumerate e %
    %INNER JOIN  %
    %   enumerates_leaves s ON s." + {like item_prototype}.Primary_key_db_column_name + " = e." + {like item_prototype}.Category_db_column_name + " %
    %) SELECT * FROM enumerates_leaves WHERE enumerates_leaves." + {like item_prototype}.Category_db_column_name + " IS NOT NULL;"

Upvotes: 0

Views: 90

Answers (1)

javierv
javierv

Reputation: 159

You can use Verbatin Strings for that purpose.

sql_select_country : STRING = "[ 
         SELECT * 
         from COUNTRY
]"
        -- Select all country

For building SQL queries dynamically, you can define a template with placeholders and then replace them with the expected values.

template_query : STRING = "[
    SELECT
        * 
    FROM 
        enumerate 
    WHERE 
        $Primary_key_db_column_name  = :id 
    UNION 
       SELECT 
           e.* 
       FROM 
           enumerate e 
    INNER JOIN  
       enumerates_leaves s ON s.$Primary_key_db_column_name = e.$Category_db_column_name 
    ) SELECT * FROM enumerates_leaves WHERE enumerates_leaves.$Category_db_column_name IS NOT NULL;"
]"
    -- Template query `query_name` ....

Using the template

l_query: STRING

create l_query.make_from_string (template_query)
l_query.replace_substring_all ("$Primary_key_db_column_name", {like item_prototype}.Primary_key_db_column_name)
...

In fact, one can generalize this idea an build something like

sql_query_builder (query_template: READABLE_STRING_GENERAL; arguments: ITERABLE [READABLE_STRING_GENERAL]) :STRING

Upvotes: 1

Related Questions