X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fstorage%2FRecordInserter.java;fp=source%2Fmir%2Fstorage%2FRecordInserter.java;h=1027970c5567757a6ae04360af75663edc4be473;hb=c9ac8fa71b679f8d967aac901bbef945c13b94c9;hp=0000000000000000000000000000000000000000;hpb=d63595f89aaa4b6a524dc0b4af9e0eef888f4c6b;p=mir.git diff --git a/source/mir/storage/RecordInserter.java b/source/mir/storage/RecordInserter.java new file mode 100755 index 00000000..1027970c --- /dev/null +++ b/source/mir/storage/RecordInserter.java @@ -0,0 +1,111 @@ +/* + * 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.PreparedStatement; +import java.sql.ResultSet; +import java.util.Date; + +/** + * Class to generate insert statements + */ +public class RecordInserter extends StatementGenerator { + + public RecordInserter(String aTableName, String aSequenceName) { + tableName = aTableName; + sequenceName = aSequenceName; + } + + /** + * 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 assignDate(String aFieldName, Date aDate) { + assignObject(aFieldName, new java.sql.Date(aDate.getTime())); + } + + public void assignVerbatim(String aFieldName, String aText) { + if (firstAssignment) { + appendQuery("insert into " + tableName + "("); + firstAssignment = false; + } + else { + appendQuery(","); + values.append(","); + } + + values.append(aText); + + appendQuery(aFieldName); + } + + /** + * Executes the statement. Returns the id of the insterted record. + */ + public String execute(Connection aConnection) throws DatabaseFailure { + appendQuery(")"); + appendQuery(CRLF); + appendQuery("values ("); + appendQuery(values.toString()); + appendQuery(")"); + + try { + int modified = executeWithModifiedCount(aConnection); + if (modified!=1) { + throw new Exception("modified count != 1 after insert"); + } + + PreparedStatement statement = aConnection.prepareStatement("select currval('" + sequenceName + "')"); + ResultSet rs = statement.executeQuery(); + rs.next(); + return rs.getString(1); + } + catch (Throwable e) { + throw new DatabaseFailure(e); + } + } + + private void assignObject(String aFieldName, Object anObject) { + appendParameter(anObject); + + assignVerbatim(aFieldName, "?"); + } + + private String tableName; + private static final String CRLF = "\n\r"; + private boolean firstAssignment = true; + private String sequenceName; + private StringBuffer values = new StringBuffer(); +} \ No newline at end of file