All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foundationdb.sql.jdbc.jdbc3.PSQLSavepoint Maven / Gradle / Ivy

There is a newer version: 2.1-0-jdbc41
Show newest version
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package com.foundationdb.sql.jdbc.jdbc3;

import java.sql.SQLException;
import java.sql.Savepoint;

import com.foundationdb.sql.jdbc.core.Utils;
import com.foundationdb.sql.jdbc.util.GT;
import com.foundationdb.sql.jdbc.util.PSQLException;
import com.foundationdb.sql.jdbc.util.PSQLState;

public class PSQLSavepoint implements Savepoint {

    private boolean _isValid;
    private boolean _isNamed;
    private int _id;
    private String _name;

    public PSQLSavepoint(int id) {
        _isValid = true;
        _isNamed = false;
        _id = id;
    }

    public PSQLSavepoint(String name) {
        _isValid = true;
        _isNamed = true;
        _name = name;
    }

    public int getSavepointId() throws SQLException {
        if (!_isValid)
            throw new PSQLException(GT.tr("Cannot reference a savepoint after it has been released."),
                                    PSQLState.INVALID_SAVEPOINT_SPECIFICATION);

        if (_isNamed)
            throw new PSQLException(GT.tr("Cannot retrieve the id of a named savepoint."),
                                    PSQLState.WRONG_OBJECT_TYPE);

        return _id;
    }

    public String getSavepointName() throws SQLException {
        if (!_isValid)
            throw new PSQLException(GT.tr("Cannot reference a savepoint after it has been released."),
                                    PSQLState.INVALID_SAVEPOINT_SPECIFICATION);

        if (!_isNamed)
            throw new PSQLException(GT.tr("Cannot retrieve the name of an unnamed savepoint."),
                                    PSQLState.WRONG_OBJECT_TYPE);

        return _name;
    }

    public void invalidate() {
        _isValid = false;
    }

    public String getPGName() throws SQLException {
        if (!_isValid)
            throw new PSQLException(GT.tr("Cannot reference a savepoint after it has been released."),
                                    PSQLState.INVALID_SAVEPOINT_SPECIFICATION);

        if (_isNamed)
        {
            // We need to quote and escape the name in case it
            // contains spaces/quotes/etc.
            //
            return Utils.appendEscapedIdentifier(null, _name).toString();
        }

        return "JDBC_SAVEPOINT_" + _id;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy