KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > frontbase > FrontBasePkGenerator


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.frontbase;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.cayenne.CayenneRuntimeException;
29 import org.apache.cayenne.access.DataNode;
30 import org.apache.cayenne.access.QueryResult;
31 import org.apache.cayenne.dba.JdbcPkGenerator;
32 import org.apache.cayenne.map.DbEntity;
33 import org.apache.cayenne.query.SQLTemplate;
34
35 /**
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

39 public class FrontBasePkGenerator extends JdbcPkGenerator {
40
41     public FrontBasePkGenerator() {
42         super();
43     }
44
45     /**
46      * Retruns zero as PK caching is not supported by FrontBaseAdapter.
47      */

48     public int getPkCacheSize() {
49         return 0;
50     }
51
52     public void createAutoPk(DataNode node, List JavaDoc dbEntities) throws Exception JavaDoc {
53         // For each entity (re)set the unique counter
54
Iterator JavaDoc it = dbEntities.iterator();
55         while (it.hasNext()) {
56             DbEntity ent = (DbEntity) it.next();
57             runUpdate(node, pkCreateString(ent.getName()));
58         }
59     }
60
61     public List JavaDoc createAutoPkStatements(List JavaDoc dbEntities) {
62         List JavaDoc list = new ArrayList JavaDoc();
63
64         Iterator JavaDoc it = dbEntities.iterator();
65         while (it.hasNext()) {
66             DbEntity ent = (DbEntity) it.next();
67             list.add(pkCreateString(ent.getName()));
68         }
69
70         return list;
71     }
72
73     public void dropAutoPk(DataNode node, List JavaDoc dbEntities) throws Exception JavaDoc {
74     }
75
76     protected String JavaDoc pkTableCreateString() {
77         return "";
78     }
79
80     protected String JavaDoc pkDeleteString(List JavaDoc dbEntities) {
81         return "-- The 'Drop Primary Key Support' option is unavailable";
82     }
83
84     protected String JavaDoc pkCreateString(String JavaDoc entName) {
85         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
86         buf.append("SET UNIQUE = 1000000 FOR \"" + entName + "\"");
87         return buf.toString();
88     }
89
90     protected String JavaDoc pkSelectString(String JavaDoc entName) {
91         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92         buf.append("SELECT UNIQUE FROM \"" + entName + "\"");
93         return buf.toString();
94     }
95
96     protected String JavaDoc pkUpdateString(String JavaDoc entName) {
97         return "";
98     }
99
100     protected String JavaDoc dropAutoPkString() {
101         return "";
102     }
103
104     protected int pkFromDatabase(DataNode node, DbEntity entity) throws Exception JavaDoc {
105         String JavaDoc template = "SELECT #result('UNIQUE' 'int') FROM " + entity.getName();
106
107         SQLTemplate query = new SQLTemplate(entity, template);
108         QueryResult observer = new QueryResult();
109         node.performQueries(Collections.singleton(query), observer);
110
111         List JavaDoc results = observer.getFirstRows(query);
112         if (results.size() != 1) {
113             throw new CayenneRuntimeException("Error fetching PK. Expected one row, got "
114                     + results.size());
115         }
116
117         Map JavaDoc row = (Map JavaDoc) results.get(0);
118         Number JavaDoc pk = (Number JavaDoc) row.get("UNIQUE");
119         return pk.intValue();
120     }
121 }
122
Popular Tags