diff src/fschmidt/db/base/LongKeySetter.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fschmidt/db/base/LongKeySetter.java	Sun Oct 05 17:24:15 2025 -0600
@@ -0,0 +1,101 @@
+/*
+Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+package fschmidt.db.base;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Connection;
+import java.sql.Statement;
+import java.util.Collection;
+import java.util.Iterator;
+import fschmidt.db.DbKey;
+import fschmidt.db.DbKeySetter;
+import fschmidt.db.DbObject;
+import fschmidt.db.LongKey;
+import fschmidt.db.DbArcana;
+import fschmidt.db.DbRecord;
+
+
+class LongKeySetter implements DbKeySetter<LongKey> {
+	protected final String fieldName;
+
+	public LongKeySetter(String fieldName) {
+		this.fieldName = fieldName;
+	}
+
+	public PreparedStatement prepareStatement(Connection con,LongKey key,String sql,int keyIndex)
+		throws SQLException
+	{
+		PreparedStatement stmt = con.prepareStatement(
+			sql + " where " + fieldName + "=?"
+		);
+		stmt.setLong(keyIndex,((LongKey)key).value());
+		return stmt;
+	}
+
+	public PreparedStatement prepareStatement(Connection con,Collection<LongKey> keys,String sql,DbArcana dbArcana)
+		throws SQLException
+	{
+		StringBuilder buf = new StringBuilder();
+		buf.append( sql );
+		buf.append( " where " );
+		buf.append( fieldName );
+		buf.append( " in (" );
+		Iterator<LongKey> iter = keys.iterator();
+		buf.append( iter.next().value() );
+		while( iter.hasNext() ) {
+			buf.append( ',' ).append( iter.next().value() );
+		}
+		buf.append( ")" );
+		return con.prepareStatement(buf.toString());
+	}
+
+	public LongKey refreshKeyAfterInsert(Connection con,DbRecord<LongKey,? extends DbObject> record) throws SQLException {
+		if( record.fields().containsKey(fieldName) ) {
+			Long key = (Long)record.fields().get(fieldName);
+			return new LongKey(key);
+		}
+		Statement stmt = con.createStatement();
+		ResultSet rs = stmt.executeQuery(
+			"select " + record.getDbTable().getDbDatabase().arcana().getLastSeqValFn() + " as id"
+		);
+		rs.next();
+		LongKey key = new LongKey( rs.getLong("id") );
+		rs.close();
+		stmt.close();
+		return key;
+	}
+
+	public LongKey getKey(ResultSet rs)
+		throws SQLException
+	{
+		return new LongKey(rs.getLong(fieldName));
+	}
+
+	public LongKey getKey(ResultSet rs,String tableName)
+		throws SQLException
+	{
+		return new LongKey(rs.getLong(tableName+'.'+fieldName));
+	}
+}