| 68 | 1 /* | 
|  | 2 Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com> | 
|  | 3 | 
|  | 4 Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 5 of this software and associated documentation files (the "Software"), to deal | 
|  | 6 in the Software without restriction, including without limitation the rights | 
|  | 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 8 copies of the Software, and to permit persons to whom the Software is | 
|  | 9 furnished to do so, subject to the following conditions: | 
|  | 10 | 
|  | 11 The above copyright notice and this permission notice shall be included in | 
|  | 12 all copies or substantial portions of the Software. | 
|  | 13 | 
|  | 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|  | 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|  | 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
|  | 20 THE SOFTWARE. | 
|  | 21 */ | 
|  | 22 | 
|  | 23 package fschmidt.db.base; | 
|  | 24 | 
|  | 25 import java.sql.PreparedStatement; | 
|  | 26 import java.sql.SQLException; | 
|  | 27 import java.sql.Connection; | 
|  | 28 import java.sql.ResultSet; | 
|  | 29 import java.util.Collection; | 
|  | 30 import java.util.Iterator; | 
|  | 31 import fschmidt.db.DbKey; | 
|  | 32 import fschmidt.db.DbObject; | 
|  | 33 import fschmidt.db.StringKey; | 
|  | 34 import fschmidt.db.DbArcana; | 
|  | 35 import fschmidt.db.DbKeySetter; | 
|  | 36 import fschmidt.db.DbRecord; | 
|  | 37 | 
|  | 38 | 
|  | 39 final class StringKeySetter implements DbKeySetter<StringKey> { | 
|  | 40 	private final String fieldName; | 
|  | 41 | 
|  | 42 	public StringKeySetter(String fieldName) { | 
|  | 43 		this.fieldName = fieldName; | 
|  | 44 	} | 
|  | 45 | 
|  | 46 	public PreparedStatement prepareStatement(Connection con,StringKey key,String sql,int keyIndex) | 
|  | 47 		throws SQLException | 
|  | 48 	{ | 
|  | 49 		PreparedStatement stmt = con.prepareStatement( | 
|  | 50 			sql + " where " + fieldName + "=?" | 
|  | 51 		); | 
|  | 52 		stmt.setString(keyIndex,key.value()); | 
|  | 53 		return stmt; | 
|  | 54 	} | 
|  | 55 | 
|  | 56 	public PreparedStatement prepareStatement(Connection con,Collection<StringKey> keys,String sql,DbArcana dbArcana) | 
|  | 57 		throws SQLException | 
|  | 58 	{ | 
|  | 59 		StringBuilder buf = new StringBuilder(); | 
|  | 60 		buf.append( sql ); | 
|  | 61 		buf.append( " where " ); | 
|  | 62 		buf.append( fieldName ); | 
|  | 63 		buf.append( " in (" ); | 
|  | 64 		Iterator<StringKey> iter = keys.iterator(); | 
|  | 65 		buf.append( dbArcana.quote(iter.next().value()) ); | 
|  | 66 		while( iter.hasNext() ) { | 
|  | 67 			buf.append( ',' ).append( dbArcana.quote(iter.next().value()) ); | 
|  | 68 		} | 
|  | 69 		buf.append( ")" ); | 
|  | 70 		return con.prepareStatement(buf.toString()); | 
|  | 71 	} | 
|  | 72 | 
|  | 73 	public StringKey refreshKeyAfterInsert(Connection con,DbRecord<StringKey,? extends DbObject> record) throws SQLException { | 
|  | 74 //		throw new RuntimeException(); | 
|  | 75 //		return record.getPrimaryKey();  // ? | 
|  | 76 		String id = (String)record.fields().get(fieldName); | 
|  | 77 		return new StringKey(id); | 
|  | 78 	} | 
|  | 79 | 
|  | 80 	public StringKey getKey(ResultSet rs) | 
|  | 81 		throws SQLException | 
|  | 82 	{ | 
|  | 83 		return new StringKey(rs.getString(fieldName)); | 
|  | 84 	} | 
|  | 85 | 
|  | 86 	public StringKey getKey(ResultSet rs,String tableName) | 
|  | 87 		throws SQLException | 
|  | 88 	{ | 
|  | 89 		return new StringKey(rs.getString(tableName+'.'+fieldName)); | 
|  | 90 	} | 
|  | 91 } |