001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: *
006: */
007:
008: package com.hp.hpl.jena.db.impl;
009:
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013:
014: import com.hp.hpl.jena.db.*;
015:
016: /**
017: * @author hkuno based on code by Dave Reynolds
018: *
019: * Extends DriverRDB with MySQL-specific parameters.
020: */
021: public class Driver_MySQL extends DriverRDB {
022:
023: /** The name of the database type this driver supports */
024:
025: /**
026: * Constructor
027: */
028: public Driver_MySQL() {
029: super ();
030:
031: String myPackageName = this .getClass().getPackage().getName();
032:
033: DATABASE_TYPE = "MySQL";
034: DRIVER_NAME = "com.mysql.jdbc.Driver";
035:
036: ID_SQL_TYPE = "INTEGER";
037: URI_COMPRESS = false;
038: INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 100;
039: LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 100;
040: TABLE_NAME_LENGTH_MAX = 64;
041: IS_XACT_DB = true;
042: PRE_ALLOCATE_ID = false;
043: SKIP_DUPLICATE_CHECK = false;
044: SQL_FILE = "etc/mysql.sql";
045: DB_NAMES_TO_UPPER = false;
046: setTableNames(TABLE_NAME_PREFIX);
047: QUOTE_CHAR = '\'';
048:
049: m_psetClassName = myPackageName + ".PSet_TripleStore_RDB";
050: m_psetReifierClassName = myPackageName + ".PSet_ReifStore_RDB";
051:
052: m_lsetClassName = myPackageName
053: + ".SpecializedGraph_TripleStore_RDB";
054: m_lsetReifierClassName = myPackageName
055: + ".SpecializedGraphReifier_RDB";
056: }
057:
058: /**
059: * Set the database connection
060: */
061: public void setConnection(IDBConnection dbcon) {
062: m_dbcon = dbcon;
063:
064: try {
065: // Properties defaultSQL = SQLCache.loadSQLFile(DEFAULT_SQL_FILE, null, ID_SQL_TYPE);
066: // m_sql = new SQLCache(SQL_FILE, defaultSQL, dbcon, ID_SQL_TYPE);
067: m_sql = new SQLCache(SQL_FILE, null, dbcon, ID_SQL_TYPE);
068: } catch (Exception e) {
069: e.printStackTrace(System.err);
070: logger.error("Unable to set connection for Driver:", e);
071: }
072: }
073:
074: /**
075: * Allocate an identifier for a new graph.
076: *
077: */
078: public int graphIdAlloc(String graphName) {
079: DBIDInt result = null;
080: int dbid = 0;
081: try {
082: PreparedStatement ps = m_sql.getPreparedSQLStatement(
083: "insertGraph", GRAPH_TABLE);
084: ps.setString(1, graphName);
085: ps.executeUpdate();
086: dbid = getInsertID(GRAPH_TABLE);
087: } catch (SQLException e) {
088: throw new RDFRDBException(
089: "Failed to get last inserted ID: " + e);
090: }
091: return dbid;
092: }
093:
094: /**
095: * Dellocate an identifier for a graph.
096: *
097: */
098: public void graphIdDealloc(int graphId) {
099: DBIDInt result = null;
100: try {
101: PreparedStatement ps = m_sql.getPreparedSQLStatement(
102: "deleteGraph", GRAPH_TABLE);
103: ps.setInt(1, graphId);
104: ps.executeUpdate();
105: } catch (SQLException e) {
106: throw new RDFRDBException("Failed to delete graph ID: " + e);
107: }
108: return;
109: }
110:
111: public int getInsertID(String tableName) {
112: DBIDInt result = null;
113: PreparedStatement ps = null;
114: try {
115: ps = m_sql.getPreparedSQLStatement("getInsertID");
116: ResultSet rs = ps.executeQuery();
117: if (rs.next()) {
118: result = wrapDBID(rs.getObject(1));
119: } else
120: throw new RDFRDBException("No last insert ID");
121: } catch (SQLException e) {
122: throw new RDFRDBException(
123: "Failed to get last inserted ID: " + e);
124: } finally {
125: if (ps != null)
126: m_sql.returnPreparedSQLStatement(ps);
127: }
128: return result.getIntID();
129: }
130:
131: /**
132: * Return the parameters for table creation.
133: * 1) column type for subj, prop, obj.
134: * 2) table implementation type.
135: * 3) index key length for subj, pred, obj.
136: * 4) column type for head.
137: * 5) index key length for head.
138: * 6) table and index name prefix.
139: * @param param array to hold table creation parameters.
140: */
141: protected void getTblParams(String[] param) {
142: String objColType;
143: String tblImpl;
144: String spoKeyLen;
145: String headKeyLen;
146: String headColType;
147:
148: spoKeyLen = Integer.toString(LONG_OBJECT_LENGTH);
149: headKeyLen = Integer.toString(INDEX_KEY_LENGTH);
150:
151: if (INDEX_KEY_LENGTH > 250)
152: throw new RDFRDBException("Key length specified ("
153: + INDEX_KEY_LENGTH
154: + ") exceeds MySQL maximum key length of 250.");
155: tblImpl = IS_XACT_DB ? "INNODB" : "MyISAM";
156: if (IS_XACT_DB) {
157: if (LONG_OBJECT_LENGTH > 250)
158: throw new RDFRDBException(
159: "Long object length specified ("
160: + LONG_OBJECT_LENGTH
161: + ") exceeds MySQL maximum VARCHAR length of 250.");
162:
163: objColType = "VARCHAR(" + LONG_OBJECT_LENGTH + ") BINARY";
164: STRINGS_TRIMMED = true;
165: EOS = ":";
166: } else {
167: objColType = LONG_OBJECT_LENGTH <= 250 ? "TINYBLOB"
168: : "MEDIUMBLOB";
169: STRINGS_TRIMMED = false;
170: EOS = "";
171: }
172: if (IS_XACT_DB) {
173: if (INDEX_KEY_LENGTH > 250)
174: throw new RDFRDBException(
175: "Index key length specified ("
176: + INDEX_KEY_LENGTH
177: + ") exceeds MySQL maximum VARCHAR length of 250.");
178:
179: headColType = "VARCHAR(" + INDEX_KEY_LENGTH + ") BINARY";
180: } else {
181: headColType = INDEX_KEY_LENGTH <= 250 ? "TINYBLOB"
182: : "MEDIUMBLOB";
183: }
184:
185: param[0] = objColType;
186: param[1] = tblImpl;
187: param[2] = spoKeyLen;
188: param[3] = headColType;
189: param[4] = headKeyLen;
190: param[5] = TABLE_NAME_PREFIX;
191: }
192:
193: /**
194: *
195: * Return the parameters for database initialization.
196: */
197: protected String[] getDbInitTablesParams() {
198: String[] res = new String[6];
199:
200: getTblParams(res);
201: if (IS_XACT_DB) {
202: STRINGS_TRIMMED = true;
203: EOS = ":";
204: } else {
205: STRINGS_TRIMMED = false;
206: EOS = "";
207: }
208: EOS_LEN = EOS.length();
209:
210: return res;
211: }
212:
213: /**
214: *
215: * Return the parameters for table creation.
216: * Generate the table name by counting the number of existing
217: * tables for the graph. This is not reliable if another client
218: * is concurrently trying to create a table so, if failure, we
219: * make several attempts to create the table.
220: */
221:
222: protected String[] getCreateTableParams(int graphId, boolean isReif) {
223: String[] parms = new String[6];
224: String[] res = new String[4];
225:
226: getTblParams(parms);
227: int tblCnt = getTableCount(graphId);
228: res[0] = genTableName(graphId, tblCnt, isReif);
229: res[1] = parms[0];
230: res[2] = parms[1];
231: res[3] = parms[2];
232: return res;
233: }
234:
235: public String genSQLStringMatchLHS_IC(String var) {
236: return "cast(" + var + " as char)";
237: }
238:
239: }
240:
241: /*
242: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
243: * All rights reserved.
244: *
245: * Redistribution and use in source and binary forms, with or without
246: * modification, are permitted provided that the following conditions
247: * are met:
248: * 1. Redistributions of source code must retain the above copyright
249: * notice, this list of conditions and the following disclaimer.
250: * 2. Redistributions in binary form must reproduce the above copyright
251: * notice, this list of conditions and the following disclaimer in the
252: * documentation and/or other materials provided with the distribution.
253: * 3. The name of the author may not be used to endorse or promote products
254: * derived from this software without specific prior written permission.
255:
256: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
257: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
258: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
259: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
260: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
261: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
262: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
263: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266: */
|