root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 1691
History | View | Annotate | Download (5.28 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.layers; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.sql.Connection; |
45 |
import java.sql.ResultSet; |
46 |
import java.sql.SQLException; |
47 |
import java.sql.Statement; |
48 |
|
49 |
import org.postgis.Geometry; |
50 |
import org.postgis.PGbox3d; |
51 |
import org.postgis.Point; |
52 |
|
53 |
import com.hardcode.driverManager.DriverLoadException; |
54 |
import com.hardcode.gdbms.engine.data.DataSource; |
55 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
56 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
57 |
import com.hardcode.gdbms.engine.data.ReadDriver; |
58 |
import com.iver.cit.gvsig.fmap.DriverException; |
59 |
import com.iver.cit.gvsig.fmap.core.FShape; |
60 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
61 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
62 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
63 |
import com.iver.cit.gvsig.fmap.drivers.postgis.WKTParser; |
64 |
import com.vividsolutions.jts.io.ParseException; |
65 |
|
66 |
|
67 |
|
68 |
/**
|
69 |
* Adapta un driver de base de datos vectorial a la interfaz vectorial,
|
70 |
* manteniendo adem?s el estado necesario por una capa vectorial de base de
|
71 |
* datos (par?metros de la conexi?n)
|
72 |
*/
|
73 |
public class VectorialDBAdapter extends VectorialAdapter { |
74 |
private int numReg=-1; |
75 |
private DataSource ds = null; |
76 |
/**
|
77 |
* incrementa el contador de las veces que se ha abierto el fichero.
|
78 |
* Solamente cuando el contador est? a cero pide al driver que conecte con
|
79 |
* la base de datos
|
80 |
*/
|
81 |
public void start() { |
82 |
} |
83 |
|
84 |
/**
|
85 |
* decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
86 |
* al driver que cierre la conexion con el servidor de base de datos
|
87 |
*/
|
88 |
public void stop() { |
89 |
} |
90 |
|
91 |
/**
|
92 |
* @return devuelve la Conexi?n a la base de datos, para que
|
93 |
* el usuario pueda hacer la consulta que quiera, si lo desea.
|
94 |
* Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
|
95 |
* avanazado y lanzar peticiones del tipo "Devuelveme un buffer
|
96 |
* a las autopistas", y con el resultset que te venga, escribir
|
97 |
* un shape, o cosas as?.
|
98 |
*/
|
99 |
public Connection getConnection() |
100 |
{ |
101 |
return ((VectorialDatabaseDriver)driver).getConnection();
|
102 |
} |
103 |
public String getFields() |
104 |
{ |
105 |
return ((VectorialDatabaseDriver)driver).getFields();
|
106 |
} |
107 |
public String getWhereClause() |
108 |
{ |
109 |
return ((VectorialDatabaseDriver)driver).getWhereClause();
|
110 |
} |
111 |
public String getTableName() |
112 |
{ |
113 |
return ((VectorialDatabaseDriver)driver).getTableName();
|
114 |
} |
115 |
|
116 |
|
117 |
/**
|
118 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
119 |
*/
|
120 |
public IGeometry getShape(int index) throws DriverIOException { |
121 |
IGeometry geom = null;
|
122 |
geom = ((VectorialDatabaseDriver)driver).getShape(index); |
123 |
return geom;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* @throws DriverException
|
128 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
|
129 |
*/
|
130 |
public int getShapeCount() throws DriverIOException { |
131 |
if (numReg == -1) |
132 |
{ |
133 |
try
|
134 |
{ |
135 |
Statement s = ((VectorialDatabaseDriver)driver).getConnection().createStatement();
|
136 |
ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + getTableName()); |
137 |
r.next(); |
138 |
numReg = r.getInt(1);
|
139 |
System.err.println("numReg = " + numReg); |
140 |
} |
141 |
catch (SQLException e) |
142 |
{ |
143 |
throw new DriverIOException(e); |
144 |
} |
145 |
} |
146 |
|
147 |
return numReg;
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
152 |
*/
|
153 |
public Rectangle2D getFullExtent() { |
154 |
return ((VectorialDatabaseDriver)driver).getFullExtent();
|
155 |
} |
156 |
|
157 |
|
158 |
/**
|
159 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
160 |
*/
|
161 |
public int getShapeType() throws DriverIOException { |
162 |
return FShape.MULTI;
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
167 |
*/
|
168 |
public DataSource getRecordset(String name) throws DriverLoadException { |
169 |
if (driver instanceof ReadDriver) |
170 |
{ |
171 |
DataSourceFactory.addDataSource((ReadDriver)driver, name); |
172 |
try {
|
173 |
ds = DataSourceFactory.createRandomDataSource(name); |
174 |
} catch (NoSuchTableException e) {
|
175 |
throw new DriverLoadException(e); |
176 |
} |
177 |
} |
178 |
return ds;
|
179 |
} |
180 |
|
181 |
} |