root/trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdb.java @ 308

Revision 308, 5.9 KB (checked in by martijn, 7 months ago)

Big project Reogranization:
- all modules are named with their main package name,

e.g. "picr" becomes org.bridgedb.webservice.picr,
and the jar will be org.bridgedb.webservice.picr.jar

- webservice renamed to org.bridgedb.server
- corelib is split in org.bridgedb, org.bridgedb.rdb,

org.bridgedb.webservice.biomart and org.bridgedb.webservice.bridgerest

  • Property svn:eol-style set to native
Line 
1// BridgeDb,
2// An abstraction layer for identifer mapping services, both local and online.
3// Copyright 2006-2009 BridgeDb developers
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.bridgedb.rdb;
18
19import java.sql.Connection;
20import java.sql.PreparedStatement;
21import java.sql.ResultSet;
22import java.sql.SQLException;
23
24import org.bridgedb.DataSource;
25import org.bridgedb.IDMapperException;
26
27/**
28 * SimpleGdb is the main implementation of the Gdb interface,
29 * for dealing with single SQL-based pgdb's.
30 * It's responsible for creating and querying a single
31 * pgdb relational database through the JDBC interface.
32 * <p>
33 * It wraps SQL statements in methods,
34 * so the rest of the apps don't need to know the
35 * details of the Database schema.
36 * <p>
37 * It delegates dealing with the differences between
38 * various RDBMS's (Derby, Hsqldb etc.)
39 * to a DBConnector instance.
40 * A correct DBConnector instance needs to be
41 * passed to the constructor of SimpleGdb.
42 * <p>
43 * In the PathVisio GUI environment, use GdbManager
44 * to create and connect one or two centralized Gdb's.
45 * This will also automatically
46 * find the right DBConnector from the preferences.
47 * <p>
48 * In a head-less or test environment, you can bypass GdbManager
49 * and use SimpleGdb directly
50 * to create or connect to one or more pgdb's of any type.
51 */
52public abstract class SimpleGdb extends IDMapperRdb implements GdbConstruct
53{
54        /**
55         * Create IDMapper based on an existing SQL connection.
56         * @param con Existing SQL Connection.
57         */
58        SimpleGdb(Connection con)
59        {
60                this.con = con;
61        }
62       
63        /**
64         * helper class that handles lazy initialization of all prepared statements used by SimpleGdb
65         * Non-static, because it needs the con database connection field.
66         */
67        protected final class LazyPst
68        {
69                /**
70                 * Initialize with given SQL string, but don't create PreparedStatement yet.
71                 * Valid to call before database connection is created.
72                 * @param aSql SQL query
73                 */
74                public LazyPst(String aSql)
75                {
76                        sql = aSql;
77                }
78               
79                private PreparedStatement pst = null;
80                private final String sql;
81               
82                /**
83                 * Get a PreparedStatement using lazy initialization.
84                 * <p>
85                 * Assumes SimpleGdbImpl2.con is already valid
86                 * @return a prepared statement for the given query.
87                 * @throws SQLException when a PreparedStatement could not be created
88                 */
89                public PreparedStatement getPreparedStatement() throws SQLException
90                {
91                        if (pst == null)
92                        {
93                                pst = con.prepareStatement(sql);
94                        }
95                        return pst;
96                }
97        }
98
99        /**
100         * The {@link Connection} to the Gene Database.
101         */
102        protected Connection con = null;
103        // dbConnector, helper class for dealing with RDBMS specifcs.
104        private DBConnector dbConnector;
105
106        /** {@inheritDoc} */
107        final public boolean isConnected() { return con != null; }
108
109        protected String dbName;
110       
111        /** {@inheritDoc} */
112        @Override final public String getDbName() { return dbName; }
113       
114        /** {@inheritDoc} */
115        final public void close() throws IDMapperException
116        {
117                if (con == null) throw new IDMapperException("Database connection already closed");
118                if (dbConnector != null) dbConnector.closeConnection(con);
119                try
120                {
121                        con.close();
122                }
123                catch (SQLException ex)
124                {
125                        throw new IDMapperException (ex);
126                }
127                con = null;
128        }
129
130        /**
131         * Excecutes several SQL statements to create the tables and indexes in the database the given
132         * connection is connected to
133         * Note: Official GDB's are created by Alex Pico's script, not with this code.
134         * This is just here for testing purposes.
135         */
136        abstract public void createGdbTables();
137       
138        public static final int NO_LIMIT = 0;
139        public static final int NO_TIMEOUT = 0;
140        public static final int QUERY_TIMEOUT = 5; //seconds
141
142        /**
143           prepare for inserting genes and/or links.
144           @throws IDMapperException on failure
145         */
146        abstract public void preInsert() throws IDMapperException;
147
148        /**
149           commit inserted data.
150           @throws IDMapperException on failure
151         */
152        final public void commit() throws IDMapperException
153        {
154                try
155                {
156                        con.commit();
157                }
158                catch (SQLException e)
159                {
160                        throw new IDMapperException (e);
161                }
162        }
163
164        /**
165           @return number of rows in gene table.
166           @throws IDMapperException on failure
167         */
168        final public int getGeneCount() throws IDMapperException
169        {
170                int result = 0;
171                try
172                {
173                        ResultSet r = con.createStatement().executeQuery("SELECT COUNT(*) FROM " + "datanode");
174                        r.next();
175                        result = r.getInt (1);
176                        r.close();
177                }
178                catch (SQLException e)
179                {
180                        throw new IDMapperException (e);
181                }
182                return result;
183        }
184
185        /**
186         * @param ds DataSource to count identifiers for.
187           @return number of identifiers table for the given datasource
188           @throws IDMapperException on failure
189         */
190        final public int getGeneCount(DataSource ds) throws IDMapperException
191        {
192                int result = 0;
193                try
194                {
195                        ResultSet r = con.createStatement().executeQuery(
196                                        "SELECT COUNT(*) FROM datanode WHERE code = '" + ds.getSystemCode() + "'");
197                        r.next();
198                        result = r.getInt (1);
199                        r.close();
200                }
201                catch (SQLException e)
202                {
203                        throw new IDMapperException (e);
204                }
205                return result;
206        }
207       
208        /**
209           compact the database.
210           @throws IDMapperException on failure
211         */
212        final public void compact() throws IDMapperException
213        {
214                dbConnector.compact(con);
215        }
216       
217        /**
218           finalize the database.
219           @throws IDMapperException on failure
220         */
221        final public void finalize() throws IDMapperException
222        {
223                dbConnector.compact(con);
224                createGdbIndices();
225                dbConnector.closeConnection(con, DBConnector.PROP_FINALIZE);
226                String newDb = dbConnector.finalizeNewDatabase(dbName);
227                dbName = newDb;
228        }
229
230       
231}
Note: See TracBrowser for help on using the browser.