createProcedure中的Liquibase标记错误

时间:2019-01-22 10:24:07

标签: postgresql liquibase

我正在使用liquibase创建函数,并试图在SQL中使用<运算符时出错。

SQL:

      <createProcedure>
        CREATE OR REPLACE FUNCTION function(dateFrom timestamp, dateTo timestamp ) 
        RETURNS TABLE
        LANGUAGE plpgsql
        AS $$
        BEGIN
        SELECT * FROM
        ORDER BY date DESC
        WHERE date >= $2 AND date <  $3
        RETURN QUERY;
        END;
        $$;
     </createProcedure>

错误:

  

元素的内容必须包含格式正确的字符数据或标记。

WHERE date >= $2 AND date < $3

1 个答案:

答案 0 :(得分:1)

XML不允许在标签的值内使用<>,除非您将该标签的内容包装到CDATA节中:

<createProcedure>
 <![CDATA[ 
    CREATE OR REPLACE FUNCTION function(dateFrom timestamp, dateTo timestamp ) 
    RETURNS TABLE
    LANGUAGE plpgsql
    AS $$
    BEGIN
    SELECT * FROM
    ORDER BY date DESC
    WHERE date >= $2 AND date <  $3
    RETURN QUERY;
    END;
    $$;
  ]]> 
</createProcedure>

与Liquibase问题无关:包装简单SQL查询的函数最好定义为SQL函数,以避免PL / pgSQL开销:

<createProcedure>
 <![CDATA[ 
    CREATE OR REPLACE FUNCTION function(dateFrom timestamp, dateTo timestamp ) 
    RETURNS TABLE (...)
    LANGUAGE sql
    AS $$
      SELECT * 
      FROM ...
      WHERE date >= $2 
        AND date <  $3
      ORDER BY date DESC
    $$;
  ]]> 
</createProcedure>