Revision 28948

View differences:

branches/v2_0_0_prep/libraries/libFMap_daldb/pom.xml
28 28
		<postgresql-jarIncludes>org/gvsig/fmap/dal/store/postgresql/**/**</postgresql-jarIncludes>
29 29
		<postgresql-jarExcludes>NONE</postgresql-jarExcludes>
30 30

  
31
		<!-- mysql -->
32
		<mysql-jarIncludes>org/gvsig/fmap/dal/store/mysql/**/**</mysql-jarIncludes>
33
		<mysql-jarExcludes>NONE</mysql-jarExcludes>
34

  
31 35
		<!-- Common -->
32 36
		<common-jarIncludes>**/**</common-jarIncludes>
33 37
		<build-dir>${basedir}/../build</build-dir>
......
148 152
				  <excludes>
149 153
					<exclude>${jdbc-jarIncludes}</exclude>
150 154
					<exclude>${postgresql-jarIncludes}</exclude>
155
					<exclude>${mysql-jarIncludes}</exclude>
151 156
				  </excludes>
152 157
		        </configuration>		
153 158
				<executions>
......
189 194
							</excludes>
190 195
						</configuration>
191 196
					</execution>
197
					<!--
198
						Generates a jar file only with the DAL MySQL Store classes
199
					-->
200
					<execution>
201
						<id>mysql</id>
202
						<phase>package</phase>
203
						<goals>
204
							<goal>jar</goal>
205
						</goals>
206
						<configuration>
207
							<classifier>store.mysql</classifier>
208
							<includes>
209
								<include>${mysql-jarIncludes}</include>
210
							</includes>
211
							<excludes>
212
								<exclude>${mysql-jarExcludes}</exclude>
213
							</excludes>
214
						</configuration>
215
					</execution>
192 216
				</executions>
193 217
			</plugin>
194 218
			
195
			<!-- PostgreSQL is needed to run the tests -->
219
			<!-- PostgreSQL and MySQL is needed to run the tests -->
196 220
			<plugin>
197 221
				<groupId>org.apache.maven.plugins</groupId>
198 222
				<artifactId>maven-surefire-plugin</artifactId>
branches/v2_0_0_prep/libraries/libFMap_daldb/src-test/org/gvsig/fmap/dal/store/mysql/MySQLTestWrite.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.store.mysql;
29

  
30
import org.cresques.ProjectionLibrary;
31
import org.cresques.impl.CresquesCtsLibrary;
32
import org.gvsig.fmap.dal.DALDbLibrary;
33
import org.gvsig.fmap.dal.DataStoreParameters;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore;
36
import org.gvsig.fmap.dal.feature.EditableFeatureType;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
40
import org.gvsig.fmap.dal.index.spatial.jts.JTSIndexLibrary;
41
import org.gvsig.fmap.dal.store.db.DBStoreLibrary;
42
import org.gvsig.fmap.dal.store.jdbc.JDBCLibrary;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
44
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.persistence.xmlentity.XMLEntityManager;
47

  
48

  
49
public class MySQLTestWrite extends BaseTestEditableFeatureStore {
50
	private MySQLNewStoreParameters newParams;
51
	private MySQLServerExplorer myExplorer;
52

  
53

  
54
	protected void setUp() throws Exception {
55
		super.setUp();
56
		ToolsLocator.registerDefaultPersistenceManager(XMLEntityManager.class);
57

  
58
		DefaultGeometryLibrary defGeomLib = new DefaultGeometryLibrary();
59
		defGeomLib.initialize();
60

  
61
		ProjectionLibrary projLib = new ProjectionLibrary();
62
		projLib.initialize();
63

  
64
		CresquesCtsLibrary cresquesLib = new CresquesCtsLibrary();
65
		cresquesLib.initialize();
66

  
67
		DALDbLibrary libDb = new DALDbLibrary();
68
		libDb.initialize();
69

  
70
		DBStoreLibrary libDbStore = new DBStoreLibrary();
71
		libDbStore.initialize();
72

  
73
		JDBCLibrary libJDBC = new JDBCLibrary();
74
		libJDBC.initialize();
75

  
76
		MySQLLibrary libMySQL = new MySQLLibrary();
77
		libMySQL.initialize();
78

  
79
		JTSIndexLibrary jtsIndex = new JTSIndexLibrary();
80
		jtsIndex.initialize();
81

  
82
		projLib.postInitialize();
83
		cresquesLib.postInitialize();
84
		defGeomLib.postInitialize();
85
		libDb.postInitialize();
86
		libDbStore.postInitialize();
87
		libJDBC.postInitialize();
88
		libMySQL.postInitialize();
89
		jtsIndex.postInitialize();
90
	}
91

  
92
	public DataStoreParameters getDefaultDataStoreParameters()
93
			throws DataException {
94
		MySQLStoreParameters parameters = null;
95
		parameters = (MySQLStoreParameters) dataManager
96
				.createStoreParameters(MySQLStoreProvider.NAME);
97

  
98
		parameters.setHost("localhost");
99
		parameters.setPort(3306);
100
		parameters.setUser("test");
101
		parameters.setPassword("test");
102
		parameters.setDBName("gis");
103
		// parameters.setTable("alfanum_autoid");
104
		parameters.setTable("comunic_lin_300k");
105

  
106
		return parameters;
107
	}
108

  
109
	/*
110
	 * (non-Javadoc)
111
	 *
112
	 * @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer()
113
	 */
