View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Eric Long <erilong@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.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.jumpmind.symmetric.db.IDbDialect;
28  import org.jumpmind.symmetric.model.Node;
29  
30  public class SqlUpgradeTask extends AbstractSqlUpgradeTask {
31  
32      private static final Log logger = LogFactory.getLog(SqlUpgradeTask.class);
33  
34      protected IDbDialect dbDialect;
35  
36      protected String dialectName;
37  
38      protected List<String> sqlList;
39  
40      protected boolean ignoreFailure;
41  
42      public void upgrade(int[] fromVersion) {
43          for (String sql : sqlList) {
44              logger.warn("Upgrade: " + sql);
45              jdbcTemplate.update(sql);
46          }
47      }
48  
49      public void upgrade(Node node, int[] fromVersion) {
50          if (dialectName == null || (dbDialect != null && dbDialect.getName().equalsIgnoreCase((dialectName)))) {
51              for (String sql : sqlList) {
52                  sql = prepareSql(node, sql);
53                  logger.warn("Upgrade: " + sql);
54                  if (ignoreFailure) {
55                      try {
56                          jdbcTemplate.update(sql);
57                      } catch (Exception e) {
58                          logger.warn("Ignoring failure of last upgrade statement: " + e.getMessage());
59                      }
60                  } else {
61                      jdbcTemplate.update(sql);
62                  }
63              }
64          }
65      }
66  
67      public void setSqlList(List<String> sqlList) {
68          this.sqlList = sqlList;
69      }
70  
71      public void setIgnoreFailure(boolean ignoreFailure) {
72          this.ignoreFailure = ignoreFailure;
73      }
74  
75      public void setDbDialect(IDbDialect dbDialect) {
76          this.dbDialect = dbDialect;
77      }
78  
79      public void setDialectName(String dialectName) {
80          this.dialectName = dialectName;
81      }
82  
83  }