KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > Driver_MySQL


1 /*
2  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  *
6  */

7
8 package com.hp.hpl.jena.db.impl;
9
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13
14 import com.hp.hpl.jena.db.*;
15
16
17 /**
18  * @author hkuno based on code by Dave Reynolds
19  *
20  * Extends DriverRDB with MySQL-specific parameters.
21  */

22 public class Driver_MySQL extends DriverRDB {
23     
24     /** The name of the database type this driver supports */
25     
26     /**
27      * Constructor
28      */

29     public Driver_MySQL( ){
30         super();
31
32         String JavaDoc myPackageName = this.getClass().getPackage().getName();
33         
34         DATABASE_TYPE = "MySQL";
35         DRIVER_NAME = "com.mysql.jdbc.Driver";
36         
37         ID_SQL_TYPE = "INTEGER";
38         URI_COMPRESS = false;
39         INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 250;
40         LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 250;
41         TABLE_NAME_LENGTH_MAX = 64;
42         IS_XACT_DB = true;
43         PRE_ALLOCATE_ID = false;
44         SKIP_DUPLICATE_CHECK = false;
45         SQL_FILE = "etc/mysql.sql";
46         DB_NAMES_TO_UPPER = false;
47         setTableNames(TABLE_NAME_PREFIX);
48         QUOTE_CHAR = '\'';
49
50         
51         m_psetClassName = myPackageName + ".PSet_TripleStore_RDB";
52         m_psetReifierClassName = myPackageName + ".PSet_ReifStore_RDB";
53         
54         m_lsetClassName = myPackageName + ".SpecializedGraph_TripleStore_RDB";
55         m_lsetReifierClassName = myPackageName + ".SpecializedGraphReifier_RDB";
56     }
57     
58     /**
59      * Set the database connection
60      */

61     public void setConnection( IDBConnection dbcon ) {
62         m_dbcon = dbcon;
63         
64         try {
65             // Properties defaultSQL = SQLCache.loadSQLFile(DEFAULT_SQL_FILE, null, ID_SQL_TYPE);
66
// m_sql = new SQLCache(SQL_FILE, defaultSQL, dbcon, ID_SQL_TYPE);
67
m_sql = new SQLCache(SQL_FILE, null, dbcon, ID_SQL_TYPE);
68         } catch (Exception JavaDoc e) {
69             e.printStackTrace( System.err );
70             logger.error("Unable to set connection for Driver:", e);
71         }
72     }
73     
74     /**
75      * Allocate an identifier for a new graph.
76      *
77      */

78     public int graphIdAlloc ( String JavaDoc graphName ) {
79         DBIDInt result = null;
80         int dbid = 0;
81         try {
82             PreparedStatement JavaDoc ps = m_sql.getPreparedSQLStatement("insertGraph",GRAPH_TABLE);
83             ps.setString(1,graphName);
84             ps.executeUpdate();
85             dbid = getInsertID(GRAPH_TABLE);
86         } catch (SQLException JavaDoc e) {
87             throw new RDFRDBException("Failed to get last inserted ID: " + e);
88         }
89         return dbid;
90     }
91     
92     /**
93      * Dellocate an identifier for a graph.
94      *
95      */

96     public void graphIdDealloc ( int graphId ) {
97         DBIDInt result = null;
98         try {
99             PreparedStatement JavaDoc ps = m_sql.getPreparedSQLStatement("deleteGraph",GRAPH_TABLE);
100             ps.setInt(1,graphId);
101             ps.executeUpdate();
102         } catch (SQLException JavaDoc e) {
103             throw new RDFRDBException("Failed to delete graph ID: " + e);
104         }
105         return;
106     }
107
108     public int getInsertID ( String JavaDoc tableName ) {
109         DBIDInt result = null;
110         try {
111             PreparedStatement JavaDoc ps = m_sql.getPreparedSQLStatement("getInsertID");
112             ResultSet JavaDoc rs = ps.executeQuery();
113             if (rs.next()) {
114                 result = wrapDBID(rs.getObject(1));
115             } else
116                 throw new RDFRDBException("No last insert ID");
117         } catch (SQLException JavaDoc e) {
118             throw new RDFRDBException("Failed to get last inserted ID: " + e);
119         }
120         return result.getIntID();
121     }
122
123     
124     /**
125      * Return the parameters for table creation.
126      * 1) column type for subj, prop, obj.
127      * 2) table implementation type.
128      * 3) index key length for subj, pred, obj.
129      * 4) column type for head.
130      * 5) index key length for head.
131      * 6) table and index name prefix.
132      * @param param array to hold table creation parameters.
133      */

134     protected void getTblParams ( String JavaDoc [] param ) {
135         String JavaDoc objColType;
136         String JavaDoc tblImpl;
137         String JavaDoc spoKeyLen;
138         String JavaDoc headKeyLen;
139         String JavaDoc headColType;
140         
141         spoKeyLen = Integer.toString(LONG_OBJECT_LENGTH);
142         headKeyLen = Integer.toString(INDEX_KEY_LENGTH);
143
144         if ( INDEX_KEY_LENGTH > 250 )
145             throw new RDFRDBException("Key length specified (" + INDEX_KEY_LENGTH +
146                     ") exceeds MySQL maximum key length of 250.");
147         tblImpl = IS_XACT_DB ? "INNODB" : "MyISAM";
148         if ( IS_XACT_DB ) {
149             if ( LONG_OBJECT_LENGTH > 250 )
150                 throw new RDFRDBException("Long object length specified (" + LONG_OBJECT_LENGTH +
151                         ") exceeds MySQL maximum VARCHAR length of 250.");
152
153             objColType = "VARCHAR(" + LONG_OBJECT_LENGTH + ") BINARY";
154             STRINGS_TRIMMED = true;
155             EOS = ":";
156         } else {
157             objColType = LONG_OBJECT_LENGTH <= 250 ?
158                 "TINYBLOB" : "MEDIUMBLOB";
159             STRINGS_TRIMMED = false;
160             EOS = "";
161         }
162         if ( IS_XACT_DB ) {
163             if ( INDEX_KEY_LENGTH > 250 )
164                 throw new RDFRDBException("Index key length specified (" + INDEX_KEY_LENGTH +
165                         ") exceeds MySQL maximum VARCHAR length of 250.");
166
167             headColType = "VARCHAR(" + INDEX_KEY_LENGTH + ") BINARY";
168         } else {
169             headColType = INDEX_KEY_LENGTH <= 250 ?
170                 "TINYBLOB" : "MEDIUMBLOB";
171         }
172
173         param[0] = objColType;
174         param[1] = tblImpl;
175         param[2] = spoKeyLen;
176         param[3] = headColType;
177         param[4] = headKeyLen;
178         param[5] = TABLE_NAME_PREFIX;
179     }
180
181     
182     
183     /**
184      *
185      * Return the parameters for database initialization.
186      */

187     protected String JavaDoc[] getDbInitTablesParams() {
188         String JavaDoc [] res = new String JavaDoc[6];
189         
190         getTblParams (res);
191         if ( IS_XACT_DB ) {
192             STRINGS_TRIMMED = true;
193             EOS = ":";
194         } else {
195             STRINGS_TRIMMED = false;
196             EOS = "";
197         }
198         EOS_LEN = EOS.length();
199
200         return res;
201     }
202     
203     /**
204     *
205     * Return the parameters for table creation.
206     * Generate the table name by counting the number of existing
207     * tables for the graph. This is not reliable if another client
208     * is concurrently trying to create a table so, if failure, we
209     * make several attempts to create the table.
210     */

211
212     protected String JavaDoc[] getCreateTableParams( int graphId, boolean isReif ) {
213         String JavaDoc [] parms = new String JavaDoc[6];
214         String JavaDoc [] res = new String JavaDoc[4];
215                 
216         getTblParams (parms);
217         int tblCnt = getTableCount(graphId);
218         res[0] = genTableName(graphId,tblCnt,isReif);
219         res[1] = parms[0];
220         res[2] = parms[1];
221         res[3] = parms[2];
222         return res;
223     }
224     
225     public String JavaDoc genSQLStringMatchLHS_IC(String JavaDoc var) {
226         return "cast(" + var + " as char)";
227     }
228     
229 }
230
231 /*
232  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
233  * All rights reserved.
234  *
235  * Redistribution and use in source and binary forms, with or without
236  * modification, are permitted provided that the following conditions
237  * are met:
238  * 1. Redistributions of source code must retain the above copyright
239  * notice, this list of conditions and the following disclaimer.
240  * 2. Redistributions in binary form must reproduce the above copyright
241  * notice, this list of conditions and the following disclaimer in the
242  * documentation and/or other materials provided with the distribution.
243  * 3. The name of the author may not be used to endorse or promote products
244  * derived from this software without specific prior written permission.
245
246  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
247  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
249  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
250  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
251  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256  */

257
Popular Tags