使用JDBC创建PostgreSQL触发器

Fyn*_*ynn 5 scala jdbc plpgsql playframework-2.0 postgresql-9.1

我试图在Play2.0数据库演变脚本中创建PostgreSQL触发器.sql代码相对简单,在pgAdminIII中运行正常:

CREATE OR REPLACE FUNCTION update_modified() RETURNS TRIGGER AS $$
  BEGIN
    NEW.modified = now();
    RETURN NEW;
  END;
$$ LANGUAGE 'plpgsql';
Run Code Online (Sandbox Code Playgroud)

但是,在运行evolution时遇到错误:ERROR: unterminated dollar-quoted string at or near "$$ BEGIN NEW.modified = now()".SQL代码似乎在函数遇到的第一个分号处被截断.我正在为PostgreSQL使用"9.1-901.jdbc4"JDBC驱动程序.

更新:

Evolutions.scala(219+行)中的代码执行简单的拆分;.在框架本身似乎有缺陷:

// Execute script
s.sql.split(";").map(_.trim).foreach {
  case "" =>
  case statement => execute(statement)
}
Run Code Online (Sandbox Code Playgroud)

有解决方案吗

Bha*_*ani 2

您可以尝试以下操作。

    String sql = "CREATE OR REPLACE FUNCTION update_modified() RETURNS TRIGGER AS $$ " +
            "BEGIN " +
                "NEW.modified = now(); " +
                "RETURN NEW; " +
                "END; " +
            "$$ LANGUAGE 'plpgsql';";
    DataSource ds = getDataSource();
    try {
        Connection conn = ds.getConnection();
        conn.setAutoCommit(true);
        Statement st = conn.createStatement();
        st.executeUpdate(sql);
        st.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

希望这对你有用。