114
	@Override
115
	public boolean hasExplorer() {
116
		// TODO Auto-generated method stub
117
		return true;
118
	}
119

  
120
	/*
121
	 * (non-Javadoc)
122
	 *
123
	 * @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#usesResources()
124
	 */
125
	@Override
126
	public boolean usesResources() {
127
		// TODO Auto-generated method stub
128
		return true;
129
	}
130

  
131
	/*
132
	public void testImportDB() throws Exception {
133
		JDBCNewStoreParameters newParasm = (JDBCNewStoreParameters) getDefaultNewDataStoreParameters();
134
		JDBCServerExplorerParameters seParams = (JDBCServerExplorerParameters) dataManager
135
				.createServerExplorerParameters(JDBCServerExplorer.NAME);
136

  
137
		assertTrue(myExplorer.add(newParasm, true));
138

  
139
		FeatureStore store = (FeatureStore) dataManager
140
				.createStore(getDefaultDataStoreParameters());
141

  
142
		FeatureStore newstore = (FeatureStore) dataManager
143
				.createStore(newParasm);
144

  
145

  
146

  
147
		newstore.edit(FeatureStore.MODE_APPEND);
148
		FeatureSet set = store.getFeatureSet();
149
		DisposableIterator iter = set.iterator();
150
		Feature org;
151
		EditableFeature trg;
152
		Iterator attrIter;
153
		FeatureAttributeDescriptor attr;
154
		while (iter.hasNext()) {
155
			org = (Feature) iter.next();
156
			trg = newstore.createNewFeature(false);
157
			attrIter = trg.getType().iterator();
158
			while (attrIter.hasNext()) {
159
				attr = (FeatureAttributeDescriptor) attrIter.next();
160
				trg.set(attr.getName(), org.get(attr.getName()));
161
			}
162
			newstore.insert(trg);
163
		}
164
		newstore.finishEditing();
165

  
166
		iter.dispose();
167
		set.dispose();
168
		store.dispose();
169
		newstore.dispose();
170

  
171

  
172
	}
173
	*/
174

  
175
	@Override
176
	public NewFeatureStoreParameters getDefaultNewDataStoreParameters()
177
			throws Exception {
178

  
179

  
180
		JDBCStoreParameters params = (JDBCStoreParameters) getDefaultDataStoreParameters();
181

  
182
		if (this.myExplorer == null) {
183

  
184
			MySQLServerExplorerParameters seParameters = null;
185
			seParameters = (MySQLServerExplorerParameters) dataManager
186
					.createServerExplorerParameters(MySQLServerExplorer.NAME);
187

  
188
			seParameters.setHost("localhost");
189
			seParameters.setPort(3306);
190
			seParameters.setUser("test");
191
			seParameters.setPassword("test");
192
			seParameters.setDBName("gis");
193

  
194

  
195
			myExplorer = (MySQLServerExplorer) dataManager
196
					.createServerExplorer(seParameters);
197
		}
198
		if (this.newParams == null) {
199
			FeatureStore store = (FeatureStore) dataManager.createStore(params);
200

  
201
			newParams = (MySQLNewStoreParameters) myExplorer
202
					.getAddParameters();
203

  
204

  
205
			newParams.setTable(params.getTable() + "_test");
206
			FeatureType ftOrg = store.getDefaultFeatureType();
207
			EditableFeatureType ftTrg = ftOrg.getEditable();
208

  
209
//			EditableFeatureType ftTrg = (EditableFeatureType) newParams
210
//					.getDefaultFeatureType();
211
			newParams.setDefaultFeatureType(ftTrg);
212

  
213
//			FeatureAttributeDescriptor org;
214
//			EditableFeatureAttributeDescriptor trg;
215
//			Iterator iter = ftOrg.iterator();
216
//			while (iter.hasNext()) {
217
//				org = (FeatureAttributeDescriptor) iter.next();
218
//				trg = ftTrg.add(org.getName(), org.getDataType());
219
//				trg.setAllowNull(org.allowNull());
220
//				trg.setDefaultValue(org.getDefaultValue());
221
//				trg.setGeometrySubType(org.getGeometrySubType());
222
//				trg.setGeometryType(org.getGeometryType());
223
//				trg.setIsAutomatic(org.isAutomatic());
224
//				trg.setIsPrimaryKey(org.isPrimaryKey());
225
//				trg.setIsReadOnly(org.isReadOnly());
226
//				trg.setMaximumOccurrences(org.getMaximumOccurrences());
227
//				trg.setMinimumOccurrences(org.getMinimumOccurrences());
228
//				trg.setPrecision(org.getPrecision());
229
//				trg.setSize(org.getSize());
230
//				trg.setSRS(org.getSRS());
231
//				trg.setAdditionalInfo("SQLType", org
232
//						.getAdditionalInfo("SQLType"));
233
//				trg.setAdditionalInfo("SQLTypeName", org
234
//						.getAdditionalInfo("SQLTypeName"));
235
//
236
//			}
237
//			ftTrg.setDefaultGeometryAttributeName(ftOrg
238
//					.getDefaultGeometryAttributeName());
239
//			ftTrg.setHasOID(ftOrg.hasOID());
240

  
241
			store.dispose();
242
		}
243

  
244
		return this.newParams;
245
	}
