Mercurial Hosting > nabble
comparison src/fschmidt/db/DbUtils.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; | |
| 24 | |
| 25 import java.sql.Connection; | |
| 26 import java.sql.SQLException; | |
| 27 import java.sql.ResultSet; | |
| 28 import java.sql.Statement; | |
| 29 import java.util.Date; | |
| 30 | |
| 31 | |
| 32 public final class DbUtils { | |
| 33 private DbUtils() {} // never | |
| 34 | |
| 35 public static <K extends DbKey,V extends DbObject<K,V>> V getGoodCopy(V obj) { | |
| 36 if( obj==null ) | |
| 37 return null; | |
| 38 DbRecord<K,V> record = obj.getDbRecord(); | |
| 39 return !record.isStale() ? obj | |
| 40 : record.getDbTable().findByPrimaryKey(record.getPrimaryKey()); | |
| 41 } | |
| 42 | |
| 43 public static <K extends DbKey,V extends DbObject<K,V>> void uncache(V obj) { | |
| 44 DbRecord<K,V> record = obj.getDbRecord(); | |
| 45 record.getDbTable().uncache(record.getPrimaryKey()); | |
| 46 } | |
| 47 | |
| 48 public static boolean isStale(DbObject obj) { | |
| 49 return obj==null || obj.getDbRecord().isStale(); | |
| 50 } | |
| 51 | |
| 52 public static boolean isStale(DbObject[] objs) { | |
| 53 if( objs==null ) | |
| 54 return true; | |
| 55 for( int i=0; i<objs.length; i++ ) { | |
| 56 if( isStale(objs[i]) ) | |
| 57 return true; | |
| 58 } | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 public static boolean isEqual(DbObject o1,DbObject o2) { | |
| 63 DbRecord r1 = o1.getDbRecord(); | |
| 64 DbRecord r2 = o2.getDbRecord(); | |
| 65 if( !r1.isInDb() ) | |
| 66 return o1.equals(o2); | |
| 67 return r1.getPrimaryKey().equals(r2.getPrimaryKey()); | |
| 68 } | |
| 69 | |
| 70 public static Date getDate(ResultSet rs,String columnName) throws SQLException { | |
| 71 java.sql.Timestamp ts = rs.getTimestamp(columnName); | |
| 72 return ts==null ? null : new Date(ts.getTime()); | |
| 73 } | |
| 74 | |
| 75 public static void test(DbDatabase db) { | |
| 76 try { | |
| 77 Connection con = db.getConnection(); | |
| 78 try { | |
| 79 Statement stmt = con.createStatement(); | |
| 80 stmt.executeQuery("select 1"); | |
| 81 stmt.close(); | |
| 82 } finally { | |
| 83 con.close(); | |
| 84 } | |
| 85 } catch(SQLException e) { | |
| 86 throw new RuntimeException("test failed",e); | |
| 87 } | |
| 88 } | |
| 89 } |
