KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > PostgreSQLAdapter


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: PostgreSQLAdapter.java,v 1.4 2003/10/20 17:50:43 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.sql.Connection JavaDoc;
14 import java.sql.DatabaseMetaData JavaDoc;
15 import java.sql.ResultSet JavaDoc;
16 import java.sql.SQLException JavaDoc;
17 import java.sql.Types JavaDoc;
18 import javax.jdo.JDODataStoreException;
19 import javax.jdo.JDOFatalDataStoreException;
20
21 /**
22  * Provides methods for adapting SQL language elements to the PostgreSQL
23  * database.
24  *
25  * @author <a HREF="mailto:slevente@yahoo.com">Levente Sántha</a>
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  */

28
29 public class PostgreSQLAdapter extends DatabaseAdapter
30 {
31     public PostgreSQLAdapter(DatabaseMetaData JavaDoc metadata)
32     {
33         super(metadata);
34
35         if (databaseMajorVersion < 7)
36             throw new JDODataStoreException("PostgreSQL version is " + databaseMajorVersion + '.' + databaseMinorVersion + ", 7.0 or later required");
37         else if (databaseMajorVersion == 7)
38         {
39             if (databaseMinorVersion <= 2)
40             {
41                 /*
42                  * The driver correctly reports the max table name length as 32.
43                  * However, constraint names are apparently limited to 31. In
44                  * this case we get better looking names by simply treating them
45                  * all as limited to 31.
46                  */

47                 --maxTableNameLength;
48                 --maxConstraintNameLength;
49                 --maxIndexNameLength;
50             }
51         }
52     }
53
54     public String JavaDoc getVendorID()
55     {
56         return "postgresql";
57     }
58
59     public SQLState getSQLState(SQLException JavaDoc se)
60     {
61         String JavaDoc state = se.getSQLState();
62
63         if (state == null)
64             return null;
65
66         try
67         {
68             return new PostgreSQLSQLState(state);
69         }
70         catch (IllegalArgumentException JavaDoc e)
71         {
72             return null;
73         }
74     }
75
76     public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar)
77     {
78         return new TableExprAsSubquery(qs, table, rangeVar);
79     }
80
81     public TypeInfo newTypeInfo(ResultSet JavaDoc rs)
82     {
83         TypeInfo ti = new PostgreSQLTypeInfo(rs);
84
85         /*
86          * Since PostgreSQL supports many user defined data types and uses
87          * many type aliases the default methods have trouble finding the
88          * right associations between JDBC and PostgreSQL data types. We
89          * filter the returned type info to be sure we use the appropriate
90          * base PostgreSQL types for the important JDBC types.
91          */

92
93         switch (ti.dataType)
94         {
95             case Types.BIT:
96                 if (!ti.typeName.equalsIgnoreCase("bool"))
97                     return null;
98                 break;
99             case Types.CHAR:
100                 if (!ti.typeName.equalsIgnoreCase("char"))
101                     return null;
102                 break;
103             case Types.SMALLINT:
104                 if (!ti.typeName.equalsIgnoreCase("int2"))
105                     return null;
106                 break;
107             case Types.INTEGER:
108                 if (!ti.typeName.equalsIgnoreCase("int4"))
109                     return null;
110                 break;
111             case Types.BIGINT:
112                 if (!ti.typeName.equalsIgnoreCase("int8"))
113                     return null;
114                 break;
115             case Types.REAL:
116                 if (!ti.typeName.equalsIgnoreCase("float4"))
117                     return null;
118                 break;
119             case Types.DOUBLE:
120                 if (!ti.typeName.equalsIgnoreCase("float8"))
121                     return null;
122                 break;
123             case Types.NUMERIC:
124                 if (!ti.typeName.equalsIgnoreCase("numeric"))
125                     return null;
126                 break;
127             case Types.DATE:
128                 if (!ti.typeName.equalsIgnoreCase("date"))
129                     return null;
130                 break;
131             case Types.TIME:
132                 if (!ti.typeName.equalsIgnoreCase("time"))
133                     return null;
134                 break;
135             case Types.TIMESTAMP:
136                 if (!ti.typeName.equalsIgnoreCase("timestamptz"))
137                     return null;
138                 break;
139             case Types.VARCHAR:
140                 if (!ti.typeName.equalsIgnoreCase("varchar"))
141                     return null;
142                 break;
143             case Types.OTHER:
144                 /* Discard the boatload of Types.OTHER types. */
145                 return null;
146         }
147
148         return ti;
149     }
150
151     public ColumnInfo newColumnInfo(ResultSet JavaDoc rs)
152     {
153         return new PostgreSQLColumnInfo(rs);
154     }
155
156     public ForeignKeyInfo newForeignKeyInfo(ResultSet JavaDoc rs)
157     {
158         return new PostgreSQLForeignKeyInfo(rs);
159     }
160
161     public boolean supportsAlterTableDropConstraint()
162     {
163         return false;
164     }
165
166     public String JavaDoc getDropTableStatement(BaseTable table)
167     {
168         /* DROP TABLE t CASCADE is supported beginning in 7.3 */
169         if (databaseMajorVersion == 7 && databaseMinorVersion < 3)
170             return "DROP TABLE " + table.getName();
171         else
172             return "DROP TABLE " + table.getName() + " CASCADE";
173     }
174 }
175
Popular Tags