Mercurial Hosting > nabble
comparison src/fschmidt/db/base/DbTableImpl.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 fschmidt.db.DbKey; | |
26 import fschmidt.db.DbKeySetter; | |
27 import fschmidt.db.DbObject; | |
28 import fschmidt.db.DbObjectFactory; | |
29 import fschmidt.db.ListenerList; | |
30 import fschmidt.db.SQLRuntimeException; | |
31 import fschmidt.db.extend.DbDatabaseExt; | |
32 import fschmidt.db.extend.DbTableExt; | |
33 | |
34 import java.sql.Connection; | |
35 import java.sql.PreparedStatement; | |
36 import java.sql.ResultSet; | |
37 import java.sql.SQLException; | |
38 import java.util.Collection; | |
39 import java.util.HashMap; | |
40 import java.util.Map; | |
41 | |
42 | |
43 final class DbTableImpl<K extends DbKey,V extends DbObject<K,V>> extends DbTableExt<K,V> { | |
44 private final String tableName; | |
45 private final DbKeySetter<K> keySetter; | |
46 private final DbObjectFactory<K,V> objFactory; | |
47 | |
48 DbTableImpl(DbDatabaseExt database,String tableName,DbKeySetter<K> keySetter,DbObjectFactory<K,V> objFactory) { | |
49 super(database); | |
50 this.tableName = tableName; | |
51 this.keySetter = keySetter; | |
52 this.objFactory = objFactory; | |
53 } | |
54 | |
55 public String getTableName() { | |
56 return tableName; | |
57 } | |
58 | |
59 public DbKeySetter<K> getKeySetter() { | |
60 return keySetter; | |
61 } | |
62 | |
63 public final V findByPrimaryKey(K key) { | |
64 try { | |
65 Connection con = database.getConnection(); | |
66 try { | |
67 PreparedStatement stmt = keySetter.prepareStatement( | |
68 con, key, "select * from "+tableName, 1 | |
69 ); | |
70 ResultSet rs = stmt.executeQuery(); | |
71 V obj = null; | |
72 if( rs.next() ) | |
73 obj = getDbObject(key,rs); | |
74 stmt.close(); | |
75 return obj; | |
76 } finally { | |
77 con.close(); | |
78 } | |
79 } catch(SQLException e) { | |
80 throw new SQLRuntimeException("key = "+key,e); | |
81 } | |
82 } | |
83 | |
84 public final Map<K,V> findByPrimaryKey(Collection<K> keys) { | |
85 Map<K,V> map = new HashMap<K,V>(); | |
86 try { | |
87 Connection con = database.getConnection(); | |
88 try { | |
89 PreparedStatement stmt = keySetter.prepareStatement( | |
90 con, keys, "select * from "+tableName, getDbDatabase().arcana() | |
91 ); | |
92 ResultSet rs = stmt.executeQuery(); | |
93 while( rs.next() ) { | |
94 K key = keySetter.getKey(rs); | |
95 map.put( key, getDbObject(key,rs) ); | |
96 } | |
97 stmt.close(); | |
98 } finally { | |
99 con.close(); | |
100 } | |
101 } catch(SQLException e) { | |
102 throw new SQLRuntimeException("keys = "+keys,e); | |
103 } | |
104 return map; | |
105 } | |
106 | |
107 public V getDbObject(K key,ResultSet rs,String tableName) | |
108 throws SQLException | |
109 { | |
110 return objFactory.makeDbObject(key,rs,tableName); | |
111 } | |
112 | |
113 | |
114 private ListenerList<V> preInsertListeners; | |
115 private ListenerList<V> preUpdateListeners; | |
116 private ListenerList<V> preDeleteListeners; | |
117 private ListenerList<V> postInsertListeners; | |
118 private ListenerList<V> postUpdateListeners; | |
119 private ListenerList<V> postDeleteListeners; | |
120 | |
121 public synchronized ListenerList<V> getPreInsertListeners() { | |
122 if (preInsertListeners == null) | |
123 preInsertListeners = new ListenerList<V>(); | |
124 return preInsertListeners; | |
125 } | |
126 | |
127 public synchronized ListenerList<V> getPreUpdateListeners() { | |
128 if (preUpdateListeners == null) | |
129 preUpdateListeners = new ListenerList<V>(); | |
130 return preUpdateListeners; | |
131 } | |
132 | |
133 public synchronized ListenerList<V> getPreDeleteListeners() { | |
134 if (preDeleteListeners == null) | |
135 preDeleteListeners = new ListenerList<V>(); | |
136 return preDeleteListeners; | |
137 } | |
138 | |
139 public synchronized ListenerList<V> getPostInsertListeners() { | |
140 if (postInsertListeners == null) | |
141 postInsertListeners = new ListenerList<V>(); | |
142 return postInsertListeners; | |
143 } | |
144 | |
145 public synchronized ListenerList<V> getPostUpdateListeners() { | |
146 if (postUpdateListeners == null) | |
147 postUpdateListeners = new ListenerList<V>(); | |
148 return postUpdateListeners; | |
149 } | |
150 | |
151 public synchronized ListenerList<V> getPostDeleteListeners() { | |
152 if (postDeleteListeners == null) | |
153 postDeleteListeners = new ListenerList<V>(); | |
154 return postDeleteListeners; | |
155 } | |
156 | |
157 public void uncache(DbKey key) {} | |
158 | |
159 } |