246

  
247
	@Override
248
	public boolean resourcesNotifyChanges() {
249
		// TODO Auto-generated method stub
250
		return false;
251
	}
252

  
253
}
0 254

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/jdbc/JDBCStoreProvider.java
786 786
	}
787 787

  
788 788
	public boolean allowAutomaticValues() {
789
		// TODO Check this
790
		return true;
789
		return this.helper.allowAutomaticValues();
791 790
	}
792 791

  
793 792
	public DataServerExplorer getExplorer() throws ReadException {
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/jdbc/JDBCHelper.java
95 95
	protected FromWKBGeometryOperationContext fromWKBContext = null;
96 96
	protected ToWKBOperationContext toWKBContext = new ToWKBOperationContext();
97 97

  
98
	private Boolean allowAutomaticValues;
99

  
98 100
	protected JDBCHelper(JDBCHelperUser consumer,
99 101
			JDBCConnectionParameters params) throws InitializeException {
100 102

  
......
797 799
			return "time";
798 800

  
799 801
		case DataTypes.BYTEARRAY:
802
		case DataTypes.GEOMETRY:
800 803
			return "blob";
801 804

  
802 805
		case DataTypes.DOUBLE:
......
808 811
		case DataTypes.FLOAT:
809 812
			return "real";
810 813

  
811
		case DataTypes.GEOMETRY:
812
			return "geometry";
813

  
814 814
		case DataTypes.INT:
815
			if (attr.isAutomatic()) {
815
			if (attr.isAutomatic() && allowAutomaticValues()) {
816 816
				return "serial";
817 817
			} else {
818 818
				return "integer";
......
961 961
	boolean hasGeometrySupport() {
962 962
		return false;
963 963
	}
964

  
965
	public boolean allowAutomaticValues() {
966
		if (allowAutomaticValues == null) {
967
			ConnectionAction action = new ConnectionAction(){
968

  
969
				public Object action(Connection conn) throws DataException {
970

  
971
					ResultSet rs;
972
					try {
973
						DatabaseMetaData meta = conn.getMetaData();
974
						rs = meta.getTypeInfo();
975
						try{
976
							while (rs.next()) {
977
								if (rs.getInt("DATA_TYPE") == java.sql.Types.INTEGER) {
978
									if (rs.getBoolean("AUTO_INCREMENT")) {
979
										return Boolean.TRUE;
980
									} else {
981
										return Boolean.FALSE;
982
									}
983
								}
984
							}
985
						}finally{
986
							try{ rs.close();} catch (SQLException ex) {logger.error("Exception closing resulset", ex);};
987
						}
988
					} catch (SQLException e) {
989
						throw new JDBCSQLException(e);
990
					}
991
					return Boolean.FALSE;
992
				}
993

  
994
			};
995

  
996

  
997

  
998
			try {
999
				allowAutomaticValues = (Boolean) doConnectionAction(action);
1000
			} catch (Exception e) {
1001
				logger.error("Exception checking for automatic integers", e);
1002
				allowAutomaticValues = Boolean.FALSE;
1003
			}
1004
		}
1005
		return allowAutomaticValues.booleanValue();
1006
	}
964 1007
}
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/postgresql/PostgreSQLHelper.java
792 792
	}
793 793

  
794 794

  
795
	public boolean allowAutomaticValues() {
796
		return Boolean.TRUE;
797
	}
795 798

  
796 799
}
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLHelper.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.store.mysql;
32

  
33
import java.sql.Connection;
34
import java.sql.ResultSet;
35
import java.sql.ResultSetMetaData;
36
import java.sql.SQLException;
37
import java.sql.Statement;
38

  
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataTypes;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.InitializeException;
43
import org.gvsig.fmap.dal.exception.ReadException;
44
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.EditableFeatureType;
46
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException;
48
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
49
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
50
import org.gvsig.fmap.dal.store.jdbc.JDBCHelperUser;
51
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
52
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException;
53
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
54
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
55
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB;
59
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext;
60
import org.gvsig.fmap.geom.primitive.Envelope;
61
import org.gvsig.tools.exception.BaseException;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

  
65
/**
66
 * @author jmvivo
67
 *
68
 */
69
public class MySQLHelper extends JDBCHelper {
70

  
71
	private static Logger logger = LoggerFactory
72
			.getLogger(MySQLHelper.class);
73

  
74

  
75
	MySQLHelper(JDBCHelperUser consumer,
76
			MySQLConnectionParameters params)
77
			throws InitializeException {
78

  
79
		super(consumer, params);
80
	}
81

  
82
	protected void initializeResource() throws InitializeException {
83
		ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator
84
		.getResourceManager();
85
		MySQLResource resource = (MySQLResource) manager
86
		.createResource(
87
				MySQLResource.NAME, new Object[] {
88
						params.getUrl(), params.getHost(),
89
						params.getPort(), params.getDBName(), params.getUser(),
90
						params.getPassword(),
91
						params.getJDBCDriverClassName(),
92
						((MySQLConnectionParameters) params).getUseSSL() });
93
		this.setResource(resource);
94
	}
95

  
96

  
97
	protected String getDefaultSchema(Connection conn)
98
			throws JDBCException {
99
		if (defaultSchema == null) {
100
			defaultSchema = params.getDBName();
101
		}
102

  
103
		return defaultSchema;
104
	}
105

  
106
	public Envelope getFullEnvelopeOfField(
107
			JDBCStoreParameters storeParams,
108
			String geometryAttrName, Envelope limit)
109
			throws DataException {
110

  
111
		StringBuilder strb = new StringBuilder();
112
		strb.append("Select asbinary(envelope(");
113
		strb.append(geometryAttrName);
114
		strb.append(")) from ");
115

  
116
		if (storeParams.getSQL() != null
117
				&& storeParams.getSQL().trim().length() == 0) {
118
			strb.append('(');
119
			strb.append(storeParams.getSQL());
120
			strb.append(") as __tmp__ ");
121
		} else {
122
			strb.append(storeParams.tableID());
123
		}
124

  
125

  
126
		if (limit != null){
127
			strb.append(" where  intersects(GeomFromText('");
128
			strb.append(limit.toString());
129
			strb.append("')), boundary(");
130
			strb.append(geometryAttrName);
131
			strb.append(")) ");
132
		}
133

  
134
		String sql = strb.toString();
135

  
136

  
137
		ResultSet rs = null;
138
		Statement st = null;
139
		String schema = null;
140
		Connection conn = null;
141

  
142
		GeometryManager geoMan = GeometryLocator.getGeometryManager();
143

  
144
		Envelope fullEnvelope = null;
145
		this.open();
146
		this.begin();
147
		try{
148
			conn = getConnection();
149
			st = conn.createStatement();
150
			try {
151
				rs = st.executeQuery(sql);
152
			} catch (java.sql.SQLException e) {
153
				throw new JDBCExecuteSQLException(sql, e);
154
			}
155
			if (!rs.next()) {
156
				return null;
157
			}
158

  
159
			byte[] data = rs.getBytes(1);
160
			if (data == null) {
161
				return null;
162
			}
163
			initializeFromWKBOperation();
164
			fromWKBContext.setData(data);
165
			Geometry geom = (Geometry) fromWKB.invoke(null, fromWKBContext);
166

  
167
			fullEnvelope = geom.getEnvelope();
168

  
169
			return fullEnvelope;
170
		} catch (java.sql.SQLException e) {
171
			throw new JDBCSQLException(e);
172
		} catch (BaseException e) {
173
			throw new ReadException(user.getName(), e);
174
		} finally {
175
			try{ rs.close(); } catch (Exception e){};
176
			try{ st.close(); } catch (Exception e){};
177
			try{ conn.close(); } catch (Exception e){};
178
			rs = null;
179
			st = null;
180
			conn = null;
181
			end();
182
		}
183

  
184

  
185
	}
186

  
187
	protected void initializeFromWKBOperation() throws BaseException {
188
		if (fromWKB == null) {
189
			fromWKB = (FromWKB) GeometryLocator.getGeometryManager()
190
					.getGeometryOperation(FromWKB.CODE,
191
							Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D);
192
			fromWKBContext = new FromWKBGeometryOperationContext();
193

  
194
		}
195
	}
196

  
197
	public Geometry getGeometry(byte[] buffer) throws BaseException {
198
		if (buffer == null) {
199
			return null;
200
		}
201
		initializeFromWKBOperation();
202
		Geometry geom;
203
		try {
204
			fromWKBContext.setData(buffer);
205

  
206
			geom = (Geometry) fromWKB.invoke(null, fromWKBContext);
207
		} finally {
208
			fromWKBContext.setData(null);
209
		}
210
		return geom;
211
	}
212

  
213
	public String getSqlColumnTypeDescription(FeatureAttributeDescriptor attr) {
214

  
215
		switch (attr.getDataType()) {
216
		case DataTypes.STRING:
217
			return "VARCHAR(" + attr.getSize() + ")";
218
		case DataTypes.BOOLEAN:
219
			return "BOOL";
220

  
221
		case DataTypes.BYTE:
222
			return "TINYINT UNSIGNED";
223

  
224
		case DataTypes.DATE:
225
			return "DATE";
226

  
227
		case DataTypes.TIMESTAMP:
228
			return "TIMESTAMP";
229

  
230
		case DataTypes.TIME:
231
			return "TIME";
232

  
233
		case DataTypes.BYTEARRAY:
234
			if (attr.getSize() > 0) {
235
				return "BLOB(" + attr.getSize() + ")";
236
			} else {
237
				return "BLOB";
238
			}
239

  
240
		case DataTypes.DOUBLE:
241
			if (attr.getSize() > 0) {
242
				return "DOUBLE(" + attr.getSize() + "," + attr.getPrecision()
243
						+ ")";
244
			} else {
245
				return "DOBLE";
246
			}
247
		case DataTypes.FLOAT:
248
			return "FLOAT";
249

  
250
		case DataTypes.GEOMETRY:
251
			switch (attr.getGeometryType()) {
252
			case Geometry.TYPES.POINT:
253
				return "POINT";
254
			case Geometry.TYPES.CURVE:
255
				return "LINESTRING";
256
			case Geometry.TYPES.SURFACE:
257
				return "SURFACE";
258
			case Geometry.TYPES.SOLID:
259
				return "POLYGON";
260

  
261
			case Geometry.TYPES.MULTIPOINT:
262
				return "MULTIPOIN";
263
			case Geometry.TYPES.MULTICURVE:
264
				return "MULTILINESTRING";
265
			case Geometry.TYPES.MULTISURFACE:
266
				return "MULTISURFACE";
267
			case Geometry.TYPES.MULTISOLID:
268
				return "MULTIPOLYGON";
269

  
270
			default:
271
				return "GEOMETRY";
272
			}
273
		case DataTypes.INT:
274
			if (attr.getSize() > 0) {
275
				return "INT(" + attr.getSize() + ")";
276
			}
277
		case DataTypes.LONG:
278
			return "BIGINT";
279

  
280
		default:
281
			String typeName = (String) attr.getAdditionalInfo("SQLTypeName");
282
			if (typeName != null) {
283
				return typeName;
284
			}
285

  
286
			throw new UnsupportedDataTypeException(attr.getDataTypeName(), attr
287
					.getDataType());
288
		}
289
	}
290

  
291

  
292
	public String getSqlFieldName(FeatureAttributeDescriptor attribute) {
293
		if (attribute.getDataType() == DataTypes.GEOMETRY) {
294
			return "asBinary(" + super.getSqlFieldName(attribute) + ")";
295
		}
296
		return super.getSqlFieldName(attribute);
297
	}
298

  
299
	protected EditableFeatureAttributeDescriptor createAttributeFromJDBC(
300
			EditableFeatureType type, Connection conn,
301
			ResultSetMetaData rsMetadata, int colIndex) throws SQLException {
302
		int colType = rsMetadata.getColumnType(colIndex);
303
		if (colType == java.sql.Types.OTHER || colType == java.sql.Types.STRUCT
304
				|| colType == java.sql.Types.BLOB
305
				|| colType == java.sql.Types.BINARY) {
306
			Integer geoType = null;
307

  
308
			if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase(
309
					"geometry")) {
310
				geoType = new Integer(Geometry.TYPES.GEOMETRY);
311
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase("POINT")) {
312
				geoType = new Integer(Geometry.TYPES.POINT);
313
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase("LINESTRING")) {
314
				geoType = new Integer(Geometry.TYPES.CURVE);
315
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase("SURFACE")) {
316
				geoType = new Integer(Geometry.TYPES.SURFACE);
317
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase("POLYGON")) {
318
				geoType = new Integer(Geometry.TYPES.SOLID);
319
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase("MULTIPOIN")) {
320
				geoType = new Integer(Geometry.TYPES.MULTIPOINT);
321
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase("MULTILINESTRING")) {
322
				geoType = new Integer(Geometry.TYPES.MULTICURVE);
323
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase(
324
					"MULTISURFACE")) {
325
				geoType = new Integer(Geometry.TYPES.MULTISURFACE);
326
			} else if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase(
327
					"MULTIPOLYGON")) {
328
				geoType = new Integer(Geometry.TYPES.MULTISOLID);
329
			}
330
			if (geoType != null){
331
				EditableFeatureAttributeDescriptor attr = type.add(rsMetadata
332
						.getColumnName(colIndex), DataTypes.GEOMETRY);
333
				attr.setGeometrySubType(Geometry.SUBTYPES.GEOM2D);
334

  
335
				return attr;
336
			}
337

  
338
		}
339

  
340
		return super.createAttributeFromJDBC(type, conn, rsMetadata, colIndex);
341
	}
