Revision 597

View differences:

tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2

  
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4

  
5
  <modelVersion>4.0.0</modelVersion>
6
  <artifactId>org.gvsig.postgresql.provider</artifactId>
7
  <packaging>jar</packaging>
8
  <name>${project.artifactId}</name>
9
  <description>DAL provider for PostgeSQL files</description>
10
  <parent>
11
    <groupId>org.gvsig</groupId>
12
    <artifactId>org.gvsig.postgresql</artifactId>
13
    <version>2.0.119</version>
14
  </parent>
15
  <dependencies>
16
    <dependency>
17
      <groupId>com.googlecode.log4jdbc</groupId>
18
      <artifactId>log4jdbc</artifactId>
19
      <scope>compile</scope>
20
    </dependency>
21
    <dependency>
22
      <groupId>org.gvsig</groupId>
23
      <artifactId>org.gvsig.tools.lib</artifactId>
24
      <scope>compile</scope>
25
    </dependency>
26
    <dependency>
27
      <groupId>org.gvsig</groupId>
28
      <artifactId>org.gvsig.projection.api</artifactId>
29
      <scope>compile</scope>
30
    </dependency>
31
    <dependency>
32
      <groupId>org.gvsig</groupId>
33
      <artifactId>org.gvsig.fmap.dal.api</artifactId>
34
      <scope>compile</scope>
35
    </dependency>
36
    
37
    <dependency>
38
      <groupId>org.gvsig</groupId>
39
      <artifactId>org.gvsig.fmap.dal.db.jdbc</artifactId>
40
      <scope>compile</scope>
41
    </dependency>
42

  
43
    <dependency>
44
      <groupId>org.gvsig</groupId>
45
      <artifactId>org.gvsig.fmap.dal.db.lib</artifactId>
46
      <scope>compile</scope>
47
    </dependency>
48
    
49
    <dependency>
50
      <groupId>org.gvsig</groupId>
51
      <artifactId>org.gvsig.fmap.dal.spi</artifactId>
52
      <scope>compile</scope>
53
    </dependency>
54
    <dependency>
55
      <groupId>org.gvsig</groupId>
56
      <artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
57
      <scope>compile</scope>
58
    </dependency>
59
    <dependency>
60
      <groupId>org.gvsig</groupId>
61
      <artifactId>org.gvsig.fmap.geometry.api</artifactId>
62
      <scope>compile</scope>
63
    </dependency>
64
    <!--
65
    <dependency>
66
        <groupId>org.gvsig</groupId>
67
        <artifactId>org.gvsig.fmap.geometry.operation</artifactId>
68
        <scope>compile</scope>
69
    </dependency>
70
    -->
71
    <dependency>
72
      <groupId>org.gvsig</groupId>
73
      <artifactId>org.gvsig.timesupport.lib.api</artifactId>
74
      <scope>compile</scope>
75
    </dependency>  
76
    <dependency>
77
      <groupId>org.gvsig</groupId>
78
      <artifactId>org.gvsig.utils</artifactId>
79
      <scope>compile</scope>
80
    </dependency>
81
    <dependency>
82
      <groupId>org.postgresql</groupId>
83
      <artifactId>postgresql</artifactId>
84
      <scope>compile</scope>
85
    </dependency>
86
    <dependency>
87
      <groupId>commons-dbcp</groupId>
88
      <artifactId>commons-dbcp</artifactId>
89
      <scope>compile</scope>
90
    </dependency>
91
    <dependency>
92
      <groupId>commons-collections</groupId>
93
      <artifactId>commons-collections</artifactId>
94
      <scope>compile</scope>
95
    </dependency>
96

  
97

  
98
    <!-- TESTS -->
99
    <dependency>
100
      <groupId>org.gvsig</groupId>
101
      <artifactId>org.gvsig.tools.lib</artifactId>
102
      <type>test-jar</type>
103
      <scope>test</scope>
104
    </dependency>
105
    <dependency>
106
      <groupId>org.gvsig</groupId>
107
      <artifactId>org.gvsig.compat.se</artifactId>
108
      <scope>test</scope>
109
    </dependency>
110
    <dependency>
111
      <groupId>org.gvsig</groupId>
112
      <artifactId>org.gvsig.fmap.dal.impl</artifactId>
113
      <scope>test</scope>
114
    </dependency>
115
    <dependency>
116
      <groupId>org.gvsig</groupId>
117
      <artifactId>org.gvsig.fmap.dal.file.csv</artifactId>
118
      <scope>test</scope>
119
    </dependency>
120
    <dependency>
121
      <groupId>org.gvsig</groupId>
122
      <artifactId>org.gvsig.tools.util.impl</artifactId>
123
      <scope>test</scope>
124
    </dependency>
125
    <dependency>
126
      <groupId>org.gvsig</groupId>
127
      <artifactId>org.gvsig.tools.swing.impl</artifactId>
128
      <scope>test</scope>
129
    </dependency>
130
    <dependency>
131
      <groupId>org.gvsig</groupId>
132
      <artifactId>${org.gvsig.fmap.geometry.impl}</artifactId>
133
      <scope>test</scope>
134
    </dependency>    
135
    <dependency>
136
      <groupId>org.gvsig</groupId>
137
      <artifactId>org.gvsig.expressionevaluator.geometry.lib.impl</artifactId>
138
      <scope>test</scope>
139
    </dependency>    
140
    <dependency>
141
      <groupId>org.gvsig</groupId>
142
      <artifactId>${org.gvsig.proj.lib.impl}</artifactId>
143
      <scope>test</scope>
144
    </dependency>
145
    <dependency>
146
      <groupId>org.gvsig</groupId>
147
      <artifactId>org.gvsig.projection.cresques.impl</artifactId>
148
      <scope>test</scope>
149
    </dependency>
150
    <dependency>
151
      <groupId>org.gvsig</groupId>
