Revision 178

View differences:

tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.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.mssqlserver.provider</artifactId>
7
    <packaging>jar</packaging>
8
    <name>${project.artifactId}</name>
9
    <description>DAL provider for MS SQLServer</description>
10
    <parent>
11
        <groupId>org.gvsig</groupId>
12
        <artifactId>org.gvsig.mssqlserver</artifactId>
13
        <version>1.0.30</version>
14
    </parent>
15

  
16
    <dependencies>
17
        <dependency>
18
            <groupId>com.googlecode.log4jdbc</groupId>
19
            <artifactId>log4jdbc</artifactId>
20
            <scope>compile</scope>
21
        </dependency>
22
        <dependency>
23
            <groupId>org.gvsig</groupId>
24
            <artifactId>org.gvsig.tools.lib</artifactId>
25
            <scope>compile</scope>
26
        </dependency>
27
        <dependency>
28
            <groupId>org.gvsig</groupId>
29
            <artifactId>org.gvsig.projection.api</artifactId>
30
            <scope>compile</scope>
31
        </dependency>
32
        <dependency>
33
            <groupId>org.gvsig</groupId>
34
            <artifactId>org.gvsig.fmap.dal.api</artifactId>
35
            <scope>compile</scope>
36
        </dependency>
37
        <dependency>
38
            <groupId>org.gvsig</groupId>
39
            <artifactId>org.gvsig.fmap.dal.impl</artifactId>
40
            <scope>test</scope>
41
        </dependency>
42
        <dependency>
43
            <groupId>org.gvsig</groupId>
44
            <artifactId>org.gvsig.fmap.dal.spi</artifactId>
45
            <scope>test</scope>
46
        </dependency>
47
    
48
        <dependency>
49
            <groupId>org.gvsig</groupId>
50
            <artifactId>org.gvsig.fmap.dal.db.jdbc</artifactId>
51
            <scope>compile</scope>
52
        </dependency>
53

  
54
        <dependency>
55
            <groupId>org.gvsig</groupId>
56
            <artifactId>org.gvsig.fmap.dal.db.lib</artifactId>
57
            <scope>compile</scope>
58
        </dependency>
59
    
60
        <dependency>
61
            <groupId>org.gvsig</groupId>
62
            <artifactId>org.gvsig.fmap.dal.spi</artifactId>
63
            <scope>compile</scope>
64
        </dependency>
65
        <dependency>
66
            <groupId>org.gvsig</groupId>
67
            <artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
68
            <scope>compile</scope>
69
        </dependency>
70
        <dependency>
71
            <groupId>org.gvsig</groupId>
72
            <artifactId>org.gvsig.fmap.geometry.api</artifactId>
73
            <scope>compile</scope>
74
        </dependency>
75
        <dependency>
76
            <groupId>org.gvsig</groupId>
77
            <artifactId>${org.gvsig.fmap.geometry.impl}</artifactId>
78
            <scope>test</scope>
79
        </dependency>
80
        <dependency>
81
            <groupId>org.gvsig</groupId>
82
            <artifactId>org.gvsig.timesupport.lib.api</artifactId>
83
            <scope>compile</scope>
84
        </dependency>  
85
        <dependency>
86
            <groupId>org.gvsig</groupId>
87
            <artifactId>org.gvsig.utils</artifactId>
88
            <scope>compile</scope>
89
        </dependency>
90
        <dependency>
91
            <groupId>com.microsoft.sqlserver</groupId>
92
            <artifactId>sqlserver-jdbc</artifactId>
93
            <scope>compile</scope>
94
        </dependency>
95
        <dependency>
96
            <groupId>commons-dbcp</groupId>
97
            <artifactId>commons-dbcp</artifactId>
98
            <scope>compile</scope>
99
        </dependency>
100
        <dependency>
101
            <groupId>commons-collections</groupId>
102
            <artifactId>commons-collections</artifactId>
103
            <scope>compile</scope>
104
        </dependency>
105

  
106

  
107
        <!-- TESTS -->
108
        <dependency>
109
            <groupId>org.gvsig</groupId>
110
            <artifactId>org.gvsig.tools.lib</artifactId>
111
            <type>test-jar</type>
112
            <scope>test</scope>
113
        </dependency>
114
        <dependency>
115
            <groupId>org.gvsig</groupId>
116
            <artifactId>org.gvsig.compat.se</artifactId>
117
            <scope>test</scope>
118
        </dependency>
119
        <dependency>
120
            <groupId>org.gvsig</groupId>
121
            <artifactId>org.gvsig.fmap.dal.impl</artifactId>
122
            <type>test-jar</type>
123
            <scope>test</scope>
124
        </dependency>
125
        <dependency>
126
            <groupId>org.gvsig</groupId>
127
            <artifactId>org.gvsig.metadata.lib.basic.impl</artifactId>
128
            <scope>compile</scope>
129
        </dependency>    
130
        <dependency>
131
            <groupId>org.gvsig</groupId>
132
            <artifactId>org.gvsig.projection.cresques.impl</artifactId>
133
            <scope>test</scope>
134
        </dependency>
135
        <dependency>
136
            <groupId>org.gvsig</groupId>
137
            <artifactId>org.gvsig.timesupport.lib.impl</artifactId>
138
            <scope>test</scope>
139
        </dependency>
140
        <dependency>
141
            <groupId>junit</groupId>
142
            <artifactId>junit</artifactId>
143
            <version>3.8.1</version>
144
            <type>jar</type>
145
        </dependency>
146
    </dependencies>
147
    <build>
148
        <plugins>
149
                        
150
            <plugin>
151
                <groupId>org.codehaus.mojo</groupId>
152
                <artifactId>animal-sniffer-maven-plugin</artifactId>
153
                <executions>
154
                    <execution>
155
                        <id>check-java-api</id>
156
                        <phase>package</phase>
157
                        <goals>
158
                            <goal>check</goal>
159
                        </goals>
160
                        <configuration>
161
                            <skip>true</skip>
162
                            <signature>
163
                                <groupId>org.codehaus.mojo.signature</groupId>
164
                                <artifactId>java18</artifactId>
165
                                <version>1.0</version>
166
                            </signature>
167
                        </configuration>
168
                    </execution>
169
                </executions>
170
            </plugin>            
171
            
172
            
173
            
174
        </plugins>
175
    </build>