342

  
343
	public String escapeFieldName(String field) {
344
		if (field.matches("[a-z][a-z0-9_]*")) {
345
			return field;
346
		}
347
		return "`" + field + "`";
348
	}
349

  
350

  
351
	public boolean allowAutomaticValues() {
352
		return Boolean.TRUE;
353
	}
354

  
355

  
356
	public String getSqlFieldDescription(FeatureAttributeDescriptor attr)
357
			throws DataException {
358

  
359
		/**
360
			column_definition:
361
			    data_type [NOT NULL | NULL] [DEFAULT default_value]
362
			      [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
363
			      [COMMENT 'string'] [reference_definition]
364

  
365
		 */
366

  
367
		StringBuilder strb = new StringBuilder();
368
		// name
369
		strb.append(escapeFieldName(attr.getName()));
370
		strb.append(" ");
371

  
372
		// Type
373
		strb.append(this.getSqlColumnTypeDescription(attr));
374
		strb.append(" ");
375

  
376
		boolean allowNull = attr.allowNull()
377
				&& !(attr.isPrimaryKey() || attr.isAutomatic());
378

  
379
		// Null
380
		if (allowNull) {
381
			strb.append("NULL ");
382
		} else {
383
			strb.append("NOT NULL ");
384
		}
385
		if (attr.isAutomatic()) {
386
			strb.append("AUTO_INCREMENT ");
387
		}
388

  
389
		// Default
390
		if (attr.getDefaultValue() == null) {
391
			if (allowNull) {
392
				strb.append("DEFAULT NULL ");
393
			}
394
		} else {
395
			String value = getDefaltFieldValueString(attr);
396
			strb.append("DEFAULT '");
397
			strb.append(value);
398
			strb.append("' ");
399
		}
400

  
401
		// Primery key
402
		if (attr.isPrimaryKey()) {
403
			strb.append("PRIMARY KEY ");
404
		}
405
		return strb.toString();
406
	}
