X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fstorage%2FRecordUpdater.java;fp=source%2Fmir%2Fstorage%2FRecordUpdater.java;h=7aaa7bef8ebc476cd400b3fc69a590e236df5f2d;hb=c9ac8fa71b679f8d967aac901bbef945c13b94c9;hp=0000000000000000000000000000000000000000;hpb=d63595f89aaa4b6a524dc0b4af9e0eef888f4c6b;p=mir.git diff --git a/source/mir/storage/RecordUpdater.java b/source/mir/storage/RecordUpdater.java new file mode 100755 index 00000000..7aaa7bef --- /dev/null +++ b/source/mir/storage/RecordUpdater.java @@ -0,0 +1,94 @@ +/* + * 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.util.*; + +/** + * Class to generate update statements + */ +public class RecordUpdater extends StatementGenerator{ + public RecordUpdater(String aTableName, String aRecordId) { + tableName = aTableName; + recordId = aRecordId; + } + + /** + * Assigns a value to a string typed field + */ + public void assignString(String aFieldName, String aValue) { + assignObject(aFieldName, aValue); + } + + /** + * Assigns a value to a date/time typed field + */ + public void assignDateTime(String aFieldName, Date aDate) { + assignObject(aFieldName, new java.sql.Timestamp(aDate.getTime())); + } + + /** + * Assigns a verbatim value to a field. Use with case: no quoting/escaping + * will be performed + **/ + public void assignVerbatim(String aFieldName, String aValue) { + if (firstAssignment) { + appendQuery("update " + tableName + " set "); + firstAssignment = false; + } + else { + appendQuery(","); + } + + appendQuery(CRLF); + appendQuery(" " + aFieldName + " = " + aValue); + } + + /** + * Executes the statement. Returns then number of records + * changed + */ + public int execute(Connection aConnection) throws DatabaseFailure { + appendQuery(CRLF); + appendQuery("where id = ?"); + appendParameter(recordId); + + return executeWithModifiedCount(aConnection); + } + + private void assignObject(String aFieldName, Object anObject) { + appendParameter(anObject); + assignVerbatim(aFieldName, "?"); + } + + private String tableName; + private static final String CRLF = "\n\r"; + private String recordId; + private boolean firstAssignment = true; +} \ No newline at end of file