176

  
177
</project>
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.fmap.dal.store.jdbc2.JDBCLibrary
2
org.gvsig.mssqlserver.dal.MSSQLServerLibrary
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/resources/org/gvsig/fmap/dal/store/jdbc2/JDBC2Metadata.xml
1
<?xml version="1.0"?>
2
<!--
3

  
4
    gvSIG. Desktop Geographic Information System.
5

  
6
    Copyright (C) 2007-2013 gvSIG Association.
7

  
8
    This program is free software; you can redistribute it and/or
9
    modify it under the terms of the GNU General Public License
10
    as published by the Free Software Foundation; either version 3
11
    of the License, or (at your option) any later version.
12

  
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17

  
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
    MA  02110-1301, USA.
22

  
23
    For any additional information, do not hesitate to contact us
24
    at info AT gvsig.com, or visit our website www.gvsig.com.
25

  
26
-->
27
<definitions>
28
  <version>1.0.0</version>
29
  <classes>
30
    <class name="JDBC2" namespace="Metadata">
31
      <extends>
32
      	<class name="SpatialProvider" namespace="Metadata"/>
33
      </extends>
34
      <description>Metadata of a JDBC store</description>
35
      <fields>
36
      </fields>
37
    </class>
38

  
39
  </classes>
40
</definitions>  
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/resources/org/gvsig/fmap/dal/store/jdbc2/JDBC2Parameters.xml
1
<?xml version="1.0"?>
2
<!--
3

  
4
    gvSIG. Desktop Geographic Information System.
5

  
6
    Copyright (C) 2007-2013 gvSIG Association.
7

  
8
    This program is free software; you can redistribute it and/or
9
    modify it under the terms of the GNU General Public License
10
    as published by the Free Software Foundation; either version 3
11
    of the License, or (at your option) any later version.
12

  
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17

  
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
    MA  02110-1301, USA.
22

  
23
    For any additional information, do not hesitate to contact us
24
    at info AT gvsig.com, or visit our website www.gvsig.com.
25

  
26
-->
27
<definitions>
28
  <version>1.0.0</version>
29
  <classes>
30
    <class name="JDBC2ResourceParameters">
31
      <description></description>
32
      <extends>
33
        <class>DBResourceParameters</class>
34
      </extends>
35
      <fields>
36
        <field name="JDBCDriverClass" type="string" mandatory="true" group="Advanced">
37
          <description>JDBC Driver class</description>
38
        </field>
39
        <field name="URL" type="string" mandatory="true" group="Connection">
40
          <description>JDBC url connection</description>
41
        </field>
42
        <field name="Catalog" type="string" mandatory="false" group="Basic">
43
          <description>Database catalog</description>
44
        </field>
45
        <field name="Schema" type="string" mandatory="false" group="Basic">
46
          <description>Database schema</description>
47
        </field>
48
      </fields>
49
    </class>
50

  
51
    <class name="JDBC2StoreParameters">
52
      <extends>
53
        <class>JDBC2ResourceParameters</class>
54
        <class>DBStoreParameters</class>
55
      </extends>
56
      <tags>
57
          <value name="defaultGroup" value="Basic"/>
58
      </tags>
59
      <fields>
60
      </fields>
61
    </class>
62

  
63
    <class name="JDBC2NewStoreParameters">
64
      <description></description>
65
      <extends>
66
        <class>JDBC2ResourceParameters</class>
67
        <class>DBNewStoreParameters</class>
68
      </extends>
69
      <tags>
70
          <value name="defaultGroup" value="Basic"/>
71
      </tags>
72
      <fields>
73
        <field name="selectRole" label="Roles for select" type="String" mandatory="false" defaultValue="" group="Permissions">
74
          <description>Roles asigned to have select privilegies</description>
75
        </field>
76
        <field name="insertRole" label="Roles for insert" type="String" mandatory="false" defaultValue="" group="Permissions">
77
          <description>Roles asigned to have insert privilegies</description>
78
        </field>
79
        <field name="updateRole" label="Roles for update" type="String" mandatory="false" defaultValue="" group="Permissions">
80
          <description>Roles asigned to have update privilegies</description>
81
        </field>
82
        <field name="deleteRole" label="Roles for delete" type="String" mandatory="false" defaultValue="" group="Permissions">
83
          <description>Roles asigned to have delete privilegies</description>
84
        </field>
85
        <field name="truncateRole" label="Roles for truncate" type="String" mandatory="false" defaultValue="" group="Permissions">
86
          <description>Roles asigned to have truncate privilegies</description>
87
        </field>
88
        <field name="referenceRole" label="Roles for reference" type="String" mandatory="false" defaultValue="" group="Permissions">
89
          <description>Roles asigned to have reference privilegies</description>
90
        </field>
91
        <field name="triggerRole" label="Roles for trigger" type="String" mandatory="false" defaultValue="" group="Permissions">
92
          <description>Roles asigned to have trigger privilegies</description>
93
        </field>
94
        <field name="allRole" label="Roles for all" type="String" mandatory="false" defaultValue="" group="Permissions">
95
          <description>Roles asigned to have all privilegies</description>
96
        </field>
97
        <field name="postCreatingStatement" label="Post-creating statement" type="String" mandatory="false" defaultValue="" group="Advanced">
98
          <description>Roles asigned to have all privilegies</description>
99
        </field>
100

  
101
      </fields>
102
    </class>
103
    
104
    <class name="JDBC2ServerExplorerParameters">
105
      <extends>
106
        <class>JDBC2ResourceParameters</class>
107
        <class>DBServerExplorerParameters</class>
108
      </extends>
109
      <tags>
110
          <value name="defaultGroup" value="Basic"/>
111
      </tags>
112
      <fields>
113
        <field name="ShowInformationDBTables" type="boolean" 
114
            mandatory="false" defaultValue="false" group="Basic">
115
          <description>Show information tables</description>
116
        </field>
117
      </fields>
118
    </class>
119

  
120

  
121
  </classes>
122
</definitions>  
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/resources/org/gvsig/mssqlserver/dal/MSSQLServerConfig.xml
1
<?xml version="1.0"?>
2
<definitions>
3
  <version>1.0.0</version>
4
  <classes>
5
    <class name="JDBC2">
6
      <extends>
7
      </extends>
8
      <fields>
9
        <field name="function_ST_AsWKB" type="string" group="functions" 
10
          defaultValue="ST_AsWKB({0})" >
11
          <description></description>
12
        </field>
13
        <field name="function_ST_envelope" type="string" group="functions" 
14
          defaultValue="ST_envelope({0})" >
15
          <description></description>
16
        </field>
17
        <field name="function_ST_Extent" type="string" group="functions" 
18
          defaultValue="ST_Extent({0})" >
19
          <description></description>
20
        </field>
21
        <field name="function_ST_intersects" type="string" group="functions" 
22
          defaultValue="ST_intersects({0}, {1})" >
23
          <description></description>
24
        </field>
25
        <field name="function_ST_GeomFromText" type="string" group="functions" 
26
          defaultValue="ST_GeomFromText({0}, {1})" >
27
          <description></description>
28
        </field>
29
        <field name="function_ST_GeomFromWKB" type="string" group="functions" 
30
          defaultValue="ST_GeomFromWKB({0}, {1})" >
31
          <description></description>
32
        </field>
33
        <field name="function_ST_GeomFromEWKB" type="string" group="functions" 
34
          defaultValue="ST_GeomFromEWKB({0}, {1})" >
35
          <description></description>
36
        </field>
37
        <field name="function_current_schema" type="string" group="functions" 
38
          defaultValue="current_schema()" >
39
          <description></description>
40
        </field>
41
        <field name="function_lcase" type="string" group="functions" 
42
          defaultValue="LCASE({0})" >
43
          <description></description>
44
        </field>
45
        <field name="function_count" type="string" group="functions" 
46
          defaultValue="count({0})" >
47
          <description></description>
48
        </field>
49
        <field name="function_crs" type="string" group="functions" 
50
          defaultValue="{0}" >
51
          <description>Use {0} for the abbreviature, {1} for the autority name and {2} for the autority code</description>
52
        </field>
53
        
54
        <!-- operators -->
55
        <field name="operator_isnull" type="string" group="operators" 
56
          defaultValue="{0} is null" >
57
          <description></description>
58
        </field>
59
        <field name="operator_eq" type="string" group="operators" 
60
          defaultValue="{0} = {1}" >
61
          <description></description>
62
        </field>
63
        <field name="operator_neq" type="string" group="operators" 
64
          defaultValue="{0} &lt;&gt; {1}" >
65
          <description></description>
66
        </field>
67
        <field name="operator_lt" type="string" group="operators" 
68
          defaultValue="{0} &lt; {1}" >
69
          <description></description>
70
        </field>
71
        <field name="operator_le" type="string" group="operators" 
72
          defaultValue="{0} &lt;= {1}" >
73
          <description></description>
74
        </field>
75
        <field name="operator_gt" type="string" group="operators" 
76
          defaultValue="{0} &gt; {1}" >
77
          <description></description>
78
        </field>
79
        <field name="operator_ge" type="string" group="operators" 
80
          defaultValue="{0} &gt;= {1}" >
81
          <description></description>
82
        </field>
83
        <field name="operator_and" type="string" group="operators" 
84
          defaultValue="{0} AND {1}" >
85
          <description></description>
86
        </field>
87
        <field name="operator_or" type="string" group="operators" 
88
          defaultValue="{0} OR {1}" >
89
          <description></description>
90
        </field>
91
        <field name="operator_like" type="string" group="operators" 
92
          defaultValue="{0} LIKE {1}" >
93
          <description></description>
94
        </field>
95
        <field name="operator_ilike" type="string" group="operators" 
96
          defaultValue="{0} ILIKE {1}" >
97
          <description></description>
98
        </field>
99
        
100
        <!-- properties -->
101
        <field name="AllowAutomaticValues" type="boolean" group="properties" 
102
          defaultValue="true" >
103
          <description></description>
104
        </field>
105
        <field name="QuoteForStrings" type="string" group="properties" 
106
          defaultValue="'" >
107
          <description></description>
108
        </field>
109
        <field name="QuoteForIdentifiers" type="string" group="properties" 
110
          defaultValue="" >
111
          <description></description>
112
        </field>
113
        <field name="GeometrySupportType" type="string" group="properties" 
114
            defaultValue="WKB" >
115
            <description></description>
116
            <availableValues>
117
                <value label="WKT">WKT</value>
118
                <value label="WKB">WKB</value>
119
                <value label="EWKB">EWKB</value>
120
            </availableValues>          
121
        </field>
122

  
123

  
124

  
125
      </fields>
126
    </class>
127
    
128
    
129
    <class name="MSSQLServer">
130
      <extends>
131
          <class>JDBC2</class>
132
      </extends>
133
      <fields>
134
        <field name="function_ST_AsWKB" type="string" group="functions" 
135
          defaultValue="({0}).STAsWKB" >
136
          <description></description>
137
        </field>
138
        <field name="function_ST_envelope" type="string" group="functions" 
139
          defaultValue="({0}).STEnvelope" >
140
          <description></description>
141
        </field>
142
        <field name="function_ST_Extent" type="string" group="functions" 
143
          defaultValue="({0}).STEnvelope" >
144
          <description></description>
145
        </field>
146
        <field name="function_ST_intersects" type="string" group="functions" 
147
          defaultValue="({0}).STIntersects({1})" >
148
          <description></description>
149
        </field>
150
        <field name="function_ST_GeomFromText" type="string" group="functions" 
151
          defaultValue="geometry::STGeomFromText({0}, {1})" >
152
          <description></description>
153
        </field>
154
        <field name="function_ST_GeomFromWKB" type="string" group="functions" 
155
          defaultValue="geometry::STGeomFromWKB({0}, {1})" >
156
          <description></description>
157
        </field>
158
        <field name="function_ST_GeomFromEWKB" type="string" group="functions" 
159
          defaultValue="geometry::STGeomFromWKB({0}, {1})" >
160
          <description></description>
161
        </field>
162
        <field name="function_current_schema" type="string" group="functions" 
163
          defaultValue="current_schema()" >
164
          <description></description>
165
        </field>
166
        <field name="function_crs" type="string" group="functions" 
167
          defaultValue="{2}" >
168
          <description>Use {0} for the abbreviature, {1} for the autority name and {2} for the autority code</description>
169
        </field>
170
        <field name="function_lcase" type="string" group="functions" 
171
          defaultValue="LOWER({0})" >
172
          <description></description>
173
        </field>
