简体   繁体   English

如何在Spring bean中执行PL / pgSQL脚本?

[英]How to execute PL/pgSQL script in Spring bean?

I have Spring app and on every launch I need to check table existense. 我有Spring应用程序,每次启动时我都需要检查表existense。 Like this 像这样

@PostConstruct
    public void init() {

        DataSource auditDataSource = DataSourceBuilder.create().url(url).driverClassName(driver).username(username).password(password).build();
        this.jdbcTemplate = new NamedParameterJdbcTemplate(auditDataSource);

        Resource initSchema = new ClassPathResource("audit-data.sql");
        DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initSchema);
        try {
            DatabasePopulatorUtils.execute(databasePopulator, auditDataSource);
        } catch (ScriptException e) {
        }
    } 

Every launch I need to start PL SQL script Here its is 每次启动我都需要启动PL SQL脚本。

DO $$
DECLARE
columns_count INTEGER;
table_exists BOOLEAN;
BEGIN
table_exists = (SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'audit'));

IF (table_exists) THEN
columns_count = (SELECT COUNT(column_name)
FROM information_schema.columns
WHERE table_name='audit' and (
column_name='entry_da1te' or
column_name='payload' or
column_name='uid' or
column_name='type' or
column_name='session_id' or
column_name='user_uid' or
column_name='username'));
END IF;

IF (table_exists = false) OR (columns_count != 7) THEN
DROP TABLE IF EXISTS public.audit;
CREATE TABLE public.audit (
    entry_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    payload text,
    uid character varying(36),
    type character varying(1),
    session_id character varying(128),
    user_uid character varying(36),
    username character varying(1024)
);

ALTER TABLE public.audit OWNER TO postgres;
END IF;

END;
$$

After launch I got exception 发射后我得到了例外

Failed to execute SQL script statement #1 of class path resource [audit-data.sql]: DO $$ DECLARE columns_count INTEGER**Expected terminating $$

I tried to launch this script in POSTGRES pgAdmin 4. And everything is fine. 我试图在POSTGRES pgAdmin 4中启动此脚本。一切都很好。 But when I try to start my application - I got exception above. 但是,当我尝试启动我的应用程序时-上面出现了异常。

I solved this problem. 我解决了这个问题。 I placed DO part in commas ' and replaced " with double commas '' . Now it working fine. I'm not sure is there IDE type is important, but I used IDEA 2019.1.3 (not tested on Eclipse) Now code above looks like this 我将DO部分放在逗号中'并用"双逗号''代替。现在它可以正常工作。我不确定IDE类型是否很重要,但是我使用了IDEA 2019.1.3(未在Eclipse上测试),现在上面的代码看起来像像这样

DO '
DECLARE
columns_count INTEGER;
table_exists BOOLEAN;
BEGIN
table_exists = (SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = ''public'' AND table_name = ''audit''));

IF (table_exists) THEN
columns_count = (SELECT COUNT(column_name)
FROM information_schema.columns
WHERE table_name=''audit'' and (
column_name=''entry_date'' or
column_name=''payload'' or
column_name=''uid'' or
column_name=''type'' or
column_name=''session_id'' or
column_name=''user_uid'' or
column_name=''username''));
END IF;

IF (table_exists = false) OR (columns_count != 7) THEN
DROP TABLE IF EXISTS public.audit;
CREATE TABLE public.audit (
    entry_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    payload text,
    uid character varying(36),
    type character varying(1),
    session_id character varying(128),
    user_uid character varying(36),
    username character varying(1024)
);

ALTER TABLE public.audit OWNER TO postgres;
END IF;

END';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM