KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > ingres > IngresPkGenerator


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.ingres;
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  * Ingres-specific sequence based PK generator.
39  *
40  * @since 1.2
41  * @author Tore Halset
42  */

43 public class IngresPkGenerator extends OraclePkGenerator {
44     /**
45      * Generates primary key by calling Oracle sequence corresponding to the
46      * <code>dbEntity</code>. Executed SQL looks like this:
47      *
48      * <pre>
49      * SELECT nextval(pk_table_name)
50      * </pre>
51      */

52     protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception JavaDoc {
53
54         DbKeyGenerator pkGenerator = ent.getPrimaryKeyGenerator();
55         String JavaDoc pkGeneratingSequenceName;
56         if (pkGenerator != null
57                 && DbKeyGenerator.ORACLE_TYPE.equals(pkGenerator.getGeneratorType())
58                 && pkGenerator.getGeneratorName() != null)
59             pkGeneratingSequenceName = pkGenerator.getGeneratorName();
60         else
61             pkGeneratingSequenceName = sequenceName(ent);
62
63         Connection JavaDoc con = node.getDataSource().getConnection();
64         try {
65             Statement JavaDoc st = con.createStatement();
66             try {
67                 String JavaDoc sql = "SELECT " + pkGeneratingSequenceName + ".nextval";
68                 QueryLogger.logQuery(sql, Collections.EMPTY_LIST);
69                 ResultSet JavaDoc rs = st.executeQuery(sql);
70                 try {
71                     // Object pk = null;
72
if (!rs.next()) {
73                         throw new CayenneRuntimeException(
74                                 "Error generating pk for DbEntity " + ent.getName());
75                     }
76                     return rs.getInt(1);
77                 }
78                 finally {
79                     rs.close();
80                 }
81             }
82             finally {
83                 st.close();
84             }
85         }
86         finally {
87             con.close();
88         }
89     }
90
91     protected List JavaDoc getExistingSequences(DataNode node) throws SQLException JavaDoc {
92
93         // check existing sequences
94
Connection JavaDoc connection = node.getDataSource().getConnection();
95
96         try {
97             Statement JavaDoc select = connection.createStatement();
98             try {
99                 String JavaDoc sql = "select seq_name from iisequences where seq_owner != 'DBA'";
100                 QueryLogger.logQuery(sql, Collections.EMPTY_LIST);
101                 ResultSet JavaDoc rs = select.executeQuery(sql);
102                 try {
103                     List JavaDoc sequenceList = new ArrayList JavaDoc();
104                     while (rs.next()) {
105                         String JavaDoc name = rs.getString(1);
106                         if(name != null) {
107                            sequenceList.add(name.trim());
108                         }
109                     }
110                     return sequenceList;
111                 }
112                 finally {
113                     rs.close();
114                 }
115             }
116             finally {
117                 select.close();
118             }
119         }
120         finally {
121             connection.close();
122         }
123     }
124 }
125
Popular Tags