174
        
175
        <!-- operators -->
176
        <field name="operator_ilike" type="string" group="operators" 
177
          defaultValue="{0} ILIKE {1}" >
178
          <description></description>
179
        </field>
180
    
181
    
182
  </classes>
183
</definitions>  
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/resources/org/gvsig/mssqlserver/dal/MSSQLServerMetadata.xml
1
<?xml version="1.0"?>
2
<definitions>
3
  <version>1.0.0</version>
4
  <classes>
5
    <class name="MSSQLServer" namespace="Metadata">
6
      <extends>
7
      	<class name="JDBC2" namespace="Metadata"/>
8
      </extends>
9
      <description>Metadata for MSSQLServer</description>
10
      <fields>
11
      </fields>
12
    </class>
13

  
14
  </classes>
15
</definitions>  
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/resources/org/gvsig/mssqlserver/dal/MSSQLServerParameters.xml
1
<?xml version="1.0"?>
2
<definitions>
3
  <version>1.0.0</version>
4
  <classes>
5
    <class name="MSSQLServerResourceParameters">
6
      <extends>
7
        <class>JDBC2ResourceParameters</class>
8
      </extends>
9
      <fields>
10
        <field name="JDBCDriverClass" type="string" mandatory="true"
11
          defaultValue="com.microsoft.sqlserver.jdbc.SQLServerDriver" group="Advanced">
12
          <description>JDBC driver class for MS-SQlServer</description>
13
        </field>
14
        <field name="port" type="integer" mandatory="false"
15
          defaultValue="1433" group="Connection">
16
          <description></description>
17
        </field>
18
        <field name="host" type="string" mandatory="false" 
19
            defaultValue="127.0.0.1" group="Connection">
20
          <description></description>
21
        </field>
22
        <field name="instanceName" type="string" mandatory="false" 
23
            defaultValue="SQLEXPRESS" group="Connection">
24
          <description></description>
25
        </field>     
26
        <field name="dbname" type="string" mandatory="false" defaultValue="master" group="Basic">
27
          <description></description>
28
        </field>
29
        <field name="dbuser" type="string" mandatory="false" defaultValue="sa" group="Basic">
30
          <description></description>
31
        </field>
32
        <field name="password" type="string" mandatory="false" defaultValue="123" group="Basic">
33
          <description></description>
34
        </field>           
35
      </fields>
36
    </class>
37

  
38
    <class name="MSSQLServerStoreParameters">
39
      <extends>
40
        <class>JDBC2StoreParameters</class>
41
        <class>MSSQLServerResourceParameters</class>
42
      </extends>
43
      <fields/>
44
    </class>
45

  
46
    <class name="MSSQLServerNewStoreParameters">
47
      <extends>
48
        <class>JDBC2NewStoreParameters</class>
49
        <class>MSSQLServerResourceParameters</class>
50
      </extends>
51
      <fields/>
52
    </class>
53

  
54

  
55
    <class name="MSSQLServerServerExplorerParameters">
56
      <extends>
57
        <class>MSSQLServerResourceParameters</class>
58
        <class>JDBC2ServerExplorerParameters</class>
59
      </extends>
60
      <fields/>
61
    </class>
62

  
63

  
64
  </classes>
