X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fstorage%2FRecordRetriever.java;fp=source%2Fmir%2Fstorage%2FRecordRetriever.java;h=3e631efc1fdfdb6808c23e385c2151ea2b84128d;hb=c9ac8fa71b679f8d967aac901bbef945c13b94c9;hp=0000000000000000000000000000000000000000;hpb=d63595f89aaa4b6a524dc0b4af9e0eef888f4c6b;p=mir.git diff --git a/source/mir/storage/RecordRetriever.java b/source/mir/storage/RecordRetriever.java new file mode 100755 index 00000000..3e631efc --- /dev/null +++ b/source/mir/storage/RecordRetriever.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2001-2006 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ +package mir.storage; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.util.*; + +/** + * Class to generate update statements + */ +public class RecordRetriever extends StatementGenerator { + public RecordRetriever(String aTableName, String aPrefix) { + tableName = aTableName; + prefix = aPrefix; + if (prefix == null || "".equals(prefix)) { + prefix = null; + } + extraTables = new ArrayList(); + whereClause = new StringBuffer(); + orderByClause = new StringBuffer(); + + appendQuery("SELECT "); + } + + public void addExtraTable(String aTableSpecification) { + extraTables.add(aTableSpecification); + } + + public void addField(String aField) { + if (firstField) { + firstField = false; + } + else { + appendQuery(", "); + } + if (prefix!=null) { + appendQuery(prefix); + appendQuery("."); + } + + appendQuery(aField); + } + + public void appendStringParameter(String aValue) { + appendParameter(aValue); + } + + public void appendWhereClause(String aPart) { + whereClause.append(aPart); + } + + public void appendOrderByClause(String aPart) { + orderByClause.append(aPart); + } + + public void setLimit(int aLimit) { + limit = aLimit; + } + + public void setOffset(int anOffset) { + offset = anOffset; + } + + /** + * Executes the statement. Returns then number of records + * changed + */ + public ResultSet execute(Connection aConnection) throws DatabaseFailure { + appendQuery(CRLF); + appendQuery("FROM "); + appendQuery(tableName); + if (prefix!=null) { + appendQuery(" "); + appendQuery(prefix); + } + + Iterator i = extraTables.iterator(); + while (i.hasNext()) { + appendQuery(", "); + appendQuery((String) i.next()); + } + + if (whereClause.toString().trim().length()>0) { + appendQuery(CRLF); + appendQuery("WHERE "); + appendQuery(whereClause.toString()); + } + + if (orderByClause.toString().trim().length()>0) { + appendQuery(CRLF); + appendQuery("ORDER BY "); + appendQuery(orderByClause.toString()); + } + + if (limit > -1) { + appendQuery(CRLF); + appendQuery("LIMIT "); + appendQuery(Integer.toString(limit)); + } + + if (offset > -1) { + appendQuery(CRLF); + appendQuery("OFFSET "); + appendQuery(Integer.toString(offset)); + } + + return executeWithResultSet(aConnection); + } + + private String tableName; + private String prefix; + private List extraTables; + private boolean firstField = true; + private StringBuffer whereClause; + private StringBuffer orderByClause; + private int limit = -1; + private int offset = -1; + + private static final String CRLF = "\n\r"; +} \ No newline at end of file