1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.PostgresDatabase; |
5 | |
import liquibase.exception.LiquibaseException; |
6 | |
import liquibase.sql.Sql; |
7 | |
import liquibase.sql.UnparsedSql; |
8 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
9 | |
import liquibase.statement.core.InsertOrUpdateStatement; |
10 | |
|
11 | 10 | public class InsertOrUpdateGeneratorPostgres extends InsertOrUpdateGenerator { |
12 | |
@Override |
13 | |
public boolean supports(InsertOrUpdateStatement statement, Database database) { |
14 | 0 | return database instanceof PostgresDatabase; |
15 | |
} |
16 | |
|
17 | |
@Override |
18 | |
public Sql[] generateSql(InsertOrUpdateStatement insertOrUpdateStatement, Database database, |
19 | |
SqlGeneratorChain sqlGeneratorChain) { |
20 | 0 | StringBuilder generatedSql = new StringBuilder(); |
21 | 0 | generatedSql.append("DO\n"); |
22 | 0 | generatedSql.append("$$\n"); |
23 | 0 | generatedSql.append("BEGIN\n"); |
24 | |
try { |
25 | 0 | generatedSql.append(getUpdateStatement(insertOrUpdateStatement, database, |
26 | |
getWhereClause(insertOrUpdateStatement, database), sqlGeneratorChain)); |
27 | 0 | } catch (LiquibaseException e) { |
28 | |
|
29 | 0 | generatedSql.append("select * from " |
30 | |
+ database.escapeTableName(insertOrUpdateStatement.getSchemaName(), |
31 | |
insertOrUpdateStatement.getTableName()) + " WHERE " |
32 | |
+ getWhereClause(insertOrUpdateStatement, database) + "\n"); |
33 | 0 | } |
34 | 0 | generatedSql.append("IF not found THEN\n"); |
35 | 0 | generatedSql.append(getInsertStatement(insertOrUpdateStatement, database, sqlGeneratorChain)); |
36 | 0 | generatedSql.append("END IF;\n"); |
37 | 0 | generatedSql.append("END;\n"); |
38 | 0 | generatedSql.append("$$\n"); |
39 | 0 | generatedSql.append("LANGUAGE plpgsql;\n"); |
40 | 0 | return new Sql[] { new UnparsedSql(generatedSql.toString()) }; |
41 | |
} |
42 | |
|
43 | |
@Override |
44 | |
protected String getElse(Database arg0) { |
45 | 0 | throw new UnsupportedOperationException(); |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
protected String getRecordCheck(InsertOrUpdateStatement arg0, Database arg1, String arg2) { |
50 | 0 | throw new UnsupportedOperationException(); |
51 | |
} |
52 | |
} |