View Javadoc

1   package liquibase.ext.spatial.sqlgenerator;
2   
3   import java.util.Map.Entry;
4   
5   import liquibase.database.Database;
6   import liquibase.exception.ValidationErrors;
7   import liquibase.sql.Sql;
8   import liquibase.sqlgenerator.SqlGeneratorChain;
9   import liquibase.sqlgenerator.core.UpdateGenerator;
10  import liquibase.statement.core.UpdateStatement;
11  
12  /**
13   * Implementations of <code>AbstractSpatialInsertGenerator</code> convert a Well-Known Text string
14   * and EPSG SRID string to the database-specific geometry.
15   * 
16   * @author Lonny
17   */
18  public abstract class AbstractSpatialUpdateGenerator extends UpdateGenerator implements
19        WktInsertOrUpdateGenerator {
20     @Override
21     public int getPriority() {
22        return super.getPriority() + 1;
23     }
24  
25     @Override
26     public ValidationErrors validate(final UpdateStatement statement, final Database database,
27           final SqlGeneratorChain sqlGeneratorChain) {
28        return sqlGeneratorChain.validate(statement, database);
29     }
30  
31     /**
32      * Find any fields that look like WKT or EWKT and replace them with the database-specific value.
33      */
34     @Override
35     public Sql[] generateSql(final UpdateStatement statement, final Database database,
36           final SqlGeneratorChain sqlGeneratorChain) {
37        for (final Entry<String, Object> entry : statement.getNewColumnValues().entrySet()) {
38           entry.setValue(handleColumnValue(entry.getValue(), database));
39        }
40        return super.generateSql(statement, database, sqlGeneratorChain);
41     }
42  
43     /**
44      * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#looksLikeFunctionCall(java.lang.String,
45      *      liquibase.database.Database)
46      */
47     @Override
48     public boolean looksLikeFunctionCall(final String value, final Database database) {
49        final boolean result;
50        if (value.trim().toUpperCase().startsWith(getGeomFromWktFunction().trim().toUpperCase())) {
51           result = true;
52        } else {
53           result = super.looksLikeFunctionCall(value, database);
54        }
55        return result;
56     }
57  
58     /**
59      * Indicates if the SRID parameter is required in the function returned from
60      * {@link #getGeomFromWktFunction()}.
61      * 
62      * @param database
63      *           the database instance.
64      * 
65      * @return <code>true</code> if the SRID parameter is required in order to invoke the function.
66      */
67     @Override
68     public boolean isSridRequiredInFunction(final Database database) {
69        return false;
70     }
71  
72     /**
73      * If the old value is a geometry or a Well-Known Text, convert it to the appropriate new value.
74      * Otherwise, this method returns the old value.
75      * 
76      * @param oldValue
77      *           the old value.
78      * @param database
79      *           the database instance.
80      * 
81      * @return the new value.
82      */
83     protected Object handleColumnValue(final Object oldValue, final Database database) {
84        final Object newValue = WktConversionUtils.handleColumnValue(oldValue, database, this);
85        return newValue;
86     }
87  
88     /**
89      * @param wkt
90      *           the Well-Known Text string.
91      * @param srid
92      *           the SRID string which may be an empty string.
93      * @param database
94      *           the database instance.
95      * @return the string that converts the WKT to a database-specific geometry.
96      */
97     @Override
98     public String convertToFunction(final String wkt, final String srid, final Database database) {
99        final String function = WktConversionUtils.convertToFunction(wkt, srid, database, this);
100       return function;
101    }
102 }