001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package com.mysql.jdbc;
026:
027: import java.rmi.server.UID;
028: import java.sql.SQLException;
029: import java.sql.Savepoint;
030:
031: /**
032: * Represents SQL SAVEPOINTS in MySQL.
033: *
034: * @author Mark Matthews
035: *
036: * @version $Id: MysqlSavepoint.java 4489 2005-11-01 00:43:01Z mmatthews $
037: */
038: public class MysqlSavepoint implements Savepoint {
039: private static String getUniqueId() {
040: // no need to re-invent the wheel here...
041: String uidStr = new UID().toString();
042:
043: int uidLength = uidStr.length();
044:
045: StringBuffer safeString = new StringBuffer(uidLength);
046:
047: for (int i = 0; i < uidLength; i++) {
048: char c = uidStr.charAt(i);
049:
050: if (Character.isLetter(c) || Character.isDigit(c)) {
051: safeString.append(c);
052: } else {
053: safeString.append('_');
054: }
055: }
056:
057: return safeString.toString();
058: }
059:
060: private String savepointName;
061:
062: /**
063: * Creates an unnamed savepoint.
064: *
065: * @param conn
066: *
067: * @throws SQLException
068: * if an error occurs
069: */
070: MysqlSavepoint() throws SQLException {
071: this (getUniqueId());
072: }
073:
074: /**
075: * Creates a named savepoint
076: *
077: * @param name
078: * the name of the savepoint.
079: *
080: * @throws SQLException
081: * if name == null or is empty.
082: */
083: MysqlSavepoint(String name) throws SQLException {
084: if (name == null || name.length() == 0) {
085: throw SQLError.createSQLException(
086: "Savepoint name can not be NULL or empty",
087: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
088: }
089:
090: this .savepointName = name;
091: }
092:
093: /**
094: * @see java.sql.Savepoint#getSavepointId()
095: */
096: public int getSavepointId() throws SQLException {
097: throw SQLError.createSQLException(
098: "Only named savepoints are supported.",
099: SQLError.SQL_STATE_DRIVER_NOT_CAPABLE);
100: }
101:
102: /**
103: * @see java.sql.Savepoint#getSavepointName()
104: */
105: public String getSavepointName() throws SQLException {
106: return this.savepointName;
107: }
108: }
|