Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / testPostGis.java @ 4740

History | View | Annotate | Download (12.9 KB)

1
/*
2
 * Created on 03-mar-2005
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
package com.iver.cit.gvsig.fmap.drivers.jdbc.postgis;
45

    
46
import java.awt.Shape;
47
import java.io.InputStream;
48
import java.sql.Blob;
49
import java.sql.Connection;
50
import java.sql.DriverManager;
51
import java.sql.PreparedStatement;
52
import java.sql.ResultSet;
53
import java.sql.SQLException;
54
import java.sql.Statement;
55
import java.util.Enumeration;
56

    
57
import org.postgresql.core.Field;
58
import org.postgresql.fastpath.Fastpath;
59
import org.postgresql.fastpath.FastpathArg;
60
import org.postgresql.largeobject.LargeObjectManager;
61
import org.postgresql.util.PGobject;
62

    
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser;
65

    
66

    
67
/**
68
 * @author FJP
69
 *
70
 * TODO To change the template for this generated type comment go to
71
 * Window - Preferences - Java - Code Generation - Code and Comments
72
 */
73
public class testPostGis {
74

    
75
      public static void main(String[] args) 
76
      { 
77
      /*    System.err.println("dburl has the following format:");
78
          System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME");
79
          System.err.println("tablename is 'jdbc_test' by default.");
80
          System.exit(1); */
81

    
82
          
83
          String dburl = "jdbc:postgresql://localhost/latin1";
84
          String dbuser = "postgres";
85
          String dbpass = "aquilina";
86
    
87
          String dbtable = "vias";
88
          
89
          Connection conn = null; 
90
          System.out.println("Creating JDBC connection...");
91
          try {
92
            Class.forName("org.postgresql.Driver");
93
              Enumeration enumDrivers = DriverManager.getDrivers();
94
              while (enumDrivers.hasMoreElements())
95
              {
96
                  System.out.println("Driver " + enumDrivers.nextElement().toString());
97
              }
98
              conn = DriverManager.getConnection(dburl, dbuser, dbpass);
99
          
100
              conn.setAutoCommit(false);
101
          
102
              long t1 = System.currentTimeMillis();
103
              test1(conn, dburl, dbuser, dbpass, dbtable);
104
              long t2 = System.currentTimeMillis();          
105
              System.out.println("Tiempo de consulta1:" + (t2 - t1) + " milisegundos");
106
              
107
              
108
              t1 = System.currentTimeMillis();
109
              test2(conn, dburl, dbuser, dbpass, dbtable);
110
              t2 = System.currentTimeMillis();
111
    
112
              System.out.println("Tiempo de consulta2:" + (t2 - t1) + " milisegundos");
113
               
114
              conn.close();
115

    
116
          } catch (ClassNotFoundException e) {
117
              // TODO Auto-generated catch block
118
              e.printStackTrace();
119
          } catch (SQLException e) {
120
            // TODO Auto-generated catch block
121
            e.printStackTrace();
122
          }
123
          
124
          
125
      }
126
      private static void test1(Connection conn, String dburl, String dbuser, String dbpass, String dbtable)
127
      {          
128
          try 
129
          { 
130
              // magic trickery to be pgjdbc 7.2 compatible
131
              // This works due to the late binding of data types in most java VMs. As
132
              // this is more a demo source than a real-world app, we can risk this
133
              // problem.
134
              /* if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
135
                  ((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
136
                  ((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
137
              } else {
138
                  ((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry");
139
                  ((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
140
              } */
141

    
142
              
143

    
144
            /* 
145
            * Create a statement and execute a select query. 
146
            */
147
              // String strSQL = "select AsBinary(the_geom) as geom, nom_provin from " + dbtable;
148
              String strSQL = "select ASBINARY(the_geom) as geom from " + dbtable;
149
              // strSQL = "select ASTEXT(the_geom), nom_provin as geom from " + dbtable;
150
              /* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; 
151
              strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; 
152
              strSQL = strSQL + " WHERE TRUE";
153
              */
154
              // PreparedStatement s = conn.prepareStatement(strSQL);
155
              
156
              Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
157
              // s.execute("begin");
158
              
159
              // Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
160
              // s.setFetchSize(5);
161
              int fetchSize = 150000;
162
            s.execute("declare wkb_cursor binary cursor for " + strSQL);
163
            ResultSet r =   s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor"); 
164
              /// ResultSet r =   s.executeQuery("fetch forward all in wkb_cursor");
165
            
166
            // String strSQL2 = "select AsBinary(the_geom) as geom, nom_provin from " + dbtable;
167
            // PreparedStatement ps = conn.prepareStatement(strSQL2);
168
            // ResultSet r = ps.executeQuery();
169
                        
170
              // ResultSet r = s.executeQuery(strSQL);
171
            WKBParser parser = new WKBParser();
172
            long id=0;
173
            /* for (int i=1; i< 100; i++)
174
            {
175
                r.absolute(i);
176
                System.out.println("Row " + i + ":" + r.getString(2));
177
            }
178
            r.beforeFirst();
179
              for (int i=1; i< 100; i++)
180
              {
181
                  r.absolute(i);
182
                  System.out.println("Row " + i + ":" + r.getString(2));
183
              } */
184
              
185
            while( r.next() ) 
186
            { 
187
              /* 
188
              * Retrieve the geometry as an object then cast it to the geometry type. 
189
              * Print things out. 
190
              */ 
191
                // Object obj = r.getObject(2);
192
                byte[] arrayByte = r.getBytes(1);
193
                IGeometry gp = parser.parse(arrayByte);
194
                // String strAux = r.getString(2);
195
                // int id = r.getInt(2);
196
                // System.out.println("Fila " + id + ":" + obj.toString());
197
                id++;
198
                // Geometry regeom = PGgeometry.geomFromString(obj.toString());
199
                
200
              // PGgeometry geom = (PGgeometry)obj; 
201
               // int id = r.getInt(2);
202
              // System.out.println("Row " + id + ":" + strAux); 
203
              // System.out.println(geom.toString()); 
204
                // System.out.println("provin=" + r.getString(2));
205
                /* if ((id % fetchSize) == 0)
206
                {
207
                    r =   s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor");
208
                } */
209
                            
210
            }
211
            // s.execute("end");
212
            s.close();
213
            
214
          } 
215
          catch( Exception e ) 
216
          { 
217
            e.printStackTrace(); 
218
          }  
219
        }
220
      private static void test2(Connection conn, String dburl, String dbuser, String dbpass, String dbtable)
221
      {          
222
          try 
223
          { 
224
            /* 
225
            * Create a statement and execute a select query. 
226
            */
227
              String strSQL = "select gid from " + dbtable;
228
              PreparedStatement s = conn.prepareStatement(strSQL);
229
              
230
              // Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
231
              int fetchSize = 5000;
232
              ResultSet r = s.executeQuery(strSQL);
233
              int id=0;
234
              while( r.next() ) 
235
              { 
236
                String strAux = r.getString(1);
237
                id++;
238
                // System.out.println("Row " + id + ":" + strAux); 
239
              } 
240
              s.close();
241
          } 
242
          catch( Exception e ) 
243
          { 
244
            e.printStackTrace(); 
245
          }  
246
        }
247

    
248
      
249
      private static void test3(Connection conn, String dburl, String dbuser, String dbpass, String dbtable)
250
      {          
251
          try 
252
          { 
253
                  Fastpath  fp;
254
                  if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
255
                          // ((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
256
                          // ((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
257
                  } else {
258
                          ((org.postgresql.PGConnection) conn).addDataType("geometry", "org.postgis.PGgeometry");
259
                          ((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
260
                          fp =  ((org.postgresql.PGConnection) conn).getFastpathAPI();
261
                  } 
262

    
263
              
264

    
265
            /* 
266
            * Create a statement and execute a select query. 
267
            */
268
              String strSQL = "select * from " + dbtable;
269
              // String strSQL = "select ASBINARY(the_geom) as geom from " + dbtable;
270
              // strSQL = "select ASTEXT(the_geom), nom_provin as geom from " + dbtable;
271
              /* String strSQL = "SELECT gid, rd_3, rd_5, rd_6, rd_10, rd_11, rd_12, rd_13, rd_14,"; 
272
              strSQL = strSQL + " rd_15, rd_16, kilometers, cost, metros, AsText(force_2d(the_geom)) FROM vias"; 
273
              strSQL = strSQL + " WHERE TRUE";
274
              */
275
              // PreparedStatement s = conn.prepareStatement(strSQL);
276
              
277
              Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
278
              // s.execute("begin");
279
              
280
              // Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
281
              // s.setFetchSize(5);
282
            /*  int fetchSize = 150000;
283
            s.execute("declare wkb_cursor2 binary cursor for " + strSQL);
284
            ResultSet r =   s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor2"); */
285
            
286
            // String strSQL2 = "select AsBinary(the_geom) as geom, nom_provin from " + dbtable;
287
            // PreparedStatement ps = conn.prepareStatement(strSQL2);
288
            // ResultSet r = ps.executeQuery();
289
                        
290
            ResultSet r = s.executeQuery(strSQL);
291
            WKBParser parser = new WKBParser();
292
            long id=0;
293
            /* FastpathArg args[] = new FastpathArg[2];
294
            args[0] = new FastpathArg(fd);
295
            args[1] = new FastpathArg(len);
296
            return fp.getData("loread", args); */  
297
            while( r.next() ) 
298
            { 
299
              /* 
300
              * Retrieve the geometry as an object then cast it to the geometry type. 
301
              * Print things out. 
302
              */ 
303
                Object obj = r.getObject(9);
304
                // fp.
305
                    // final Shape current = (Shape) r.getObject(1);
306
                // byte[] arrayByte = r.getBytes(1);
307
                // String strAux = r.getString(2);
308
                
309
                id++;
310
                // Geometry regeom = PGgeometry.geomFromString(obj.toString());
311
                
312
              // PGgeometry geom = (PGgeometry)obj; 
313
               // int id = r.getInt(2);
314
              // System.out.println("Row " + id + ":" + strAux); 
315
              // System.out.println(geom.toString()); 
316
                // System.out.println("provin=" + r.getString(2));
317
                /* if ((id % fetchSize) == 0)
318
                {
319
                    r =   s.executeQuery("fetch forward " + fetchSize + " in wkb_cursor");
320
                } */
321
                
322
                            
323
            }
324
            // s.execute("end");
325
            s.close();
326
            
327
          } 
328
          catch( Exception e ) 
329
          { 
330
            e.printStackTrace(); 
331
          }  
332
      }
333
      
334
}