Я использую следующий код Java для запуска SQL Script: < /p>
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
DataSource ds;
@Value("classpath:test.sql")
Resource resource;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
try {
@Cleanup
Connection c = ds.getConnection();
ScriptUtils.executeSqlScript(c, resource);
} catch (Exception e) {
e.printStackTrace();
}
}
}
< /code>
Версия Spring Boot, которую я использую, IS 2.1.7.Release, а PostgreSQL работает в локальном изображении Docker через: < /p>
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=PASSWORD -d -p 5432:5432 -v postgresql:/var/lib/postgresql/data postgres:11.3
< /code>
Скрипт SQL, который я использую,: < /p>
CREATE OR REPLACE FUNCTION totalRecords ()
RETURNS integer AS $total$
declare
total integer;
BEGIN
SELECT count(*) into total FROM COMPANY;
RETURN total;
END;
$total$ LANGUAGE plpgsql;
< /code>
, который, по моему мнению, совершенно действителен и работает OK в Pgadmin.Caused by: org.postgresql.util.PSQLException: Unterminated dollar quote started at position 62 in SQL CREATE OR REPLACE FUNCTION totalRecords () RETURNS integer AS $total$ declare total integer. Expected terminating $$
at org.postgresql.core.Parser.checkParsePosition(Parser.java:1274)
at org.postgresql.core.Parser.parseSql(Parser.java:1173)
at org.postgresql.core.Parser.replaceProcessing(Parser.java:1125)
at org.postgresql.core.CachedQueryCreateAction.create(CachedQueryCreateAction.java:41)
at org.postgresql.core.QueryExecutorBase.createQueryByKey(QueryExecutorBase.java:314)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:274)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:269)
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:488)
... 9 more
< /code>
Что не так? Как я могу это исправить, пожалуйста? Или как надежно запустить сценарий PostgreSQL с Java, если ScriptUtils не может этого сделать?
Подробнее здесь: https://stackoverflow.com/questions/578 ... postgresql