root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / mysql / MySQL.java @ 9767
History | View | Annotate | Download (5.61 KB)
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: MySQL.java 9767 2007-01-16 20:06:02Z azabala $
|
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 |
|