KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > dialect > PostgresqlDialect


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt.dialect;
25
26 import org.riotfamily.revolt.Script;
27 import org.riotfamily.revolt.definition.Column;
28 import org.riotfamily.revolt.definition.Index;
29 import org.riotfamily.revolt.support.TypeMap;
30
31 /**
32  * @author Felix Gnass [fgnass at neteye dot de]
33  *
34  */

35 public class PostgresqlDialect extends Sql92Dialect {
36
37     protected void registerTypes() {
38         registerType(TypeMap.BIT, "BOOLEAN");
39         registerType(TypeMap.TINYINT, "SMALLINT");
40         registerType(TypeMap.SMALLINT, "SMALLINT");
41         registerType(TypeMap.INTEGER, "INTEGER");
42         registerType(TypeMap.BIGINT, "BIGINT");
43         registerType(TypeMap.FLOAT, "DOUBLE PRECISION");
44         registerType(TypeMap.REAL, "REAL");
45         registerType(TypeMap.DOUBLE, "DOUBLE PRECISION");
46         registerType(TypeMap.NUMERIC, "NUMERIC");
47         registerType(TypeMap.DECIMAL, "NUMERIC");
48         registerType(TypeMap.CHAR, "CHAR", true);
49         registerType(TypeMap.VARCHAR, "VARCHAR", true);
50         registerType(TypeMap.LONGVARCHAR, "TEXT");
51         registerType(TypeMap.DATE, "DATE");
52         registerType(TypeMap.TIME, "TIME");
53         registerType(TypeMap.TIMESTAMP, "TIMESTAMP");
54         registerType(TypeMap.BINARY, "BYTEA", true);
55         registerType(TypeMap.VARBINARY, "BYTEA", true);
56         registerType(TypeMap.LONGVARBINARY, "BYTEA", true);
57         registerType(TypeMap.BLOB, "BYTEA");
58         registerType(TypeMap.CLOB, "TEXT");
59     }
60
61     public boolean supports(String JavaDoc databaseProductName,
62             int majorVersion, int minorVersion) {
63
64         return "PostgreSQL".equals(databaseProductName) && majorVersion < 8;
65     }
66         
67     public Script renameTable(String JavaDoc name, String JavaDoc renameTo) {
68         return alterTable(name).append("RENAME TO").append(quote(renameTo));
69     }
70
71     public Script createIndex(String JavaDoc table, Index index) {
72         Script sql = new Script("CREATE INDEX").append(index.getName())
73                 .append("ON").append(quote(table));
74
75         addColumnNames(sql, index.getColumns());
76         return sql;
77     }
78
79     public Script dropIndex(String JavaDoc table, String JavaDoc name) {
80         return new Script("DROP INDEX").append(name);
81     }
82
83     public Script modifyColumn(String JavaDoc table, Column column) {
84         Script sql = new Script();
85         if (column.isDefaultValueSet()) {
86             addAlterColumn(sql, table, column);
87             if (column.getDefaultValue() != null) {
88                 sql.append("SET DEFAULT").append(convertQuotes(column.getDefaultValue()));
89             }
90             else {
91                 sql.append("DROP DEFAULT");
92             }
93             sql.newStatement();
94         }
95         if (column.isNotNullSet()) {
96             addAlterColumn(sql, table, column);
97             sql.append(column.isNotNull() ? "SET" : "DROP").append("NOT NULL");
98
99             sql.newStatement();
100         }
101         if (column.getType() != null) {
102             sql.append(modifyColumnType(table, column));
103         }
104         return sql;
105     }
106
107     protected Script modifyColumnType(String JavaDoc table, Column column) {
108         throw new OperationNotSupportedException("The column type can't be" +
109                 " changed in PostgreSQL < 8.0");
110     }
111
112     protected void addAlterColumn(Script sql, String JavaDoc table, Column column) {
113         sql.append("ALTER TABLE").append(quote(table))
114                 .append("ALTER COLUMN").append(quote(column));
115     }
116
117     public Script renameColumn(String JavaDoc table, String JavaDoc name, String JavaDoc renameTo) {
118         return alterTable(table).append("RENAME COLUMN").append(quote(name))
119                 .append("TO").append(quote(renameTo));
120     }
121     
122     public Script createAutoIncrementSequence(String JavaDoc name) {
123         return new Script("CREATE SEQUENCE").append(name);
124     }
125
126 }
127
Popular Tags