407

  
408

  
409
}
0 410

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLStoreParameters.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.store.mysql;
29

  
30
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
31
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
32
import org.gvsig.tools.ToolsLocator;
33
import org.gvsig.tools.dynobject.DelegatedDynObject;
34
import org.gvsig.tools.dynobject.DynClass;
35
import org.gvsig.tools.dynobject.DynField;
36
import org.gvsig.tools.dynobject.DynObjectManager;
37

  
38
public class MySQLStoreParameters extends JDBCStoreParameters implements
39
		MySQLConnectionParameters {
40
    public static final String DYNCLASS_NAME = "MySQLStoreParameters";
41

  
42

  
43
    protected static void registerDynClass() {
44
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
45
		DynClass dynClass = dynman.get(DYNCLASS_NAME);
46
		DynField field;
47
		if (dynClass == null) {
48
			dynClass = dynman.add(DYNCLASS_NAME);
49

  
50
			dynClass.extend(JDBCStoreParameters.DYNCLASS_NAME);
51

  
52
			dynClass.extend(MySQLResourceParameters.DYNCLASS_NAME);
53

  
54
		}
55

  
56
	}
57

  
58
	public MySQLStoreParameters() {
59
		super();
60
		initialize();
61
	}
62

  
63
	protected void initialize() {
64
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
65
		DynClass dynClass = dynman.get(DYNCLASS_NAME);
66
		this.delegatedDynObject = (DelegatedDynObject) dynman
67
				.createDynObject(dynClass);
68
	}
69

  
70

  
71
	public String getDataStoreName() {
72
		return MySQLStoreProvider.NAME;
73
	}
74

  
75
	public String getDescription() {
76
		return MySQLStoreProvider.DESCRIPTION;
77
	}
78

  
79
	public Boolean getUseSSL() {
80
		return (Boolean) this.getDynValue(DYNFIELDNAME_USESSL);
81
	}
82

  
83
	public void setUseSSL(Boolean useSSL) {
84
		this.setDynValue(DYNFIELDNAME_USESSL, useSSL);
85
	}
86

  
87
	public void setUseSSL(boolean useSSL) {
88
		this.setDynValue(DYNFIELDNAME_USESSL, new Boolean(useSSL));
89
	}
90

  
91

  
92
	public void validate() throws ValidateDataParametersException {
93
		if (getJDBCDriverClassName() == null) {
94
			setJDBCDriverClassName(MySQLLibrary.DEFAULT_JDCB_DRIVER_NAME);
95
		}
96
		if (getUrl() == null) {
97
			setUrl(MySQLLibrary.getJdbcUrl(getHost(), getPort(),
98
					getDBName()));
99
		}
100

  
101
		if (getPort() == null) {
102
			setPort(new Integer(3306));
103
		}
104
		super.validate();
105
	}
106

  
107
}
0 108

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLConnectionParameters.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.store.mysql;
32

  
33
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
34

  
35
/**
36
 * @author jmvivo
37
 *
38
 */
