001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (C) 2012 Paul Ferraro 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package net.sf.hajdbc.dialect.db2; 019 020import java.sql.DatabaseMetaData; 021import java.sql.ResultSet; 022import java.sql.SQLException; 023import java.sql.Statement; 024import java.util.Collection; 025import java.util.LinkedList; 026import java.util.List; 027 028import net.sf.hajdbc.IdentityColumnSupport; 029import net.sf.hajdbc.SequenceProperties; 030import net.sf.hajdbc.SequencePropertiesFactory; 031import net.sf.hajdbc.SequenceSupport; 032import net.sf.hajdbc.dialect.StandardDialect; 033import net.sf.hajdbc.util.Resources; 034 035/** 036 * Dialect for DB2 (commercial). 037 * @author Paul Ferraro 038 * @since 1.1 039 */ 040@SuppressWarnings("nls") 041public class DB2Dialect extends StandardDialect 042{ 043 /** 044 * {@inheritDoc} 045 * @see net.sf.hajdbc.dialect.StandardDialect#vendorPattern() 046 */ 047 @Override 048 protected String vendorPattern() 049 { 050 return "db2"; 051 } 052 053 /** 054 * @see net.sf.hajdbc.dialect.StandardDialect#executeFunctionFormat() 055 */ 056 @Override 057 protected String executeFunctionFormat() 058 { 059 return "VALUES {0}"; 060 } 061 062 /** 063 * {@inheritDoc} 064 * @see net.sf.hajdbc.dialect.StandardDialect#getSequenceSupport() 065 */ 066 @Override 067 public SequenceSupport getSequenceSupport() 068 { 069 return this; 070 } 071 072 /** 073 * {@inheritDoc} 074 * @see net.sf.hajdbc.dialect.StandardDialect#getIdentityColumnSupport() 075 */ 076 @Override 077 public IdentityColumnSupport getIdentityColumnSupport() 078 { 079 return this; 080 } 081 082 @Override 083 public Collection<SequenceProperties> getSequences(DatabaseMetaData metaData, SequencePropertiesFactory factory) throws SQLException 084 { 085 Statement statement = metaData.getConnection().createStatement(); 086 087 try 088 { 089 ResultSet resultSet = statement.executeQuery("SELECT SEQSCHEMA, SEQNAME, INCREMENT FROM SYSCAT.SEQUENCES"); 090 091 List<SequenceProperties> sequences = new LinkedList<SequenceProperties>(); 092 093 while (resultSet.next()) 094 { 095 sequences.add(factory.createSequenceProperties(resultSet.getString(1), resultSet.getString(2), resultSet.getInt(3))); 096 } 097 098 return sequences; 099 } 100 finally 101 { 102 Resources.close(statement); 103 } 104 } 105 106 /** 107 * @see net.sf.hajdbc.dialect.StandardDialect#sequencePattern() 108 */ 109 @Override 110 protected String sequencePattern() 111 { 112 return "(?:NEXT|PREV)VAL\\s+FOR\\s+'?([^',\\s\\(\\)]+)"; 113 } 114 115 /** 116 * @see net.sf.hajdbc.dialect.StandardDialect#nextSequenceValueFormat() 117 */ 118 @Override 119 protected String nextSequenceValueFormat() 120 { 121 return "NEXTVAL FOR {0}"; 122 } 123 124 /** 125 * @see net.sf.hajdbc.dialect.StandardDialect#dateLiteralFormat() 126 */ 127 @Override 128 protected String dateLiteralFormat() 129 { 130 return this.timestampLiteralFormat(); 131 } 132 133 /** 134 * @see net.sf.hajdbc.dialect.StandardDialect#timeLiteralFormat() 135 */ 136 @Override 137 protected String timeLiteralFormat() 138 { 139 return this.timestampLiteralFormat(); 140 } 141 142 /** 143 * @see net.sf.hajdbc.dialect.StandardDialect#timestampLiteralFormat() 144 */ 145 @Override 146 protected String timestampLiteralFormat() 147 { 148 return "''{0}''"; 149 } 150}