JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

Several create function statements are stuck in a single statement

Open JakkuSakura opened this issue 3 years ago • 1 comments

Describe the bug Several create function statements are stuck in a single statement

To Reproduce

  def parse(sql: String): Seq[AstNode] = {
    var collected = ArrayBuffer[AstNode]()

    val stmts = CCJSqlParserUtil.parseStatements(sql)
    stmts.getStatements.asScala.foreach {
      case table: CreateTable   => collected += parseCreateTable(table)
      case func: CreateFunction => collected += parseCreateFunction(func)
      case _                    =>
    }
    collected.toSeq
  }
CREATE OR REPLACE FUNCTION func_example(foo integer)
RETURNS integer AS $$
BEGIN
  RETURN foo + 1;
END
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION func_example2(IN foo integer, OUT bar integer)
AS $$
BEGIN
    SELECT foo + 1 INTO bar;
END
$$ LANGUAGE plpgsql;

image

Expected behavior

System

  • Database you are using: Postgres
  • Java Version JDK 1.8
  • JSqlParser version 4.3

JakkuSakura avatar Feb 18 '22 18:02 JakkuSakura

Thats right. Look into line 6248. JSqlParser does not parse the statements but reads all tokens from $$ to $$. Unfortunately whitespaces are skipped.

wumpz avatar Mar 15 '22 20:03 wumpz

Fixed this issue via commit https://github.com/JSQLParser/JSqlParser/pull/1778/commits/5263b91f3e555b79973e4cde68373760dfe4c998, functions/procedures can end now in two ways:

  1. <BEGIN> ... <END> ( ; | EOF)
  2. <$$> ... <$$> ... ( ; | EOF)

manticore-projects avatar May 18 '23 02:05 manticore-projects