KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > MckoiConnection


1 /**
2  * com.mckoi.database.jdbc.MckoiConnection 04 Oct 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import java.sql.*;
28
29 /**
30  * Wraps a Connection and provides Mckoi specific extensions that are
31  * outside the JDBC specification.
32  * <p>
33  * Example,
34  * <pre>
35  * Connection connection = java.sql.DriverManager.getConnection( .... );
36  * MckoiConnection mckoi_connection = new MckoiConnection(connection);
37  * // 'mckoi_connection' is used for mckoi specific comms.
38  * </pre>
39  *
40  * @author Tobias Downer
41  */

42
43 public final class MckoiConnection {
44
45   /**
46    * The wrapped MConnection.
47    */

48   private MConnection connection;
49
50   /**
51    * Constructs the Mckoi specific extension access object.
52    */

53   public MckoiConnection(Connection connection) {
54     if (connection instanceof MConnection) {
55       this.connection = (MConnection) connection;
56     }
57     else {
58       throw new Error JavaDoc("Can only wrap a Mckoi Database JDBC connection.");
59     }
60   }
61
62   /**
63    * This method can be used to disable strict get object in ResultSet. If
64    * strict get object is disabled then the 'getObject' method will return the
65    * raw data type that the engine uses to represent the respective data
66    * item. If it is enabled the 'getObject' method returns the correct type
67    * as specified by the JDBC spec.
68    * <p>
69    * Strict get is enabled by default.
70    */

71   public void setStrictGetObject(boolean status) {
72     connection.setStrictGetObject(status);
73   }
74
75   /**
76    * This method is used to enable verbose column names in ResultSetMetaData.
77    * If verbose column names is enabled the getColumnName method returns
78    * a string which includes the schema and table name. This property is
79    * disabled by default and provided only for compatibility with older
80    * Mckoi applications.
81    */

82   public void setVerboseColumnNames(boolean status) {
83     connection.setVerboseColumnNames(status);
84   }
85
86   /**
87    * Registers a TriggerListener to listen for any triggers that are fired
88    * with the given name. A TriggerListener may be registered to listen for
89    * multiple database triggers.
90    * <p>
91    * NOTE: All trigger events are fired on a dedicated trigger thread. All
92    * triggers are fired from this thread in sequence.
93    *
94    * @param trigger_name the name of the database trigger to listen for.
95    * @param trigger_listener the listener to be notified when the trigger
96    * event occurs.
97    */

98   public void addTriggerListener(String JavaDoc trigger_name,
99                                  TriggerListener trigger_listener) {
100     connection.addTriggerListener(trigger_name, trigger_listener);
101   }
102
103   /**
104    * Removes a TriggerListener that is listening for triggers with the given
105    * name.
106    *
107    * @param trigger_name the name of the database trigger to stop listening
108    * for.
109    * @param trigger_listener the listener to stop being notified of trigger
110    * events for this trigger name.
111    */

112   public void removeTriggerListener(String JavaDoc trigger_name,
113                                     TriggerListener trigger_listener) {
114     connection.removeTriggerListener(trigger_name, trigger_listener);
115   }
116
117
118   // ---------- Static methods ----------
119

120   /**
121    * Given a string, this will use escape codes to convert the Java string into
122    * a Mckoi SQL string that can be parsed correctly by the database.
123    * For example;<p>
124    * <pre>
125    * String user_input = [some untrusted string]
126    * Statement statement = connection.createStatement();
127    * ResultSet result = statement.executeQuery(
128    * "SELECT number FROM Part WHERE number = " +
129    * MckoiConnection.quote(user_input));
130    * </pre>
131    * If the user supplies the string "Gr's\nut\'", this method will generate
132    * the SQL query string;<p>
133    * <pre>
134    * SELECT number FROM Part WHERE number = 'Gr\'s\\nut\\\''
135    * </pre>
136    * This is used for generating secure dynamic SQL commands. It is
137    * particularly important if the quoted strings are coming from an untrusted
138    * source.
139    * <p>
140    * This security precaution is not necessary if using PreparedStatement to
141    * form the SQL parameters.
142    */

143   public static String JavaDoc quote(String JavaDoc java_string) {
144     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
145     int str_len = java_string.length();
146     for (int i = 0; i < str_len; ++i) {
147       char c = java_string.charAt(i);
148       if (c == '\'' || c == '\\') {
149         buf.append('\\');
150       }
151       if (c == '\n') {
152         buf.append("\\n");
153       }
154       else if (c == '\r') {
155         buf.append("\\r");
156       }
157       else if (c == '\t') {
158         buf.append("\\t");
159       }
160       else {
161         buf.append(c);
162       }
163     }
164     return new String JavaDoc(buf);
165   }
166
167 }
168
Popular Tags