Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/db/DbSequence.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (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
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.db;
22  import java.sql.*;
23  import java.io.*;
24  import javatools.util.FileLog;
25  import javatools.util.Props;
26  import javatools.util.SubstituteVariable;
27  
28  /**
29   * Generates unique values for use as keys. Constructor is not public. Use
30   * DbDatabase.getSequence().
31   *
32   * @author Chris
33   * @created December 13, 2001
34   * @version 0.7
35   * @commentedby Antonio Petrelli
36   */
37  
38  public class DbSequence {
39      /** The database to be used.
40       */    
41    DbDatabase db;
42          /** The name for this sequence.
43           */        
44    String name;
45  
46          /** Creates a new DbSequence.
47           * @param db The database to use.
48           * @param name The name of this sequence.
49           */        
50    DbSequence(DbDatabase db, String name) {
51      this.db = db;
52      this.name = name;
53    }
54  
55          /** Gets the next value.
56           * @throws DbException If something goes wrong.
57           * @return The requested value.
58           */        
59    public int next() throws DbException {
60      return next(db.getThreadConnection());
61    }
62  
63          /** Returns the next value, with a specific connection.
64           * @param dbcon The connection to use.
65           * @throws DbException If something goes wrong.
66           * @return The needed value.
67           */        
68    public int next(DbConnection dbcon) throws DbException {
69      try {
70        Connection con = dbcon.getSqlConnection();
71        Props props = Props.singleton("dbvendor");
72        String sql = props.getProperty(db.getProperty("vendor") + ".nextSequenceSql");
73        sql = SubstituteVariable.substitute(sql, "${name}", name);
74        FileLog.singleton().error("DbSequence.next", sql);
75        PreparedStatement ps = con.prepareStatement(sql);
76        ResultSet rs = ps.executeQuery();
77        if (!rs.next()) {
78          throw new DbException("Can't read " + name + " sequence");
79        }
80        int rtn = rs.getInt(1);
81        ps.close();
82        return rtn;
83      } catch (SQLException e) {
84        throw new DbException(e);
85      } catch (IOException e) {
86        throw new DbException(e);
87      }
88    }
89  }