Mercurial Hosting > nabble
comparison 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 |
comparison
equal
deleted
inserted
replaced
67:9d0fefce6985 | 68:00520880ad02 |
---|---|
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.ResultSet; | |
27 import java.sql.SQLException; | |
28 import java.sql.Connection; | |
29 import java.sql.Statement; | |
30 import java.util.Collection; | |
31 import java.util.Iterator; | |
32 import fschmidt.db.DbKey; | |
33 import fschmidt.db.DbKeySetter; | |
34 import fschmidt.db.DbObject; | |
35 import fschmidt.db.LongKey; | |
36 import fschmidt.db.DbArcana; | |
37 import fschmidt.db.DbRecord; | |
38 | |
39 | |
40 class LongKeySetter implements DbKeySetter<LongKey> { | |
41 protected final String fieldName; | |
42 | |
43 public LongKeySetter(String fieldName) { | |
44 this.fieldName = fieldName; | |
45 } | |
46 | |
47 public PreparedStatement prepareStatement(Connection con,LongKey key,String sql,int keyIndex) | |
48 throws SQLException | |
49 { | |
50 PreparedStatement stmt = con.prepareStatement( | |
51 sql + " where " + fieldName + "=?" | |
52 ); | |
53 stmt.setLong(keyIndex,((LongKey)key).value()); | |
54 return stmt; | |
55 } | |
56 | |
57 public PreparedStatement prepareStatement(Connection con,Collection<LongKey> keys,String sql,DbArcana dbArcana) | |
58 throws SQLException | |
59 { | |
60 StringBuilder buf = new StringBuilder(); | |
61 buf.append( sql ); | |
62 buf.append( " where " ); | |
63 buf.append( fieldName ); | |
64 buf.append( " in (" ); | |
65 Iterator<LongKey> iter = keys.iterator(); | |
66 buf.append( iter.next().value() ); | |
67 while( iter.hasNext() ) { | |
68 buf.append( ',' ).append( iter.next().value() ); | |
69 } | |
70 buf.append( ")" ); | |
71 return con.prepareStatement(buf.toString()); | |
72 } | |
73 | |
74 public LongKey refreshKeyAfterInsert(Connection con,DbRecord<LongKey,? extends DbObject> record) throws SQLException { | |
75 if( record.fields().containsKey(fieldName) ) { | |
76 Long key = (Long)record.fields().get(fieldName); | |
77 return new LongKey(key); | |
78 } | |
79 Statement stmt = con.createStatement(); | |
80 ResultSet rs = stmt.executeQuery( | |
81 "select " + record.getDbTable().getDbDatabase().arcana().getLastSeqValFn() + " as id" | |
82 ); | |
83 rs.next(); | |
84 LongKey key = new LongKey( rs.getLong("id") ); | |
85 rs.close(); | |
86 stmt.close(); | |
87 return key; | |
88 } | |
89 | |
90 public LongKey getKey(ResultSet rs) | |
91 throws SQLException | |
92 { | |
93 return new LongKey(rs.getLong(fieldName)); | |
94 } | |
95 | |
96 public LongKey getKey(ResultSet rs,String tableName) | |
97 throws SQLException | |
98 { | |
99 return new LongKey(rs.getLong(tableName+'.'+fieldName)); | |
100 } | |
101 } |