使用Apache Ant与PLPGSQL函数无法执行编程sql脚本 [英] Using Apache ANT to programmatically execute sql script with plpgsql function fails
问题描述
我需要以编程方式执行的SQL脚本。该脚本定义了升级依赖于它的版本PostgreSQL数据库的功能。它看起来是这样的:
创建或替换功能update_auto_increment(版本为varchar(20))返回整数
作为$$
宣布
v整数;
开始
五:= CAST(版本为int);
如果V族590则
- 一些升级code在这里
五:= 590;
提高的通知更新基础版本%',V;
万一;
返回伏;
结束;
$$语言PLPGSQL;选择信息,其中name ='版本'update_auto_increment(值);降功能update_auto_increment(VARCHAR(20));
然后我prepare一个Ant任务并执行它:
SQLEXEC任务=新SQLEXEC();
项目项目=新的项目();
project.init();
task.setProject(项目);
task.setTaskType(SQL);
task.setTaskName(SQL);
task.setSrc(新文件(updateScript.sql));
task.setDriver(驾驶员);
task.setPassword(密码);
task.setUserid(用户名);
task.setUrl(的ConnectionURL);
task.execute();
和当它遇到$$执行过程中失败:
org.postgresql.util.PSQLException:错误:语法错误(近:$)
位置:91
在org.apache.tools.ant.taskdefs.SQLExec.execute(SQLExec.java:672)
(错误信息是局部,我试图把它翻译成英文)。
所以,问题是:我怎么能编程在它执行与函数定义的SQL脚本
最有可能SQLEXEC由分裂的说法;
这是SQL默认的语句终止字符<。 / p>
在你的情况,分裂是不应该的。
您可以尝试使用这样的:
task.setDelimiterType(SQLExec.DelimiterType.ROW);
task.setDelimiter(/);
,然后在文件中单独的SQL语句 /
上一行:
创建或替换功能update_auto_increment(版本为varchar(20))返回整数
作为$$
宣布
v整数;
开始
五:= CAST(版本为int);
如果V族590则
- 一些升级code在这里
五:= 590;
提高的通知更新基础版本%',V;
万一;
返回伏;
结束;
$$语言PLPGSQL;
/选择信息update_auto_increment(值),其中name ='版本'
/降功能update_auto_increment(VARCHAR(20))
/
I need to execute a SQL script programmatically. The script defines a function which upgrades the postgreSQL database depending on its version. It looks like this:
create or replace function update_auto_increment(version varchar(20)) returns integer
as $$
declare
v integer;
begin
v := cast(version as int);
if v < 590 then
-- some upgrade code here
v := 590;
raise notice 'Updated base to version %', v;
end if;
return v;
end;
$$ language plpgsql;
select update_auto_increment(value) from info where name = 'version';
drop function update_auto_increment(varchar(20));
Then I prepare an ANT task and execute it:
SQLExec task = new SQLExec();
Project project = new Project();
project.init();
task.setProject(project);
task.setTaskType("sql");
task.setTaskName("sql");
task.setSrc(new File("updateScript.sql"));
task.setDriver(driver);
task.setPassword(password);
task.setUserid(username);
task.setUrl(connectionURL);
task.execute();
And the execution process fails when it encounters $$ :
org.postgresql.util.PSQLException: ERROR: syntax error (near: "$")
position: 91
at org.apache.tools.ant.taskdefs.SQLExec.execute(SQLExec.java:672)
(the error message was localized and I tried to translate it into English). So, the question is: how can I programmatically execute a SQL script with function definition in it?
Most probably SQLExec is splitting the statement by ;
which is the default statement termination character in SQL.
In your case, that splitting should not happen.
You might try to use something like this:
task.setDelimiterType(SQLExec.DelimiterType.ROW);
task.setDelimiter("/");
and then separate your SQL statements in the file with a /
on a single line:
create or replace function update_auto_increment(version varchar(20)) returns integer
as $$
declare
v integer;
begin
v := cast(version as int);
if v < 590 then
-- some upgrade code here
v := 590;
raise notice 'Updated base to version %', v;
end if;
return v;
end;
$$ language plpgsql;
/
select update_auto_increment(value) from info where name = 'version'
/
drop function update_auto_increment(varchar(20))
/
这篇关于使用Apache Ant与PLPGSQL函数无法执行编程sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!