KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > MysqlSavepoint


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 JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Savepoint JavaDoc;
30
31 /**
32  * Represents SQL SAVEPOINTS in MySQL.
33  *
34  * @author Mark Matthews
35  *
36  * @version $Id: MysqlSavepoint.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
37  */

38 public class MysqlSavepoint implements Savepoint JavaDoc {
39     private static String JavaDoc getUniqueId() {
40         // no need to re-invent the wheel here...
41
String JavaDoc uidStr = new UID JavaDoc().toString();
42
43         int uidLength = uidStr.length();
44
45         StringBuffer JavaDoc safeString = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc name) throws SQLException JavaDoc {
84         if (name == null || name.length() == 0) {
85             throw new SQLException JavaDoc("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 JavaDoc {
96         throw new SQLException JavaDoc("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 JavaDoc getSavepointName() throws SQLException JavaDoc {
104         return this.savepointName;
105     }
106 }
107
Popular Tags