View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.jumpmind.symmetric.upgrade;
22  
23  import java.sql.ResultSet;
24  import java.sql.SQLException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.jumpmind.symmetric.model.Node;
29  import org.springframework.jdbc.core.RowCallbackHandler;
30  
31  public class SqlDrivenUpgradeTask extends AbstractSqlUpgradeTask {
32  
33      private static final Log logger = LogFactory.getLog(SqlDrivenUpgradeTask.class);
34  
35      protected String driverSql;
36  
37      protected String updateSql;
38  
39      public void upgrade(final Node node, int[] fromVersion) {
40          String sql = prepareSql(node, driverSql);
41          logger.warn("Upgrade for each: " + sql);
42          logger.warn("Upgrade do: " + updateSql);
43          jdbcTemplate.query(sql, new RowCallbackHandler() {
44              public void processRow(ResultSet rs) throws SQLException {
45                  int count = rs.getMetaData().getColumnCount();
46                  Object[] params = new Object[count];
47                  for (int i = 0; i < count; i++) {
48                      params[i] = rs.getObject(i + 1);
49                  }
50                  jdbcTemplate.update(prepareSql(node, updateSql), params);
51              }
52          });
53      }
54  
55      public void setDriverSql(String driverSql) {
56          this.driverSql = driverSql;
57      }
58  
59      public void setUpdateSql(String updateSql) {
60          this.updateSql = updateSql;
61      }
62  
63  }