PostgreSQL/JPA - Import functions in import.sql file?

后端 未结 1 1079
谎友^
谎友^ 2021-01-19 15:10

I\'m trying to define some PostgreSQL functions and triggers in my JPA import.sql file. I\'m using Hibernate 5.x as my underlying JPA provider. Since my

相关标签:
1条回答
  • 2021-01-19 15:59

    the problem with hibernate's default SqlStatementParser implementation, which is used in multiline sql command extractor.

    if you look at grammar definition hibernate-core/src/main/antlr/sql-stmt.g there is definition of Statement End:

    STMT_END
        : ';' ( '\t' | ' ' | '\r' | '\n' )*
        ;
    
    NOT_STMT_END
        : ~( ';' )
        ;
    

    This tells that statement end is semicolon symbol followed by "Space" "tab" "carret return" or "new line" symbols.

    THUS: DEFAULT IMPLEMENTATION IN HIBERNATE DOESN'T SUPPORT DOLLAR QUOTING.

    If you don't want to implement custom hibernate's parser you can rewrite all functions without dollar quoting, using simple ' quoting. But you will need to carefully escape ' chars.

    UPDATE: you can create your custom ImportSqlCommandExtractor. For example: separate your commands with --****** (6 star symbols in comment, just to make your file proper SQL file, but with custom command separation in comments, or choose any insane combination, which you like) and then split those in simple implementation

    public class ImportSqlCE implements ImportSqlCommandExtractor {
    
        private static final Logger log = LoggerFactory.getLogger(ImportSqlCE.class);
    
        @Override
        public String[] extractCommands(Reader reader) {
            try {
                String allCommands = IOUtils.toString(reader);
    
                return allCommands.split("--******");
            } catch (IOException e) {
                log.error("error reading import commands", e);
                log.info("sengind default empty command set");
                return new String[0];
            }
        }
    }
    

    and then configure hibernate to use it <property name="hibernate.hbm2ddl.import_files_sql_extractor" value="example.ImportSqlCE" />

    with this your import.sql will support dollar quoting (i.e. it will simply ignore any sql awareness of what is happening.)

    0 讨论(0)
提交回复
热议问题