39
public interface MySQLConnectionParameters extends
40
		JDBCConnectionParameters {
41

  
42
	public static final String DYNFIELDNAME_USESSL = "usessl";
43

  
44
	public Boolean getUseSSL();
45
}
0 46

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLLibrary.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I. S.A.   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.store.mysql;
29

  
30
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
32
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
33
import org.gvsig.tools.locator.BaseLibrary;
34
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
35

  
36

  
37
public class MySQLLibrary extends BaseLibrary {
38

  
39

  
40
	static String DEFAULT_JDCB_DRIVER_NAME = "com.mysql.jdbc.Driver";
41

  
42

  
43
	public static String getJdbcUrl(String host, Integer port, String db) {
44
		String url;
45
		String sport = "";
46
		if (port != null) {
47
			sport = ":" + port;
48
		}
49
		url = "jdbc:mysql://" + host + sport + "/" + db;
50

  
51
		return url;
52
	}
53

  
54

  
55
	public void initialize() throws ReferenceNotRegisteredException {
56
		super.initialize();
57

  
58
	}
59

  
60
	public void postInitialize() throws ReferenceNotRegisteredException {
61
		super.postInitialize();
62

  
63
    	ResourceManagerProviderServices resman = (ResourceManagerProviderServices) DALLocator
64
				.getResourceManager();
65

  
66
    	MySQLResourceParameters.registerDynClass();
67

  
68
    	if (!resman.getResourceProviders().contains(MySQLResource.NAME)) {
69
			resman.register(MySQLResource.NAME,
70
					MySQLResource.DESCRIPTION, MySQLResource.class,
71
					MySQLResourceParameters.class);
72
		}
73

  
74

  
75
    	MySQLStoreParameters.registerDynClass();
76
    	MySQLNewStoreParameters.registerDynClass();
77
		MySQLStoreProvider.registerDynClass();
78

  
79
        DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator
80
				.getDataManager();
81

  
82
		if (!dataman.getStoreProviders().contains(MySQLStoreProvider.NAME)) {
83
			dataman.registerStoreProvider(MySQLStoreProvider.NAME,
84
					MySQLStoreProvider.class,
85
					MySQLStoreParameters.class);
86
		}
87

  
88
		if (!dataman.getExplorerProviders().contains(
89
				MySQLStoreProvider.NAME)) {
90
			dataman.registerExplorerProvider(MySQLServerExplorer.NAME,
91
					MySQLServerExplorer.class,
92
					MySQLServerExplorerParameters.class);
93
		}
94

  
95

  
96

  
97
	}
98

  
99
}
0 100

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLSetProvider.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.store.mysql;
32

  
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.FeatureQuery;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.fmap.dal.store.jdbc.JDBCSetProvider;
37
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider;
38

  
39
/**
40
 * @author jmvivo
41
 *
42
 */
