001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/datastore/sql/StatementBuffer.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 package org.deegree.io.datastore.sql; 037 038 import java.sql.PreparedStatement; 039 import java.util.ArrayList; 040 import java.util.Iterator; 041 import java.util.List; 042 043 /** 044 * Helper class for the creation and logging of {@link PreparedStatement}s. 045 * <p> 046 * It allows to concatenate the query step by step and holds the arguments of the query as well. 047 * 048 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a> 049 * @author last edited by: $Author: mschneider $ 050 * 051 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 052 */ 053 public class StatementBuffer { 054 055 // contains the SQL-query string 056 private StringBuffer queryBuffer = new StringBuffer( 500 ); 057 058 // contains the arguments of the query 059 private List<StatementArgument> argumentList = new ArrayList<StatementArgument>(); 060 061 /** 062 * Appends the given character to the statement. 063 * 064 * @param c 065 */ 066 public void append( char c ) { 067 this.queryBuffer.append( c ); 068 } 069 070 /** 071 * Appends the given string to the statement. 072 * 073 * @param s 074 */ 075 public void append( String s ) { 076 this.queryBuffer.append( s ); 077 } 078 079 /** 080 * Appends the given {@link StringBuffer} to the statement. 081 * 082 * @param sb 083 */ 084 public void append( StringBuffer sb ) { 085 this.queryBuffer.append( sb ); 086 } 087 088 /** 089 * Appends the given argument (as the replacement value for the '?' character in the query) to the statement. 090 * 091 * @param o 092 * @param typeCode 093 */ 094 public void addArgument( Object o, int typeCode ) { 095 StatementArgument argument = new StatementArgument( o, typeCode ); 096 this.argumentList.add( argument ); 097 } 098 099 /** 100 * Returns the query string (without the arguments' values). 101 * 102 * @return the query string (without the arguments' values) 103 */ 104 public String getQueryString() { 105 return this.queryBuffer.toString(); 106 } 107 108 /** 109 * Returns an {@link Iterator} over the arguments of the query. 110 * 111 * @return an Iterator over the arguments of the query 112 */ 113 public Iterator<StatementArgument> getArgumentsIterator() { 114 return this.argumentList.iterator(); 115 } 116 117 @Override 118 public String toString() { 119 return queryBuffer.toString(); 120 } 121 122 /** 123 * Encapsulates an argument value and the SQL type code for the target column. 124 * 125 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a> 126 * @author last edited by: $Author: mschneider $ 127 * 128 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 129 */ 130 public class StatementArgument { 131 132 private Object argumentValue; 133 134 private int typeCode; 135 136 /** 137 * Creates a new instance of {@link StatementArgument}. 138 * 139 * @param argumentValue 140 * argument for a statement 141 * @param typeCode 142 * SQL type code of the target column 143 */ 144 StatementArgument( Object argumentValue, int typeCode ) { 145 this.argumentValue = argumentValue; 146 this.typeCode = typeCode; 147 } 148 149 /** 150 * Returns the argument value. 151 * 152 * @return the argument value 153 */ 154 public Object getArgument() { 155 return this.argumentValue; 156 } 157 158 /** 159 * Returns the SQL type code for the column that is targeted by the argument. 160 * 161 * @return the SQL type code for the column that is targeted by the argument 162 */ 163 public int getTypeCode() { 164 return this.typeCode; 165 } 166 } 167 }