65
</definitions>  
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/operations/MSSQLServerFetchFeatureTypeOperation.java
1
package org.gvsig.mssqlserver.dal.operations;
2

  
3
import java.sql.Connection;
4
import java.sql.ResultSet;
5
import java.sql.ResultSetMetaData;
6
import java.sql.Statement;
7
import java.util.List;
8
import org.cresques.cts.IProjection;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
11
import org.gvsig.fmap.dal.feature.EditableFeatureType;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
14
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
15
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
16
import org.gvsig.fmap.geom.Geometry;
17
import org.gvsig.fmap.geom.GeometryLocator;
18
import org.gvsig.fmap.geom.type.GeometryType;
19
import org.gvsig.mssqlserver.dal.MSSQLServerHelper;
20

  
21
public class MSSQLServerFetchFeatureTypeOperation extends FetchFeatureTypeOperation {
22

  
23
    public MSSQLServerFetchFeatureTypeOperation(
24
            JDBCHelper helper
25
    ) {
26
        super(helper);
27
    }
28

  
29
    public MSSQLServerFetchFeatureTypeOperation(
30
            JDBCHelper helper,
31
            EditableFeatureType featureType,
32
            String dbname,
33
            String schema,
34
            String table,
35
            List<String> primaryKeys,
36
            String defaultGeometryColumn,
37
            IProjection crs
38
    ) {
39
        super(helper, featureType, dbname, schema, table, primaryKeys, defaultGeometryColumn, crs);
40
    }
41

  
42
    @Override
43
    public void fetch(EditableFeatureType featureType, Connection conn, String dbname, String schema, String table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException {
44
        super.fetch(featureType, conn, dbname, schema, table, pks, defaultGeometryColumn, crs);
45
        ((MSSQLServerHelper) this.helper).setLastUsedFeatureType(featureType);
46
    }
47

  
48
    @Override
49
    protected void fetchGeometryTypeAndSRS(
50
            EditableFeatureAttributeDescriptor attr,
51
            ResultSetMetaData rsMetadata,
52
            int colIndex
53
    ) {
54
        try {
55
            GeometryType geomType = GeometryLocator.getGeometryManager().getGeometryType(
56
                    Geometry.TYPES.GEOMETRY,
57
                    Geometry.SUBTYPES.GEOM2D
58
            );
59
            attr.setGeometryType(geomType);
60

  
61
            String typeName = rsMetadata.getColumnTypeName(colIndex);
62
            attr.setAdditionalInfo("SQLServer_type_name", typeName);
63
            attr.setSRS(getProjectionOfColumn(attr.getName()));
64
        } catch (Exception ex) {
65
            logger.warn("Can't get default geometry type.", ex);
66
        }
67
    }
68
    
69
    private IProjection getProjectionOfColumn(String columnName) {
70
       
71
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
72
        sqlbuilder.select().limit(1);
73
        sqlbuilder.select().column().value(
74
                sqlbuilder.ST_SRID(sqlbuilder.column(columnName))
75
        );
76
        sqlbuilder.select().where().set(
77
                sqlbuilder.and(
78
                        sqlbuilder.notIsNull(sqlbuilder.column(columnName)),
79
                        sqlbuilder.notIsNull(sqlbuilder.ST_SRID(sqlbuilder.column(columnName)))
80
                )
81
        );
82
        sqlbuilder.select().from().table().schema(this.getSchema()).name(this.getTablename());
83
        Statement st = null;
84
        ResultSet rs = null;
85
        try {
86
            st = this.getConnection().createStatement();
87
            rs = JDBCUtils.executeQuery(st, sqlbuilder.toString());
88
            if (rs.next()) {
89
                int code = rs.getInt(1);
90
                try {
91
                    IProjection proj = this.helper.getSRSSolver().getProjection(conn, code);
92
                    return proj;
93
                } catch (Exception ex) {
94
                    logger.warn("Can't create projection from code '" + code + "'.", ex);
95
                }
96
            }
97
        } catch (Exception ex) {
98
            // Do nothing, return null
99
        } finally {
100
            JDBCUtils.closeQuietly(rs);
101
            JDBCUtils.closeQuietly(st);
102
        }
103
        return null;
104

  
105
    }
106
}
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/operations/MSSQLServerOperationsFactory.java
1

  
2
package org.gvsig.mssqlserver.dal.operations;
3

  
4
import java.util.Iterator;
5
import java.util.List;
6
import org.apache.commons.lang3.tuple.Pair;
7
import org.cresques.cts.IProjection;
8
import org.gvsig.fmap.dal.SQLBuilder;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.feature.EditableFeatureType;
11
import org.gvsig.fmap.dal.feature.FeatureType;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
13
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CanModifyTableOperation;
14
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation;
15
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
16
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.OperationsFactoryBase;
17
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation;
18

  
19

  
20
public class MSSQLServerOperationsFactory extends OperationsFactoryBase {
21
    
22
    public MSSQLServerOperationsFactory(JDBCHelper helper) {
23
        super(helper);
24
    }
25

  
26
    @Override
27
    public FetchFeatureTypeOperation createFetchFeatureType(EditableFeatureType type, String database, String schema, String table, List<String> primaryKeys, String defaultGeometryField, IProjection crs) {
28
        return new MSSQLServerFetchFeatureTypeOperation(
29
                helper, type, database, schema, table, primaryKeys, 
30
                defaultGeometryField, crs
31
        );
32
    }
33

  
34
    @Override
35
    public CanModifyTableOperation createCanModifyTableOperation(String dbName, String schemaName, String tableName) {
36
        return new MSSQLServerCanModifyTableOperation(
37
                helper, dbName, schemaName, tableName
38
        );
39
    }
40

  
41
    @Override
42
    public CreateTableOperation createTable(String dbName, String schemaName, String tableName, FeatureType type, List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges, List<String> additionalSQLs) throws DataException {
43
        return new MSSQLServerCreateTableOperation( helper, dbName, schemaName, tableName, type, userAndPrivileges, additionalSQLs);
44
    }
45

  
46
    @Override
47
    public PerformChangesOperation createPerformChanges(String database, String schema, String table, FeatureType type, Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator featureTypesChanged) {
48
        return new MSSQLServerPerformChangesOperation(helper, database, schema, table, type, deleteds, inserteds, updateds, featureTypesChanged);
49
    }
50
}
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/operations/MSSQLServerCreateTableOperation.java
1

  
2
package org.gvsig.mssqlserver.dal.operations;
3

  
4
import java.util.ArrayList;
5
import java.util.List;
6
import org.apache.commons.lang3.tuple.Pair;
7
import org.gvsig.fmap.dal.SQLBuilder;
8
import org.gvsig.fmap.dal.feature.FeatureType;
9
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
10
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation;
11

  
12

  
13
public class MSSQLServerCreateTableOperation extends CreateTableOperation {
14
    
15
    public MSSQLServerCreateTableOperation(JDBCHelper helper) {
16
        this(helper, null, null, null, null, null, null);
17
    }
18
    
19
    public MSSQLServerCreateTableOperation(
20
            JDBCHelper helper,
21
            String dbName,
22
            String schemaName,
23
            String tableName,
24
            FeatureType type,
25
            List<Pair<String, SQLBuilder.Privilege>> userAndPrivileges,
26
            List<String> additionalSQLs
27
        ) {
28
        super(helper, dbName, schemaName, tableName, type, userAndPrivileges, additionalSQLs);
29
    }
30

  
31
    @Override
32
    protected List<String> buildCreateIndexesSQL(
33
            String database,
34
            String schema,
35
            String table,
36
            FeatureType type
37
        ) {
38
        // No podemos crear indices espaciales con la tabla vacia ya que SQLServer
39
        // requiere que se indique en bounding box y aun no lo sabemos.
40
        return new ArrayList<>();
41
    }
42
}
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/operations/MSSQLServerCanModifyTableOperation.java
1
package org.gvsig.mssqlserver.dal.operations;
2

  
3
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.*;
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.sql.Statement;
8
import org.apache.commons.lang3.StringUtils;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
11
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
13

  
14
public class MSSQLServerCanModifyTableOperation extends CanModifyTableOperation {
15

  
16
    public MSSQLServerCanModifyTableOperation(
17
            JDBCHelper helper
18
        ) {
19
        this(helper, null, null, null);
20
    }
21

  
22
    public MSSQLServerCanModifyTableOperation(
23
            JDBCHelper helper,
24
            String dbName,
25
            String schemaName,
26
            String tableName
27
        ) {
28
        super(helper,dbName,schemaName,tableName);
29
    }
30

  
31
    private String getFullTableName( 
32
            String dbName,
33
            String schemaName,
34
            String tableName
35
    ) {
36
        StringBuilder builder = new StringBuilder();
37
        if( !StringUtils.isEmpty(dbName) ) {
38
            builder.append(dbName);
39
        }
40
        if( !StringUtils.isEmpty(schemaName) ) {
41
            if( builder.length()>0 ) {
42
                builder.append(".");
43
            }
44
            builder.append(schemaName);
45
        }
46
        if( !StringUtils.isEmpty(tableName) ) {
47
            if( builder.length()>0 ) {
48
                builder.append(".");
49
            }
50
            builder.append(tableName);
51
        }
52
        return builder.toString();
53
    }
54
    
55
    @Override
56
    public boolean canModifyTable(Connection conn,
57
            String dbName,
58
            String schemaName,
59
            String tableName
60
        ) throws DataException {
61

  
62
        String sql = "SELECT count(*) " + 
63
                     "FROM fn_my_permissions('"+getFullTableName(dbName, schemaName, tableName)+"', 'OBJECT') " +
64
                     "WHERE permission_name in ('UPDATE', 'INSERT', 'DELETE') AND subentity_name = ''";
65
        Statement st = null;
66
        ResultSet rs = null;
67
        try {
68
            st = conn.createStatement();
69
            rs = JDBCUtils.executeQuery(st, sql);
70
            if (!rs.next()) {
71
                return false;
72
            }
73
            return rs.getInt(1)==3;
74

  
75
        } catch (SQLException ex) {
76
            throw new JDBCSQLException(ex);
77
        } finally {
78
            JDBCUtils.closeQuietly(st);
79
            JDBCUtils.closeQuietly(rs);
80
        }
81

  
82
    }
83

  
84
}
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/operations/MSSQLServerPerformChangesOperation.java
1
package org.gvsig.mssqlserver.dal.operations;
2

  
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
import org.gvsig.fmap.dal.DataTypes;
7
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
8
import org.gvsig.fmap.dal.feature.FeatureType;
9
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
10
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
11
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
13
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
14
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation;
15
import org.gvsig.mssqlserver.dal.MSSQLServerHelper;
16
import org.gvsig.mssqlserver.dal.MSSQLServerSQLBuilder.MSSQLServerCreateIndexBuilder;
17

  
18
public class MSSQLServerPerformChangesOperation extends PerformChangesOperation {
19

  
20
    public MSSQLServerPerformChangesOperation(JDBCHelper helper) {
21
        this(helper, null, null, null, null, null, null, null, null);
22
    }
23

  
24
    public MSSQLServerPerformChangesOperation(JDBCHelper helper,
25
            String dbName,
26
            String schemaName,
27
            String tableName,
28
            FeatureType featureType,
29
            Iterator<FeatureReferenceProviderServices> deleteds,
30
            Iterator<FeatureProvider> inserteds,
31
            Iterator<FeatureProvider> updateds,
32
            Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged) {
33
        super(helper, dbName, schemaName, tableName, featureType, deleteds, inserteds, updateds, featureTypesChanged);
34
    }
35
    
36
    protected MSSQLServerHelper getHelper() {
37
        return (MSSQLServerHelper) this.helper;
38
    }
39
    
40
    @Override
41
    protected List<String> buildCreateIndexSQL(
42
            String database,
43
            String schema,
44
            String table,
45
            FeatureType original,
46
            FeatureType target
47
        ) {
48
        for (FeatureAttributeDescriptor attrTarget : target) {
49
            boolean createIndex = false;
50
            if( attrTarget.isIndexed() ) {
51
                FeatureAttributeDescriptor attrOriginal = original.getAttributeDescriptor(attrTarget.getName());
52
                if ( attrOriginal == null) {
53
                    createIndex = true;
54
                } else {
55
                    if( attrOriginal.isIndexed() ) {
56
                        createIndex = false;
57
                    } else {
58
                        createIndex = true;
59
                    }
60
                }
61
            }
62
            if( createIndex ) {
63
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
64
                    try {
65
                        return this.getHelper().createOrUpdateSpatialIndexSql(
66
                                this.getConnection(),
67
                                database,
68
                                schema,
69
                                table,
70
                                attrTarget.getName()
71
                        );
72
                    } catch (Exception ex) {
73
                        throw new RuntimeException("Can't create or update spatial index.", ex);
74
                    }
75
                } else {
76
                    JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
77
                    MSSQLServerCreateIndexBuilder create_index = (MSSQLServerCreateIndexBuilder) sqlbuilder.create_index();
78
                    create_index.if_not_exist();
79
                    create_index.name("idx_" + table + "_" + attrTarget.getName());
80
                    create_index.column(attrTarget.getName());
81
                    create_index.table().database(database).schema(schema).name(table);
82
                    return create_index.toStrings();
83
                }
84
            }
85
        }
86
        return new ArrayList<>();
87
    }    
88
}
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerHelper.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2016 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 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
 */
