File: PSQLSavepoint.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (82 lines) | stat: -rw-r--r-- 2,525 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/jdbc3/PSQLSavepoint.java,v 1.10 2008/01/08 06:56:29 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.jdbc3;

import java.sql.SQLException;
import java.sql.Savepoint;
import org.postgresql.core.Utils;
import org.postgresql.util.PSQLException;
import org.postgresql.util.GT;
import org.postgresql.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;
    }

}