1 16 package org.apache.juddi.datastore.jdbc; 17 18 import java.sql.Connection ; 19 import java.sql.PreparedStatement ; 20 import java.sql.ResultSet ; 21 import java.util.Vector ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.juddi.datatype.request.FindQualifiers; 26 import org.apache.juddi.util.jdbc.DynamicQuery; 27 28 31 class FindBindingByServiceKeyQuery 32 { 33 private static Log log = LogFactory.getLog(FindBindingByServiceKeyQuery.class); 35 36 static String selectSQL; 37 static 38 { 39 StringBuffer sql = new StringBuffer (200); 41 sql.append("SELECT B.BINDING_KEY,B.LAST_UPDATE "); 42 sql.append("FROM BINDING_TEMPLATE B "); 43 selectSQL = sql.toString(); 44 } 45 46 55 public static Vector select(String serviceKey,Vector keysIn,FindQualifiers qualifiers,Connection connection) 56 throws java.sql.SQLException 57 { 58 if ((keysIn != null) && (keysIn.size() == 0)) 63 return keysIn; 64 65 Vector keysOut = new Vector (); 66 PreparedStatement statement = null; 67 ResultSet resultSet = null; 68 DynamicQuery dynStmt = new DynamicQuery(); 69 70 DynamicQuery sql = new DynamicQuery(selectSQL); 71 appendWhere(sql,serviceKey,qualifiers); 72 appendIn(sql,keysIn); 73 appendOrderBy(sql,qualifiers); 74 75 try 76 { 77 log.debug(sql.toString()); 78 79 statement = sql.buildPreparedStatement(connection); 80 resultSet = statement.executeQuery(); 81 while (resultSet.next()) 82 { 83 keysOut.addElement(resultSet.getString(1)); } 85 86 return keysOut; 87 } 88 finally 89 { 90 try { 91 resultSet.close(); 92 } 93 catch (Exception e) 94 { 95 log.warn("An Exception was encountered while attempting to close " + 96 "the Find BindingTemplate ResultSet: "+e.getMessage(),e); 97 } 98 99 try { 100 statement.close(); 101 } 102 catch (Exception e) 103 { 104 log.warn("An Exception was encountered while attempting to close " + 105 "the Find BindingTemplate Statement: "+e.getMessage(),e); 106 } 107 } 108 } 109 110 113 private static void appendWhere(DynamicQuery sql,String serviceKey,FindQualifiers qualifiers) 114 { 115 sql.append("WHERE B.SERVICE_KEY = ? "); 116 sql.addValue(serviceKey); 117 } 118 119 128 private static void appendIn(DynamicQuery sql,Vector keysIn) 129 { 130 if (keysIn == null) 131 return; 132 133 sql.append("AND B.BINDING_KEY IN ("); 134 135 int keyCount = keysIn.size(); 136 for (int i=0; i<keyCount; i++) 137 { 138 String key = (String )keysIn.elementAt(i); 139 140 sql.append("?"); 141 sql.addValue(key); 142 143 if ((i+1) < keyCount) 144 sql.append(","); 145 } 146 147 sql.append(") "); 148 } 149 150 153 private static void appendOrderBy(DynamicQuery sql,FindQualifiers qualifiers) 154 { 155 sql.append("ORDER BY "); 156 157 if (qualifiers == null) 158 sql.append("B.LAST_UPDATE DESC"); 159 else if (qualifiers.sortByDateAsc) 160 sql.append("B.LAST_UPDATE ASC"); 161 else 162 sql.append("B.LAST_UPDATE DESC"); 163 } 164 } 165 | Popular Tags |