001 //$HeadURL: svn+ssh://mschneider@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/io/datastore/sql/postgis/PostGISDatastore.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 037 package org.deegree.io.datastore.sql.mysql; 038 039 import java.sql.Connection; 040 import java.sql.SQLException; 041 042 import org.deegree.framework.log.ILogger; 043 import org.deegree.framework.log.LoggerFactory; 044 import org.deegree.io.datastore.Datastore; 045 import org.deegree.io.datastore.DatastoreException; 046 import org.deegree.io.datastore.schema.MappedFeatureType; 047 import org.deegree.io.datastore.sql.AbstractSQLDatastore; 048 import org.deegree.io.datastore.sql.StatementBuffer; 049 import org.deegree.io.datastore.sql.TableAliasGenerator; 050 import org.deegree.io.datastore.sql.VirtualContentProvider; 051 import org.deegree.io.datastore.sql.postgis.PGgeometryAdapter; 052 import org.deegree.io.datastore.sql.wherebuilder.WhereBuilder; 053 import org.deegree.model.crs.CoordinateSystem; 054 import org.deegree.model.filterencoding.Filter; 055 import org.deegree.model.spatialschema.Geometry; 056 import org.deegree.model.spatialschema.GeometryException; 057 import org.deegree.ogcbase.SortProperty; 058 import org.postgis.PGgeometry; 059 import org.postgis.binary.BinaryParser; 060 import org.postgis.binary.BinaryWriter; 061 062 /** 063 * {@link Datastore} implementation for MySQL spatial databases. 064 * 065 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a> 066 * @author last edited by: $Author: aschmitz $ 067 * 068 * @version $Revision: 12552 $, $Date: 2008-06-26 12:01:34 +0200 (Do, 26 Jun 2008) $ 069 */ 070 public class MySQLDatastore extends AbstractSQLDatastore { 071 072 private static final ILogger LOG = LoggerFactory.getLogger( MySQLDatastore.class ); 073 074 /** 075 * Returns a specific {@link WhereBuilder} implementation for MySQL. 076 * 077 * @param rootFts 078 * involved (requested) feature types 079 * @param aliases 080 * aliases for the feature types, may be null 081 * @param filter 082 * filter that restricts the matched features 083 * @param sortProperties 084 * sort criteria for the result, may be null or empty 085 * @param aliasGenerator 086 * used to generate unique table aliases 087 * @param vcProvider 088 * @return <code>WhereBuilder</code> implementation for MySQL 089 * @throws DatastoreException 090 */ 091 @Override 092 public MySQLWhereBuilder getWhereBuilder( MappedFeatureType[] rootFts, String[] aliases, Filter filter, 093 SortProperty[] sortProperties, TableAliasGenerator aliasGenerator, 094 VirtualContentProvider vcProvider ) 095 throws DatastoreException { 096 return new MySQLWhereBuilder( rootFts, aliases, filter, sortProperties, aliasGenerator, vcProvider ); 097 } 098 099 /** 100 * Converts a MySQL specific geometry <code>Object</code> (a byte[] containing WKB) from the <code>ResultSet</code> 101 * to a deegree <code>Geometry</code>. 102 * 103 * @param value 104 * @param targetCS 105 * @param conn 106 * @return corresponding deegree geometry 107 * @throws SQLException 108 */ 109 @Override 110 public Geometry convertDBToDeegreeGeometry( Object value, CoordinateSystem targetCS, Connection conn ) 111 throws SQLException { 112 Geometry geometry = null; 113 if ( value != null && value instanceof byte[] ) { 114 try { 115 LOG.logDebug( "Converting MySQL geometry (WKB byte array) to deegree geometry ('" 116 + targetCS.getIdentifier() + "')" ); 117 byte[] wkb = (byte[]) value; 118 // parsing WKB using PostGIS' binary parser to generate PostGIS' geometry 119 org.postgis.Geometry pgGeometry = new BinaryParser().parse( wkb ); 120 geometry = PGgeometryAdapter.wrap( pgGeometry, targetCS ); 121 } catch ( Exception e ) { 122 e.printStackTrace(); 123 throw new SQLException( "Error converting MySQL geometry to deegree geometry: " + e.getMessage() ); 124 } 125 } 126 return geometry; 127 } 128 129 /** 130 * Converts a deegree <code>Geometry</code> to a MySQL specific geometry object (a byte[] containing WKB). 131 * 132 * @param geometry 133 * @param targetSRS 134 * @param conn 135 * @return corresponding PostGIS specific geometry object 136 * @throws DatastoreException 137 */ 138 @Override 139 public byte[] convertDeegreeToDBGeometry( Geometry geometry, int targetSRS, Connection conn ) 140 throws DatastoreException { 141 byte[] wkb; 142 try { 143 PGgeometry pgGeometry = PGgeometryAdapter.export( geometry, targetSRS ); 144 wkb = new BinaryWriter().writeBinary( pgGeometry.getGeometry() ); 145 } catch ( GeometryException e ) { 146 throw new DatastoreException( "Error converting deegree geometry to MySQL geometry: " + e.getMessage(), e ); 147 } 148 return wkb; 149 } 150 151 @Override 152 public void appendGeometryColumnGet( StatementBuffer query, String tableAlias, String column ) { 153 query.append( "AsBinary(" ); 154 query.append( tableAlias ); 155 query.append( '.' ); 156 query.append( column ); 157 query.append( ')' ); 158 } 159 }