1 32 33 package com.mockobjects.eziba.sql; 34 import java.sql.SQLException ; 35 import java.util.Map ; 36 import java.util.Iterator ; 37 import java.util.HashMap ;class ConnectionMap 38 { 39 40 ConnectionMap(String p_type) 41 { 42 m_type = p_type; 43 } 44 45 public String toString() 46 { 47 StringBuffer sb = new StringBuffer (); 48 sb.append(m_type); 49 sb.append(NEWLINE); 50 for (Iterator i = m_map.keySet().iterator(); 51 i.hasNext(); 52 ) 53 { 54 Object sql = i.next(); 55 Map map = (Map ) m_map.get(sql); 56 sb.append(sql); 57 sb.append(NEWLINE); 58 for (Iterator j = map.keySet().iterator(); 59 j.hasNext(); 60 ) 61 { 62 Object args = j.next(); 63 sb.append(args); 64 sb.append(NEWLINE); 65 } 66 sb.append(NEWLINE); 67 } 68 return sb.toString(); 69 } 70 71 public int size() 72 { 73 int result = 0; 74 for (Iterator i = m_map.values().iterator(); 75 i.hasNext(); 76 ) 77 { 78 Map m = (Map ) i.next(); 79 result += m.size(); 80 } 81 return result; 82 } 83 84 public void put(String p_sql, Object [] p_args, Object p_obj) 85 { 86 Map map = (Map ) m_map.get(p_sql); 87 if (map == null) 88 { 89 map = new HashMap (); 90 m_map.put(p_sql,map); 91 } 92 map.put(new ArgumentArray(p_args),p_obj); 93 } 94 95 public Object get(String p_sql, Object [] p_args) 96 throws SQLException 97 { 98 Map map = (Map ) m_map.get(p_sql); 99 if (map == null) 100 { 101 throw new SQLException ("No " + m_type 102 + " registered for " + p_sql); 103 } 104 else 105 { 106 ArgumentArray args = new ArgumentArray(p_args); 107 Object result = map.get(args); 108 if (result == null) 109 { 110 throw new SQLException ("No " + m_type + " registered for " 111 + p_sql + " with args "+ args 112 + ". Possible argument matches are " 113 + get(p_sql)); 114 } 115 map.remove(args); 116 if (map.size() == 0) 117 { 118 m_map.remove(p_sql); 119 } 120 return result; 121 } 122 } 123 124 128 private ArgumentArray get(String p_sql) 129 { 130 Map map = (Map ) m_map.get(p_sql); 131 if (map == null) 132 { 133 return new ArgumentArray(new Object [0]); 134 } 135 return new ArgumentArray(map.keySet().toArray()); 136 } 137 138 private final Map m_map = new HashMap (); 139 140 private final String m_type; 141 142 private static final String NEWLINE = 143 System.getProperty("line.separator"); 144 } | Popular Tags |