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