001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.dialect;
022:
023: import java.sql.Connection;
024: import java.sql.DatabaseMetaData;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import java.sql.Types;
029: import java.util.ArrayList;
030: import java.util.List;
031: import java.util.regex.Pattern;
032:
033: import net.sf.hajdbc.ColumnProperties;
034: import net.sf.hajdbc.util.Strings;
035:
036: /**
037: * Dialect for <a href="http://postgresql.org">PostgreSQL</a>.
038: * @author Paul Ferraro
039: * @since 1.1
040: */
041: @SuppressWarnings("nls")
042: public class PostgreSQLDialect extends StandardDialect {
043: /**
044: * PostgreSQL uses a schema search path to locate unqualified table names.
045: * The default search path is [$user,public], where $user is the current user.
046: * @see net.sf.hajdbc.dialect.StandardDialect#getDefaultSchemas(java.sql.DatabaseMetaData)
047: */
048: @Override
049: public List<String> getDefaultSchemas(DatabaseMetaData metaData)
050: throws SQLException {
051: Connection connection = metaData.getConnection();
052: Statement statement = connection.createStatement();
053:
054: ResultSet resultSet = statement
055: .executeQuery("SHOW search_path");
056:
057: resultSet.next();
058:
059: String[] schemas = resultSet.getString(1).split(Strings.COMMA);
060:
061: resultSet.close();
062: statement.close();
063:
064: List<String> schemaList = new ArrayList<String>(schemas.length);
065:
066: for (String schema : schemas) {
067: schemaList.add(schema.equals("$user") ? metaData
068: .getUserName() : schema);
069: }
070:
071: return schemaList;
072: }
073:
074: /**
075: * PostgreSQL uses the native type OID to identify BLOBs.
076: * However the JDBC driver incomprehensibly maps OIDs to INTEGERs.
077: * The PostgreSQL JDBC folks claim this intentional.
078: * @see net.sf.hajdbc.dialect.StandardDialect#getColumnType(net.sf.hajdbc.ColumnProperties)
079: */
080: @Override
081: public int getColumnType(ColumnProperties properties) {
082: return properties.getNativeType().equalsIgnoreCase("oid") ? Types.BLOB
083: : properties.getType();
084: }
085:
086: /**
087: * @see net.sf.hajdbc.dialect.StandardDialect#isIdentity(net.sf.hajdbc.ColumnProperties)
088: */
089: @Override
090: public boolean isIdentity(ColumnProperties properties) {
091: String type = properties.getNativeType();
092:
093: return type.equalsIgnoreCase("serial")
094: || type.equalsIgnoreCase("bigserial");
095: }
096:
097: /**
098: * Versions >=8.1 of the PostgreSQL JDBC driver return incorrect values for DatabaseMetaData.getExtraNameCharacters().
099: * @see net.sf.hajdbc.dialect.StandardDialect#getIdentifierPattern(java.sql.DatabaseMetaData)
100: */
101: @Override
102: public Pattern getIdentifierPattern(DatabaseMetaData metaData)
103: throws SQLException {
104: if ((metaData.getDriverMajorVersion() >= 8)
105: && (metaData.getDriverMinorVersion() >= 1)) {
106: return Pattern
107: .compile("[A-Za-z\\0200-\\0377_][A-Za-z\\0200-\\0377_0-9\\$]*");
108: }
109:
110: return super .getIdentifierPattern(metaData);
111: }
112:
113: /**
114: * @see net.sf.hajdbc.dialect.StandardDialect#truncateTableFormat()
115: */
116: @Override
117: protected String truncateTableFormat() {
118: return "TRUNCATE TABLE {0}";
119: }
120:
121: /**
122: * @see net.sf.hajdbc.dialect.StandardDialect#sequencePattern()
123: */
124: @Override
125: protected String sequencePattern() {
126: return "(?:CURR|NEXT)VAL\\s*\\(\\s*'([^']+)'\\s*\\)";
127: }
128:
129: /**
130: * @see net.sf.hajdbc.dialect.StandardDialect#nextSequenceValueFormat()
131: */
132: @Override
133: protected String nextSequenceValueFormat() {
134: return "NEXTVAL(''{0}'')";
135: }
136:
137: /**
138: * @see net.sf.hajdbc.dialect.StandardDialect#alterIdentityColumnFormat()
139: */
140: @Override
141: protected String alterIdentityColumnFormat() {
142: return "ALTER SEQUENCE {0}_{1}_seq RESTART WITH {2}";
143: }
144:
145: /**
146: * @see net.sf.hajdbc.dialect.StandardDialect#currentTimestampPattern()
147: */
148: @Override
149: protected String currentTimestampPattern() {
150: return super .currentTimestampPattern()
151: + "|(?<=\\W)NOW\\s*\\(\\s*\\)|(?<=\\W)TRANSACTION_TIMESTAMP\\s*\\(\\s*\\)|(?<=\\W)STATEMENT_TIMESTAMP\\s*\\(\\s*\\)|(?<=\\W)CLOCK_TIMESTAMP\\s*\\(\\s*\\)";
152: }
153:
154: /**
155: * @see net.sf.hajdbc.dialect.StandardDialect#randomPattern()
156: */
157: @Override
158: protected String randomPattern() {
159: return "(?<=\\W)RANDOM\\s*\\(\\s*\\)";
160: }
161:
162: /**
163: * Recognizes FOR SHARE and FOR UPDATE.
164: * @see net.sf.hajdbc.dialect.StandardDialect#selectForUpdatePattern()
165: */
166: @Override
167: protected String selectForUpdatePattern() {
168: return "SELECT\\s+.+\\s+FOR\\s+(SHARE|UPDATE)";
169: }
170: }
|