152
      <artifactId>org.gvsig.timesupport.lib.impl</artifactId>
153
      <scope>test</scope>
154
    </dependency>
155
    <dependency>
156
      <groupId>org.gvsig</groupId>
157
      <artifactId>org.gvsig.metadata.lib.basic.impl</artifactId>
158
      <scope>test</scope>
159
    </dependency>      
160
    
161
    
162
    <dependency>
163
      <artifactId>junit</artifactId>
164
      <groupId>junit</groupId>
165
      <scope>test</scope>
166
    </dependency>
167
  </dependencies>
168

  
169
  <build>
170
    <plugins>
171

  
172
      <!-- Skip test execution ? -->
173
      <plugin>
174
        <groupId>org.apache.maven.plugins</groupId>
175
        <artifactId>maven-surefire-plugin</artifactId>
176
        <configuration>
177
          <skipTests>false</skipTests>
178
        </configuration>
179
      </plugin>
180
        
181
      <!-- Skip test compilation ? -->
182
      <plugin>
183
        <groupId>org.apache.maven.plugins</groupId>
184
        <artifactId>maven-compiler-plugin</artifactId>
185
        <executions>
186
          <execution>
187
            <id>default-testCompile</id>
188
            <phase>process-test-sources</phase>
189
            <goals>
190
              <goal>testCompile</goal>
191
            </goals>
192
            <configuration>
193
              <skip>false</skip>
194
            </configuration>
195
          </execution>
196
        </executions>
197
      </plugin>
198

  
199
      <!-- Ignore test execution failure ? -->
200
      <plugin>
201
        <groupId>org.apache.maven.plugins</groupId>
202
        <artifactId>maven-surefire-plugin</artifactId>
203
        <configuration>
204
          <testFailureIgnore>true</testFailureIgnore>
205
        </configuration>
206
      </plugin>
207

  
208
    </plugins>
209
  </build>
210

  
211
</project>
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLStoreParameters.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
 */
22
package org.gvsig.postgresql.dal;
23

  
24
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
25
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
26

  
27
public class PostgreSQLStoreParameters extends JDBCStoreParameters implements PostgreSQLConnectionParameters {
28

  
29
    private final PostgreSQLConnectionParametersHelper helper;
30
    
31
    public PostgreSQLStoreParameters() {
32
        super(
33
                PostgreSQLLibrary.NAME + "StoreParameters",
34
                PostgreSQLLibrary.NAME
35
        );
36
        this.helper = new PostgreSQLConnectionParametersHelper(this);
37
    }
38

  
39
    @Override
40
    public String getUrl() {
41
        return this.helper.getUrl();
42
    }
43
    
44
    @Override
45
    public void validate() throws ValidateDataParametersException {
46
        this.helper.validate();
47
        super.validate();
48
    }
49

  
50
    @Override
51
    public boolean getUseSSL() {
52
        return this.helper.getUseSSL();
53
    }
54

  
55
    @Override
56
    public int getMaxIdle() {
57
        return this.helper.getMaxIdle();
58
    }
59

  
60
    public void setUseSSL(boolean v) {
61
        this.helper.setUseSSL(v);
62
    }
63

  
64

  
65
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLConnectionParameters.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.gvsig.fmap.dal.resource.db.DBParameters;
5
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
6

  
7
public interface PostgreSQLConnectionParameters extends JDBCConnectionParameters, DBParameters {
8
	public static final String USESSL_PARAMTER_NAME = "UseSSL";
9
	public static final String MAXIDLE_PARAMTER_NAME = "maxIdle";
10

  
11
	public boolean getUseSSL();   
12
    
13
    public int getMaxIdle();
14
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLConnectionParametersHelper.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.fmap.dal.DataParameters;
6
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
7
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
8

  
9

  
10
public class PostgreSQLConnectionParametersHelper {
11

  
12
    private final JDBCConnectionParameters parameters;
13
    
14
    public PostgreSQLConnectionParametersHelper(JDBCConnectionParameters parameters) {
15
        this.parameters = parameters;
16
    }
17

  
18
    public String getUrl() {
19
        String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME);
20
        if( StringUtils.isEmpty(url) ) {
21
            url = PostgreSQLHelper.getConnectionURL((PostgreSQLConnectionParameters) this.parameters);
22
            this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME,url);
23
        }
24
        return url;
25
    }
26
    
27
    public void validate() throws ValidateDataParametersException {
28
        if (this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null) {
29
            this.setDynValue(
30
                JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME,
31
                PostgreSQLHelper.POSTGRESQL_JDBC_DRIVER
32
            );
33
        }
34
        if( this.getDynValue(JDBCConnectionParameters.PORT_PARAMTER_NAME)==null ) {
35
            this.setDynValue(JDBCConnectionParameters.PORT_PARAMTER_NAME, 5432);
36
        }
37
		if ( StringUtils.isEmpty((CharSequence) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME))) {
38
            String url = PostgreSQLHelper.getConnectionURL(
39
                parameters.getHost(),
40
                parameters.getPort(),
41
                parameters.getDBName()
42
            );
43
            this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url);
44
		}
45
    }
46

  
47
    private Object getDynValue(String name) {
48
        return ((DataParameters)this.parameters).getDynValue(name);
49
    }
50
    
51
    private void setDynValue(String name, Object value) {
52
        ((DataParameters)this.parameters).setDynValue(name,value);
53
    }
54
    
55
    public int getMaxIdle() {
56
        return (int) this.getDynValue(PostgreSQLConnectionParameters.MAXIDLE_PARAMTER_NAME);
57
    }
58

  
59
    public boolean getUseSSL() {
60
        return (boolean) this.getDynValue(PostgreSQLConnectionParameters.USESSL_PARAMTER_NAME);
61
    }
