public static String addSchemaToText ( String procedureText , String schemaName , String keywordBeforeName , Database database ) { if ( schemaName == null ) { return procedureText ; } if ( ( StringUtil . trimToNull ( schemaName ) != null ) && LiquibaseConfiguration . getInstance ( ) . getProperty ( ChangeLogParserCofiguration . class , ChangeLogParserCofiguration . USE_PROCEDURE_SCHEMA ) . getValue ( Boolean . class ) ) { StringClauses parsedSql = SqlParser . parse ( procedureText , true , true ) ; StringClauses . ClauseIterator clauseIterator = parsedSql . getClauseIterator ( ) ; Object next = "START" ; while ( ( next != null ) && ! next . toString ( ) . equalsIgnoreCase ( keywordBeforeName ) && clauseIterator . hasNext ( ) ) { if ( ! "PACKAGE" . equalsIgnoreCase ( keywordBeforeName ) && "PACKAGE" . equalsIgnoreCase ( ( String ) next ) ) { return procedureText ; } next = clauseIterator . nextNonWhitespace ( ) ; } if ( ( next != null ) && clauseIterator . hasNext ( ) ) { Object procNameClause = clauseIterator . nextNonWhitespace ( ) ; if ( procNameClause instanceof String ) { String [ ] nameParts = ( ( String ) procNameClause ) . split ( "\\." ) ; String finalName ; if ( nameParts . length == 1 ) { finalName = database . escapeObjectName ( schemaName , Schema . class ) + "." + nameParts [ 0 ] ; } else if ( nameParts . length == 2 ) { finalName = database . escapeObjectName ( schemaName , Schema . class ) + "." + nameParts [ 1 ] ; } else if ( nameParts . length == 3 ) { finalName = nameParts [ 0 ] + "." + database . escapeObjectName ( schemaName , Schema . class ) + "." + nameParts [ 2 ] ; } else { finalName = ( String ) procNameClause ; //just go with what was there } clauseIterator . replace ( finalName ) ; } procedureText = parsedSql . toString ( ) ; } } return procedureText ; }
Convenience method for other classes similar to this that want to be able to modify the procedure text to add the schema