1 56 57 package org.objectstyle.cayenne.access.jdbc; 58 59 import java.sql.Connection ; 60 import java.sql.PreparedStatement ; 61 import java.sql.SQLException ; 62 import java.util.Arrays ; 63 import java.util.Collections ; 64 import java.util.Iterator ; 65 import java.util.Map ; 66 67 import org.apache.commons.collections.IteratorUtils; 68 import org.objectstyle.cayenne.CayenneException; 69 import org.objectstyle.cayenne.access.OperationObserver; 70 import org.objectstyle.cayenne.access.QueryLogger; 71 import org.objectstyle.cayenne.dba.DbAdapter; 72 import org.objectstyle.cayenne.query.SQLAction; 73 import org.objectstyle.cayenne.query.SQLTemplate; 74 import org.objectstyle.cayenne.util.Util; 75 76 82 public class SQLTemplateAction implements SQLAction { 83 84 protected DbAdapter adapter; 85 protected boolean removingLineBreaks; 86 protected SQLTemplate query; 87 88 public SQLTemplateAction(SQLTemplate query, DbAdapter adapter) { 89 this.query = query; 90 this.adapter = adapter; 91 } 92 93 96 public DbAdapter getAdapter() { 97 return adapter; 98 } 99 100 103 public void performAction(Connection connection, OperationObserver observer) 104 throws SQLException , Exception { 105 106 String template = extractTemplateString(); 107 108 if (template == null) { 110 throw new CayenneException("No template string configured for adapter " 111 + getAdapter().getClass().getName()); 112 } 113 114 boolean loggable = QueryLogger.isLoggable(query.getLoggingLevel()); 115 int size = query.parametersSize(); 116 117 SQLTemplateProcessor templateProcessor = new SQLTemplateProcessor(); 118 119 int[] counts = new int[size > 0 ? size : 1]; 122 Iterator it = (size > 0) ? query.parametersIterator() : IteratorUtils 123 .singletonIterator(Collections.EMPTY_MAP); 124 for (int i = 0; i < counts.length; i++) { 125 Map nextParameters = (Map ) it.next(); 126 127 SQLStatement compiled = templateProcessor.processTemplate(template, 128 nextParameters); 129 130 if (loggable) { 131 QueryLogger.logQuery(query.getLoggingLevel(), compiled.getSql(), Arrays 132 .asList(compiled.getBindings())); 133 } 134 135 PreparedStatement statement = connection.prepareStatement(compiled.getSql()); 139 try { 140 bind(statement, compiled.getBindings()); 141 counts[i] = statement.executeUpdate(); 142 QueryLogger.logUpdateCount(query.getLoggingLevel(), counts[i]); 143 } 144 finally { 145 statement.close(); 146 } 147 } 148 149 observer.nextBatchCount(query, counts); 150 } 151 152 158 protected String extractTemplateString() { 159 String sql = query.getTemplate(getAdapter().getClass().getName()); 160 return isRemovingLineBreaks() ? Util.stripLineBreaks(sql, " ") : sql; 161 } 162 163 166 protected void bind(PreparedStatement preparedStatement, ParameterBinding[] bindings) 167 throws SQLException , Exception { 168 if (bindings.length > 0) { 170 int len = bindings.length; 171 for (int i = 0; i < len; i++) { 172 adapter.bindParameter(preparedStatement, 173 bindings[i].getValue(), 174 i + 1, 175 bindings[i].getJdbcType(), 176 bindings[i].getPrecision()); 177 } 178 } 179 } 180 181 185 public boolean isRemovingLineBreaks() { 186 return removingLineBreaks; 187 } 188 189 public void setRemovingLineBreaks(boolean removingLineBreaks) { 190 this.removingLineBreaks = removingLineBreaks; 191 } 192 193 196 public SQLTemplate getQuery() { 197 return query; 198 } 199 } | Popular Tags |