| 1 | /* |
| 2 | Copyright (C) 2002-2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 25 | package com.mysql.jdbc; |
| 26 | |
| 27 | import java.rmi.server.UID; |
| 28 | import java.sql.SQLException; |
| 29 | import java.sql.Savepoint; |
| 30 | |
| 31 | /** |
| 32 | * Represents SQL SAVEPOINTS in MySQL. |
| 33 | * |
| 34 | * @author Mark Matthews |
| 35 | * |
| 36 | * @version $Id: MysqlSavepoint.java 4489 2005-10-31 18:43:01 -0600 (Mon, 31 Oct 2005) mmatthews $ |
| 37 | */ |
| 38 | public class MysqlSavepoint implements Savepoint { |
| 39 | private static String getUniqueId() { |
| 40 | // no need to re-invent the wheel here... |
| 41 | String uidStr = new UID().toString(); |
| 42 | |
| 43 | int uidLength = uidStr.length(); |
| 44 | |
| 45 | StringBuffer safeString = new StringBuffer(uidLength); |
| 46 | |
| 47 | for (int i = 0; i < uidLength; i++) { |
| 48 | char c = uidStr.charAt(i); |
| 49 | |
| 50 | if (Character.isLetter(c) || Character.isDigit(c)) { |
| 51 | safeString.append(c); |
| 52 | } else { |
| 53 | safeString.append('_'); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return safeString.toString(); |
| 58 | } |
| 59 | |
| 60 | private String savepointName; |
| 61 | |
| 62 | /** |
| 63 | * Creates an unnamed savepoint. |
| 64 | * |
| 65 | * @param conn |
| 66 | * |
| 67 | * @throws SQLException |
| 68 | * if an error occurs |
| 69 | */ |
| 70 | MysqlSavepoint() throws SQLException { |
| 71 | this(getUniqueId()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates a named savepoint |
| 76 | * |
| 77 | * @param name |
| 78 | * the name of the savepoint. |
| 79 | * |
| 80 | * @throws SQLException |
| 81 | * if name == null or is empty. |
| 82 | */ |
| 83 | MysqlSavepoint(String name) throws SQLException { |
| 84 | if (name == null || name.length() == 0) { |
| 85 | throw SQLError.createSQLException("Savepoint name can not be NULL or empty", |
| 86 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 87 | } |
| 88 | |
| 89 | this.savepointName = name; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @see java.sql.Savepoint#getSavepointId() |
| 94 | */ |
| 95 | public int getSavepointId() throws SQLException { |
| 96 | throw SQLError.createSQLException("Only named savepoints are supported.", |
| 97 | SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @see java.sql.Savepoint#getSavepointName() |
| 102 | */ |
| 103 | public String getSavepointName() throws SQLException { |
| 104 | return this.savepointName; |
| 105 | } |
| 106 | } |