Revision 237

View differences:

tags/org.gvsig.mssqlserver-1.0.41/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.41/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.41/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.41/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.41/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.41/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.41/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerSQLBuilder.java
1
package org.gvsig.mssqlserver.dal;
2

  
3
import java.text.MessageFormat;
4
import java.util.ArrayList;
5
import java.util.List;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import org.gvsig.expressionevaluator.Formatter;
8
import org.gvsig.fmap.dal.DataTypes;
9
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
10
import org.gvsig.fmap.geom.primitive.Envelope;
11
import org.gvsig.mssqlserver.dal.expressionbuilderformatter.MSSQLServerFormatter;
12

  
13
public class MSSQLServerSQLBuilder extends JDBCSQLBuilderBase {
14
    
15
    protected Formatter formatter = null;
16
    public static final String FORMAT_ST_GEOMFROMEWKB= "geometry::STGeomFromWKB({0}, {1})";
17
    public static final String FORMAT_ST_GEOMFROMWKB= "geometry::STGeomFromWKB({0}, {1})";
18
    public static final String FORMAT_ST_GEOMFROMTEXT= "geometry::STGeomFromText({0}, {1})";
19
    public static final String FORMAT_OPERATOR_ILIKE = "LOWER({0}) LIKE LOWER({1})";
20
    public static final String FORMAT_OPERATOR_NOTISNULL = "( ({0}) IS NOT NULL )";
21
    
22
//    public interface MSSQLServerSQLConfig extends SQLConfig {
23
//        public static final String ST_GeomFromTextEx = "ST_GeomFromTextEx";
24
//        public static final String ST_GeomFromWKBEx = "ST_GeomFromWKBEx";
25
//        public static final String ST_GeomFromEWKBEx = "ST_GeomFromEWKBEx";
26
//        public static final String ST_ExtentAggregateEx = "ST_ExtentAggregateEx";
27
//        public static final String ST_UnionAggregateEx = "ST_UnionAggregateEx";
28
//    }
29
 
30
    public MSSQLServerSQLBuilder(MSSQLServerHelper helper) {
31
        super(helper);
32
        
33
        this.defaultSchema = "dbo";
34
        this.allowAutomaticValues = true;
35
        this.geometrySupportType = this.helper.getGeometrySupportType();
36
        this.hasSpatialFunctions = this.helper.hasSpatialFunctions();
37

  
38
        STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = null;
39
        STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = null;
40
 
41
        this.STMT_UPDATE_TABLE_STATISTICS_table ="UPDATE STATISTICS {0}";
42

  
43
        this.type_geometry = "GEOMETRY";
44
        this.constant_true = "1=1"; // TODO ?
45
        this.constant_false = "1<>1"; // TODO ?
46
    
47
        this.type_boolean = "BIT";
48
        this.type_double = "FLOAT"; //float con 53 bits de mantisa = float(54)
49
        this.type_numeric_p = "NUMERIC({0})";
50
        this.type_numeric_ps = "NUMERIC({0},{1})";
51
        this.type_bigdecimal = "NUMERIC({0},{1})";
52
        this.type_float = "REAL"; //float con 24 bits de mantisa = float(24)
53
        this.type_int = "INT";
54
        this.type_long = "BIGINT";        
55
        this.type_byte = "TINYINT";
56
        
57
        this.type_date = "DATE";
58
        this.type_time = "TIME";
59

  
60
        this.type_char = "CHAR(1)";
61
        this.type_string = "TEXT";
62
        this.type_string_p = "VARCHAR({0})";
63

  
64
        this.type_version = "VARCHAR(30)";
65
        this.type_URI = "TEXT";
66
        this.type_URL = "TEXT";
67
        this.type_FILE = "TEXT";
68
        this.type_FOLDER = "TEXT";
69
        
70
        this.type_bytearray = "VARBINARY";
71
        
72
        this.type_timestamp = "TIMESTAMP";
73

  
74
//       DID this.ST_SRID, "(({0}).STSrid)");
75
//       DID this.ST_AsText, "({0}).STAsText()");
76
//       DID this.ST_AsBinary, "({0}).STAsBinary()");
77
//       DID this.ST_AsEWKB, "({0}).STAsBinary()");
78
//       DID this.ST_ExtentAggregate, "geometry::EnvelopeAggregate({0})");
79
//       DID this.ST_UnionAggregate, "geometry::UnionAggregate({0})");
80
//       DID this.ST_Contains, "(({0}).STContains({1})=1)");
81
//       DID this.ST_Crosses, "(({0}).STCrosses({1})=1)");
82
//       DID this.ST_Disjoint, "(({0}).STDisjoint({1})=1)");
83
//       DID this.ST_Equals, "(({0}).STEquals({1})=1)");
84
//       DID this.ST_IsClosed, "(({0}).STIsClosed()=1)");
85
//       DID this.ST_Overlaps, "(({0}).STOverlaps({1})=1)");
86
//       DID this.ST_Touches, "(({0}).STTouches({1})=1)");
87
//       DID this.ST_Within, "(({0}).STWithin ({1})=1)");
88
//       DID this.ST_Envelope, "({0}).STEnvelope()");
89
//       DID this.ST_Intersects, "(({0}).STIntersects({1})=1)");
90
//       DID this.ST_GeomFromText, "geometry::STGeomFromText({0}, {1})");
91
//       DID this.ST_GeomFromWKB, "geometry::STGeomFromWKB({0}, {1})");
92
//       DID this.ST_GeomFromEWKB, "geometry::STGeomFromWKB({0}, {1})");
93
//       DID this.ST_Simplify, "({0}).Reduce({1})");
94
//        this.lcase, "LOWER({0})");
95
//        this.ucase, "UPPER({0})");
96
//
97
//        config.set(MSSQLServerSQLConfig.ST_ExtentAggregateEx, "{1}::EnvelopeAggregate({0})");
98
//        config.set(MSSQLServerSQLConfig.ST_UnionAggregateEx, "{1}::UnionAggregate({0})");
99
//        config.set(MSSQLServerSQLConfig.ST_GeomFromTextEx, "{2}::STGeomFromText({0}, {1})");
100
//        config.set(MSSQLServerSQLConfig.ST_GeomFromWKBEx, "{2}::STGeomFromWKB({0}, {1})");
101
//        config.set(MSSQLServerSQLConfig.ST_GeomFromEWKBEx, "{2}::STGeomFromWKB({0}, {1})");
102

  
103
    }
104

  
105
    @Override
106
    public MSSQLServerHelper getHelper() {
107
        return (MSSQLServerHelper) helper;
108
    }
109
    
110
    @Override
111
    protected Formatter formatter() {
112
        if( this.formatter==null ) {
113
            this.formatter = new MSSQLServerFormatter(this);
114
        }
115
        return this.formatter;
116
    }
117
    
118
    protected class MSSQLServerDropTableBuilder extends DropTableBuilderBase {
119

  
120
        @Override
121
           public List<String> toStrings(Formatter formatter) {
122
            List<String> sqls = new ArrayList<>();
123

  
124
            sqls.add(
125
                    MessageFormat.format(
126
                            STMT_DROP_TABLE_table,
127
                            this.table.toString(formatter)
128
                    )
129
            );
130
            return sqls;
131
        }
132
    }
133
    
134
    public class MSSQLServerCreateIndexBuilder extends CreateIndexBuilderBase {
135

  
136
        private Envelope boundingBox;
137
        
138
        public MSSQLServerCreateIndexBuilder() {
139
            super();
140
            this.boundingBox = null;
141
        }
142
        
143
        public void setBoundingBox(Envelope boundingBox) {
144
            this.boundingBox = boundingBox;
145
        }
146
        
147
        private double getXMin() {
148
            // https://alastaira.wordpress.com/2011/07/26/determining-the-geographic-extent-of-spatial-features-in-a-sql-server-table/
149
            return Math.min(boundingBox.getUpperCorner().getX(), boundingBox.getLowerCorner().getX());
150
        }
151
        
152
        private double getYMin() {
153
            return Math.min(boundingBox.getUpperCorner().getY(), boundingBox.getLowerCorner().getY());
154
        }
155
        
156
        private double getXMax() {
157
            return Math.max(boundingBox.getUpperCorner().getX(), boundingBox.getLowerCorner().getX());
158
        }
159
        
160
        private double getYMax() {
161
            return Math.max(boundingBox.getUpperCorner().getY(), boundingBox.getLowerCorner().getY());
162
        }
163
        
164
        @Override
165
        public List<String> toStrings() {
166
            StringBuilder builder = new StringBuilder();
167
            if( this.isSpatial ) {
168
                builder.append("CREATE SPATIAL INDEX ");
169
                builder.append(as_identifier(this.indexName));
170
                builder.append(" ON ");
171
                builder.append(this.table.toString());
172
                builder.append(" ( ");
173
                boolean is_first_column = true;
174
                for( String column : this.columns) {
175
                    if( is_first_column ) {
176
                        is_first_column = false;
177
                    } else {
178
                        builder.append(", ");
179
                    }
180
                    builder.append(column);
181
                }
182
                builder.append(" ) ");
183
                builder.append("USING GEOMETRY_GRID ");
184
                builder.append("WITH( ");
185
                builder.append("BOUNDING_BOX  = ( ");
186
                builder.append("xmin  = ").append(this.getXMin()).append(", ");
187
                builder.append("ymin  = ").append(this.getYMin()).append(", ");
188
                builder.append("xmax  = ").append(this.getXMax()).append(", ");
189
                builder.append("ymax  = ").append(this.getYMax());
190
                builder.append(" )");
191
//                builder.append(", DROP_EXISTING = ON");
192
//                builder.append(", GRIDS  = ( LEVEL_1  = MEDIUM, LEVEL_2  = MEDIUM, LEVEL_3  = MEDIUM, LEVEL_4  = MEDIUM)");
193
//                builder.append(", CELLS_PER_OBJECT  = 16");
194
//                builder.append(", STATISTICS_NORECOMPUTE = OFF");
195
//                builder.append(", ALLOW_ROW_LOCKS = ON");
196
//                builder.append(", ALLOW_PAGE_LOCKS = ON");
197
                builder.append(" ) ");
198
            } else {
199
                builder.append("CREATE ");
200
                if( this.isUnique ) {
201
                    builder.append("UNIQUE ");
202
                }
203
                builder.append("INDEX ");
204
//                if( this.ifNotExist ) {
205
//                    builder.append("IF NOT EXISTS ");
206
//                }
207
                builder.append(as_identifier(this.indexName));
208
                builder.append(" ON ");
209
                builder.append(this.table.toString());
210
                builder.append(" ( ");
211
                boolean is_first_column = true;
212
                for( String column : this.columns) {
213
                    if( is_first_column ) {
214
                        is_first_column = false;
215
                    } else {
216
                        builder.append(", ");
217
                    }
218
                    builder.append(column);
219
                }
220
                builder.append(" )");
221
            }
222
            List<String> sqls = new ArrayList<>();
223
            sqls.add(builder.toString());
224
            return sqls;
225
        }
226
        
227
    }
228
    
229
//    public class MSSQLServerParameter extends ParameterBase {
230
//
231
//        public MSSQLServerParameter() {
232
//            super();
233
//        }
234
//
235
//        @Override
236
//        public String toString() {
237
//            if( this.type == ParameterType.Geometry ) {
238
//                String spatialType = getHelper().getSpatialType(this.getName());
239
//                switch( config.getGeometryTypeSupport() ) {
240
//                    case EWKB:
241
//                        return MessageFormat.format(
242
//                                config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx),
243
//                                "?",
244
//                                String.valueOf(this.srs.toString()),
245
//                                custom(spatialType)
246
//                        );
247
//                    case NATIVE:
248
//                    case WKB:
249
//                        return MessageFormat.format(
250
//                                config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx),
251
//                                "?",
252
//                                String.valueOf(this.srs.toString()),
253
//                                custom(spatialType)
254
//                        );
255
//                    case WKT:
256
//                    default:
257
//                        return MessageFormat.format(
258
//                                config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx),
259
//                                "?",
260
//                                String.valueOf(this.srs.toString()),
261
//                                custom(spatialType)
262
//                        );                        
263
//                }                            
264
//            }
265
//            return super.toString();
266
//        }
267
//    }
268
//    
269
//    public class MSSQLServerGeometryValue extends GeometryValueBase {
270
//        
271
//        public MSSQLServerGeometryValue(Geometry geometry, IProjection projection) {
272
//            super(geometry, projection);
273
//        }
274
//        
275
//        @Override
276
//        public String toString() {
277
//            try {
278
//                String spatialType = getHelper().getSpatialType();
279
//                switch( config.getGeometryTypeSupport() ) {
280
//                    case EWKB:
281
//                        return MessageFormat.format(
282
//                                config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx),
283
//                                bytearray(this.geometry.convertToEWKB()),
284
//                                String.valueOf(getSRSId(this.projection)),
285
//                                custom(spatialType)
286
//                        );
287
//                    case NATIVE:
288
//                    case WKB:
289
//                        return MessageFormat.format(
290
//                                config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx),
291
//                                bytearray(this.geometry.convertToWKB()),
292
//                                String.valueOf(getSRSId(this.projection)),
293
//                                custom(spatialType)
294
//                        );
295
//                    case WKT:
296
//                    default:
297
//                        return MessageFormat.format(
298
//                                config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx),
299
//                                string(this.geometry.convertToWKT()),
300
//                                String.valueOf(getSRSId(this.projection)),
301
//                                custom(spatialType)
302
//                        );                        
303
//                }
304
//            } catch (Exception ex) {
305
//                throw new RuntimeException("Can't convert geometry to string.",ex);
306
//            }
307
//        }
308
//    }
309

  
310
    protected class MSSQLServerSelectBuilder extends SelectBuilderBase {
311
        
312
        @Override
313
        protected boolean isValid(StringBuilder message) {
314
            if( message == null ) {
315
                message = new StringBuilder();
316
            }
317
            if( this.has_offset() && !this.has_order_by() ) {
318
                // Algunos gestores de BBDD requieren que se especifique un
319
                // orden para poder usar OFFSET. Como eso parece buena idea para
320
                // asegurar que siempre tengamos los mismo resultados, lo exijimos
321
                // siempre.
322
                message.append("Can't use OFFSET without an ORDER BY.");
323
                return false;
324
            }
325
            return true;
326
        }
327
        
328
        @Override
329
        public String toString(Formatter<ExpressionBuilder.Value> formatter) {
330
            // MSSQLServer usa TOP en lugar de LIMIT y la sintaxis para OFFSET
331
            // es ligeramente distinta de la que hay en SelectBuilderBase
332
            StringBuilder builder = new StringBuilder();
333

  
334
            builder.append("SELECT ");
335
            if( this.distinct ) {
336
                builder.append("DISTINCT ");
337
            }
338
            if (this.has_limit() && !this.has_offset() ) {
339
                builder.append("TOP ");
340
                builder.append(this.limit);
341
                builder.append(" ");
342
           }
343
            boolean first = true;
344
            for (SelectColumnBuilder column : columns) {
345
                if (first) {
346
                    first = false;
347
                } else {
348
                    builder.append(", ");
349
                }
350
                builder.append(column.toString());
351
            }
352

  
353
            if (this.has_from()) {
354
                builder.append(" FROM ");
355
                builder.append(this.from.toString(formatter));
356
            }
357
            if (this.has_where()) {
358
                builder.append(" WHERE ");
359
                builder.append(this.where.toString(formatter));
360
            }
361

  
362
            if( this.has_order_by() ) {
363
                builder.append(" ORDER BY ");
364
                first = true;
365
                for (OrderByBuilder item : this.order_by) {
366
                    if (first) {
367
                        first = false;
368
                    } else {
369
                        builder.append(", ");
370
                    }
371
                    builder.append(item.toString());
372
                }
373
            }
374

  
375
            if (this.has_offset()) {
376
                // Require SQLSeerver >= 2012
377
                builder.append(" OFFSET ");
378
                builder.append(this.offset);
379
                builder.append(" ROWS");
380
                if( this.has_limit() ) {
381
                    builder.append(" FETCH NEXT ");
382
                    builder.append(this.limit);
383
                    builder.append(" ROWS ONLY ");
384
                }
385
            }
386
            return builder.toString();
387
        }
388
    }
389

  
390
    protected class MSSQLServerCreateTableBuilder extends CreateTableBuilderBase {
391

  
392
        @Override
393
//        List<String> toStrings(Formatter formatter)
394
        public List<String> toStrings(Formatter formatter) {
395
            //
396
            // Respecto al base cambia la declaracion de campo automaticos
397
            // SQLServer usa IDENTITY en lugar de SERIAL.
398
            //
399
            List<String> sqls = new ArrayList<>();
400
            StringBuilder builder = new StringBuilder();
401

  
402
            builder.append("CREATE TABLE ");
403
            builder.append(this.table.toString());
404
            builder.append(" (");
405
            boolean first = true;
406
            for (ColumnDescriptor column : columns) {
407
                if (first) {
408
                    first = false;
409
                } else {
410
                    builder.append(", ");
411
                }
412
                builder.append(as_identifier(column.getName()));
413
                builder.append(" ");
414
                if( column.isAutomatic() && column.getType() == DataTypes.INT ) {
415
                    builder.append("INT");
416
                    builder.append(" IDENTITY(1,1)");
417
                } else if( column.isAutomatic() && column.getType() == DataTypes.LONG ) {
418
                    builder.append("BIGINT");
419
                    builder.append(" IDENTITY(1,1)");
420
                } else {
421
                    builder.append(
422
                        sqltype(
423
                            column.getType(), 
424
                            column.getPrecision(), 
425
                            column.getSize(),
426
                            column.getGeometryType(), 
427
                            column.getGeometrySubtype()
428
                        )
429
                    );
430
                }
431
                if (column.getDefaultValue() == null) {
432
                    if (column.allowNulls()) {
433
                        builder.append(" DEFAULT NULL");
434
                    }
435
                } else {
436
                    builder.append(" DEFAULT '");
437
                    builder.append(column.getDefaultValue().toString());
438
                    builder.append("'");
439
                }
440
                if (column.allowNulls()) {
441
                    builder.append(" NULL");
442
                } else {
443
                    builder.append(" NOT NULL");
444
                }
445
                if (column.isPrimaryKey()) {
446
                    builder.append(" PRIMARY KEY");
447
                }
448
            }
449
            builder.append(" )");
450
            sqls.add(builder.toString());
451

  
452
            return sqls;
453
        }
454
    }
455

  
456
    @Override
457
    protected SelectBuilder createSelectBuilder() {
458
        return new MSSQLServerSelectBuilder();
459
    }
460

  
461
    @Override
462
    protected CreateTableBuilder createCreateTableBuilder() {
463
        return new MSSQLServerCreateTableBuilder();
464
    }
465
    
466
    protected DropTableBuilder createDropTableBuilder() {
467
        return new MSSQLServerDropTableBuilder();
468
    }
469
    
470
    @Override
471
    protected CreateIndexBuilder createCreateIndexBuilder() {
472
        return new MSSQLServerCreateIndexBuilder();
473
    }
474
      
475
    @Override
476
    public String as_identifier(String id) {
477
        // En SQLServer se aceptan las comillas dobles pero se prefiere
478
        // corchetes [xx]. Asi que si hay comillas dobles las quitamos
479
        // y ponemos los corchetes.
480
        String quote = this.quote_for_identifiers();
481
        if (id.startsWith(quote)) {
482
            id = id.substring(1, id.length()-1);
483
        } else if( id.startsWith("[") ) {
484
            return id;
485
        }
486
        if( id.contains("(") ) {
487
            LOGGER.warn("Suspicious use of 'identifier' in sql.");
488
        }
489
        return "[" + id + "]";
490
    }
491

  
492
//    @Override
493
//    public GeometryValue geometry(Geometry geom, IProjection projection) {
494
//        return new MSSQLServerGeometryValue(geom, projection);
495
//    }
496

  
497
//    @Override
498
//    public Parameter parameter() {
499
//        return new MSSQLServerParameter();
500
//    }
501
    
502
//    @Override
503
//    public Function ST_ExtentAggregate(Value geom) {
504
//        String spatialType = getHelper().getSpatialType();
505
//        return builtin_function("ST_ExtentAggregate", 
506
//                config.getString(MSSQLServerSQLConfig.ST_ExtentAggregateEx), 
507
//                geom,
508
//                custom(spatialType)
509
//        );
510
//    }
511
//
512
//    @Override
513
//    public Function ST_UnionAggregate(Value geom) {
514
//        String spatialType = getHelper().getSpatialType();
515
//        return builtin_function("ST_UnionAggregate", 
516
//                config.getString(MSSQLServerSQLConfig.ST_UnionAggregateEx), 
517
//                geom,
518
//                custom(spatialType)
519
//        );
520
//    }
521
//
522
//    @Override
523
//    public Function ST_GeomFromText(Value geom, Value crs) {
524
//        String spatialType = getHelper().getSpatialType();
525
//        return builtin_function("ST_GeomFromText", 
526
//                config.getString(MSSQLServerSQLConfig.ST_GeomFromTextEx), 
527
//                geom, 
528
//                crs,
529
//                custom(spatialType)
530
//        );
531
//    }
532
//
533
//    @Override
534
//    public Function ST_GeomFromWKB(Value geom, Value crs) {
535
//        String spatialType = getHelper().getSpatialType();
536
//        return builtin_function("ST_GeomFromWKB", 
537
//                config.getString(MSSQLServerSQLConfig.ST_GeomFromWKBEx), 
538
//                geom, 
539
//                crs,
540
//                custom(spatialType)
541
//        );
542
//    }
543
//
544
//    @Override
545
//    public Function ST_GeomFromEWKB(Value geom, Value crs) {
546
//        String spatialType = getHelper().getSpatialType();
547
//        return builtin_function("ST_GeomFromEWKB", 
548
//                config.getString(MSSQLServerSQLConfig.ST_GeomFromEWKBEx), 
549
//                geom, 
550
//                crs,
551
//                custom(spatialType)
552
//        );
553
//    }
554

  
555
}
tags/org.gvsig.mssqlserver-1.0.41/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerExplorerFactory.java
1

  
2
package org.gvsig.mssqlserver.dal;
3

  
4
import org.gvsig.fmap.dal.DataServerExplorer;
5
import org.gvsig.fmap.dal.DataServerExplorerParameters;
6
import org.gvsig.fmap.dal.exception.InitializeException;
7
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
8
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
9
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
10
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
11
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
12
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCServerExplorerFactory;
13

  
14

  
15
public class MSSQLServerExplorerFactory extends JDBCServerExplorerFactory {
16

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

  
26
    @Override
27
    public JDBCServerExplorer create(
28
            DataServerExplorerParameters parameters, 
29
            DataServerExplorerProviderServices providerServices
30
        ) throws InitializeException {
31
        JDBCHelper helper = new MSSQLServerHelper((JDBCConnectionParameters) parameters);
32
        JDBCServerExplorer server = helper.createServerExplorer(
33
                (JDBCServerExplorerParameters) parameters, 
34
                providerServices
35
        );
36
        return server;
37
    }
38
        
39

  
40
    @Override
41
    public JDBCServerExplorerParameters createParameters() {
42
        JDBCServerExplorerParameters params = new MSSQLServerExplorerParameters();
43
        return params;    
44
    }
45
    
46
}
tags/org.gvsig.mssqlserver-1.0.41/org.gvsig.mssqlserver.provider/src/main/java/org/gvsig/mssqlserver/dal/MSSQLServerNewStoreParameters.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.mssqlserver.dal;
23

  
24
import org.apache.commons.lang3.StringUtils;
25
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
26

  
27
public class MSSQLServerNewStoreParameters extends JDBCNewStoreParameters {
28

  
29
    public MSSQLServerNewStoreParameters() {
30
        super(
31
            MSSQLServerLibrary.NAME + "NewStoreParameters", 
32
            MSSQLServerLibrary.NAME
33
        );
34
    }
35

  
36
    @Override
37
    public String getUrl() {
38
        String url = super.getUrl();
39
        if( StringUtils.isEmpty(url) ) {
40
            url = MSSQLServerHelper.getConnectionURL(this);
41
            this.setUrl(url);
42
        }
43
        return url;
44
    }
45
}
tags/org.gvsig.mssqlserver-1.0.41/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.OperationsFactory.TableReference;
11
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation;
12

  
13

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

  
30
    @Override