62

  
63
    public void setUseSSL(boolean v) {
64
        this.setDynValue(PostgreSQLConnectionParameters.USESSL_PARAMTER_NAME, v);
65
    }
66
    
67
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLLibrary.java
1

  
2
package org.gvsig.postgresql.dal;
3

  
4
import org.gvsig.fmap.dal.DALLibrary;
5
import org.gvsig.fmap.dal.DALLocator;
6
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
7
import org.gvsig.fmap.dal.store.db.DBHelper;
8
import org.gvsig.fmap.dal.store.jdbc2.JDBCLibrary;
9
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCStoreProviderBase;
10
import org.gvsig.metadata.exceptions.MetadataException;
11
import org.gvsig.tools.library.AbstractLibrary;
12
import org.gvsig.tools.library.LibraryException;
13

  
14

  
15
public class PostgreSQLLibrary extends AbstractLibrary {
16

  
17
    public static final String NAME = "PostgreSQL";
18

  
19
    @Override
20
    public void doRegistration() {
21
        registerAsServiceOf(DALLibrary.class);
22
        require(JDBCLibrary.class);
23
    }
24

  
25
    @Override
26
    protected void doInitialize() throws LibraryException {
27
    }
28

  
29
    @Override
30
    protected void doPostInitialize() throws LibraryException {
31
        LibraryException ex = null;
32

  
33
        DataManagerProviderServices dataman = 
34
                (DataManagerProviderServices) DALLocator.getDataManager();
35

  
36
        try {
37
            Class.forName(PostgreSQLHelper.POSTGRESQL_JDBC_DRIVER);
38
        } catch(Throwable th) {
39
            PostgreSQLHelper.LOGGER.warn("Can't load PostgreSQL JDBC Driver.",th);
40
        }
41
        
42
        DBHelper.registerParametersDefinition(
43
                NAME + "StoreParameters",
44
                PostgreSQLStoreParameters.class,
45
                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
46
        );
47
        DBHelper.registerParametersDefinition(
48
                NAME + "NewStoreParameters",
49
                PostgreSQLNewStoreParameters.class,
50
                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
51
        );
52
        DBHelper.registerParametersDefinition(
53
                NAME + "ServerExplorerParameters",
54
                PostgreSQLServerExplorerParameters.class,
55
                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
56
        );
57
//        DBHelper.registerParametersDefinition(
58
//                NAME + "ResourceParameters",
59
//                PostgreSQLResourceParameters.class,
60
//                dataman.getResourceAsStream(this, NAME + "Parameters.xml")
61
//        );
62
        try {
63
            DBHelper.registerMetadataDefinition(
64
                NAME,
65
                JDBCStoreProviderBase.class,
66
                dataman.getResourceAsStream(this, NAME + "Metadata.xml")
67
            );
68
        } catch (MetadataException e) {
69
            ex = new LibraryException(this.getClass(), e);
70
        }
71

  
72
//        ResourceManagerProviderServices resman = (ResourceManagerProviderServices) DALLocator
73
//                .getResourceManager();
74
//
75
//        if (!resman.getResourceProviders().contains(NAME)) {
76
//            resman.register(NAME,
77
//                "Resource for " + NAME,
78
//                PostgreSQLResource.class,
79
//                PostgreSQLResourceParameters.class
80
//            );
81
//        }
82

  
83
        if (!dataman.getStoreProviderRegister().exits(NAME)) {
84
            dataman.registerStoreProviderFactory(new PostgreSQLStoreProviderFactory());
85
        }
86

  
87
        if (!dataman.getServerExplorerRegister().exits(NAME)) {
88
            dataman.registerServerExplorerFactory(new PostgreSQLServerExplorerFactory());
89
        }
90

  
91
        // Por compatibilidad con gvSIG 2.3 registramos otra vez la factoria con
92
        // el nombre que tenia antes.
93
        if (!dataman.getServerExplorerRegister().exits("PostgreSQLExplorer")) {
94
            dataman.registerServerExplorerFactory(new PostgreSQLServerExplorerFactory("PostgreSQLExplorer"));
95
        }
96
        
97
        if (ex != null) {
98
            throw ex;
99
        }
100
    }
101

  
102
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLBuilder.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

  
25
package org.gvsig.postgresql.dal;
26

  
27
import java.sql.Connection;
28
import java.sql.DatabaseMetaData;
29
import java.text.MessageFormat;
30
import java.util.ArrayList;
31
import java.util.List;
32
import org.apache.commons.lang3.StringUtils;
33
import org.gvsig.expressionevaluator.Formatter;
34
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
35
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
36
import org.gvsig.postgresql.dal.expressionbuilderformatter.PostgreSQLFormatter;
37

  
38
@SuppressWarnings("UseSpecificCatch")
39
public class PostgreSQLBuilder extends JDBCSQLBuilderBase {
40

  
41
    protected Formatter formatter = null;
42
    
43
    public static class Version {
44

  
45
        private final int major;
46
        private final int minor;
47
        
48
        public Version(int major, int minor) {
49
            this.major = major;
50
            this.minor = minor;
51
        }
52

  
53
        public int getMajor() {
54
            return major;
55
        }
56

  
57
        public int getMinor() {
58
            return minor;
59
        }
60
        
61
    }
62
    private Version databaseVersion = null;
63
    
64
    public Version getDatabaseVersion() {
65
        if( databaseVersion == null ) {
66
            Connection conn = null;
67
            try {
68
                conn = this.getHelper().getConnection();
69
                DatabaseMetaData metadata = conn.getMetaData();
70
                databaseVersion = new Version(
71
                    metadata.getDatabaseMajorVersion(),
72
                    metadata.getDatabaseMinorVersion()
73
                );
74
            } catch (Exception ex) {
75
                databaseVersion = new Version(0,0);
76
            } finally {
77
                this.getHelper().closeConnectionQuietly(conn);
78
            }
79
        }
80
        return databaseVersion;
81
    }
82
    
83
    public PostgreSQLBuilder(JDBCHelper helper) {
84
        super(helper);
85
        this.defaultSchema = "public";
86
        this.supportSchemas = true;
87
        this.allowAutomaticValues = true;
88
        this.geometrySupportType = this.helper.getGeometrySupportType();
89
        this.hasSpatialFunctions = this.helper.hasSpatialFunctions();
90

  
91
        this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = null;
92
        this.STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = null;
93

  
94
        this.STMT_UPDATE_TABLE_STATISTICS_table = "ANALYZE {0}";
95

  
96
//        config.set(SQLConfig.ST_GeomFromEWKB, "ST_GeomFromEWKB({0}, {1})");
97
//        config.set(SQLConfig.ST_AsEWKB, "ST_AsEWKB(({0}))");        
98
//        config.set(SQLConfig.ST_ExtentAggregate, "ST_Extent({0})");        
99
//        config.set(SQLConfig.ST_UnionAggregate, "ST_Union({0})");
100
        
101
//        config.set(SQLConfig.isNull, "( ({0}) ISNULL )");
102
//        config.set(SQLConfig.notIsNull, "( ({0}) NOT NULL )");
103

  
104
    }
105
    
106
    public class PostgreSQLUpdateTableStatisticsBuilderBase extends UpdateTableStatisticsBuilderBase {
107
        @Override
108
        public List<String> toStrings() {
109
            List<String> sqls = new ArrayList<>();
110
            
111
            if( !StringUtils.isBlank(STMT_UPDATE_TABLE_STATISTICS_table) ) {
112
                // In postGIS, UpdateLayerStatistics function, don't allow to 
113
                // use the database name in the table name.
114
                String name = as_identifier(this.table.getName());
115
                if( table.has_schema()) {
116
                    name = as_identifier(this.table.getSchema()) + "." + name;
117
                }
118
                String sql = MessageFormat.format(
119
                        STMT_UPDATE_TABLE_STATISTICS_table,
120
                        name
121
                    );
122
                if( !StringUtils.isEmpty(sql) ) {
123
                    sqls.add(sql);
124
                }
125
            }
126
            return sqls;
127
        }        
128
    }
129
    protected class PostgreSQLCreateIndexBuilder extends CreateIndexBuilderBase {
130

  
131
        @Override
132
        public List<String> toStrings(Formatter formatter) {
133
            StringBuilder builder = new StringBuilder();
134
            builder.append("CREATE ");
135
//            if( this.isUnique ) {
136
//                builder.append("UNIQUE ");
137
//            }
138
            builder.append("INDEX ");
139
            if( this.ifNotExist ) {
140
                Version version = getDatabaseVersion();
141
                if( version.getMajor()>=9 && version.getMinor()>=5 ) {
142
                    builder.append("IF NOT EXISTS ");
143
                }
144
            }
145
            builder.append(as_identifier(this.indexName));
146
            builder.append(" ON ");
147
            builder.append(this.table.toString(formatter));
148
            if( this.isSpatial ) {
149
                builder.append(" USING GIST ");
150
            }
151
            builder.append(" ( ");
152
            boolean is_first_column = true;
153
            for( String column : this.columns) {
154
                if( is_first_column ) {
155
                    is_first_column = false;
156
                } else {
157
                    builder.append(", ");
158
                }
159
                builder.append(as_identifier(column));
160
            }
161
            builder.append(" )");
162
            
163
            List<String> sqls = new ArrayList<>();
164
            sqls.add(builder.toString());
165
            return sqls;
166
        }
167
        
168
    }
169
    
170
    protected class PostgreSQLCreateTableBuilder extends CreateTableBuilderBase {
171

  
172
        @Override
173
       public List<String> toStrings(Formatter formatter) {
174
           // 
175
           // https://www.postgresql.org/docs/9.1/static/sql-createtable.html
176
           //
177
            List<String> sqls = new ArrayList<>();
178
            StringBuilder builder = new StringBuilder();
179

  
180
            builder.append("CREATE TABLE ");
181
            builder.append(this.table.toString(formatter));
182
            builder.append(" (");
183
            boolean first = true;
184
            for (ColumnDescriptor column : columns) {
185
                if( column.isGeometry() ) {
186
                    continue;
187
                }
188
                if (first) {
189
                    first = false;
190
                } else {
191
                    builder.append(", ");
192
                }
193
                builder.append(as_identifier(column.getName()));
194
                builder.append(" ");
195
                if( column.isAutomatic() ) {
196
                    builder.append(" SERIAL");
197
                } else {
198
                    builder.append(sqltype(
199
                            column.getType(), 
200
                            column.getSize(),
201
                            column.getPrecision(), 
202
                            column.getScale(), 
203
                            column.getGeometryType(),
204
                            column.getGeometrySubtype()
205
                    ));
206
                    if (column.getDefaultValue() == null) {
207
                        if (column.allowNulls()) {
208
                            builder.append(" DEFAULT NULL");
209
                        }
210
                    } else {
211
                        builder.append(" DEFAULT '");
212
                        builder.append(column.getDefaultValue().toString());
213
                        builder.append("'");
214
                    }
215
                    if (column.allowNulls()) {
216
                        builder.append(" NULL");
217
                    } else {
218
                        builder.append(" NOT NULL");
219
                    }
220
                }
221
                if (column.isPrimaryKey()) {
222
                    builder.append(" PRIMARY KEY");
223
                }
224
            }
225
            builder.append(" )");
226
            sqls.add(builder.toString());
227

  
228
            String AddGeometryColumn = "SELECT AddGeometryColumn({0} , {1} , {2}, {3,number,#######} , {4} , {5}, {6})";
229
            for (ColumnDescriptor column : columns) {
230
                if( column.isGeometry() ) {
231
                    String sql = MessageFormat.format(
232
                        AddGeometryColumn,
233
                        as_string(this.table.has_schema()?this.table.getSchema():"public"),
234
                        as_string(this.table.getName()),
235
                        as_string(column.getName()),
236
                        column.getGeometrySRSId(),
237
                        as_string(sqlgeometrytype(column.getGeometryType(), column.getGeometrySubtype())),
238
                        as_string(sqlgeometrydimension(column.getGeometryType(), column.getGeometrySubtype())),
239
                        as_string(column.allowNulls())
240
                    );
241
                    sqls.add(sql);
242
                }
243
            }
244
            return sqls;
245
        }
246
    }
247

  
248
    public class PostgreSQLSelectBuilderBase extends SelectBuilderBase {
249
        
250
        @Override
251
        protected boolean isValid(StringBuilder message) {
252
            if( message == null ) {
253
                message = new StringBuilder();
254
            }
255
            if( this.has_offset() && !this.has_order_by() ) {
256
                // Algunos gestores de BBDD requieren que se especifique un
257
                // orden para poder usar OFFSET. Como eso parece buena idea para
258
                // asegurar que siempre tengamos los mismo resultados, lo exijimos
259
                // siempre.
260
                message.append("Can't use OFFSET without an ORDER BY.");
261
                return false;
262
            }
263
            return true;
264
        }        
265
        
266
        @Override
267
        public String toString(Formatter formatter) {
268
            //
269
            // https://www.postgresql.org/docs/9.1/static/sql-select.html
270
            //
271
            StringBuilder builder = new StringBuilder();
272
            if( !isValid(builder) ) {
273
                throw new IllegalStateException(builder.toString());
274
            }
275
            builder.append("SELECT ");
276
            if( this.distinct ) {
277
                builder.append("DISTINCT ");
278
            }
279
            boolean first = true;
280
            for (SelectColumnBuilder column : columns) {
281
                if (first) {
282
                    first = false;
283
                } else {
284
                    builder.append(", ");
285
                }
286
                builder.append(column.toString(formatter));
287
            }
288

  
289
            if ( this.has_from() ) {
290
                builder.append(" FROM ");
291
                builder.append(this.from.toString(formatter));
292
            }
293
            if( this.has_group_by() ) {
294
                builder.append(" GROUP BY ");
295
                builder.append(this.groupColumn.get(0).toString(formatter));
296
                for (int i = 1; i < groupColumn.size(); i++) {
297
                    builder.append(", ");
298
                    builder.append(this.groupColumn.get(i).toString(formatter));
299
                }
300
            }            
301
            if ( this.has_where() ) {
302
                builder.append(" WHERE ");
303
                builder.append(this.where.toString(formatter));
304
            }
305
            
306
            if( this.has_order_by() ) {
307
                builder.append(" ORDER BY ");
308
                first = true;
309
                for (OrderByBuilder item : this.order_by) {
310
                    if (first) {
311
                        first = false;
312
                    } else {
313
                        builder.append(", ");
314
                    }
315
                    builder.append(item.toString(formatter));
316
                }   
317
            }
318
            
319
            if ( this.has_limit() && this.has_offset() ) {
320
                builder.append(" OFFSET ");
321
                builder.append(this.offset);
322
                builder.append(" FETCH NEXT ");
323
                builder.append(this.limit);
324
                builder.append(" ROWS ONLY");
325

  
326
            } else if ( this.has_limit()) {
327
                builder.append(" LIMIT ");
328
                builder.append(this.limit);
329

  
330
            } else if ( this.has_offset() ) {
331
                builder.append(" LIMIT ALL OFFSET ");
332
                builder.append(this.offset);    
333
            }
334
            return builder.toString();
335

  
336
        }
337
    }
338

  
339
    @Override
340
    protected Formatter formatter() {
341
        if( this.formatter==null ) {
342
            this.formatter = new PostgreSQLFormatter(this);
343
        }
344
        return this.formatter;
345
    }
346

  
347
    @Override
348
    public PostgreSQLHelper getHelper() {
349
        return (PostgreSQLHelper) helper;
350
    }
351
    
352
    @Override
353
    protected CreateTableBuilder createCreateTableBuilder() {
354
        return new PostgreSQLCreateTableBuilder();
355
    }
356

  
357
    @Override
358
    protected CreateIndexBuilder createCreateIndexBuilder() {
359
        return new PostgreSQLCreateIndexBuilder();
360
    }
361

  
362
    @Override
363
    protected SelectBuilder createSelectBuilder() {
364
        return new PostgreSQLSelectBuilderBase();
365
    }
366

  
367
    @Override
368
    protected UpdateTableStatisticsBuilder createUpdateTableStatisticsBuilder() {
369
        return new PostgreSQLUpdateTableStatisticsBuilderBase();
370
    }       
371
   
372
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/Constant.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import org.gvsig.expressionevaluator.ExpressionBuilder;
5
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
6
import org.gvsig.expressionevaluator.Formatter;
7
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
8
import org.gvsig.fmap.dal.SQLBuilder;
9
import org.gvsig.fmap.geom.Geometry;
10
import org.gvsig.fmap.geom.GeometryUtils;
11

  
12
/**
13
 *
14
 * @author jjdelcerro
15
 */
16
public class Constant implements Formatter<Value> {
17
    
18
    private static final String FORMAT_ST_GEOMFROMWKB = "ST_GeomFromWKB(({0}), ({1}))";
19
    private static final String FORMAT_ST_GEOMFROMEWKB = "ST_GeomFromEWKB(({0}), ({1}))";
20
    private static final String FORMAT_ST_GEOMFROMTEXT = "ST_GeomFromText(({0}), ({1}))";
21

  
22
    private final SQLBuilder sqlbuilder;
23
    private final Formatter<Value> formatter;
24
    
25
    public Constant(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
26
        this.sqlbuilder = sqlbuilder;
27
        this.formatter = formatter;
28
    }
29
    
30
    @Override
31
    public boolean canApply(ExpressionBuilder.Value value) {
32
        if (value instanceof ExpressionBuilder.Constant) {
33
            Object x = ((ExpressionBuilder.Constant)value).value();
34
            if( x instanceof byte[] ) {
35
                return true;
36
            }
37
            if( x instanceof Geometry ) {
38
                return true;
39
            }
40
        }
41
        return false;
42
    }
43

  
44
    @Override
45
    public String format(Value constant) {
46
        Object x = ((ExpressionBuilder.Constant)constant).value();
47
        GeometryExpressionBuilder builder = this.sqlbuilder.expression();
48
        if( x instanceof byte[] ) {
49
            return builder.string("\\x"+builder.bytearray_hex((byte[]) x))+"::bytea";
50
        }
51
        if( x instanceof Geometry ) {
52
            Geometry geometry = (Geometry) x;
53
            switch (builder.geometry_support_type()) {
54
                case EWKB:
55
                    return MessageFormat.format(
56
                            FORMAT_ST_GEOMFROMEWKB,
57
                            builder.string("\\x"+builder.bytearray_hex(GeometryUtils.toEWKB(geometry)))+"::bytea",
58
                            String.valueOf(builder.srs_id(geometry.getProjection()))
59
                    );
60
                case WKB:
61
                    return MessageFormat.format(
62
                            FORMAT_ST_GEOMFROMWKB,
63
                            builder.string("\\x"+builder.bytearray_hex(GeometryUtils.toWKB(geometry)))+"::bytea",
64
                            String.valueOf(builder.srs_id(geometry.getProjection()))
65
                    );
66
                case WKT:
67
                default:
68
                    return MessageFormat.format(
69
                            FORMAT_ST_GEOMFROMTEXT,
70
                            builder.string(GeometryUtils.toWKT(geometry)),
71
                            String.valueOf(builder.srs_id(geometry.getProjection()))
72
                    );
73
            }
74
            
75
        }
76
        return null;
77
    }
78
    
79
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/IfNull.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_IFNULL;
8
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
9
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
10
import org.gvsig.expressionevaluator.Formatter;
11
import org.gvsig.fmap.dal.SQLBuilder;
12

  
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
class IfNull implements Formatter<Value> {
18

  
19
    private final Formatter<Value> formatter;
20
    private final SQLBuilder builder;
21
    
22
    public IfNull(SQLBuilder builder, Formatter<Value> formatter) {
23
        this.builder = builder;
24
        this.formatter = formatter;
25
    }
26
    
27
    @Override
28
    public boolean canApply(Value value) {
29
        if (value instanceof Function) {
30
            if (value instanceof ExpressionBuilder.Function) {
31
                return StringUtils.equalsIgnoreCase(FUNCTION_IFNULL, ((Function) value).name());
32
            }
33
        }
34
        return false;
35
    }
36

  
37
    @Override
38
    public String format(Value function) {
39
            List<Value> parameters = ((Function) function).parameters();
40
            String p1 = parameters.get(0).toString(formatter);
41
            String p2 = parameters.get(1).toString(formatter);
42
            String p3 = parameters.get(2).toString(formatter);
43
            String r = MessageFormat.format(
44
                    "CASE WHEN ({0}) IS NULL THEN ({1}) ELSE ({2}) END",
45
                    p1,
46
                    p3,
47
                    p2
48
            );
49
            return r;
50
            
51
    }
52
    
53
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ILike.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
7
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_ILIKE;
8
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
9
import org.gvsig.expressionevaluator.Formatter;
10
import org.gvsig.fmap.dal.SQLBuilder;
11

  
12
/**
13
 *
14
 * @author jjdelcerro
15
 */
16
class ILike implements Formatter<Value> {
17

  
18
    private final Formatter<Value> formatter;
19
    private final SQLBuilder builder;
20
    
21
    public ILike(SQLBuilder builder, Formatter<Value> formatter) {
22
        this.builder = builder;
23
        this.formatter = formatter;
24
    }
25
    
26
    @Override
27
    public boolean canApply(Value value) {
28
        if (value instanceof Function) {
29
            return StringUtils.equalsIgnoreCase(OPERATOR_ILIKE, ((Function) value).name());
30
        }
31
        return false;
32
    }
33

  
34
    @Override
35
    public String format(Value function) {
36
        List<Value> parameters = ((Function) function).parameters();
37
        String p1 = parameters.get(0).toString(formatter);
38
        String p2 = parameters.get(1).toString(formatter);
39
        String r = MessageFormat.format("({0}) ~~* ({1})", p1, p2);
40
        return r;
41
    }
42
    
43
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/Decode.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.util.List;
4
import java.util.Objects;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECODE;
8
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
9
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
10
import org.gvsig.expressionevaluator.Formatter;
11
import org.gvsig.fmap.dal.SQLBuilder;
12

  
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
public class Decode implements Formatter<Value> {
18
    
19
    private final SQLBuilder sqlbuilder;
20
    private final Formatter<Value> formatter;
21
    
22
    public Decode(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
23
        this.sqlbuilder = sqlbuilder;
24
        this.formatter = formatter;
25
    }
26
    
27
    @Override
28
    public boolean canApply(ExpressionBuilder.Value value) {
29
        if (value instanceof ExpressionBuilder.Function) {
30
            if( StringUtils.equalsIgnoreCase(FUNCTION_DECODE, ((Function) value).name()) ) {
31
                List<Value> parameters = ((Function) value).parameters();
32
                if( parameters.size()==2) {
33
                    Value p1 = parameters.get(0);
34
                    Value p2 = parameters.get(1);
35
                    if( p1 instanceof ExpressionBuilder.Constant && 
36
                        p2 instanceof ExpressionBuilder.Constant &&
37
                        ((ExpressionBuilder.Constant)p1).value() instanceof String &&
38
                        ((ExpressionBuilder.Constant)p2).value() instanceof String 
39
                        ) {
40
                        String p2value = ((ExpressionBuilder.Constant)p2).value().toString();
41
                        if( StringUtils.equalsIgnoreCase(p2value, "hex") ) {
42
                            return true;
43
                        }
44
                    }
45
                    
46
                }
47
            }
48
        }
49
        return false;
50
    }
51

  
52
    @Override
53
    public String format(Value function) {
54
        ExpressionBuilder builder = this.sqlbuilder.expression();
55
        List<Value> parameters = ((Function) function).parameters();
56
        Value p1 = parameters.get(0);
57
        String p1value = Objects.toString(((ExpressionBuilder.Constant)p1).value(), "");
58
        String r = builder.string("\\x"+p1value)+"::bytea";
59
        return r;
60
    }
61
    
62
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ST_ExtentAggregate.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import static org.gvsig.expressionevaluator.GeometryExpressionBuilder.FUNCTION_ST_EXTENTAGGREGATE;
8
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
9
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
10
import org.gvsig.expressionevaluator.Formatter;
11
import org.gvsig.fmap.dal.SQLBuilder;
12

  
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
public class ST_ExtentAggregate implements Formatter<Value> {
18
    
19
    private final SQLBuilder sqlbuilder;
20
    private final Formatter<Value> formatter;
21
    
22
    public ST_ExtentAggregate(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
23
        this.sqlbuilder = sqlbuilder;
24
        this.formatter = formatter;
25
    }
26
    @Override
27
    public boolean canApply(ExpressionBuilder.Value value) {
28
        if (value instanceof ExpressionBuilder.Function) {
29
            return StringUtils.equalsIgnoreCase(FUNCTION_ST_EXTENTAGGREGATE, ((Function) value).name());
30
        }
31
        return false;
32
    }
33

  
34
    @Override
35
    public String format(Value function) {
36
        List<Value> parameters = ((Function) function).parameters();
37
        String p1 = parameters.get(0).toString(formatter);
38
        String r = MessageFormat.format("ST_Extent({0})", p1);
39
        return r;
40
    }
41
    
42
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ST_GeomFromWKB.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
8
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
9
import org.gvsig.expressionevaluator.Formatter;
10
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
11
import org.gvsig.fmap.dal.SQLBuilder;
12
import org.gvsig.fmap.dal.feature.FeatureType;
13
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
14

  
15
/**
16
 *
17
 * @author jjdelcerro
18
 */
19
public class ST_GeomFromWKB implements Formatter<Value> {
20
    
21
    private final SQLBuilder sqlbuilder;
22
    private final Formatter<Value> formatter;
23
    
24
    public ST_GeomFromWKB(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
25
        this.sqlbuilder = sqlbuilder;
26
        this.formatter = formatter;
27
    }
28
    @Override
29
    public boolean canApply(ExpressionBuilder.Value value) {
30
        if (value instanceof ExpressionBuilder.Function) {
31
            return StringUtils.equalsIgnoreCase(GeometryExpressionBuilder.FUNCTION_ST_GEOMFROMWKB, ((Function) value).name());
32
        }
33
        return false;
34
    }
35

  
36
    @Override
37
    public String format(Value function) {
38
        String r;
39
        String wkb;
40
        String srid;
41
        List<Value> parameters = ((Function) function).parameters();
42
        switch(parameters.size()) {
43
            case 1:
44
                wkb = parameters.get(0).toString(formatter);
45
                r = MessageFormat.format("ST_GeomFromWKB({0})", wkb);
46
                break;
47
            case 2:
48
                wkb = parameters.get(0).toString(formatter);
49
                srid = parameters.get(1).toString(formatter);
50
                if( StringUtils.equals("0", srid) ) {
51
                    r = MessageFormat.format("ST_GeomFromWKB({0})", wkb);
52
                } else {
53
                    FeatureType ftype = (FeatureType) function.getProperty(PROP_FEATURE_TYPE);
54
                    if( ftype == null || ftype.getDefaultSRS()!=null ) {
55
                        r = MessageFormat.format("ST_GeomFromWKB({0},{1})", wkb, srid);
56
                    } else {
57
                        r = MessageFormat.format("ST_GeomFromWKB({0})", wkb);
58
                    }
59
                }
60
                break;
61
            default:
62
                throw new IllegalArgumentException("Wrong number of arguments ("+parameters.size()+") in function ST_GeomFromWKB; requiered 1 or 2 arguments.");
63
        }
64
        return r;
65
    }
66
    
67
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/IsNull.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.Code.Constant;
7
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
8
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_IS;
9
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
10
import org.gvsig.expressionevaluator.Formatter;
11
import org.gvsig.fmap.dal.SQLBuilder;
12

  
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
class IsNull implements Formatter<Value> {
18

  
19
    private final Formatter<Value> formatter;
20
    private final SQLBuilder builder;
21
    
22
    public IsNull(SQLBuilder builder, Formatter<Value> formatter) {
23
        this.builder = builder;
24
        this.formatter = formatter;
25
    }
26
    
27
    @Override
28
    public boolean canApply(Value value) {
29
        if (value instanceof Function) {
30
            if( StringUtils.equalsIgnoreCase(OPERATOR_IS, ((Function) value).name()) ) {
31
                List<Value> parameters = ((Function) value).parameters();
32
                Value p2 = parameters.get(1);
33
                if( p2 instanceof Constant && ((Constant)p2).value()==null ) {
34
                    return true;
35
                }
36
            }
37
        }
38
        return false;
39
    }
40

  
41
    @Override
42
    public String format(Value function) {
43
        List<Value> parameters = ((Function) function).parameters();
44
        String p1 = parameters.get(0).toString(formatter);
45
        String r = MessageFormat.format("( ({0}) ISNULL )", p1);
46
        return r;
47
    }
48
    
49
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/ST_GeomFromText.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import java.text.MessageFormat;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import org.gvsig.expressionevaluator.ExpressionBuilder.Function;
8
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
9
import org.gvsig.expressionevaluator.Formatter;
10
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
11
import org.gvsig.fmap.dal.SQLBuilder;
12
import org.gvsig.fmap.dal.feature.FeatureType;
13
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
14

  
15
/**
16
 *
17
 * @author jjdelcerro
18
 */
19
public class ST_GeomFromText implements Formatter<Value> {
20
    
21
    private final SQLBuilder sqlbuilder;
22
    private final Formatter<Value> formatter;
23
    
24
    public ST_GeomFromText(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
25
        this.sqlbuilder = sqlbuilder;
26
        this.formatter = formatter;
27
    }
28
    @Override
29
    public boolean canApply(ExpressionBuilder.Value value) {
30
        if (value instanceof ExpressionBuilder.Function) {
31
            return StringUtils.equalsIgnoreCase(GeometryExpressionBuilder.FUNCTION_ST_GEOMFROMTEXT, ((Function) value).name());
32
        }
33
        return false;
34
    }
35

  
36
    @Override
37
    public String format(Value function) {
38
        String r;
39
        String wkt;
40
        String srid;
41
        List<Value> parameters = ((Function) function).parameters();
42
        switch(parameters.size()) {
43
            case 1:
44
                wkt = parameters.get(0).toString(formatter);
45
                r = MessageFormat.format("ST_GeomFromText({0})", wkt);
46
                break;
47
            case 2:
48
                wkt = parameters.get(0).toString(formatter);
49
                srid = parameters.get(1).toString(formatter);
50
                if( StringUtils.equals("0", srid) ) {
51
                    r = MessageFormat.format("ST_GeomFromText({0})", wkt);
52
                } else {
53
                    FeatureType ftype = (FeatureType) function.getProperty(PROP_FEATURE_TYPE);
54
                    if( ftype == null || ftype.getDefaultSRS()!=null ) {
55
                        r = MessageFormat.format("ST_GeomFromText({0},{1})", wkt, srid);
56
                    } else {
57
                        r = MessageFormat.format("ST_GeomFromText({0})", wkt);
58
                    }
59
                }
60
                break;
61
            default:
62
                throw new IllegalArgumentException("Wrong number of arguments ("+parameters.size()+") in function ST_GeomFromText; requiered 1 or 2 arguments.");
63
        }
64
        return r;
65
    }
66
    
67
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/expressionbuilderformatter/PostgreSQLFormatter.java
1
package org.gvsig.postgresql.dal.expressionbuilderformatter;
2

  
3
import org.gvsig.expressionevaluator.ExpressionBuilder;
4
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
5
import org.gvsig.expressionevaluator.Formatter;
6
import org.gvsig.fmap.dal.SQLBuilder;
7

  
8
/**
9
 *
10
 * @author jjdelcerro
11
 */
12
public class PostgreSQLFormatter implements Formatter<Value> {
13

  
14
    private final SQLBuilder builder;
15
    private final Formatter<ExpressionBuilder.Value>[] formatters;
16

  
17
    public PostgreSQLFormatter(SQLBuilder builder) {
18

  
19
        this.builder = builder;
20
        this.formatters = new Formatter[]{
21
            new Decode(this.builder, this),
22
            new Constant(this.builder, this),
23
            new ILike(this.builder, this),
24
            new IfNull(this.builder, this),
25
            new IsNull(this.builder, this),
26
//            new ST_AsEWKB(this.builder, this),
27
            new ST_ExtentAggregate(this.builder, this),
28
            new ST_GeomFromWKB(this.builder, this),
29
            new ST_GeomFromText(this.builder, this),
30
//            new ST_GeomFromEWKB(this.builder, this),
31
//            new ST_Intersects(this.builder, this),
32
//            new ST_UnionAggregate(this.builder, this)
33
        };
34
    }
35

  
36
    @Override
37
    public boolean canApply(Value value) {
38
        for (Formatter<Value> formatter : formatters) {
39
            if (formatter.canApply(value)) {
40
                return true;
41
            }
42
        }
43
        return false;
44
    }
45

  
46
    @Override
47
    public String format(Value value) {
48
        for (Formatter<Value> formatter : formatters) {
49
            if (formatter.canApply(value)) {
50
                return formatter.format(value);
51
            }
52
        }
53
        return value.toString(this);
54
    }
55
    
56
}
tags/org.gvsig.postgresql-2.0.119/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLNewStoreParameters.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.postgresql.dal;
23

  
24
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
25
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
26

  
27
public class PostgreSQLNewStoreParameters 
28
    extends 
29
        JDBCNewStoreParameters 
30
    implements 
31
        PostgreSQLConnectionParameters 
32
    {
33

  
34
    private final PostgreSQLConnectionParametersHelper helper;
35
    
36
    public PostgreSQLNewStoreParameters() {
37
        super(
38
            PostgreSQLLibrary.NAME + "NewStoreParameters", 
39
            PostgreSQLLibrary.NAME
40
        );
41
        this.helper = new PostgreSQLConnectionParametersHelper(this);
42
    }
43

  
44
    @Override
45
    public String getUrl() {
46
        return this.helper.getUrl();
47
    }
48
    
49
    @Override
50
    public void validate() throws ValidateDataParametersException {
51
        this.helper.validate();
52
    }
53
        
54
    @Override
55
    public boolean getUseSSL() {
56
        return this.helper.getUseSSL();
57
    }
58

  
59
    @Override
60
    public int getMaxIdle() {
61
        return this.helper.getMaxIdle();
62
    }
63

  
64
    public void setUseSSL(boolean v) {
65
        this.helper.setUseSSL(v);
66
    }
67

  
68
}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff