Revision 9767
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/engine/data/driver/AbstractJDBCDriver.java | ||
---|---|---|
55 | 55 |
import com.hardcode.gdbms.engine.data.db.JDBCSupport; |
56 | 56 |
import com.hardcode.gdbms.engine.values.Value; |
57 | 57 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
58 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
|
59 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
60 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
|
58 | 61 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
59 | 62 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
60 | 63 |
import com.iver.cit.gvsig.fmap.edition.writers.JdbcWriter; |
64 |
import com.iver.utiles.NumberUtilities; |
|
61 | 65 |
|
62 | 66 |
public abstract class AbstractJDBCDriver implements AlphanumericDBDriver, |
63 | 67 |
IWriteable { |
... | ... | |
281 | 285 |
public IWriter getWriter() { |
282 | 286 |
return jdbcWriter; |
283 | 287 |
} |
288 |
|
|
289 |
/** |
|
290 |
* Return the default tabledefinition (only |
|
291 |
* has field description values). |
|
292 |
* Each driver that inheret this class must overwrite |
|
293 |
* this. |
|
294 |
* |
|
295 |
* */ |
|
296 |
public ITableDefinition getTableDefinition(){ |
|
297 |
TableDefinition tableDef = new TableDefinition(); |
|
298 |
try { |
|
299 |
tableDef.setFieldsDesc(getFieldsDescription()); |
|
300 |
} catch (DriverException e) { |
|
301 |
throw new RuntimeException(e);//excepcion sin chequear |
|
302 |
} |
|
303 |
return tableDef; |
|
304 |
} |
|
305 |
|
|
306 |
|
|
307 |
/*azabala |
|
308 |
TODO Copypasteado de SelectableDataSource. |
|
309 |
?No estar?a mejor aqu?? |
|
310 |
* |
|
311 |
*/ |
|
312 |
private FieldDescription[] getFieldsDescription() throws DriverException |
|
313 |
{ |
|
314 |
int numFields = getFieldCount(); |
|
315 |
FieldDescription[] fieldsDescrip = new FieldDescription[numFields]; |
|
316 |
for (int i = 0; i < numFields; i++) { |
|
317 |
fieldsDescrip[i] = new FieldDescription(); |
|
318 |
int type = getFieldType(i); |
|
319 |
fieldsDescrip[i].setFieldType(type); |
|
320 |
fieldsDescrip[i].setFieldName(getFieldName(i)); |
|
321 |
fieldsDescrip[i].setFieldLength(getFieldWidth(i)); |
|
322 |
if (NumberUtilities.isNumeric(type)) |
|
323 |
{ |
|
324 |
if (!NumberUtilities.isNumericInteger(type)) |
|
325 |
// TODO: If there is a lost in precision, this should be changed. |
|
326 |
fieldsDescrip[i].setFieldDecimalCount(6); |
|
327 |
} |
|
328 |
else |
|
329 |
fieldsDescrip[i].setFieldDecimalCount(0); |
|
330 |
// TODO: ?DEFAULTVALUE? |
|
331 |
// fieldsDescrip[i].setDefaultValue(get) |
|
332 |
} |
|
333 |
return fieldsDescrip; |
|
334 |
} |
|
284 | 335 |
} |
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/engine/data/driver/AlphanumericDBDriver.java | ||
---|---|---|
3 | 3 |
import java.sql.Connection; |
4 | 4 |
import java.sql.SQLException; |
5 | 5 |
|
6 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
7 |
|
|
6 | 8 |
/** |
7 | 9 |
* |
8 | 10 |
*/ |
... | ... | |
24 | 26 |
*/ |
25 | 27 |
public void open(Connection con, String sql) |
26 | 28 |
throws SQLException; |
27 |
|
|
29 |
|
|
30 |
/** |
|
31 |
* Devuelve el "esquema" del origen de datos subyacente al driver. |
|
32 |
* (azabala). |
|
33 |
* Es necesario porque determinados drivers de escritura |
|
34 |
* (los de bbdd que no soportan resultsets updatables) necesitan conocer |
|
35 |
* el esquema de la tabla subyacente (nombre, p. ej) para poder construir |
|
36 |
* al vuelo la consulta SQL final. |
|
37 |
* */ |
|
38 |
public ITableDefinition getTableDefinition(); |
|
28 | 39 |
} |
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/driver/odbc/ODBCDriver.java | ||
---|---|---|
9 | 9 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
10 | 10 |
import com.hardcode.gdbms.engine.values.Value; |
11 | 11 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
12 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
|
13 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
14 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
|
12 | 15 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
13 | 16 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
14 | 17 |
import com.iver.cit.gvsig.fmap.edition.writers.JdbcWriter; |
18 |
import com.iver.utiles.NumberUtilities; |
|
15 | 19 |
|
16 | 20 |
import java.sql.Connection; |
17 | 21 |
import java.sql.Date; |
... | ... | |
283 | 287 |
public IWriter getWriter() { |
284 | 288 |
return jdbcWriter; |
285 | 289 |
} |
290 |
|
|
291 |
public ITableDefinition getTableDefinition() { |
|
292 |
TableDefinition tableDef = new TableDefinition(); |
|
293 |
try { |
|
294 |
tableDef.setFieldsDesc(getFieldsDescription()); |
|
295 |
} catch (DriverException e) { |
|
296 |
throw new RuntimeException(e);//excepcion sin chequear |
|
297 |
} |
|
298 |
return tableDef; |
|
299 |
} |
|
300 |
|
|
301 |
/*azabala |
|
302 |
TODO Codigo repetido entre ODBCDriver y AbstractJDBCDriver. |
|
303 |
Igual hay que ponerlo en una clase auxiliar comun |
|
304 |
* |
|
305 |
*/ |
|
306 |
private FieldDescription[] getFieldsDescription() throws DriverException |
|
307 |
{ |
|
308 |
int numFields = getFieldCount(); |
|
309 |
FieldDescription[] fieldsDescrip = new FieldDescription[numFields]; |
|
310 |
for (int i = 0; i < numFields; i++) { |
|
311 |
fieldsDescrip[i] = new FieldDescription(); |
|
312 |
int type = getFieldType(i); |
|
313 |
fieldsDescrip[i].setFieldType(type); |
|
314 |
fieldsDescrip[i].setFieldName(getFieldName(i)); |
|
315 |
fieldsDescrip[i].setFieldLength(getFieldWidth(i)); |
|
316 |
if (NumberUtilities.isNumeric(type)) |
|
317 |
{ |
|
318 |
if (!NumberUtilities.isNumericInteger(type)) |
|
319 |
// TODO: If there is a lost in precision, this should be changed. |
|
320 |
fieldsDescrip[i].setFieldDecimalCount(6); |
|
321 |
} |
|
322 |
else |
|
323 |
fieldsDescrip[i].setFieldDecimalCount(0); |
|
324 |
// TODO: ?DEFAULTVALUE? |
|
325 |
// fieldsDescrip[i].setDefaultValue(get) |
|
326 |
} |
|
327 |
return fieldsDescrip; |
|
328 |
} |
|
286 | 329 |
} |
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/driver/foodriver/FooDriver.java | ||
---|---|---|
14 | 14 |
import com.hardcode.gdbms.engine.spatial.GeometryImpl; |
15 | 15 |
import com.hardcode.gdbms.engine.spatial.PTTypes; |
16 | 16 |
import com.hardcode.gdbms.engine.values.Value; |
17 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
17 | 18 |
|
18 | 19 |
public class FooDriver implements AlphanumericDBDriver { |
19 | 20 |
|
... | ... | |
120 | 121 |
return 1; |
121 | 122 |
} |
122 | 123 |
|
124 |
public ITableDefinition getTableDefinition() { |
|
125 |
return null; |
|
126 |
} |
|
127 |
|
|
123 | 128 |
} |
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/driver/mysql/MySQL.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 16-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$ |
|
47 |
* $Log$ |
|
48 |
* Revision 1.1 2007-01-16 20:06:02 azabala |
|
49 |
* changes to allow edition with MySQL drivers |
|
50 |
* |
|
51 |
* |
|
52 |
*/ |
|
53 |
package com.hardcode.gdbms.driver.mysql; |
|
54 |
|
|
55 |
import java.sql.Types; |
|
56 |
|
|
57 |
import com.hardcode.gdbms.engine.values.NullValue; |
|
58 |
import com.hardcode.gdbms.engine.values.Value; |
|
59 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
|
60 |
import com.iver.cit.gvsig.fmap.core.IRow; |
|
61 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
|
62 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
|
63 |
import com.iver.cit.gvsig.fmap.drivers.XTypes; |
|
64 |
|
|
65 |
public class MySQL { |
|
66 |
public boolean isNumeric(Value val) { |
|
67 |
|
|
68 |
switch (val.getSQLType()) { |
|
69 |
case Types.DOUBLE: |
|
70 |
case Types.FLOAT: |
|
71 |
case Types.INTEGER: |
|
72 |
case Types.SMALLINT: |
|
73 |
case Types.BIGINT: |
|
74 |
case Types.NUMERIC: |
|
75 |
case Types.REAL: |
|
76 |
case Types.TINYINT: |
|
77 |
return true; |
|
78 |
} |
|
79 |
|
|
80 |
return false; |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* |
|
85 |
* @param dbLayerDef |
|
86 |
* @param fieldsDescr |
|
87 |
* @param bCreateGID |
|
88 |
* @return |
|
89 |
*/ |
|
90 |
public String getSqlCreateSpatialTable(DBLayerDefinition dbLayerDef, |
|
91 |
FieldDescription[] fieldsDescr, |
|
92 |
boolean bCreateGID) { |
|
93 |
String result = "CREATE TABLE " + dbLayerDef.getTableName() |
|
94 |
+ " (gid int(10) unsigned NOT NULL auto_increment,"; |
|
95 |
int j=0; |
|
96 |
for (int i = 0; i < dbLayerDef.getFieldNames().length; i++) { |
|
97 |
int fieldType = fieldsDescr[i].getFieldType(); |
|
98 |
//TODO ver si XTypes me los devuelve con la sintaxis MySQL |
|
99 |
String strType = XTypes.fieldTypeToString(fieldType); |
|
100 |
|
|
101 |
//We dont allow GID field. It is a reserved field name |
|
102 |
if (fieldsDescr[i].getFieldName().equalsIgnoreCase("gid")) |
|
103 |
continue; |
|
104 |
result += ", " + dbLayerDef.getFieldNames()[i] + " " + strType; |
|
105 |
j++; |
|
106 |
} |
|
107 |
result = result.substring(0, result.length()-1); |
|
108 |
result += ", PRIMARY KEY(GID))"; |
|
109 |
return result; |
|
110 |
} |
|
111 |
|
|
112 |
protected String format(Object value) { |
|
113 |
String retString = null; |
|
114 |
if (value != null) { |
|
115 |
if (value instanceof NullValue) |
|
116 |
retString = "null"; |
|
117 |
else{ |
|
118 |
retString += ("'" + value.toString().trim() + "',"); |
|
119 |
} |
|
120 |
} else { |
|
121 |
retString = "null"; |
|
122 |
} |
|
123 |
return retString; |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* Based in code from JUMP (VividSolutions) and Geotools Things to be aware: |
|
128 |
* We always will use Spatial Tables with Unique ID. IFeature has the same |
|
129 |
* field order than dbLayerDef.getFieldNames() |
|
130 |
* |
|
131 |
* @param dbLayerDef |
|
132 |
* @param feat |
|
133 |
* @return |
|
134 |
*/ |
|
135 |
public String getSqlInsertFeature(DBLayerDefinition dbLayerDef, |
|
136 |
IRow feat) { |
|
137 |
String sql = "INSERT INTO "+ |
|
138 |
dbLayerDef.getTableName() + " ("; |
|
139 |
int numAlphanumericFields = dbLayerDef.getFieldNames().length; |
|
140 |
for (int i = 0; i < numAlphanumericFields; i++) { |
|
141 |
String name = dbLayerDef.getFieldsDesc()[i].getFieldName(); |
|
142 |
if (name.equals(dbLayerDef.getFieldID())) |
|
143 |
continue; |
|
144 |
sql += " " + name + ","; |
|
145 |
}//for |
|
146 |
sql = sql.substring(0, sql.length() -1); |
|
147 |
sql += " ) VALUES ("; |
|
148 |
for (int j = 0; j < numAlphanumericFields; j++) { |
|
149 |
String name = dbLayerDef.getFieldsDesc()[j].getFieldName(); |
|
150 |
if (name.equals(dbLayerDef.getFieldID())) |
|
151 |
continue; |
|
152 |
|
|
153 |
if (isNumeric(feat.getAttribute(j))) |
|
154 |
sql += feat.getAttribute(j) + ", "; |
|
155 |
else{ |
|
156 |
sql += format(feat.getAttribute(j)) + ", "; |
|
157 |
} |
|
158 |
}//for |
|
159 |
sql = sql.substring(0, sql.length() -1); |
|
160 |
sql += " )"; |
|
161 |
return sql; |
|
162 |
} |
|
163 |
|
|
164 |
|
|
165 |
public String getSqlModifyFeature(DBLayerDefinition dbLayerDef, IRow feat) { |
|
166 |
String sql = "UPDATE " + dbLayerDef.getTableName() + " SET"; |
|
167 |
int numAlphanumericFields = dbLayerDef.getFieldsDesc().length; |
|
168 |
for (int i = 0; i < numAlphanumericFields; i++) { |
|
169 |
FieldDescription fldDesc = dbLayerDef.getFieldsDesc()[i]; |
|
170 |
if (fldDesc != null){ |
|
171 |
String name = fldDesc.getFieldName(); |
|
172 |
if (name.equalsIgnoreCase(dbLayerDef.getFieldID())) |
|
173 |
continue; |
|
174 |
Value val = feat.getAttribute(i); |
|
175 |
if (val != null) |
|
176 |
{ |
|
177 |
String strAux = val.getStringValue(ValueWriter.internalValueWriter); |
|
178 |
sql += " " + name + " = " + strAux + " ,"; |
|
179 |
}//if |
|
180 |
}//if |
|
181 |
}//for |
|
182 |
sql = sql.substring(0, sql.length() -1); |
|
183 |
sql += " WHERE "; |
|
184 |
//TODO El feature.getID() funciona? (AZO) |
|
185 |
sql += dbLayerDef.getFieldID() + " = " + feat.getID(); |
|
186 |
return sql; |
|
187 |
|
|
188 |
} |
|
189 |
|
|
190 |
/** |
|
191 |
* It builds MySQL's delete statement |
|
192 |
* @param dbLayerDef |
|
193 |
* @param row |
|
194 |
* @return |
|
195 |
*/ |
|
196 |
public String getSqlDeleteFeature(DBLayerDefinition dbLayerDef, IRow row) { |
|
197 |
String sql = "DELETE FROM " |
|
198 |
+ dbLayerDef.getTableName() + " WHERE "; |
|
199 |
int indexFieldId = dbLayerDef.getIdFieldID(); |
|
200 |
sql += dbLayerDef.getFieldID() + " = " + row.getAttribute(indexFieldId); |
|
201 |
return sql; |
|
202 |
} |
|
203 |
|
|
204 |
} |
|
205 |
|
|
0 | 206 |
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/driver/mysql/MySQLDriver.java | ||
---|---|---|
5 | 5 |
|
6 | 6 |
import java.sql.Connection; |
7 | 7 |
import java.sql.DriverManager; |
8 |
import java.sql.ResultSet; |
|
8 |
import java.sql.ResultSetMetaData;
|
|
9 | 9 |
import java.sql.SQLException; |
10 |
import java.sql.Statement; |
|
11 | 10 |
|
12 | 11 |
import com.hardcode.gdbms.engine.data.db.JDBCSupport; |
13 | 12 |
import com.hardcode.gdbms.engine.data.driver.AbstractJDBCDriver; |
14 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySQLSpatialWriter; |
|
13 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
|
14 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
15 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
|
15 | 16 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
16 |
import com.iver.cit.gvsig.fmap.edition.writers.JdbcWriter; |
|
17 | 17 |
|
18 | 18 |
|
19 | 19 |
/** |
20 | 20 |
* MySQL driver |
21 | 21 |
* |
22 | 22 |
* @author Fernando Gonz?lez Cort?s |
23 |
* @author azabala |
|
23 | 24 |
*/ |
24 | 25 |
public class MySQLDriver extends AbstractJDBCDriver { |
25 | 26 |
private static Exception driverException; |
... | ... | |
32 | 33 |
} |
33 | 34 |
} |
34 | 35 |
|
35 |
|
|
36 |
MySQLSpatialWriter writer = new MySQLSpatialWriter(); |
|
36 |
/** |
|
37 |
* IWriter implementation for MySQL DB. |
|
38 |
* It does editing operations |
|
39 |
* */ |
|
40 |
MySQLWriter writer = new MySQLWriter(); |
|
37 | 41 |
|
38 | 42 |
|
39 | 43 |
public IWriter getWriter() { |
... | ... | |
42 | 46 |
|
43 | 47 |
public void open(Connection con, String sql) throws SQLException { |
44 | 48 |
jdbcSupport = JDBCSupport.newJDBCSupport(con, sql); |
49 |
writer.initialize(con); |
|
45 | 50 |
writer.setCreateTable(false); |
46 |
writer.setWriteAll(false); |
|
47 |
// writer.initialize(con); |
|
51 |
writer.setWriteAll(false); |
|
48 | 52 |
} |
49 | 53 |
|
50 | 54 |
public void close() throws SQLException { |
51 |
jdbcSupport.close(); |
|
55 |
//commented to avoid problems with automatic datasource |
|
56 |
// jdbcSupport.close(); |
|
52 | 57 |
// writer.close(); |
53 | 58 |
} |
54 | 59 |
|
... | ... | |
95 | 100 |
public String getName() { |
96 | 101 |
return "mysql"; |
97 | 102 |
} |
103 |
|
|
104 |
/* |
|
105 |
*Sobreescribo el metodo de AbstractJDBCDriver porque |
|
106 |
*si para la gran mayoria de bbdd no hay que escribir querys |
|
107 |
*(se hace con updatableresultset) para MySQL no funciona esto. |
|
108 |
*Como se necesita construir la query al vuelo, as? se le proporciona |
|
109 |
*al ITableDefinition el nombre de la tabla |
|
110 |
* */ |
|
111 |
public ITableDefinition getTableDefinition(){ |
|
112 |
DBLayerDefinition solution = new DBLayerDefinition(); |
|
113 |
ITableDefinition schema = super.getTableDefinition(); |
|
114 |
solution.setFieldsDesc(schema.getFieldsDesc()); |
|
115 |
solution.setName(schema.getName()); |
|
116 |
try { |
|
117 |
ResultSetMetaData metadata = jdbcSupport.getResultSet(). |
|
118 |
getMetaData(); |
|
119 |
solution.setTableName(metadata.getTableName(1)); |
|
120 |
} catch (SQLException e) { |
|
121 |
throw new RuntimeException(e); |
|
122 |
} |
|
123 |
return solution; |
|
124 |
|
|
125 |
|
|
126 |
} |
|
98 | 127 |
|
99 | 128 |
} |
trunk/libraries/libGDBMS/src/main/java/com/hardcode/gdbms/driver/mysql/MySQLWriter.java | ||
---|---|---|
5 | 5 |
package com.hardcode.gdbms.driver.mysql; |
6 | 6 |
|
7 | 7 |
import java.sql.Connection; |
8 |
import java.sql.ResultSetMetaData; |
|
8 | 9 |
import java.sql.SQLException; |
9 | 10 |
import java.sql.Statement; |
10 | 11 |
import java.sql.Types; |
11 | 12 |
|
12 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
|
13 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySql; |
|
13 |
|
|
14 |
import com.iver.cit.gvsig.fmap.core.IRow; |
|
15 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
|
16 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
|
17 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
14 | 18 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
15 | 19 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
16 | 20 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.JdbcFieldManager; |
17 | 21 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
18 | 22 |
|
19 | 23 |
/** |
24 |
* |
|
25 |
* MySQL alphanumeric writer. |
|
26 |
* It is needed because MySQL JDBC driver doesnt allow updatables recordsets. |
|
20 | 27 |
* @author alzabord |
21 | 28 |
* |
22 | 29 |
* @see MySQLSpatialWriter from extJDBC project |
... | ... | |
24 | 31 |
public class MySQLWriter extends AbstractWriter{ |
25 | 32 |
|
26 | 33 |
private int numRows; |
27 |
private Connection conex;
|
|
34 |
private Connection conn;
|
|
28 | 35 |
private Statement st; |
36 |
private ResultSetMetaData metaData; |
|
29 | 37 |
private boolean bCreateTable; |
30 | 38 |
private boolean bWriteAll; |
31 |
private MySql mySql = new MySql();
|
|
39 |
private MySQL mySql = new MySQL();
|
|
32 | 40 |
private JdbcFieldManager fieldManager; |
33 | 41 |
|
42 |
public void initialize(Connection conn){ |
|
43 |
this.conn = conn; |
|
44 |
} |
|
34 | 45 |
|
46 |
public void initialize(ITableDefinition tableDefinition) throws EditionException { |
|
47 |
super.initialize(tableDefinition); |
|
48 |
try { |
|
49 |
createTableIfNeeded(); |
|
50 |
} catch (SQLException e) { |
|
51 |
throw new EditionException(e); |
|
52 |
} |
|
53 |
} |
|
35 | 54 |
|
36 | 55 |
|
56 |
private void createTableIfNeeded() throws SQLException{ |
|
57 |
st = conn.createStatement(); |
|
58 |
if (bCreateTable) { |
|
59 |
try { |
|
60 |
st.execute("DROP TABLE " + |
|
61 |
((DBLayerDefinition)tableDef).getTableName() + ";"); |
|
62 |
} catch (SQLException e1) { |
|
63 |
e1.printStackTrace(); |
|
64 |
} |
|
65 |
//In MySQL you can add geometry column in CREATE TABLE statement |
|
66 |
String sqlCreate = mySql.getSqlCreateSpatialTable((DBLayerDefinition) tableDef, |
|
67 |
tableDef.getFieldsDesc(), |
|
68 |
true); |
|
69 |
st.execute(sqlCreate); |
|
70 |
conn.commit(); |
|
71 |
}//if |
|
72 |
conn.setAutoCommit(false); |
|
73 |
fieldManager = new JdbcFieldManager(conn, |
|
74 |
((DBLayerDefinition) tableDef).getTableName()); |
|
75 |
} |
|
76 |
|
|
37 | 77 |
|
38 | 78 |
public boolean canWriteAttribute(int sqlType) { |
39 | 79 |
switch (sqlType) { |
... | ... | |
51 | 91 |
case Types.CHAR: |
52 | 92 |
case Types.LONGVARCHAR: |
53 | 93 |
return true; |
54 |
|
|
55 | 94 |
} |
56 | 95 |
return false; |
57 | 96 |
} |
... | ... | |
60 | 99 |
public void preProcess() throws EditionException { |
61 | 100 |
numRows = 0; |
62 | 101 |
try { |
63 |
conex.setAutoCommit(false);
|
|
64 |
conex.rollback();
|
|
102 |
conn.setAutoCommit(false);
|
|
103 |
conn.rollback();
|
|
65 | 104 |
alterTable(); |
66 | 105 |
} catch (SQLException e) { |
67 |
// TODO Auto-generated catch block |
|
68 |
e.printStackTrace(); |
|
106 |
throw new EditionException(e); |
|
69 | 107 |
} |
70 | 108 |
} |
71 | 109 |
|
... | ... | |
78 | 116 |
try { |
79 | 117 |
switch (row.getStatus()) { |
80 | 118 |
case IRowEdited.STATUS_ADDED: |
81 |
IFeature feat = (IFeature) row.getLinkedRow();
|
|
82 |
sqlInsert = mySql.getSqlInsertFeature(lyrDef, feat);
|
|
119 |
IRow record = row.getLinkedRow();
|
|
120 |
sqlInsert = mySql.getSqlInsertFeature((DBLayerDefinition) tableDef, record);
|
|
83 | 121 |
st.execute(sqlInsert); |
84 | 122 |
break; |
85 | 123 |
|
86 | 124 |
case IRowEdited.STATUS_MODIFIED: |
87 |
IFeature featM = (IFeature) row.getLinkedRow();
|
|
125 |
IRow featM = row.getLinkedRow();
|
|
88 | 126 |
if (bWriteAll) { |
89 |
sqlInsert = mySql.getSqlInsertFeature(lyrDef, featM);
|
|
127 |
sqlInsert = mySql.getSqlInsertFeature((DBLayerDefinition) tableDef, featM);
|
|
90 | 128 |
System.out.println("sql = " + sqlInsert); |
91 | 129 |
st.execute(sqlInsert); |
92 | 130 |
} else { |
93 |
String sqlModify = mySql.getSqlModifyFeature(lyrDef, featM);
|
|
131 |
String sqlModify = mySql.getSqlModifyFeature((DBLayerDefinition) tableDef, featM);
|
|
94 | 132 |
st.execute(sqlModify); |
95 | 133 |
} |
96 | 134 |
break; |
97 | 135 |
|
98 | 136 |
case IRowEdited.STATUS_ORIGINAL: |
99 |
IFeature featO = (IFeature) row.getLinkedRow();
|
|
137 |
IRow featO = row.getLinkedRow();
|
|
100 | 138 |
if (bWriteAll) { |
101 |
sqlInsert = mySql.getSqlInsertFeature(lyrDef, featO);
|
|
139 |
sqlInsert = mySql.getSqlInsertFeature((DBLayerDefinition) tableDef, featO);
|
|
102 | 140 |
st.execute(sqlInsert); |
103 | 141 |
} |
104 | 142 |
break; |
105 | 143 |
|
106 | 144 |
case IRowEdited.STATUS_DELETED: |
107 |
String sqlDelete = mySql.getSqlDeleteFeature(lyrDef, row);
|
|
145 |
String sqlDelete = mySql.getSqlDeleteFeature((DBLayerDefinition) tableDef, row);
|
|
108 | 146 |
System.out.println("sql = " + sqlDelete); |
109 | 147 |
st.execute(sqlDelete); |
110 |
|
|
111 | 148 |
break; |
112 | 149 |
} |
113 |
|
|
114 | 150 |
numRows++; |
115 | 151 |
} catch (SQLException e) { |
116 | 152 |
e.printStackTrace(); |
... | ... | |
120 | 156 |
|
121 | 157 |
|
122 | 158 |
public void postProcess() throws EditionException { |
123 |
// TODO Auto-generated method stub |
|
124 |
|
|
159 |
try { |
|
160 |
conn.setAutoCommit(true); |
|
161 |
} catch (SQLException e) { |
|
162 |
e.printStackTrace(); |
|
163 |
throw new EditionException(e); |
|
164 |
} |
|
125 | 165 |
} |
126 | 166 |
|
127 | 167 |
|
128 | 168 |
public boolean canAlterTable() { |
129 |
// TODO Auto-generated method stub |
|
130 |
return false; |
|
169 |
return true; |
|
131 | 170 |
} |
132 | 171 |
|
133 | 172 |
|
134 | 173 |
public boolean canSaveEdits() { |
135 |
// TODO Auto-generated method stub |
|
136 |
return false; |
|
174 |
try { |
|
175 |
return !conn.isReadOnly(); |
|
176 |
} catch (SQLException e) { |
|
177 |
e.printStackTrace(); |
|
178 |
return false; |
|
179 |
} |
|
137 | 180 |
} |
138 | 181 |
|
139 | 182 |
public String getName() { |
140 |
// TODO Auto-generated method stub |
|
141 |
return null; |
|
183 |
return "MySQL Alphanumeric Writer"; |
|
142 | 184 |
} |
185 |
|
|
186 |
public void setCreateTable(boolean createTable) { |
|
187 |
bCreateTable = createTable; |
|
188 |
} |
|
189 |
|
|
190 |
public boolean isCreateTable() { |
|
191 |
return bCreateTable; |
|
192 |
} |
|
143 | 193 |
|
194 |
public boolean isWriteAll() { |
|
195 |
return bWriteAll; |
|
196 |
} |
|
197 |
|
|
198 |
public void setWriteAll(boolean writeAll) { |
|
199 |
bWriteAll = writeAll; |
|
200 |
} |
|
201 |
|
|
202 |
/* |
|
203 |
* TODO |
|
204 |
* Esta parte no se si es necesaria, o si se usa (revisar) |
|
205 |
* |
|
206 |
* */ |
|
207 |
public FieldDescription[] getFields() { |
|
208 |
return fieldManager.getFields(); |
|
209 |
} |
|
210 |
|
|
211 |
public FieldDescription[] getOriginalFields() { |
|
212 |
return tableDef.getFieldsDesc(); |
|
213 |
} |
|
214 |
|
|
215 |
public void addField(FieldDescription fieldDesc) { |
|
216 |
fieldManager.addField(fieldDesc); |
|
217 |
|
|
218 |
} |
|
219 |
|
|
220 |
public FieldDescription removeField(String fieldName) { |
|
221 |
return fieldManager.removeField(fieldName); |
|
222 |
|
|
223 |
} |
|
224 |
|
|
225 |
public void renameField(String antName, String newName) { |
|
226 |
fieldManager.renameField(antName, newName); |
|
227 |
} |
|
228 |
|
|
144 | 229 |
} |
Also available in: Unified diff