art
art

Reputation: 105

How to execute PL/pgSQL script in Spring bean?

I have Spring app and on every launch I need to check table 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

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. But when I try to start my application - I got exception above.

Upvotes: 2

Views: 523

Answers (1)

art
art

Reputation: 105

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 '
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';

Upvotes: 3

Related Questions