43
public class MySQLSetProvider extends JDBCSetProvider {
44

  
45
	public MySQLSetProvider(JDBCStoreProvider store, FeatureQuery query,
46
			FeatureType featureType) throws DataException {
47
		super(store, query, featureType);
48
		// TODO Auto-generated constructor stub
49
	}
50

  
51

  
52
	/*
53
	 * (non-Javadoc)
54
	 *
55
	 * @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canFilter()
56
	 */
57
	public boolean canFilter() {
58
		// TODO more checks
59
		if (!super.canFilter()) {
60
			return false;
61
		}
62
		return true;
63

  
64
	}
65

  
66
}
0 67

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLResource.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.store.mysql;
29

  
30
import java.text.MessageFormat;
31

  
32
import javax.sql.DataSource;
33

  
34
import org.apache.commons.dbcp.BasicDataSource;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.exception.InitializeException;
37
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
38
import org.gvsig.fmap.dal.store.jdbc.JDBCResource;
39

  
40
public class MySQLResource extends JDBCResource {
41

  
42
	public final static String NAME = "MySQLResource";
43
	public static final String DESCRIPTION = "MySQL Connection";
44

  
45
	public MySQLResource(MySQLResourceParameters parameters)
46
			throws InitializeException {
47
		super(parameters);
48
	}
49

  
50
	public String getName() throws AccessResourceException {
51
		MySQLResourceParameters params = (MySQLResourceParameters) this.getParameters();
52
		return MessageFormat.format("MySQLResource({0},{1})",
53
				new Object[] { params.getUrl(),params.getUser() });
54
	}
55

  
56
	protected void connectToDB() throws DataException {
57
		super.connectToDB();
58
	}
59

  
60
	protected DataSource createDataSource() {
61
		MySQLResourceParameters jdbcParams = (MySQLResourceParameters) this
62
				.getParameters();
63
		BasicDataSource dataSource = new BasicDataSource();
64
		dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
65
		dataSource.setUsername(jdbcParams.getUser());
66
		dataSource.setPassword(jdbcParams.getPassword());
67
		dataSource.setUrl(jdbcParams.getUrl());
68

  
69
		dataSource.setMaxWait(60L * 1000); // FIXME
70

  
71
		// FIXME Set Pool parameters:
72
		/*
73
		dataSource.setMaxActive(maxActive);
74
		dataSource.setMaxIdle(maxActive);
75
		dataSource.setMaxOpenPreparedStatements(maxActive);
76
		dataSource.setMaxWait(maxActive);
77
		dataSource.setInitialSize(initialSize);
78
		dataSource.setDefaultReadOnly(defaultReadOnly);
79
		dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
80
		dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
81
		dataSource.setMinIdle(minIdle);
82
		dataSource.setTestOnBorrow(testOnBorrow);
83
		dataSource.setTestOnReturn(testOnReturn);
84
		dataSource.setTestWhileIdle(testOnReturn);
85
		dataSource
86
			.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
87

  
88
		dataSource.setAccessToUnderlyingConnectionAllowed(allow);
89
		dataSource.setLoginTimeout(seconds);
90
		dataSource.setLogWriter(out);
91
		 */
92
		return dataSource;
93
	}
94

  
95
}
0 96

  
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/mysql/MySQLServerExplorer.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

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.store.mysql;
32

  
33
import java.sql.Connection;
34
import java.sql.SQLException;
35
import java.sql.Statement;
36

  
37
import org.gvsig.fmap.dal.DataStoreParameters;
38
import org.gvsig.fmap.dal.NewDataStoreParameters;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.InitializeException;
41
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
42
import org.gvsig.fmap.dal.exception.RemoveException;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
44
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
45
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
46
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
47
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction;
48
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
49
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

  
53
/**
54
 * @author jmvivo
55
 *
56
 */
57
public class MySQLServerExplorer extends JDBCServerExplorer {
58
	final static private Logger logger = LoggerFactory
59
			.getLogger(MySQLServerExplorer.class);
60

  
61
	public static final String NAME = "MySQLExplorer";
62

  
63
	private MySQLServerExplorerParameters myParameters;
64

  
65

  
66
	public MySQLServerExplorer(
67
			MySQLServerExplorerParameters parameters)
68
			throws InitializeException {
69
		super(parameters);
70
	}
71

  
72
	protected void init(JDBCServerExplorerParameters parameters)
73
			throws InitializeException {
74
		myParameters = (MySQLServerExplorerParameters) parameters;
75
		super.init(parameters);
76
	}
77

  
78
	protected JDBCHelper createHelper() throws InitializeException {
79
		return new MySQLHelper(this, myParameters);
80
	}
81

  
82

  
83
	protected String getStoreName() {
84
		return MySQLStoreProvider.NAME;
85
	}
86

  
87
	public String getName() {
88
		return NAME;
89
	}
90

  
91
	protected JDBCStoreParameters createStoreParams()
92
			throws InitializeException, ProviderNotRegisteredException {
93
		MySQLStoreParameters orgParams = (MySQLStoreParameters) super
94
				.createStoreParams();
95

  
96
		orgParams.setUseSSL(myParameters.getUseSSL());
97

  
98
		return orgParams;
99
	}
100

  
101

  
102
	// ****************************
103

  
104

  
105
	public boolean canAdd() {
106
		return true;
107
	}
108

  
109
	protected void checkIsMine(DataStoreParameters dsp) {
110
		if (!(dsp instanceof MySQLStoreParameters)) {
111
			// FIXME Excpetion ???
112
			throw new IllegalArgumentException(
113
					"not instance of MySQLStoreParameters");
114
		}
115
		super.checkIsMine(dsp);
116

  
117
		MySQLStoreParameters myp = (MySQLStoreParameters) dsp;
118
		if (myp.getUseSSL().booleanValue() != myParameters.getUseSSL()) {
119
			throw new IllegalArgumentException("worng explorer: useSSL (mine:"
120
					+ myParameters.getUseSSL() + " other:" + myp.getUseSSL()
121
					+ ")");
122
		}
123
	}
124

  
125
	public void remove(DataStoreParameters dsp) throws RemoveException {
126
		final MySQLStoreParameters myParams =(MySQLStoreParameters) dsp;
127

  
128
		TransactionalAction action = new TransactionalAction() {
129
			public boolean continueTransactionAllowed() {
130
				return false;
131
			}
132
			public Object action(Connection conn) throws DataException {
133

  
134

  
135
				Statement st;
136
				try{
137
					st = conn.createStatement();
138
				} catch (SQLException e) {
139
					throw new JDBCSQLException(e);
140
				}
141

  
142
				String sqlDrop = "Drop table "
143
					+ myParams.tableID();
144

  
145
				try{
146
					try{
147
						st.execute(sqlDrop);
148
					} catch (SQLException e) {
149
						throw new JDBCExecuteSQLException(sqlDrop, e);
150
					}
151

  
152
				} finally{
153
					try{ st.close(); } catch (SQLException e) {};
154
				}
155
				return null;
156
			}
157
		};
158
		try {
159
			this.helper.doConnectionAction(action);
160
		} catch (Exception e) {
161
			throw new RemoveException(this.getName(), e);
162
		}
163
	}
164

  
165
	public NewDataStoreParameters getAddParameters() throws DataException {
166
		MySQLNewStoreParameters params = new MySQLNewStoreParameters();
167
		params.setHost(this.parameters.getHost());
168
		params.setPort(this.parameters.getPort());
169
		params.setDBName(this.parameters.getDBName());
170
		params.setUser(this.parameters.getUser());
171
		params.setPassword(this.parameters.getPassword());
172
		params.setCatalog(this.parameters.getCatalog());
173
		params.setSchema(this.parameters.getSchema());
174
		params.setJDBCDriverClassName(this.parameters.getJDBCDriverClassName());
175
		params.setUrl(this.parameters.getUrl());
176
		params.setUseSSL(((MySQLServerExplorerParameters) this.parameters)
177
				.getUseSSL());
178

  
179

  
180
		params.setDefaultFeatureType(this.getServerExplorerProviderServices()
181
				.createNewFeatureType());
182

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

Also available in: Unified diff