001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/datastore/PostGISDDLGenerator.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstraße 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.tools.datastore;
044:
045: import java.io.IOException;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.util.ArrayList;
049: import java.util.Collection;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: import org.deegree.datatypes.Types;
054: import org.deegree.datatypes.UnknownTypeException;
055: import org.deegree.framework.xml.XMLParsingException;
056: import org.deegree.framework.xml.schema.XMLSchemaException;
057: import org.deegree.model.crs.UnknownCRSException;
058: import org.xml.sax.SAXException;
059:
060: /**
061: * Generator for PostGIS DDL (CREATE / DROP) operations to create PostGIS database schemas from annotated GML schema
062: * files.
063: *
064: * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
065: * @author last edited by: $Author: apoth $
066: *
067: * @version $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
068: */
069: public class PostGISDDLGenerator extends DDLGenerator {
070:
071: /**
072: * Generates a new instance of <code>PostGISDDLGenerator</code>, ready to generate DDL for the given schema.
073: *
074: * @param schemaURL
075: * @throws MalformedURLException
076: * @throws IOException
077: * @throws SAXException
078: * @throws XMLParsingException
079: * @throws XMLSchemaException
080: * @throws UnknownCRSException
081: */
082: public PostGISDDLGenerator(URL schemaURL)
083: throws MalformedURLException, IOException, SAXException,
084: XMLParsingException, XMLSchemaException,
085: UnknownCRSException {
086: super (schemaURL);
087: }
088:
089: @Override
090: protected StringBuffer generateCreateTableStmt(TableDefinition table) {
091:
092: List<ColumnDefinition> geometryColumns = new ArrayList<ColumnDefinition>();
093: StringBuffer sb = new StringBuffer("CREATE TABLE ");
094: sb.append(table.getName());
095: sb.append('(');
096: ColumnDefinition[] columns = table.getColumns();
097: boolean needComma = false;
098: for (int i = 0; i < columns.length; i++) {
099: if (!columns[i].isGeometry()) {
100: if (needComma) {
101: sb.append(',');
102: } else {
103: needComma = true;
104: }
105: sb.append("\n ");
106: sb.append(columns[i].getName());
107: sb.append(' ');
108: String typeName;
109: try {
110: typeName = Types
111: .getTypeNameForSQLTypeCode(columns[i]
112: .getType());
113: if (typeName.equals("DOUBLE")) {
114: typeName = "DOUBLE PRECISION";
115: }
116: } catch (UnknownTypeException e) {
117: typeName = "" + columns[i].getType();
118: }
119: sb.append(typeName);
120: if (!columns[i].isNullable()) {
121: sb.append(" NOT NULL");
122: }
123: } else {
124: geometryColumns.add(columns[i]);
125: }
126: }
127:
128: // add primary key information (forces index generation)
129: ColumnDefinition[] pkColumns = table.getPKColumns();
130: if (pkColumns.length > 0) {
131: sb.append(",\n PRIMARY KEY (");
132: for (int i = 0; i < pkColumns.length; i++) {
133: sb.append(pkColumns[i].getName());
134: if (i != pkColumns.length - 1) {
135: sb.append(',');
136: }
137: }
138: sb.append(")\n);\n");
139: } else {
140: sb.append("\n);\n");
141: }
142:
143: // build addGeometryColumn() statements
144: for (int i = 0; i < geometryColumns.size(); i++) {
145: ColumnDefinition column = geometryColumns.get(i);
146: sb.append("SELECT AddGeometryColumn ('', '");
147: sb.append(table.getName().toLowerCase());
148: sb.append("', '");
149: sb.append(column.getName().toLowerCase());
150: sb.append("', ");
151: sb.append(column.getSRS());
152: sb.append(", '");
153: sb.append("GEOMETRY");
154: sb.append("', '2');\n");
155: }
156: return sb;
157: }
158:
159: @Override
160: protected StringBuffer generateCreateIndexStmts(
161: TableDefinition table) {
162: StringBuffer sb = new StringBuffer();
163:
164: // build create statements for geometry indexes
165: Collection<ColumnDefinition> geometryColumns = new ArrayList<ColumnDefinition>();
166: for (ColumnDefinition column : table.getColumns()) {
167: if (column.isGeometry()) {
168: geometryColumns.add(column);
169: }
170: }
171:
172: Iterator<ColumnDefinition> iter = geometryColumns.iterator();
173: int spatialIdxCnt = 1;
174: while (iter.hasNext()) {
175: ColumnDefinition column = iter.next();
176: sb.append("CREATE INDEX ");
177: sb.append(table.getName() + (spatialIdxCnt++));
178: sb.append("_SPATIAL_IDX ON ");
179: sb.append(table.getName());
180: sb.append(" USING GIST ( ");
181: sb.append(column.getName());
182: sb.append(" GIST_GEOMETRY_OPS );");
183: sb.append('\n');
184: }
185:
186: // build table type specific create index statements
187: switch (table.getType()) {
188: case JOIN_TABLE: {
189: // create an index on every column
190: ColumnDefinition[] columns = table.getColumns();
191: for (int i = 0; i < columns.length; i++) {
192: sb.append("CREATE INDEX ");
193: sb.append(table.getName().toUpperCase());
194: sb.append('_');
195: sb.append(columns[i].getName());
196: sb.append("_IDX ON ");
197: sb.append(table.getName());
198: sb.append('(');
199: sb.append(columns[i].getName());
200: sb.append(");\n");
201: }
202: break;
203: }
204: default: {
205: break;
206: }
207: }
208: return sb;
209: }
210:
211: @Override
212: protected StringBuffer generateDropTableStmt(TableDefinition table) {
213: StringBuffer sb = new StringBuffer();
214: for (ColumnDefinition column : table.getColumns()) {
215: if (column.isGeometry()) {
216: sb.append("SELECT DropGeometryColumn ('','");
217: sb.append(table.getName().toLowerCase());
218: sb.append("', '");
219: sb.append(column.getName().toLowerCase());
220: sb.append("');\n");
221: }
222: }
223: sb.append(super.generateDropTableStmt(table));
224: return sb;
225: }
226: }
|