svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySqlWriter.java @ 9736
History | View | Annotate | Download (7.89 KB)
1 |
/*
|
---|---|
2 |
* Created on 15-ene-2007
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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 2
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id: MySqlWriter.java 9736 2007-01-15 20:17:50Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.1 2007-01-15 20:15:35 azabala
|
49 |
* *** empty log message ***
|
50 |
*
|
51 |
*
|
52 |
*/
|
53 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql; |
54 |
import java.io.IOException; |
55 |
import java.sql.Connection; |
56 |
import java.sql.ResultSet; |
57 |
import java.sql.SQLException; |
58 |
import java.sql.Statement; |
59 |
import java.sql.Types; |
60 |
|
61 |
import com.iver.cit.gvsig.fmap.DriverException; |
62 |
import com.iver.cit.gvsig.fmap.core.FShape; |
63 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
64 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
65 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
66 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
67 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
68 |
import com.iver.cit.gvsig.fmap.edition.IFieldManager; |
69 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
70 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
71 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.JdbcFieldManager; |
72 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
73 |
|
74 |
public class MySqlWriter extends AbstractWriter |
75 |
implements ISpatialWriter, IFieldManager {
|
76 |
|
77 |
private int numRows; |
78 |
|
79 |
private DBLayerDefinition lyrDef;
|
80 |
|
81 |
private Connection conex; |
82 |
|
83 |
private Statement st; |
84 |
|
85 |
/**
|
86 |
* flag to mark if writer must create the table or it must
|
87 |
* add records to an existing one
|
88 |
*
|
89 |
*/
|
90 |
private boolean bCreateTable; |
91 |
|
92 |
private boolean bWriteAll; |
93 |
|
94 |
private MySql mySql = new MySql(); |
95 |
|
96 |
/*
|
97 |
* TODO
|
98 |
* Ver si en MySQL los tipos de datos (numeric, etc.)
|
99 |
* se nombran as? tambi?n
|
100 |
* */
|
101 |
private JdbcFieldManager fieldManager;
|
102 |
|
103 |
/**
|
104 |
*
|
105 |
* Call setFile before using this
|
106 |
* function
|
107 |
*
|
108 |
* @param lyrDef
|
109 |
* @throws IOException
|
110 |
* @throws DriverException
|
111 |
*/
|
112 |
public void initialize(ITableDefinition lyrD) throws EditionException { |
113 |
super.initialize(lyrD);
|
114 |
this.lyrDef = (DBLayerDefinition) lyrD;
|
115 |
conex = lyrDef.getConnection(); |
116 |
|
117 |
try {
|
118 |
st = conex.createStatement(); |
119 |
|
120 |
/*
|
121 |
* y en caso de que sea false usar "CREATE TABLE IF NOT EXISTS" en el
|
122 |
* else
|
123 |
* (AZO)
|
124 |
*
|
125 |
* */
|
126 |
if (bCreateTable) {
|
127 |
try {
|
128 |
st.execute("DROP TABLE " + lyrDef.getTableName() + ";"); |
129 |
} catch (SQLException e1) { |
130 |
} |
131 |
//In MySQL you can add geometry column in CREATE TABLE statement
|
132 |
String sqlCreate = mySql.getSqlCreateSpatialTable(lyrDef,
|
133 |
lyrDef.getFieldsDesc(), |
134 |
true);
|
135 |
st.execute(sqlCreate); |
136 |
conex.commit(); |
137 |
}//if
|
138 |
conex.setAutoCommit(false);
|
139 |
fieldManager = new JdbcFieldManager(conex, lyrDef.getTableName());
|
140 |
|
141 |
} catch (SQLException e) { |
142 |
e.printStackTrace(); |
143 |
throw new EditionException(e); |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
|
149 |
public void preProcess() throws EditionException { |
150 |
numRows = 0;
|
151 |
|
152 |
// ATENTION: We will transform (in PostGIS class; doubleQuote())
|
153 |
// to UTF-8 strings. Then, we tell the PostgreSQL server
|
154 |
// that we will use UTF-8, and it can translate
|
155 |
// to its charset
|
156 |
// Note: we have to translate to UTF-8 because
|
157 |
// the server cannot manage UTF-16
|
158 |
try {
|
159 |
conex.setAutoCommit(false);
|
160 |
conex.rollback(); |
161 |
alterTable(); |
162 |
} catch (SQLException e) { |
163 |
// TODO Auto-generated catch block
|
164 |
e.printStackTrace(); |
165 |
} |
166 |
} |
167 |
|
168 |
public void process(IRowEdited row) throws EditionException { |
169 |
|
170 |
String sqlInsert;
|
171 |
try {
|
172 |
switch (row.getStatus()) {
|
173 |
case IRowEdited.STATUS_ADDED:
|
174 |
IFeature feat = (IFeature) row.getLinkedRow(); |
175 |
sqlInsert = mySql.getSqlInsertFeature(lyrDef, feat); |
176 |
st.execute(sqlInsert); |
177 |
break;
|
178 |
|
179 |
case IRowEdited.STATUS_MODIFIED:
|
180 |
IFeature featM = (IFeature) row.getLinkedRow(); |
181 |
if (bWriteAll) {
|
182 |
sqlInsert = mySql.getSqlInsertFeature(lyrDef, featM); |
183 |
System.out.println("sql = " + sqlInsert); |
184 |
st.execute(sqlInsert); |
185 |
} else {
|
186 |
String sqlModify = mySql.getSqlModifyFeature(lyrDef, featM);
|
187 |
st.execute(sqlModify); |
188 |
} |
189 |
break;
|
190 |
|
191 |
case IRowEdited.STATUS_ORIGINAL:
|
192 |
IFeature featO = (IFeature) row.getLinkedRow(); |
193 |
if (bWriteAll) {
|
194 |
sqlInsert = mySql.getSqlInsertFeature(lyrDef, featO); |
195 |
st.execute(sqlInsert); |
196 |
} |
197 |
break;
|
198 |
|
199 |
case IRowEdited.STATUS_DELETED:
|
200 |
String sqlDelete = mySql.getSqlDeleteFeature(lyrDef, row);
|
201 |
System.out.println("sql = " + sqlDelete); |
202 |
st.execute(sqlDelete); |
203 |
|
204 |
break;
|
205 |
} |
206 |
|
207 |
numRows++; |
208 |
} catch (SQLException e) { |
209 |
e.printStackTrace(); |
210 |
throw new EditionException(e); |
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
public void postProcess() throws EditionException { |
216 |
try {
|
217 |
conex.setAutoCommit(true);
|
218 |
} catch (SQLException e) { |
219 |
e.printStackTrace(); |
220 |
throw new EditionException(e); |
221 |
} |
222 |
} |
223 |
|
224 |
public String getName() { |
225 |
return "MySQL Writer"; |
226 |
} |
227 |
|
228 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
229 |
switch (gvSIGgeometryType) {
|
230 |
case FShape.POINT:
|
231 |
return true; |
232 |
case FShape.LINE:
|
233 |
return true; |
234 |
case FShape.POLYGON:
|
235 |
return true; |
236 |
case FShape.ARC:
|
237 |
return false; |
238 |
case FShape.ELLIPSE:
|
239 |
return false; |
240 |
case FShape.MULTIPOINT:
|
241 |
return true; |
242 |
case FShape.TEXT:
|
243 |
return false; |
244 |
} |
245 |
return false; |
246 |
} |
247 |
|
248 |
public boolean canWriteAttribute(int sqlType) { |
249 |
switch (sqlType) {
|
250 |
case Types.DOUBLE: |
251 |
case Types.FLOAT: |
252 |
case Types.INTEGER: |
253 |
case Types.BIGINT: |
254 |
return true; |
255 |
case Types.DATE: |
256 |
return true; |
257 |
case Types.BIT: |
258 |
case Types.BOOLEAN: |
259 |
return true; |
260 |
case Types.VARCHAR: |
261 |
case Types.CHAR: |
262 |
case Types.LONGVARCHAR: |
263 |
return true; |
264 |
|
265 |
} |
266 |
|
267 |
return false; |
268 |
} |
269 |
|
270 |
/**
|
271 |
* @return Returns the bCreateTable.
|
272 |
*/
|
273 |
public boolean isCreateTable() { |
274 |
return bCreateTable;
|
275 |
} |
276 |
|
277 |
/**
|
278 |
* @param createTable
|
279 |
* The bCreateTable to set.
|
280 |
*/
|
281 |
public void setCreateTable(boolean createTable) { |
282 |
bCreateTable = createTable; |
283 |
} |
284 |
|
285 |
/**
|
286 |
* @return Returns the bWriteAll.
|
287 |
*/
|
288 |
public boolean isWriteAll() { |
289 |
return bWriteAll;
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* @param writeAll
|
294 |
* The bWriteAll to set.
|
295 |
*/
|
296 |
public void setWriteAll(boolean writeAll) { |
297 |
bWriteAll = writeAll; |
298 |
} |
299 |
|
300 |
|
301 |
// public void setFlatness(double flatness) {
|
302 |
// this.flatness = flatness;
|
303 |
// }
|
304 |
|
305 |
public FieldDescription[] getOriginalFields() { |
306 |
return lyrDef.getFieldsDesc();
|
307 |
} |
308 |
|
309 |
public void addField(FieldDescription fieldDesc) { |
310 |
fieldManager.addField(fieldDesc); |
311 |
|
312 |
} |
313 |
|
314 |
public FieldDescription removeField(String fieldName) { |
315 |
return fieldManager.removeField(fieldName);
|
316 |
|
317 |
} |
318 |
|
319 |
public void renameField(String antName, String newName) { |
320 |
fieldManager.renameField(antName, newName); |
321 |
|
322 |
} |
323 |
|
324 |
public boolean alterTable() throws EditionException { |
325 |
return fieldManager.alterTable();
|
326 |
} |
327 |
|
328 |
public FieldDescription[] getFields() { |
329 |
return fieldManager.getFields();
|
330 |
} |
331 |
|
332 |
public boolean canAlterTable() { |
333 |
return true; |
334 |
} |
335 |
|
336 |
public boolean canSaveEdits() { |
337 |
// TODO: Revisar los permisos de la tabla en cuesti?n.
|
338 |
try {
|
339 |
return !conex.isReadOnly();
|
340 |
} catch (SQLException e) { |
341 |
// TODO Auto-generated catch block
|
342 |
e.printStackTrace(); |
343 |
return false; |
344 |
} |
345 |
} |
346 |
|
347 |
} |