1 34 35 36 package com.micronova.util; 37 38 import java.util.*; 39 import java.util.regex.*; 40 import java.sql.*; 41 import com.micronova.util.*; 42 43 44 45 public class SQLObjectSource implements ObjectSource 46 { 47 protected Connection _connection; 48 protected List _list; 49 protected ObjectSource _objectSource; 50 51 public SQLObjectSource(Connection connection, ObjectSource objectSource, List list) 52 { 53 _connection = connection; 54 _list = list; 55 _objectSource = objectSource; 56 } 57 58 public SQLObjectSource(Connection connection, ObjectSource objectSource) 59 { 60 this(connection, objectSource, new ArrayList()); 61 } 62 63 public Object getObject(Object client, Object key) 64 { 65 try 66 { 67 Object replacement = _objectSource.getObject(this, key); 68 69 if (replacement != null) 70 { 71 73 List list = TypeUtil.isList(replacement); 74 75 if (list != null) 76 { 77 StringBuffer buffer = new StringBuffer (); 78 String separator = ""; 79 80 Iterator iterator = list.iterator(); 81 82 while (iterator.hasNext()) 83 { 84 _list.add(iterator.next()); 85 86 buffer.append(separator); 87 buffer.append("?"); 88 89 separator = ","; 90 } 91 92 return buffer.toString(); 93 } 94 } 95 96 _list.add(replacement); 97 return "?"; 98 } 99 catch (Exception e) 100 { 101 throw new RuntimeException (e); 102 } 103 } 104 105 public PreparedStatement apply(PreparedStatement preparedStatement) throws Exception 106 { 107 List list = _list; 108 109 for (int i = 0; i < list.size(); i ++) 110 { 111 Object replacement = list.get(i); 112 113 if (replacement != null) 114 { 115 if (replacement instanceof java.util.Date ) 116 { 117 replacement = new java.sql.Timestamp (((java.util.Date )replacement).getTime()); 118 } 119 120 preparedStatement.setObject(i + 1, replacement); 121 } 122 else 123 { 124 preparedStatement.setNull(i + 1, Types.CHAR); 125 } 126 } 127 128 return preparedStatement; 129 } 130 131 public PreparedStatement render(String query, Pattern pattern, boolean isRecursive, boolean doesAllowGroup) throws Exception 132 { 133 String renderedQuery = (String )Template.render(query, pattern, 1, this, this, isRecursive, doesAllowGroup); 134 135 return apply(_connection.prepareStatement(renderedQuery)); 136 } 137 138 public PreparedStatement render(String query, Pattern pattern) throws Exception 139 { 140 return render(query, pattern, false, false); 141 } 142 143 public static PreparedStatement prepareStatement(Connection connection, String query, Pattern pattern, ObjectSource objectSource) throws Exception 144 { 145 return prepareStatement(connection, query, pattern, objectSource, false, false); 146 } 147 148 public static PreparedStatement prepareStatement(Connection connection, String query, Pattern pattern, ObjectSource objectSource, boolean isRecursive, boolean doesAllowGroup) throws Exception 149 { 150 SQLObjectSource sqlObjectSource = new SQLObjectSource(connection, objectSource); 151 152 return sqlObjectSource.render(query, pattern, isRecursive, doesAllowGroup); 153 } 154 } 155 156 157 | Popular Tags |