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.SQLException;
012:
013: import com.hp.hpl.jena.db.IDBConnection;
014: import com.hp.hpl.jena.db.RDFRDBException;
015:
016: /**
017: * @author hkuno based on code by Dave Reynolds
018: *
019: * Extends DriverRDB with PostgreSQL-specific parameters.
020: */
021: public class Driver_PostgreSQL extends DriverRDB {
022:
023: /** The name of the database type this driver supports */
024:
025: /**
026: * Constructor
027: */
028: public Driver_PostgreSQL() {
029: super ();
030:
031: String myPackageName = this .getClass().getPackage().getName();
032:
033: DATABASE_TYPE = "PostgreSQL";
034: DRIVER_NAME = "org.postgresql.Driver";
035:
036: ID_SQL_TYPE = "INTEGER";
037: URI_COMPRESS = false;
038: INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 250;
039: LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 250;
040: TABLE_NAME_LENGTH_MAX = 63;
041: IS_XACT_DB = true;
042: PRE_ALLOCATE_ID = true;
043: SKIP_DUPLICATE_CHECK = false;
044: SQL_FILE = "etc/postgresql.sql";
045: QUOTE_CHAR = '\'';
046: DB_NAMES_TO_UPPER = false;
047: setTableNames(TABLE_NAME_PREFIX);
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: dbid = getInsertID(GRAPH_TABLE);
083: PreparedStatement ps = m_sql.getPreparedSQLStatement(
084: "insertGraph", GRAPH_TABLE);
085: ps.setInt(1, dbid);
086: ps.setString(2, graphName);
087: ps.executeUpdate();
088: } catch (SQLException e) {
089: throw new RDFRDBException(
090: "Failed to get last inserted ID: " + e);
091: }
092: return dbid;
093: }
094:
095: /**
096: * Dellocate an identifier for a graph.
097: *
098: */
099: public void graphIdDealloc(int graphId) {
100: DBIDInt result = null;
101: try {
102: PreparedStatement ps = m_sql.getPreparedSQLStatement(
103: "deleteGraph", GRAPH_TABLE);
104: ps.setInt(1, graphId);
105: ps.executeUpdate();
106: } catch (SQLException e) {
107: throw new RDFRDBException("Failed to delete graph ID: " + e);
108: }
109: return;
110: }
111:
112: //รณ Now common code moved to DriverRDB - delete this anytime after Jena 2.5.2
113: // public int getInsertID ( String tableName ) {
114: // DBIDInt result = null;
115: // try {
116: // PreparedStatement ps = m_sql.getPreparedSQLStatement("getInsertID",tableName);
117: // ResultSet rs = ps.executeQuery();
118: // if (rs.next()) {
119: // result = wrapDBID(rs.getObject(1));
120: // } else
121: // throw new RDFRDBException("No insert ID");
122: // } catch (SQLException e) {
123: // throw new RDFRDBException("Failed to insert ID: " + e);
124: // }
125: // return result.getIntID();
126: // }
127:
128: /**
129: * Return the parameters for table creation.
130: * 1) column type for subj, prop, obj.
131: * 2) column type for head.
132: * 3) table and index name prefix.
133: * @param param array to hold table creation parameters.
134: */
135: protected void getTblParams(String[] param) {
136: String spoColType;
137: String headColType;
138:
139: if (LONG_OBJECT_LENGTH > 4000)
140: throw new RDFRDBException("Long object length specified ("
141: + LONG_OBJECT_LENGTH
142: + ") exceeds maximum sane length of 4000.");
143: if (INDEX_KEY_LENGTH > 4000)
144: throw new RDFRDBException("Index key length specified ("
145: + INDEX_KEY_LENGTH
146: + ") exceeds maximum sane length of 4000.");
147:
148: spoColType = "VARCHAR(" + LONG_OBJECT_LENGTH + ")";
149: STRINGS_TRIMMED = false;
150: param[0] = spoColType;
151: headColType = "VARCHAR(" + INDEX_KEY_LENGTH + ")";
152: STRINGS_TRIMMED = false;
153: param[1] = headColType;
154: param[2] = TABLE_NAME_PREFIX;
155: }
156:
157: /**
158: *
159: * Return the parameters for database initialization.
160: */
161: protected String[] getDbInitTablesParams() {
162: String[] res = new String[3];
163:
164: getTblParams(res);
165: EOS_LEN = EOS.length();
166:
167: return res;
168: }
169:
170: /**
171: *
172: * Return the parameters for table creation.
173: * Generate the table name by counting the number of existing
174: * tables for the graph. This is not reliable if another client
175: * is concurrently trying to create a table so, if failure, we
176: * make several attempts to create the table.
177: */
178:
179: protected String[] getCreateTableParams(int graphId, boolean isReif) {
180: String[] parms = new String[3];
181: String[] res = new String[2];
182:
183: getTblParams(parms);
184: int tblCnt = getTableCount(graphId);
185: res[0] = genTableName(graphId, tblCnt, isReif);
186: res[1] = parms[0];
187: return res;
188: }
189:
190: public String genSQLStringMatchOp_IC(String fun) {
191: return "I" + genSQLLikeKW();
192: }
193:
194: }
195:
196: /*
197: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
198: * All rights reserved.
199: *
200: * Redistribution and use in source and binary forms, with or without
201: * modification, are permitted provided that the following conditions
202: * are met:
203: * 1. Redistributions of source code must retain the above copyright
204: * notice, this list of conditions and the following disclaimer.
205: * 2. Redistributions in binary form must reproduce the above copyright
206: * notice, this list of conditions and the following disclaimer in the
207: * documentation and/or other materials provided with the distribution.
208: * 3. The name of the author may not be used to endorse or promote products
209: * derived from this software without specific prior written permission.
210:
211: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
212: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
213: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
214: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
218: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
219: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
221: */
|