31
    protected List<String> buildCreateIndexesSQL(
32
            TableReference table,
33
            FeatureType type
34
        ) {
35
        // No podemos crear indices espaciales con la tabla vacia ya que SQLServer
36
        // requiere que se indique en bounding box y aun no lo sabemos.
37
        return new ArrayList<>();
38
    }
39
}
tags/org.gvsig.mssqlserver-1.0.41/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
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
14

  
15
public class MSSQLServerCanModifyTableOperation extends CanModifyTableOperation {
16

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

  
23
    public MSSQLServerCanModifyTableOperation(
24
            JDBCHelper helper,
25
            TableReference table
26
        ) {
27
        super(helper,table);
28
    }
29

  
30
    private String getFullTableName( 
31
            TableReference table
32
    ) {
33
        StringBuilder builder = new StringBuilder();
34
        if( table.hasDatabase() ) {
35
            builder.append(table.getDatabase());
36
        }
37
        if( table.hasSchema() ) {
38
            if( builder.length()>0 ) {
39
                builder.append(".");
40
            }
41
            builder.append(table.getSchema());
42
        }
43
        if( table.hasTable() ) {
44
            if( builder.length()>0 ) {
45
                builder.append(".");
46
            }
47
            builder.append(table.getTable());
48
        }
49
        return builder.toString();
50
    }
51
    
52
    @Override
53
    public boolean canModifyTable(Connection conn,
54
            TableReference table
55
        ) throws DataException {
56

  
57
        String sql = "SELECT count(*) " + 
58
                     "FROM fn_my_permissions('"+getFullTableName(table)+"', 'OBJECT') " +
59
                     "WHERE permission_name in ('UPDATE', 'INSERT', 'DELETE') AND subentity_name = ''";
60
        Statement st = null;
61
        ResultSet rs = null;
62
        try {
63
            st = conn.createStatement();
64
            rs = JDBCUtils.executeQuery(st, sql);
65
            if (!rs.next()) {
66
                return false;
67
            }
68
            return rs.getInt(1)==3;
69

  
70
        } catch (SQLException ex) {
71
            throw new JDBCSQLException(ex);
72
        } finally {
73
            JDBCUtils.closeQuietly(st);
74
            JDBCUtils.closeQuietly(rs);
75
        }
76

  
77
    }
78

  
79
}
tags/org.gvsig.mssqlserver-1.0.41/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.OperationsFactory.TableReference;
14
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
15
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation;
16
import org.gvsig.mssqlserver.dal.MSSQLServerHelper;
17
import org.gvsig.mssqlserver.dal.MSSQLServerSQLBuilder.MSSQLServerCreateIndexBuilder;
18

  
19
@SuppressWarnings("UseSpecificCatch")
20
public class MSSQLServerPerformChangesOperation extends PerformChangesOperation {
21

  
22
    public MSSQLServerPerformChangesOperation(JDBCHelper helper) {
23
        this(helper, null, null, null, null, null, null);
24
    }
25

  
26
    public MSSQLServerPerformChangesOperation(JDBCHelper helper,
27
            TableReference table,
28
            FeatureType featureType,
29
            Iterator<FeatureReferenceProviderServices> deleteds,
30
            Iterator<FeatureProvider> inserteds,
31
            Iterator<FeatureProvider> updateds,
32
            Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged) {
33
        super(helper, table, 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
            TableReference table,
43
            FeatureType original,
44
            FeatureType target
45
        ) {
46
        for (FeatureAttributeDescriptor attrTarget : target) {
47
            boolean createIndex = false;
48
            if( attrTarget.isIndexed() ) {
49
                FeatureAttributeDescriptor attrOriginal = original.getAttributeDescriptor(attrTarget.getName());
50
                if ( attrOriginal == null) {
51
                    createIndex = true;
52
                } else {
53
                    if( attrOriginal.isIndexed() ) {
54
                        createIndex = false;
55
                    } else {
56
                        createIndex = true;
57
                    }
58
                }
59
            }
60
            if( createIndex ) {
61
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
62
                    try {
63
                        return this.getHelper().createOrUpdateSpatialIndexSql(
64
                                this.getConnection(),
65
                                table.getDatabase(),
66
                                table.getSchema(),
67
                                table.getTable(),
68
                                attrTarget.getName()
69
                        );
70
                    } catch (Exception ex) {
71
                        throw new RuntimeException("Can't create or update spatial index.", ex);
72
                    }
73
                } else {
74
                    JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
75
                    MSSQLServerCreateIndexBuilder create_index = (MSSQLServerCreateIndexBuilder) sqlbuilder.create_index();
76
                    create_index.if_not_exist();
77
                    create_index.name("idx_" + table + "_" + attrTarget.getName());
78
                    create_index.column(attrTarget.getName());
79
                    create_index.table()
80
                        .database(table.getDatabase())
81
                        .schema(table.getSchema())
82
                        .name(table.getTable());
83
                    return create_index.toStrings();
84
                }
85
            }
86
        }
87
        return new ArrayList<>();
88
    }    
89
}
tags/org.gvsig.mssqlserver-1.0.41/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.expressionevaluator.ExpressionBuilder;
10
import org.gvsig.fmap.dal.exception.DataException;
11
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
12
import org.gvsig.fmap.dal.feature.EditableFeatureType;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
15
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
16
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
17
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
18
import org.gvsig.fmap.geom.Geometry;
19
import org.gvsig.fmap.geom.GeometryLocator;
20
import org.gvsig.fmap.geom.type.GeometryType;
21
import org.gvsig.mssqlserver.dal.MSSQLServerHelper;
22

  
23
public class MSSQLServerFetchFeatureTypeOperation extends FetchFeatureTypeOperation {
24

  
25
    public MSSQLServerFetchFeatureTypeOperation(
26
            JDBCHelper helper
27
    ) {
28
        super(helper);
29
    }
30

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

  
42
    @Override
43
    public void fetch(EditableFeatureType featureType, Connection conn, TableReference table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException {
44
        super.fetch(featureType, conn, 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
        ExpressionBuilder expbuilder = sqlbuilder.expression();
73
        
74
        sqlbuilder.select().limit(1);
75
        sqlbuilder.select().column().value(
76
                expbuilder.ST_SRID(expbuilder.column(columnName))
77
        );
78
        sqlbuilder.select().where().set(
79
                expbuilder.and(
80
                        expbuilder.not_is_null(expbuilder.column(columnName)),
81
                        expbuilder.not_is_null(expbuilder.ST_SRID(expbuilder.column(columnName)))
82
                )
83
        );
84
        sqlbuilder.select().from().table()
85
                .database(this.getTable().getDatabase())
86
                .schema(this.getTable().getSchema())
87
                .name(this.getTable().getTable());
88
        Statement st = null;
89
        ResultSet rs = null;
90
        try {
91
            st = this.getConnection().createStatement();
92
            rs = JDBCUtils.executeQuery(st, sqlbuilder.toString());
93
            if (rs.next()) {
94
                int code = rs.getInt(1);
95
                try {
96
                    IProjection proj = this.helper.getSRSSolver().getProjection(conn, code);
97
                    return proj;
98
                } catch (Exception ex) {
99
                    LOGGER.warn("Can't create projection from code '" + code + "'.", ex);
100
                }
101
            }
102
        } catch (Exception ex) {
103
            // Do nothing, return null
104
        } finally {
105
            JDBCUtils.closeQuietly(rs);
106
            JDBCUtils.closeQuietly(st);
107
        }
108
        return null;
109

  
110
    }
111
}
tags/org.gvsig.mssqlserver-1.0.41/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, TableReference table, List<String> primaryKeys, String defaultGeometryField, IProjection crs) {
28
        return new MSSQLServerFetchFeatureTypeOperation(
29
                helper, type, table, primaryKeys, 
30
                defaultGeometryField, crs
31
        );
32
    }
33

  
34
    @Override
35
    public CanModifyTableOperation createCanModifyTableOperation(TableReference table) {
36
        return new MSSQLServerCanModifyTableOperation(
37
                helper, table
38
        );
39
    }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff