KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > postgres > PostgresPkGenerator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba.postgres;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.cayenne.CayenneRuntimeException;
31 import org.apache.cayenne.access.DataNode;
32 import org.apache.cayenne.access.QueryLogger;
33 import org.apache.cayenne.dba.oracle.OraclePkGenerator;
34 import org.apache.cayenne.map.DbEntity;
35 import org.apache.cayenne.map.DbKeyGenerator;
36
37 /**
38  * Default PK generator for PostgreSQL that uses sequences for PK generation.
39  */

40 public class PostgresPkGenerator extends OraclePkGenerator {
41
42     protected String JavaDoc createSequenceString(DbEntity ent) {
43         // note that PostgreSQL 7.4 and newer supports INCREMENT BY and START WITH
44
// however 7.3 doesn't like BY and WITH, so using older more neutral syntax
45
// that works with all tested versions.
46
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
47         buf
48                 .append("CREATE SEQUENCE ")
49                 .append(sequenceName(ent))
50                 .append(" INCREMENT ")
51                 .append(pkCacheSize(ent))
52                 .append(" START 200");
53         return buf.toString();
54     }
55
56     /**
57      * Generates primary key by calling Oracle sequence corresponding to the
58      * <code>dbEntity</code>. Executed SQL looks like this:
59      *
60      * <pre>
61      * SELECT nextval(pk_table_name)
62      * </pre>
63      */

64     protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception JavaDoc {
65
66         DbKeyGenerator pkGenerator = ent.getPrimaryKeyGenerator();
67         String JavaDoc pkGeneratingSequenceName;
68         if (pkGenerator != null
69                 && DbKeyGenerator.ORACLE_TYPE.equals(pkGenerator.getGeneratorType())
70                 && pkGenerator.getGeneratorName() != null)
71             pkGeneratingSequenceName = pkGenerator.getGeneratorName();
72         else
73             pkGeneratingSequenceName = sequenceName(ent);
74
75         Connection JavaDoc con = node.getDataSource().getConnection();
76         try {
77             Statement JavaDoc st = con.createStatement();
78             try {
79                 String JavaDoc sql = "SELECT nextval('" + pkGeneratingSequenceName + "')";
80                 QueryLogger.logQuery(sql, Collections.EMPTY_LIST);
81                 ResultSet JavaDoc rs = st.executeQuery(sql);
82                 try {
83                     // Object pk = null;
84
if (!rs.next()) {
85                         throw new CayenneRuntimeException(
86                                 "Error generating pk for DbEntity " + ent.getName());
87                     }
88                     return rs.getInt(1);
89                 }
90                 finally {
91                     rs.close();
92                 }
93             }
94             finally {
95                 st.close();
96             }
97         }
98         finally {
99             con.close();
100         }
101     }
102
103     /**
104      * Fetches a list of existing sequences that might match Cayenne generated ones.
105      */

106     protected List JavaDoc getExistingSequences(DataNode node) throws SQLException JavaDoc {
107
108         // check existing sequences
109
Connection JavaDoc con = node.getDataSource().getConnection();
110
111         try {
112             Statement JavaDoc sel = con.createStatement();
113             try {
114                 String JavaDoc sql = "SELECT relname FROM pg_class WHERE relkind='S'";
115                 QueryLogger.logQuery(sql, Collections.EMPTY_LIST);
116                 ResultSet JavaDoc rs = sel.executeQuery(sql);
117                 try {
118                     List JavaDoc sequenceList = new ArrayList JavaDoc();
119                     while (rs.next()) {
120                         sequenceList.add(rs.getString(1));
121                     }
122                     return sequenceList;
123                 }
124                 finally {
125                     rs.close();
126                 }
127             }
128             finally {
129                 sel.close();
130             }
131         }
132         finally {
133             con.close();
134         }
135     }
136 }
137
Popular Tags