KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapping > KeyGenerator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapping;
24 import java.sql.*;
25
26 /**
27  * This generator is a built-in default implementation intended as a device
28  * for relational primary and foreign keys generation. It uses the compilation
29  * context to get the needed parameters, especially for initialization.
30  * <b>WARNING !!!</b>
31  * Currently, this generator is limited: it uses the target column provided by
32  * the compilation context to initialize a counter (performing a 'SELECT
33  * MAX(...) FROM ...' which is totally managed in memory (using 1 increments).
34  * This has the advantage to be extremely rapid (no useless database access,
35  * JDBC batch usable...) but the major drawback of this approach is the fact that
36  * <b>concurrency in insertion is not supported</b>...
37  */

38 public class KeyGenerator implements UserGenerator {
39
40     private long counter = -1;
41     private String JavaDoc table = null;
42     private String JavaDoc column = null;
43     boolean useQuotes = false;
44     
45     public KeyGenerator(CompilationContext properties) {
46         table = properties.getProperty(CompilationContext.TABLE_NAME);
47         column = properties.getProperty(CompilationContext.COLUMN_NAME);
48         useQuotes = Boolean.valueOf(
49             properties.getProperty(CompilationContext.USE_QUOTES_4_DDL)
50             ).booleanValue();
51     }
52
53     public String JavaDoc getXMLType() {
54         return "long";
55     }
56
57     public synchronized Object JavaDoc getValue(StorageContext context) {
58         if (counter < 0)
59             initCounter(context.getConnection());
60         return new Long JavaDoc(++counter);
61     }
62     
63     private void initCounter(Connection connection) {
64         ResultSet rs = null;
65         Statement statement = null;
66         try {
67             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("select max(");
68             if (useQuotes)
69                 buf.append('"');
70             buf.append(column);
71             if (useQuotes)
72                 buf.append('"');
73             buf.append(") from ");
74             if (useQuotes)
75                 buf.append('"');
76             buf.append(table);
77             if (useQuotes)
78                 buf.append('"');
79             
80             statement = connection.createStatement();
81             rs = statement.executeQuery(buf.toString());
82             while (rs.next()) {
83                 counter = rs.getLong(1);
84             }
85         }
86         catch (Exception JavaDoc ex) {
87             ex.printStackTrace();
88         }
89         finally {
90             try {
91                 if (rs != null)
92                     rs.close();
93                 if (statement != null)
94                     statement.close();
95             }
96             catch (Exception JavaDoc ex) {
97                 // no op
98
}
99         }
100     }
101 }
102
Popular Tags