View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Eric Long <erilong@users.sourceforge.net>,
5    *               Eric Weimer <eweimer@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 3 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, see
19   * <http://www.gnu.org/licenses/>.
20   */
21  
22  package org.jumpmind.symmetric.db.db2;
23  
24  import java.net.URL;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.jumpmind.symmetric.db.AbstractDbDialect;
29  import org.jumpmind.symmetric.db.BinaryEncoding;
30  import org.jumpmind.symmetric.db.IDbDialect;
31  import org.jumpmind.symmetric.db.SqlScript;
32  import org.jumpmind.symmetric.model.Trigger;
33  import org.jumpmind.symmetric.model.TriggerHistory;
34  
35  public class Db2DbDialect extends AbstractDbDialect implements IDbDialect {
36  
37      static final String SYNC_TRIGGERS_DISABLED_USER_VARIABLE = "sync_triggers_disabled";
38  
39      static final String SYNC_TRIGGERS_DISABLED_NODE_VARIABLE = "sync_node_disabled";
40  
41      static final Log logger = LogFactory.getLog(Db2DbDialect.class);
42  
43      protected void initForSpecificDialect() {
44          try {
45              enableSyncTriggers();
46          } catch (Exception e) {
47              try {
48                  logger.info("Creating environment variables " + SYNC_TRIGGERS_DISABLED_USER_VARIABLE
49                          + " and " + SYNC_TRIGGERS_DISABLED_NODE_VARIABLE);
50                  new SqlScript(getSqlScriptUrl(), getPlatform().getDataSource(), ';').execute();
51              } catch (Exception ex) {
52                  logger.error("Error while initializing DB2 dialect.", ex);
53              }
54          }
55      }
56  
57      private URL getSqlScriptUrl() {
58          return getClass().getResource("/dialects/db2.sql");
59      }
60  
61      protected boolean doesTriggerExistOnPlatform(String catalog, String schema, String tableName,
62              String triggerName) {
63          schema = schema == null ? (getDefaultSchema() == null ? null : getDefaultSchema()) : schema;
64          return jdbcTemplate.queryForInt("select count(*) from syscat.triggers where trigname = ?",
65                  new Object[] { triggerName.toUpperCase() }) > 0;
66      }
67  
68      public void removeTrigger(String schemaName, String triggerName) {
69          schemaName = schemaName == null ? "" : (schemaName + ".");
70          try {
71              jdbcTemplate.update("drop trigger " + schemaName + triggerName);
72          } catch (Exception e) {
73              logger.warn("Trigger " + triggerName + " does not exist");
74          }
75      }
76  
77      public void removeTrigger(String catalogName, String schemaName, String triggerName,
78              String tableName, TriggerHistory oldHistory) {
79          removeTrigger(schemaName, triggerName);
80      }
81  
82      public boolean isBlobSyncSupported() {
83          return true;
84      }
85  
86      public boolean isClobSyncSupported() {
87          return true;
88      }
89  
90      public BinaryEncoding getBinaryEncoding() {
91          return BinaryEncoding.HEX;
92      }
93  
94      public void disableSyncTriggers(String nodeId) {
95          jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_USER_VARIABLE + "=1");
96          if (nodeId != null) {
97              jdbcTemplate
98                      .update("set " + SYNC_TRIGGERS_DISABLED_NODE_VARIABLE + "='" + nodeId + "'");
99          }
100     }
101 
102     public void enableSyncTriggers() {
103         jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_USER_VARIABLE + "=null");
104         jdbcTemplate.update("set " + SYNC_TRIGGERS_DISABLED_NODE_VARIABLE + "=null");
105     }
106 
107     public String getSyncTriggersExpression() {
108         return SYNC_TRIGGERS_DISABLED_USER_VARIABLE + " is null";
109     }
110 
111     public String getTransactionTriggerExpression(Trigger trigger) {
112         return "nullif('','')";
113     }
114 
115     public String getSelectLastInsertIdSql(String sequenceName) {
116         return "values IDENTITY_VAL_LOCAL()";
117     }
118 
119     public boolean isCharSpacePadded() {
120         return true;
121     }
122 
123     public boolean isCharSpaceTrimmed() {
124         return false;
125     }
126 
127     public boolean isEmptyStringNulled() {
128         return false;
129     }
130 
131     public boolean storesUpperCaseNamesInCatalog() {
132         return true;
133     }
134 
135     public boolean supportsGetGeneratedKeys() {
136         return false;
137     }
138 
139     protected boolean allowsNullForIdentityColumn() {
140         return false;
141     }
142 
143     public void purge() {
144     }
145 
146     public String getDefaultCatalog() {
147         return null;
148     }
149 
150     public String getDefaultSchema() {
151         return (String) jdbcTemplate.queryForObject("values CURRENT SCHEMA", String.class);
152     }
153 
154     public String getIdentifierQuoteString() {
155         return "";
156     }
157 
158 }