svn-gvsig-desktop / tags / v1_1_Build_1007 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / mysql / MySQL.java @ 12478
History | View | Annotate | Download (5.85 KB)
1 | 9767 | azabala | /*
|
---|---|---|---|
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 | 9887 | fjp | * Revision 1.2.2.1 2007-01-24 13:16:12 fjp
|
49 | * Cambios para editar con MySQL
|
||
50 | *
|
||
51 | * Revision 1.2 2007/01/17 19:39:59 azabala
|
||
52 | 9799 | azabala | * bug solved in getInsertInto...(the rest of methods wont work well, because GDBMS is not thinked to work with schemas)
|
53 | *
|
||
54 | * Revision 1.1 2007/01/16 20:06:02 azabala
|
||
55 | 9767 | azabala | * changes to allow edition with MySQL drivers
|
56 | *
|
||
57 | *
|
||
58 | */
|
||
59 | package com.hardcode.gdbms.driver.mysql; |
||
60 | |||
61 | import java.sql.Types; |
||
62 | |||
63 | import com.hardcode.gdbms.engine.values.NullValue; |
||
64 | import com.hardcode.gdbms.engine.values.Value; |
||
65 | import com.hardcode.gdbms.engine.values.ValueWriter; |
||
66 | import com.iver.cit.gvsig.fmap.core.IRow; |
||
67 | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
||
68 | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
||
69 | import com.iver.cit.gvsig.fmap.drivers.XTypes; |
||
70 | |||
71 | public class MySQL { |
||
72 | public boolean isNumeric(Value val) { |
||
73 | |||
74 | switch (val.getSQLType()) {
|
||
75 | case Types.DOUBLE: |
||
76 | case Types.FLOAT: |
||
77 | case Types.INTEGER: |
||
78 | case Types.SMALLINT: |
||
79 | case Types.BIGINT: |
||
80 | case Types.NUMERIC: |
||
81 | case Types.REAL: |
||
82 | case Types.TINYINT: |
||
83 | return true; |
||
84 | } |
||
85 | |||
86 | return false; |
||
87 | } |
||
88 | |||
89 | /**
|
||
90 | *
|
||
91 | * @param dbLayerDef
|
||
92 | * @param fieldsDescr
|
||
93 | * @param bCreateGID
|
||
94 | * @return
|
||
95 | */
|
||
96 | public String getSqlCreateSpatialTable(DBLayerDefinition dbLayerDef, |
||
97 | FieldDescription[] fieldsDescr,
|
||
98 | boolean bCreateGID) {
|
||
99 | String result = "CREATE TABLE " + dbLayerDef.getTableName() |
||
100 | + " (gid int(10) unsigned NOT NULL auto_increment,";
|
||
101 | int j=0; |
||
102 | for (int i = 0; i < dbLayerDef.getFieldNames().length; i++) { |
||
103 | int fieldType = fieldsDescr[i].getFieldType();
|
||
104 | //TODO ver si XTypes me los devuelve con la sintaxis MySQL
|
||
105 | String strType = XTypes.fieldTypeToString(fieldType);
|
||
106 | |||
107 | //We dont allow GID field. It is a reserved field name
|
||
108 | if (fieldsDescr[i].getFieldName().equalsIgnoreCase("gid")) |
||
109 | continue;
|
||
110 | result += ", " + dbLayerDef.getFieldNames()[i] + " " + strType; |
||
111 | j++; |
||
112 | } |
||
113 | result = result.substring(0, result.length()-1); |
||
114 | result += ", PRIMARY KEY(GID))";
|
||
115 | return result;
|
||
116 | } |
||
117 | |||
118 | protected String format(Object value) { |
||
119 | String retString = null; |
||
120 | if (value != null) { |
||
121 | if (value instanceof NullValue) |
||
122 | retString = "null";
|
||
123 | else{
|
||
124 | 9799 | azabala | retString = ("'" + value.toString().trim() + "'"); |
125 | 9767 | azabala | } |
126 | } else {
|
||
127 | retString = "null";
|
||
128 | } |
||
129 | return retString;
|
||
130 | } |
||
131 | |||
132 | /**
|
||
133 | * Based in code from JUMP (VividSolutions) and Geotools Things to be aware:
|
||
134 | * We always will use Spatial Tables with Unique ID. IFeature has the same
|
||
135 | * field order than dbLayerDef.getFieldNames()
|
||
136 | *
|
||
137 | * @param dbLayerDef
|
||
138 | * @param feat
|
||
139 | * @return
|
||
140 | */
|
||
141 | public String getSqlInsertFeature(DBLayerDefinition dbLayerDef, |
||
142 | IRow feat) { |
||
143 | String sql = "INSERT INTO "+ |
||
144 | dbLayerDef.getTableName() + " (";
|
||
145 | int numAlphanumericFields = dbLayerDef.getFieldNames().length;
|
||
146 | for (int i = 0; i < numAlphanumericFields; i++) { |
||
147 | String name = dbLayerDef.getFieldsDesc()[i].getFieldName();
|
||
148 | if (name.equals(dbLayerDef.getFieldID()))
|
||
149 | continue;
|
||
150 | sql += " " + name + ","; |
||
151 | }//for
|
||
152 | sql = sql.substring(0, sql.length() -1); |
||
153 | sql += " ) VALUES (";
|
||
154 | for (int j = 0; j < numAlphanumericFields; j++) { |
||
155 | String name = dbLayerDef.getFieldsDesc()[j].getFieldName();
|
||
156 | if (name.equals(dbLayerDef.getFieldID()))
|
||
157 | continue;
|
||
158 | |||
159 | if (isNumeric(feat.getAttribute(j)))
|
||
160 | sql += feat.getAttribute(j) + ", ";
|
||
161 | else{
|
||
162 | sql += format(feat.getAttribute(j)) + ", ";
|
||
163 | } |
||
164 | }//for
|
||
165 | 9799 | azabala | sql = sql.substring(0, sql.length() -2); |
166 | 9767 | azabala | sql += " )";
|
167 | return sql;
|
||
168 | } |
||
169 | |||
170 | |||
171 | public String getSqlModifyFeature(DBLayerDefinition dbLayerDef, IRow feat) { |
||
172 | String sql = "UPDATE " + dbLayerDef.getTableName() + " SET"; |
||
173 | int numAlphanumericFields = dbLayerDef.getFieldsDesc().length;
|
||
174 | for (int i = 0; i < numAlphanumericFields; i++) { |
||
175 | FieldDescription fldDesc = dbLayerDef.getFieldsDesc()[i]; |
||
176 | if (fldDesc != null){ |
||
177 | String name = fldDesc.getFieldName();
|
||
178 | if (name.equalsIgnoreCase(dbLayerDef.getFieldID()))
|
||
179 | continue;
|
||
180 | Value val = feat.getAttribute(i); |
||
181 | if (val != null) |
||
182 | { |
||
183 | String strAux = val.getStringValue(ValueWriter.internalValueWriter);
|
||
184 | sql += " " + name + " = " + strAux + " ,"; |
||
185 | }//if
|
||
186 | }//if
|
||
187 | }//for
|
||
188 | sql = sql.substring(0, sql.length() -1); |
||
189 | sql += " WHERE ";
|
||
190 | //TODO El feature.getID() funciona? (AZO)
|
||
191 | sql += dbLayerDef.getFieldID() + " = " + feat.getID();
|
||
192 | return sql;
|
||
193 | |||
194 | } |
||
195 | |||
196 | /**
|
||
197 | * It builds MySQL's delete statement
|
||
198 | * @param dbLayerDef
|
||
199 | * @param row
|
||
200 | * @return
|
||
201 | */
|
||
202 | public String getSqlDeleteFeature(DBLayerDefinition dbLayerDef, IRow row) { |
||
203 | String sql = "DELETE FROM " |
||
204 | + dbLayerDef.getTableName() + " WHERE ";
|
||
205 | int indexFieldId = dbLayerDef.getIdFieldID();
|
||
206 | sql += dbLayerDef.getFieldID() + " = " + row.getAttribute(indexFieldId);
|
||
207 | return sql;
|
||
208 | } |
||
209 | |||
210 | } |