KickJava   Java API By Example, From Geeks To Geeks.

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


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.IDBConnection;
15 import com.hp.hpl.jena.db.RDFRDBException;
16
17
18 /**
19  * @author hkuno based on code by Dave Reynolds
20  *
21  * Extends DriverRDB with PostgreSQL-specific parameters.
22  */

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

30     public Driver_PostgreSQL( ){
31         super();
32
33         String JavaDoc myPackageName = this.getClass().getPackage().getName();
34         
35         DATABASE_TYPE = "PostgreSQL";
36         DRIVER_NAME = "org.postgresql.Driver";
37         
38         ID_SQL_TYPE = "INTEGER";
39         URI_COMPRESS = false;
40         INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 250;
41         LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 250;
42         TABLE_NAME_LENGTH_MAX = 63;
43         IS_XACT_DB = true;
44         PRE_ALLOCATE_ID = true;
45         SKIP_DUPLICATE_CHECK = false;
46         SQL_FILE = "etc/postgresql.sql";
47         QUOTE_CHAR = '\'';
48         DB_NAMES_TO_UPPER = false;
49         setTableNames(TABLE_NAME_PREFIX);
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             dbid = getInsertID(GRAPH_TABLE);
83             PreparedStatement JavaDoc ps = m_sql.getPreparedSQLStatement("insertGraph",GRAPH_TABLE);
84             ps.setInt(1,dbid);
85             ps.setString(2,graphName);
86             ps.executeUpdate();
87         } catch (SQLException JavaDoc e) {
88             throw new RDFRDBException("Failed to get last inserted ID: " + e);
89         }
90         return dbid;
91     }
92     
93     /**
94      * Dellocate an identifier for a graph.
95      *
96      */

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

132     protected void getTblParams ( String JavaDoc [] param ) {
133         String JavaDoc spoColType;
134         String JavaDoc headColType;
135         
136         if ( LONG_OBJECT_LENGTH > 4000 )
137             throw new RDFRDBException("Long object length specified (" + LONG_OBJECT_LENGTH +
138                     ") exceeds maximum sane length of 4000.");
139         if ( INDEX_KEY_LENGTH > 4000 )
140             throw new RDFRDBException("Index key length specified (" + INDEX_KEY_LENGTH +
141                     ") exceeds maximum sane length of 4000.");
142
143         spoColType = "VARCHAR(" + LONG_OBJECT_LENGTH + ")";
144         STRINGS_TRIMMED = false;
145         param[0] = spoColType;
146         headColType = "VARCHAR(" + INDEX_KEY_LENGTH + ")";
147         STRINGS_TRIMMED = false;
148         param[1] = headColType;
149         param[2] = TABLE_NAME_PREFIX;
150     }
151
152     
153     
154     /**
155      *
156      * Return the parameters for database initialization.
157      */

158     protected String JavaDoc[] getDbInitTablesParams() {
159         String JavaDoc [] res = new String JavaDoc[3];
160         
161         getTblParams (res);
162         EOS_LEN = EOS.length();
163
164         return res;
165     }
166     /**
167     *
168     * Return the parameters for table creation.
169     * Generate the table name by counting the number of existing
170     * tables for the graph. This is not reliable if another client
171     * is concurrently trying to create a table so, if failure, we
172     * make several attempts to create the table.
173     */

174
175     protected String JavaDoc[] getCreateTableParams( int graphId, boolean isReif ) {
176         String JavaDoc [] parms = new String JavaDoc[3];
177         String JavaDoc [] res = new String JavaDoc[2];
178                 
179         getTblParams (parms);
180         int tblCnt = getTableCount(graphId);
181         res[0] = genTableName(graphId,tblCnt,isReif);
182         res[1] = parms[0];
183         return res;
184     }
185     
186     public String JavaDoc genSQLStringMatchOp_IC( String JavaDoc fun ) {
187         return "I" + genSQLLikeKW();
188     }
189
190 }
191
192 /*
193  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
194  * All rights reserved.
195  *
196  * Redistribution and use in source and binary forms, with or without
197  * modification, are permitted provided that the following conditions
198  * are met:
199  * 1. Redistributions of source code must retain the above copyright
200  * notice, this list of conditions and the following disclaimer.
201  * 2. Redistributions in binary form must reproduce the above copyright
202  * notice, this list of conditions and the following disclaimer in the
203  * documentation and/or other materials provided with the distribution.
204  * 3. The name of the author may not be used to endorse or promote products
205  * derived from this software without specific prior written permission.
206
207  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
208  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
210  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
213  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
214  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
215  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
216  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
217  */

218
Popular Tags