001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.revolt.dialect;
025:
026: import org.riotfamily.revolt.Script;
027: import org.riotfamily.revolt.definition.Column;
028: import org.riotfamily.revolt.definition.Index;
029: import org.riotfamily.revolt.support.TypeMap;
030:
031: /**
032: * @author Felix Gnass [fgnass at neteye dot de]
033: *
034: */
035: public class PostgresqlDialect extends Sql92Dialect {
036:
037: protected void registerTypes() {
038: registerType(TypeMap.BIT, "BOOLEAN");
039: registerType(TypeMap.TINYINT, "SMALLINT");
040: registerType(TypeMap.SMALLINT, "SMALLINT");
041: registerType(TypeMap.INTEGER, "INTEGER");
042: registerType(TypeMap.BIGINT, "BIGINT");
043: registerType(TypeMap.FLOAT, "DOUBLE PRECISION");
044: registerType(TypeMap.REAL, "REAL");
045: registerType(TypeMap.DOUBLE, "DOUBLE PRECISION");
046: registerType(TypeMap.NUMERIC, "NUMERIC");
047: registerType(TypeMap.DECIMAL, "NUMERIC");
048: registerType(TypeMap.CHAR, "CHAR", true);
049: registerType(TypeMap.VARCHAR, "VARCHAR", true);
050: registerType(TypeMap.LONGVARCHAR, "TEXT");
051: registerType(TypeMap.DATE, "DATE");
052: registerType(TypeMap.TIME, "TIME");
053: registerType(TypeMap.TIMESTAMP, "TIMESTAMP");
054: registerType(TypeMap.BINARY, "BYTEA", true);
055: registerType(TypeMap.VARBINARY, "BYTEA", true);
056: registerType(TypeMap.LONGVARBINARY, "BYTEA", true);
057: registerType(TypeMap.BLOB, "BYTEA");
058: registerType(TypeMap.CLOB, "TEXT");
059: }
060:
061: public boolean supports(String databaseProductName,
062: int majorVersion, int minorVersion) {
063:
064: return "PostgreSQL".equals(databaseProductName)
065: && majorVersion < 8;
066: }
067:
068: public Script renameTable(String name, String renameTo) {
069: return alterTable(name).append("RENAME TO").append(
070: quote(renameTo));
071: }
072:
073: public Script createIndex(String table, Index index) {
074: Script sql = new Script("CREATE INDEX").append(index.getName())
075: .append("ON").append(quote(table));
076:
077: addColumnNames(sql, index.getColumns());
078: return sql;
079: }
080:
081: public Script dropIndex(String table, String name) {
082: return new Script("DROP INDEX").append(name);
083: }
084:
085: public Script modifyColumn(String table, Column column) {
086: Script sql = new Script();
087: if (column.isDefaultValueSet()) {
088: addAlterColumn(sql, table, column);
089: if (column.getDefaultValue() != null) {
090: sql.append("SET DEFAULT").append(
091: convertQuotes(column.getDefaultValue()));
092: } else {
093: sql.append("DROP DEFAULT");
094: }
095: sql.newStatement();
096: }
097: if (column.isNotNullSet()) {
098: addAlterColumn(sql, table, column);
099: sql.append(column.isNotNull() ? "SET" : "DROP").append(
100: "NOT NULL");
101:
102: sql.newStatement();
103: }
104: if (column.getType() != null) {
105: sql.append(modifyColumnType(table, column));
106: }
107: return sql;
108: }
109:
110: protected Script modifyColumnType(String table, Column column) {
111: throw new OperationNotSupportedException(
112: "The column type can't be"
113: + " changed in PostgreSQL < 8.0");
114: }
115:
116: protected void addAlterColumn(Script sql, String table,
117: Column column) {
118: sql.append("ALTER TABLE").append(quote(table)).append(
119: "ALTER COLUMN").append(quote(column));
120: }
121:
122: public Script renameColumn(String table, String name,
123: String renameTo) {
124: return alterTable(table).append("RENAME COLUMN").append(
125: quote(name)).append("TO").append(quote(renameTo));
126: }
127:
128: public Script createAutoIncrementSequence(String name) {
129: return new Script("CREATE SEQUENCE").append(name);
130: }
131:
132: }
|