root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySql.java @ 9736
History | View | Annotate | Download (8.97 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: MySql.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 |
|
55 |
import java.io.ByteArrayOutputStream; |
56 |
import java.io.PrintStream; |
57 |
import java.io.UnsupportedEncodingException; |
58 |
import java.sql.Types; |
59 |
|
60 |
import com.hardcode.gdbms.engine.values.NullValue; |
61 |
import com.hardcode.gdbms.engine.values.Value; |
62 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
63 |
import com.iver.cit.gvsig.fmap.core.FShape; |
64 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
65 |
import com.iver.cit.gvsig.fmap.core.IRow; |
66 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
67 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
68 |
import com.iver.cit.gvsig.fmap.drivers.XTypes; |
69 |
import com.vividsolutions.jts.io.WKTWriter; |
70 |
|
71 |
/**
|
72 |
* It builds sql sintax for Create, Delete, Update operations in MySQL
|
73 |
*
|
74 |
* */
|
75 |
public class MySql { |
76 |
/**
|
77 |
* Converts a JTS geometry to WKT
|
78 |
* */
|
79 |
private static WKTWriter geometryWriter = new WKTWriter(); |
80 |
|
81 |
/**
|
82 |
* Mover esto a IverUtiles
|
83 |
*
|
84 |
* @param val
|
85 |
* @return
|
86 |
*/
|
87 |
public boolean isNumeric(Value val) { |
88 |
|
89 |
switch (val.getSQLType()) {
|
90 |
case Types.DOUBLE: |
91 |
case Types.FLOAT: |
92 |
case Types.INTEGER: |
93 |
case Types.SMALLINT: |
94 |
case Types.BIGINT: |
95 |
case Types.NUMERIC: |
96 |
case Types.REAL: |
97 |
case Types.TINYINT: |
98 |
return true; |
99 |
} |
100 |
|
101 |
return false; |
102 |
} |
103 |
|
104 |
/**
|
105 |
* It builds MySQL "CREATE TABLE" statement.
|
106 |
*
|
107 |
* In PostGIS, its usual first create alphanumeric table, and
|
108 |
* after that add a geometry column for geometries.
|
109 |
*
|
110 |
*
|
111 |
* @param dbLayerDef data of the new table (name, etc)
|
112 |
* @param fieldsDescr schema of the new table
|
113 |
* @param bCreateGID @DEPRECATED
|
114 |
* @return SQL statement with MySQL sintax
|
115 |
*/
|
116 |
public String getSqlCreateSpatialTable(DBLayerDefinition dbLayerDef, |
117 |
FieldDescription[] fieldsDescr, boolean bCreateGID) { |
118 |
|
119 |
String result;
|
120 |
result = "CREATE TABLE " + dbLayerDef.getTableName()
|
121 |
+ " (gid serial PRIMARY KEY ";
|
122 |
int j=0; |
123 |
for (int i = 0; i < dbLayerDef.getFieldNames().length; i++) { |
124 |
int fieldType = fieldsDescr[i].getFieldType();
|
125 |
//TODO ver si XTypes me los devuelve con la sintaxis MySQL
|
126 |
String strType = XTypes.fieldTypeToString(fieldType);
|
127 |
|
128 |
//We dont allow GID field. It is a reserved field name
|
129 |
if (fieldsDescr[i].getFieldName().equalsIgnoreCase("gid")) |
130 |
continue;
|
131 |
result += ", " + dbLayerDef.getFieldNames()[i] + " " + strType; |
132 |
j++; |
133 |
} |
134 |
|
135 |
String geometryFieldName = dbLayerDef.getFieldGeometry();
|
136 |
|
137 |
//TODO Ver si MySQL se traga los tipos geometricos que devuelve XTypes
|
138 |
String geometryFieldType = "GEOMETRY"; |
139 |
switch (dbLayerDef.getShapeType()) {
|
140 |
case FShape.POINT:
|
141 |
geometryFieldType = XTypes.fieldTypeToString(XTypes.POINT2D); |
142 |
break;
|
143 |
case FShape.LINE:
|
144 |
geometryFieldType = XTypes.fieldTypeToString(XTypes.LINE2D); |
145 |
break;
|
146 |
case FShape.POLYGON:
|
147 |
geometryFieldType = XTypes.fieldTypeToString(XTypes.POLYGON2D); |
148 |
break;
|
149 |
case FShape.MULTI:
|
150 |
geometryFieldType = XTypes.fieldTypeToString(XTypes.MULTI2D); |
151 |
break;
|
152 |
} |
153 |
result += "," + geometryFieldName + " " + |
154 |
geometryFieldType + " NOT NULL, SPATIAL INDEX(" + geometryFieldName + "))"; |
155 |
return result;
|
156 |
} |
157 |
|
158 |
protected String format(Object value) { |
159 |
String retString = null; |
160 |
if (value != null) { |
161 |
if (value instanceof NullValue) |
162 |
retString = "null";
|
163 |
else{
|
164 |
// retString = "'" + doubleQuote(value) + "'";
|
165 |
retString += ("'" + value.toString().trim() + "',"); |
166 |
} |
167 |
} else {
|
168 |
retString = "null";
|
169 |
} |
170 |
return retString;
|
171 |
} |
172 |
|
173 |
|
174 |
|
175 |
// private String doubleQuote(Object obj) {
|
176 |
// String aux = obj.toString().replaceAll("'", "''");
|
177 |
// StringBuffer strBuf = new StringBuffer(aux);
|
178 |
// ByteArrayOutputStream out = new ByteArrayOutputStream(strBuf.length());
|
179 |
// PrintStream printStream = new PrintStream(out);
|
180 |
// printStream.print(aux);
|
181 |
// String aux2 = "ERROR";
|
182 |
// try {
|
183 |
// aux2 = out.toString(toEncode);
|
184 |
// System.out.println(aux + " " + aux2);
|
185 |
// } catch (UnsupportedEncodingException e) {
|
186 |
// // TODO Auto-generated catch block
|
187 |
// e.printStackTrace();
|
188 |
// }
|
189 |
//
|
190 |
// return aux2;
|
191 |
// }
|
192 |
|
193 |
|
194 |
|
195 |
/**
|
196 |
* Based in code from JUMP (VividSolutions) and Geotools Things to be aware:
|
197 |
* We always will use Spatial Tables with Unique ID. IFeature has the same
|
198 |
* field order than dbLayerDef.getFieldNames()
|
199 |
*
|
200 |
* @param dbLayerDef
|
201 |
* @param feat
|
202 |
* @return
|
203 |
*/
|
204 |
public String getSqlInsertFeature(DBLayerDefinition dbLayerDef, |
205 |
IFeature feat) { |
206 |
StringBuffer sqlBuf = new StringBuffer("INSERT INTO " |
207 |
+ dbLayerDef.getTableName() + " (");
|
208 |
String sql = null; |
209 |
int numAlphanumericFields = dbLayerDef.getFieldNames().length;
|
210 |
|
211 |
for (int i = 0; i < numAlphanumericFields; i++) { |
212 |
String name = dbLayerDef.getFieldsDesc()[i].getFieldName();
|
213 |
if (name.equals(dbLayerDef.getFieldID()))
|
214 |
continue;
|
215 |
sqlBuf.append(" " + name + ","); |
216 |
} |
217 |
sqlBuf.append(" " + dbLayerDef.getFieldGeometry());
|
218 |
sqlBuf.append(" ) VALUES (");
|
219 |
|
220 |
String insertQueryHead = sqlBuf.toString();
|
221 |
sqlBuf = new StringBuffer(insertQueryHead); |
222 |
|
223 |
for (int j = 0; j < numAlphanumericFields; j++) { |
224 |
String name = dbLayerDef.getFieldsDesc()[j].getFieldName();
|
225 |
if (name.equals(dbLayerDef.getFieldID()))
|
226 |
continue;
|
227 |
|
228 |
if (isNumeric(feat.getAttribute(j)))
|
229 |
sqlBuf.append(feat.getAttribute(j) + ", ");
|
230 |
else{
|
231 |
sqlBuf.append(format(feat.getAttribute(j)) + ", ");
|
232 |
} |
233 |
}//for
|
234 |
sqlBuf.append(" GeometryFromText( '"
|
235 |
+ geometryWriter.write(feat.getGeometry().toJTSGeometry()) + "', "
|
236 |
+ dbLayerDef.getSRID_EPSG() + ")");
|
237 |
sqlBuf.append(" ) ");
|
238 |
sql = sqlBuf.toString(); |
239 |
return sql;
|
240 |
} |
241 |
|
242 |
|
243 |
|
244 |
// public String getSqlCreateIndex(DBLayerDefinition lyrDef) {
|
245 |
// String indexName = lyrDef.getTableName() + "_"
|
246 |
// + lyrDef.getFieldGeometry() + "_gist";
|
247 |
// String sql = "CREATE INDEX \"" + indexName + "\" ON \""
|
248 |
// + lyrDef.getTableName() + "\" USING GIST (\""
|
249 |
// + lyrDef.getFieldGeometry() + "\" GIST_GEOMETRY_OPS)";
|
250 |
//
|
251 |
// return sql;
|
252 |
// }
|
253 |
|
254 |
|
255 |
|
256 |
public String getSqlModifyFeature(DBLayerDefinition dbLayerDef, IFeature feat) { |
257 |
StringBuffer sqlBuf = new StringBuffer("UPDATE " |
258 |
+ dbLayerDef.getTableName() + " SET");
|
259 |
String sql = null; |
260 |
int numAlphanumericFields = dbLayerDef.getFieldsDesc().length;
|
261 |
for (int i = 0; i < numAlphanumericFields; i++) { |
262 |
FieldDescription fldDesc = dbLayerDef.getFieldsDesc()[i]; |
263 |
if (fldDesc != null){ |
264 |
String name = fldDesc.getFieldName();
|
265 |
if (name.equalsIgnoreCase(dbLayerDef.getFieldID()))
|
266 |
continue;
|
267 |
Value val = feat.getAttribute(i); |
268 |
if (val != null) |
269 |
{ |
270 |
String strAux = val.getStringValue(ValueWriter.internalValueWriter);
|
271 |
sqlBuf.append(" " + name + " = " + strAux + " ,"); |
272 |
} |
273 |
} |
274 |
} |
275 |
sqlBuf.deleteCharAt(sqlBuf.lastIndexOf(","));
|
276 |
if (feat.getGeometry() != null){ |
277 |
sqlBuf.append(", " + dbLayerDef.getFieldGeometry());
|
278 |
sqlBuf.append(" = ");
|
279 |
sqlBuf.append(" GeometryFromText( '"
|
280 |
+ geometryWriter.write(feat.getGeometry().toJTSGeometry()) + "', "
|
281 |
+ dbLayerDef.getSRID_EPSG() + ")");
|
282 |
} |
283 |
sqlBuf.append(" WHERE ");
|
284 |
|
285 |
//TODO El feature.getID() funciona? (AZO)
|
286 |
sqlBuf.append(dbLayerDef.getFieldID() + " = " + feat.getID());
|
287 |
sql = sqlBuf.toString(); |
288 |
return sql;
|
289 |
|
290 |
} |
291 |
|
292 |
|
293 |
|
294 |
/**
|
295 |
* It builds MySQL's delete statement
|
296 |
* @param dbLayerDef
|
297 |
* @param row
|
298 |
* @return
|
299 |
*/
|
300 |
public String getSqlDeleteFeature(DBLayerDefinition dbLayerDef, IRow row) { |
301 |
// TODO: NECESITAMOS OTRO M?TODO PARA BORRAR CORRECTAMENTE.
|
302 |
// Esto provocar? errores, ya que getID que viene en un row no
|
303 |
// nos sirve dentro de un writer para modificar y/o borrar entidades
|
304 |
// Por ahora, cojo el ID del campo que me indica el dbLayerDev
|
305 |
StringBuffer sqlBuf = new StringBuffer("DELETE FROM " |
306 |
+ dbLayerDef.getTableName() + " WHERE ");
|
307 |
String sql = null; |
308 |
int indexFieldId = dbLayerDef.getIdFieldID();
|
309 |
sqlBuf.append(dbLayerDef.getFieldID() + " = " + row.getAttribute(indexFieldId));
|
310 |
sql = sqlBuf.toString(); |
311 |
|
312 |
return sql;
|
313 |
} |
314 |
|
315 |
} |
316 |
|