identitykeygenerator.java

来自「JDBF是一个实现o/r mapping 的软件」· Java 代码 · 共 151 行

JAVA
151
字号
/*
 * 20/01/2003 - 10:17:56
 *
 * $RCSfile: IdentityKeyGenerator.java,v $ - JDBF Object Relational mapping system
 * Copyright (C) 2002 JDBF Development Team
 * 
 * http://jdbf.sourceforge.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
 /*

 $Id: IdentityKeyGenerator.java,v 1.5 2004/05/20 22:40:02 gmartone Exp $

*/
package org.jdbf.engine.keygen;
    
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jdbf.castor.Messages;
import org.jdbf.engine.mapping.MappingException;
import org.jdbf.engine.repository.RepositoryView;
import org.jdbf.engine.sql.SqlInterface;

/**
 * The idea for this and the class name are from 
 * <a href="on@ibis.odessa.ua">Oleg Nitz's </a> work for castor. 
 *
 * @version $Revision: 1.5 $
 * last changed by $Author: gmartone $
 */
public class IdentityKeyGenerator implements KeyGenerator{
    
    /**
     * Class name
     */
    private static final String CLASS_NAME = "org.jdbf.engine.keygen.IdentityKeyGenerator";

    /**
     * Logger object
     */
    private Logger logger = Logger.getLogger(CLASS_NAME);
    
    
    /**
     * Generation of key is before insert.
     * 
     * @return boolean
     */
    public boolean isBeforeInsert(){
        return false;
    }

    
    /**
     * Retrieves a value generated by an auto-increment field.
     *
     * @param view RepositoryView object
     * @param conn Connection the databas
     * @param vendor name of database vendor may be null
     * @param sqlInterface
     * @return the key
     * @exception KeyGenerationException if an error occurs
     * 
     */
    public Object generateKey(RepositoryView view, Connection conn,
    						  String vendor,SqlInterface sqlInterface)
		throws KeyGenerationException{
	
		Object identity = null;
        Statement stmt = null;
        ResultSet rs = null;

		logger.log(Level.INFO,Messages.message("IdentityKeyGen.generateKey"));
		try {	    	
    		stmt = conn.createStatement();
    		rs = stmt.executeQuery(sqlInterface.getSelectInsertIdStatement());
    		if (rs.next()) {
			identity = rs.getObject(1);
    		}
    		else
        		logger.throwing(CLASS_NAME,"generateKey()",
			    new KeyGenerationException(Messages.message("mapping.keyGenFailed")
		           ));	
        		throw new KeyGenerationException(Messages.message("mapping.keyGenFailed"));
		}
		catch (SQLException excep) {
    		logger.throwing(CLASS_NAME,"generateKey()",
    				new KeyGenerationException(excep)
		           );
    		throw new KeyGenerationException(excep);
		}
		catch (MappingException excep) {
    		logger.throwing(CLASS_NAME,"generateKey()",
    				new KeyGenerationException(excep)
		           );
    		throw new KeyGenerationException(excep);
		}
		finally {
    		try {
        		if (rs != null)
	    			rs.close();
	    		stmt.close();
    		}
    		catch (SQLException excep) {
    			logger.throwing(CLASS_NAME,"generateKey()",
    				new KeyGenerationException(excep)
		           );
    			throw new KeyGenerationException(excep);	
    		}
        	finally {
	    		rs = null;
	    		stmt = null;
    		}
    		
			logger.log(Level.INFO,Messages.format("IdentityKeyGen.generatedKey",identity));
			return identity;
    	}
    }
}

/*
  $Log: IdentityKeyGenerator.java,v $
  Revision 1.5  2004/05/20 22:40:02  gmartone
  Changed for task 99073 (Coverage Javadocs)

  Revision 1.4  2004/04/29 22:38:19  gmartone

  Task 66484 (Logging System)

  
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?