001: /*
002: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import java.sql.Connection;
010: import java.sql.PreparedStatement;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013:
014: import com.hp.hpl.jena.db.IDBConnection;
015: import com.hp.hpl.jena.db.RDFRDBException;
016:
017: public class Driver_HSQL extends DriverRDB {
018: public Driver_HSQL() {
019: super ();
020:
021: String myPackageName = this .getClass().getPackage().getName();
022:
023: // Important!
024: DB_NAMES_TO_UPPER = true;
025:
026: DATABASE_TYPE = "HSQL";
027: DRIVER_NAME = "org.hsqldb.jdbcDriver";
028:
029: ID_SQL_TYPE = "INTEGER";
030: URI_COMPRESS = false;
031: INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 250;
032: LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 250;
033: TABLE_NAME_LENGTH_MAX = 63;
034: IS_XACT_DB = true;
035: PRE_ALLOCATE_ID = false; // DB has auto increment fields - don't preallocate
036: SKIP_DUPLICATE_CHECK = false;
037: SQL_FILE = "etc/hsql.sql";
038: QUOTE_CHAR = '\'';
039: setTableNames(TABLE_NAME_PREFIX);
040:
041: m_psetClassName = myPackageName + ".PSet_TripleStore_RDB";
042: m_psetReifierClassName = myPackageName + ".PSet_ReifStore_RDB";
043:
044: m_lsetClassName = myPackageName
045: + ".SpecializedGraph_TripleStore_RDB";
046: m_lsetReifierClassName = myPackageName
047: + ".SpecializedGraphReifier_RDB";
048: }
049:
050: public void close() {
051: // This cause problems with in-memory and on disk because there can be multiple models open.
052: //shutdown() ;
053: }
054:
055: public void shutdown() {
056: try {
057: Connection c = getConnection().getConnection();
058: Statement s = c.createStatement();
059: s.execute("SHUTDOWN COMPACT");
060: } catch (SQLException ex) {
061: System.err.println("HSQL shutdown exception");
062: ex.printStackTrace(System.err);
063: }
064: }
065:
066: String[] getDbInitTablesParams() {
067: String[] res = new String[3];
068:
069: getTblParams(res);
070: EOS_LEN = EOS.length();
071:
072: return res;
073: }
074:
075: /**
076: * Return the parameters for table creation.
077: * 1) column type for subj, prop, obj.
078: * 2) column type for head.
079: * 3) table and index name prefix.
080: * @param param array to hold table creation parameters.
081: */
082: protected void getTblParams(String[] param) {
083: String spoColType;
084: String headColType;
085:
086: if (LONG_OBJECT_LENGTH > 4000)
087: throw new RDFRDBException("Long object length specified ("
088: + LONG_OBJECT_LENGTH
089: + ") exceeds maximum sane length of 4000.");
090: if (INDEX_KEY_LENGTH > 4000)
091: throw new RDFRDBException("Index key length specified ("
092: + INDEX_KEY_LENGTH
093: + ") exceeds maximum sane length of 4000.");
094:
095: spoColType = "VARCHAR(" + LONG_OBJECT_LENGTH + ")";
096: headColType = "VARCHAR(" + INDEX_KEY_LENGTH + ")";
097: STRINGS_TRIMMED = false;
098:
099: param[0] = spoColType;
100: param[1] = headColType;
101: param[2] = TABLE_NAME_PREFIX;
102: }
103:
104: protected String[] getCreateTableParams(int graphId, boolean isReif) {
105: String[] parms = new String[3];
106: String[] res = new String[2];
107:
108: getTblParams(parms);
109: int tblCnt = getTableCount(graphId);
110: res[0] = genTableName(graphId, tblCnt, isReif);
111: res[1] = parms[0];
112: return res;
113: }
114:
115: public int graphIdAlloc(String graphName) {
116: DBIDInt result = null;
117: int dbid = 0;
118: try {
119: //dbid = getInsertID(GRAPH_TABLE);
120: PreparedStatement ps = m_sql.getPreparedSQLStatement(
121: "insertGraph", GRAPH_TABLE);
122: // Autoincr field
123: //ps.setInt(1,dbid);
124: //ps.setString(2,graphName);
125: ps.setString(1, graphName);
126: ps.executeUpdate();
127: dbid = getInsertID(GRAPH_TABLE);
128: } catch (SQLException e) {
129: throw new RDFRDBException(
130: "Failed to get last inserted ID: " + e);
131: }
132: return dbid;
133: }
134:
135: public void graphIdDealloc(int graphId) {
136: DBIDInt result = null;
137: try {
138: PreparedStatement ps = m_sql.getPreparedSQLStatement(
139: "deleteGraph", GRAPH_TABLE);
140: ps.setInt(1, graphId);
141: ps.executeUpdate();
142: } catch (SQLException e) {
143: throw new RDFRDBException("Failed to delete graph ID: " + e);
144: }
145: return;
146: }
147:
148: // Now common code moved to DriverRDB - delete this anytime after Jena 2.5.2
149: // public int getInsertID(String tableName)
150: // {
151: // DBIDInt result = null;
152: // try {
153: // PreparedStatement ps = m_sql.getPreparedSQLStatement("getInsertID",tableName);
154: // ResultSet rs = ps.executeQuery();
155: // if (rs.next()) {
156: // result = wrapDBID(rs.getObject(1));
157: // } else
158: // throw new RDFRDBException("No insert ID");
159: // } catch (SQLException e) {
160: // throw new RDFRDBException("Failed to insert ID: " + e);
161: // }
162: // return result.getIntID();
163: // }
164:
165: public void setConnection(IDBConnection dbcon) {
166: m_dbcon = dbcon;
167:
168: try {
169: // Properties defaultSQL = SQLCache.loadSQLFile(DEFAULT_SQL_FILE, null, ID_SQL_TYPE);
170: // m_sql = new SQLCache(SQL_FILE, defaultSQL, dbcon, ID_SQL_TYPE);
171: m_sql = new SQLCache(SQL_FILE, null, dbcon, ID_SQL_TYPE);
172: } catch (Exception e) {
173: e.printStackTrace(System.err);
174: logger.error("Unable to set connection for Driver:", e);
175: }
176: }
177:
178: }
179:
180: /*
181: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
182: * All rights reserved.
183: *
184: * Redistribution and use in source and binary forms, with or without
185: * modification, are permitted provided that the following conditions
186: * are met:
187: * 1. Redistributions of source code must retain the above copyright
188: * notice, this list of conditions and the following disclaimer.
189: * 2. Redistributions in binary form must reproduce the above copyright
190: * notice, this list of conditions and the following disclaimer in the
191: * documentation and/or other materials provided with the distribution.
192: * 3. The name of the author may not be used to endorse or promote products
193: * derived from this software without specific prior written permission.
194: *
195: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
203: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
204: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205: */
|