22
package org.gvsig.mssqlserver.dal;
23

  
24
import org.gvsig.mssqlserver.dal.operations.MSSQLServerOperationsFactory;
25
import java.sql.Connection;
26
import java.sql.ResultSet;
27
import java.sql.SQLException;
28
import java.sql.Statement;
29
import java.text.MessageFormat;
30
import java.util.ArrayList;
31
import java.util.List;
32
import org.apache.commons.dbcp.BasicDataSource;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.fmap.dal.DataParameters;
35
import org.gvsig.fmap.dal.DataTypes;
36
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.dal.SQLBuilder;
39
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
40
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
41
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
42
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
44
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException;
45
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
46
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
47
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
48
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
49
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
50
import org.gvsig.fmap.geom.Geometry;
51
import org.gvsig.fmap.geom.primitive.Envelope;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

  
55
public class MSSQLServerHelper extends JDBCHelperBase {
56

  
57
    private static final Logger logger = LoggerFactory.getLogger(MSSQLServerHelper.class);
58

  
59
    public static final String NAME = "MSSQLServer";
60
    public static final String INSTANCE_NAME = "SQLEXPRESS";
61
    public static final int PORT = 1433;
62
    public static final String MSSQLServerJDBCDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
63
    
64
//    private static final boolean ALLOW_AUTOMATIC_VALUES = true;
65
//    private static final String QUOTE_FOR_USE_IN_IDENTIFIERS = "\"";
66
//    private static final String QUOTE_FOR_USE_IN_STRINGS = "'";
67

  
68
    public static String getConnectionURL(JDBCConnectionParameters params) {
69
        String connectionURL = MessageFormat.format(
70
                //"jdbc:sqlserver://{0}:{1,number,#};databaseName={2};instanceName={3};",
71
                "jdbc:sqlserver://{0};databaseName={2};instanceName={3};",
72
                params.getHost(),
73
                params.getPort(),
74
                params.getDBName(),
75
                ((DataParameters) params).getDynValue("instanceName")
76
        );
77
        logger.debug("connectionURL: {}", connectionURL);
78
        return connectionURL;
79
    }
80

  
81
    private static class ConnectionProvider {
82

  
83
        private static boolean needRegisterDriver = true;
84

  
85
        private BasicDataSource dataSource = null;
86

  
87
        private final JDBCConnectionParameters connectionParameters;
88

  
89
        public ConnectionProvider(JDBCConnectionParameters connectionParameters) {
90
            this.connectionParameters = connectionParameters;
91
        }
92

  
93
        public Connection getConnection() throws SQLException {
94
            if (this.dataSource == null) {
95
                this.dataSource = this.createDataSource();
96
            }
97
            Connection conn = this.dataSource.getConnection();
98
            return conn;
99
        }
100

  
101
        private BasicDataSource createDataSource() throws SQLException {
102
            if (!this.isRegistered()) {
103
                this.registerDriver();
104
            }
105
            JDBCConnectionParameters params = connectionParameters;
106

  
107
            BasicDataSource dataSource = new BasicDataSource();
108
            dataSource.setDriverClassName(params.getJDBCDriverClassName());
109
            dataSource.setUsername(params.getUser());
110
            dataSource.setPassword(params.getPassword());
111
            dataSource.setUrl(params.getUrl());
112

  
113
            dataSource.setMaxWait(60L * 1000);
114
            return dataSource;
115
        }
116

  
117
        private boolean isRegistered() {
118
            return needRegisterDriver;
119
        }
120

  
121
        public void registerDriver() throws SQLException {
122
            String className = this.connectionParameters.getJDBCDriverClassName();
123
            if (className == null) {
124
                return;
125
            }
126
            try {
127
                Class theClass = Class.forName(className);
128
                if (theClass == null) {
129
                    throw new JDBCDriverClassNotFoundException(MSSQLServerLibrary.NAME, className);
130
                }
131
            } catch (Exception e) {
132
                throw new SQLException("Can't register JDBC driver '" + className + "'.", e);
133
            }
134
            needRegisterDriver = false;
135
        }
136

  
137
    }
138

  
139
    private ConnectionProvider connectionProvider = null;
140

  
141
    private FeatureType lastUsedFeatureType = null;
142
    private String lastUsedSpatialType = null;
143
    
144
    public MSSQLServerHelper(JDBCConnectionParameters connectionParameters) {
145
        super(connectionParameters);
146
    }
147

  
148
    @Override
149
    public Connection getConnection() throws AccessResourceException {
150
        try {
151
            if (this.connectionProvider == null) {
152
                this.connectionProvider = new ConnectionProvider(this.getConnectionParameters());
153
            }
154
            return this.connectionProvider.getConnection();
155
        } catch (SQLException ex) {
156
            throw new AccessResourceException(MSSQLServerLibrary.NAME, ex);
157
        }
158
    }
159
    
160
    @Override
161
    public String getConnectionURL() {
162
        return getConnectionURL(this.getConnectionParameters());
163
    }
164

  
165
    @Override
166
    protected String getResourceType() {
167
        return MSSQLServerLibrary.NAME;
168
    }
169

  
170
    @Override
171
    public String getProviderName() {
172
        return MSSQLServerLibrary.NAME;
173
    }
174

  
175
    @Override
176
    public JDBCSQLBuilderBase createSQLBuilder() {
177
        return new MSSQLServerSQLBuilder(this);
178
    }
179

  
180
    /**
181
     * Devuelbe el nombre del tipo de datos espacial de SQLServer asociado 
182
     * al ultimo FeatureType cargado.
183
     * 
184
     * Esta funcion es usada para dar un soporte limitado al tipo de datos
185
     * spaciales "geography".
186
     * 
187
     * Si no disponemos de un FeatureType asumimos "geometry".
188
     * Si hay un solo campo espacial asume el tipo de ese campo, y si 
189
     * hay mas de uno asume "geometry".
190
     * En caso de que no haya ningun campo espacial asumimos "geometry".
191
     * 
192
     * @return "geometry" or "geography" for the last used table.
193
     */
194
    public String getSpatialType() {
195
        if( this.lastUsedSpatialType != null ) {
196
            return this.lastUsedSpatialType;
197
        }
198
        if( this.lastUsedFeatureType == null ) {
199
            this.lastUsedSpatialType = "geometry";
200
            return this.lastUsedSpatialType;
201
        }
202
        String spatialType = null;
203
        for (FeatureAttributeDescriptor attr : lastUsedFeatureType) {
204
            if( attr.getType() == DataTypes.GEOMETRY ) {
205
                if( spatialType != null ) {
206
                    this.lastUsedSpatialType = "geometry";
207
                    return this.lastUsedSpatialType;
208
                }
209
                spatialType = (String) attr.getAdditionalInfo("SQLServer_type_name");
210
            }
211
        }
212
        if( StringUtils.isEmpty(spatialType) ) {
213
            this.lastUsedSpatialType = "geometry";
214
            return this.lastUsedSpatialType;
215
        }
216
        this.lastUsedSpatialType = spatialType;
217
        return this.lastUsedSpatialType;
218
    }
219

  
220
    /**
221
     * Devuelbe el tipo de datos espacial de SQLServer asociado a la columna 
222
     * indicada.
223
     * 
224
     * Esta funcion es usada para dar un soporte limitado al tipo de datos
225
     * spaciales "geography".
226
     * 
227
     * Si no disponemos de un FeatureType asumimos "geometry".
228
     * Si no existe el campo solicitado o no es de tipo geoemtria, asumimos 
229
     * el tipo espacia asociado al FeatureType (getSpatialType()).
230
     * 
231
     * @param columnName
232
     * @return "geometry" or "geography" for column in the last used table.
233
     */
234
    public String getSpatialType(String columnName) {
235
        if( this.lastUsedFeatureType == null ) {
236
            return "geometry";
237
        }
238
        FeatureAttributeDescriptor attr = this.lastUsedFeatureType.getAttributeDescriptor(columnName);
239
        if( attr == null ) {
240
            return this.getSpatialType();
241
        }
242
        if( attr.getType() != DataTypes.GEOMETRY ) {
243
            return this.getSpatialType();
244
        }
245
        String spatialType = (String) attr.getAdditionalInfo("SQLServer_type_name");
246
        if( StringUtils.isEmpty(spatialType) ) {
247
            return "geometry";
248
        }
249
        return spatialType;
250
    }
251
    
252
    public void setLastUsedFeatureType(FeatureType featureType) {
253
        this.lastUsedSpatialType = null;
254
        this.lastUsedFeatureType = featureType;
255
    }
256

  
257
    public void createOrUpdateSpatialIndex(
258
            Connection conn,
259
            String database,
260
            String schema,
261
            String table,
262
            String columnName
263
        ) throws JDBCSQLException {
264
        List<String> sqls = this.createOrUpdateSpatialIndexSql(
265
                conn, database, schema, table, columnName
266
        );
267
        Statement st = null;
268
        try {
269
            st = conn.createStatement();
270
            for (String sql : sqls) {
271
                JDBCUtils.execute(st, sql);
272
            }
273
        } catch (SQLException ex) {
274
            throw new JDBCSQLException(ex);
275
        } finally {
276
            JDBCUtils.closeQuietly(st);
277
        }
278
        
279
    }
280
    
281
    public List<String> createOrUpdateSpatialIndexSql(
282
            Connection conn,
283
            String database,
284
            String schema,
285
            String table,
286
            String columnName
287
        ) throws JDBCSQLException {
288
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
289
        MSSQLServerSQLBuilder.MSSQLServerCreateIndexBuilder create_index = (MSSQLServerSQLBuilder.MSSQLServerCreateIndexBuilder) sqlbuilder.create_index();
290

  
291
        sqlbuilder.create_index().spatial();
292
        create_index.setBoundingBox(
293
                this.getBoundingBox(
294
                        conn,
295
                        database,
296
                        schema,
297
                        table,
298
                        columnName
299
                )
300
        );
301
        create_index.if_not_exist();
302
        create_index.name("idx_" + table + "_" + columnName);
303
        create_index.column(columnName);
304
        create_index.table().database(database).schema(schema).name(table);
305

  
306
        return create_index.toStrings();
307
    }    
308
    
309
    public Envelope getBoundingBox(
310
            Connection conn,
311
            String database,
312
            String schema,
313
            String table,
314
            String columnName
315
        ) throws JDBCSQLException {
316
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
317
        sqlbuilder.select().column().value(
318
            sqlbuilder.getAsGeometry(
319
                sqlbuilder.ST_ExtentAggregate(
320
                        sqlbuilder.column(columnName)
321
                )
322
            )
323
        );
324
        sqlbuilder.select().from().table().database(database).schema(schema).name(table);
325

  
326
        String sql = sqlbuilder.select().toString();
327
        Statement st = null;
328
        ResultSet rs = null;
329
        try {
330
            st = conn.createStatement();
331
            rs = JDBCUtils.executeQuery(st, sql);
332
            if (!rs.next()) {
333
                return null;
334
            }
335
            Geometry geom = this.getGeometryFromColumn(rs, 1);
336
            if (geom == null) {
337
                return null;
338
            }
339
            return geom.getEnvelope();
340

  
341
        } catch (Exception ex) {
342
            throw new JDBCSQLException(ex);
343
        } finally {
344
            JDBCUtils.closeQuietly(st);
345
            JDBCUtils.closeQuietly(rs);
346
        }        
347
    }
348
    
349
    @Override
350
    public OperationsFactory getOperations() {
351
        if (this.operationsFactory == null) {
352
            this.operationsFactory = new MSSQLServerOperationsFactory(this);
353
        }
354
        return operationsFactory;
355
    }
356

  
357
    @Override
358
    public SQLBuilder.GeometrySupportType getGeometrySupportType() {
359
        return SQLBuilder.GeometrySupportType.WKB;
360
    }
361

  
362
    @Override
363
    public boolean hasSpatialFunctions() {
364
        return true;
365
    }
366

  
367
    @Override
368
    public boolean canWriteGeometry(int geometryType, int geometrySubtype) {
369
        return true;
370
    }
371

  
372
    @Override
373
    public String getQuoteForIdentifiers() {
374
        return "\"";
375
    }
376

  
377
    @Override
378
    public boolean allowAutomaticValues() {
379
        return true;
380
    }
381

  
382
    @Override
383
    public boolean supportOffsetInSelect() {
384
        return true;
385
    }
386

  
387
    @Override
388
    public String getQuoteForStrings() {
389
        return "'";
390
    }
391

  
392
    @Override
393
    public String getSourceId(JDBCStoreParameters parameters) {
394
        return parameters.getHost() + ":" +
395
               parameters.getDynValue("InstanceName")+ ":" + 
396
               parameters.getDBName() + "." + 
397
               parameters.getSchema()+ "." + 
398
               parameters.getTable();
399
    }
400

  
401
    @Override
402
    public JDBCNewStoreParameters createNewStoreParameters() {
403
        return new MSSQLServerNewStoreParameters();
404
    }
405

  
406
    @Override
407
    public JDBCStoreParameters createOpenStoreParameters() {
408
        return new MSSQLServerStoreParameters();
409
    }
410

  
411
    @Override
412
    public JDBCServerExplorerParameters createServerExplorerParameters() {
413
        return new MSSQLServerExplorerParameters();
414
    }
415

  
416
}
tags/org.gvsig.mssqlserver-1.0.30/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerStoreProviderFactory.java
1

  
2
package org.gvsig.mssqlserver.dal;
3

  
4
import org.gvsig.fmap.dal.DataParameters;
5
import org.gvsig.fmap.dal.exception.InitializeException;
6
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
7
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
8
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
9
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
10
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider;
11
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCStoreProviderFactory;
12

  
13

  
14
public class MSSQLServerStoreProviderFactory extends JDBCStoreProviderFactory {
15

  
16
    private static final String NAME = MSSQLServerLibrary.NAME;
17
    
18
    public MSSQLServerStoreProviderFactory() {
19
        super(
20
                NAME, 
21
                "Microsoft SQL Server store"
22
        );
23
    }
24

  
25
    @Override
26
    public JDBCStoreProvider createProvider(
27
            DataParameters parameters,
28
            DataStoreProviderServices providerServices
29
    ) throws InitializeException {
30
        JDBCHelper helper = new MSSQLServerHelper((JDBCConnectionParameters) parameters);
31
        JDBCStoreProvider provider = helper.createProvider(
32
                (JDBCStoreParameters) parameters, 
33
                providerServices
34
        );
35
        return provider;
36
    }
37

  
38
    @Override
39
    public JDBCStoreParameters createParameters() {
40
        JDBCStoreParameters params = new MSSQLServerStoreParameters();
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff