KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > mappeddata > persist > db > hsqldb > HsqlDBMappingDatabaseSchema


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: HsqlDBMappingDatabaseSchema.java,v 1.8 2007/01/07 06:15:11 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.patterns.mappeddata.persist.db.hsqldb;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import org.opensubsystems.core.error.OSSException;
32 import org.opensubsystems.core.persist.db.hsqldb.HsqlDBDataUtils;
33 import org.opensubsystems.core.util.DatabaseUtils;
34 import org.opensubsystems.core.util.Log;
35 import org.opensubsystems.patterns.mappeddata.data.MappedData;
36 import org.opensubsystems.patterns.mappeddata.persist.db.MappingDatabaseSchema;
37
38 /**
39  * Database specific operations related to persistence of mapping tables
40  * for HsqlDB database.
41  *
42  * @version $Id: HsqlDBMappingDatabaseSchema.java,v 1.8 2007/01/07 06:15:11 bastafidli Exp $
43  * @author Julian Legeny
44  * @code.reviewer Miro Halas
45  * @code.reviewed 1.6 2006/08/14 22:42:34 jlegeny
46  */

47 public class HsqlDBMappingDatabaseSchema extends MappingDatabaseSchema
48 {
49    /*
50       Use autogenerated numbers for IDs using IDENTITY column.
51       Identity automatically defines primary key
52       Name all constraints to easily identify them later.
53       For all unique constraint we need to define unique indexes instead of
54       unique constrant otherwise we won't be able to indentify the violation of
55       this constraint by name.
56       
57       create table BF_MAP_TABLE
58       (
59          ID INTEGER IDENTITY,
60          ID1 INTEGER NOT NULL,
61          ID2 INTEGER NOT NULL,
62          MAPPING_TYPE INTEGER NOT NULL,
63          CUSTOM_DATA VARCHAR(4000) DEFAULT NULL,
64          CREATION_DATE TIMESTAMP NOT NULL,
65          MODIFICATION_DATE TIMESTAMP NOT NULL,
66          CONSTRAINT BF_MAP_TABLE_UQ UNIQUE (ID1, ID2, MAPPING_TYPE),
67          // Identity automatically defines primary key
68          // CONSTRAINT BF_MAP_TABLE_PK PRIMARY KEY (ID),
69          CONSTRAINT BF_MAP_TABLE_FK1 FOREIGN KEY (ID1)
70             REFERENCES BF_TABLE1 (COLUMN_ID1) ON DELETE CASCADE,
71          CONSTRAINT BF_MAP_TABLE_FK2 FOREIGN KEY (ID2)
72             REFERENCES BF_TABLE2 (COLUMN_ID2) ON DELETE CASCADE
73       )
74    */

75
76    // Cached values ////////////////////////////////////////////////////////////
77

78    /**
79     * Logger for this class
80     */

81    private static Logger JavaDoc s_logger = Log.getInstance(HsqlDBMappingDatabaseSchema.class);
82
83    // Constructors /////////////////////////////////////////////////////////////
84

85    /**
86     * Full constructor.
87     *
88     * @param strMapTableName - table name for mapping table
89     * @param schema1 - schema name the table 1 was defined in
90     * @param strTableName1 - name of the table 1
91     * @param strColumnName1 - name of the column 1
92     * @param schema2 - schema name the table 2 was defined in
93     * @param strTableName2 - name of the table 2
94     * @param strColumnName2 - name of the column 2
95     * @throws OSSException - an error has occured
96     */

97    public HsqlDBMappingDatabaseSchema(
98       String JavaDoc strMapTableName,
99       Class JavaDoc schema1,
100       String JavaDoc strTableName1,
101       String JavaDoc strColumnName1,
102       Class JavaDoc schema2,
103       String JavaDoc strTableName2,
104       String JavaDoc strColumnName2
105    ) throws OSSException
106    {
107       super(strMapTableName, schema1, strTableName1, strColumnName1,
108             schema2, strTableName2, strColumnName2);
109    }
110
111    // Public methods ///////////////////////////////////////////////////////////
112

113    /**
114     * {@inheritDoc}
115     */

116    public void create(
117       Connection JavaDoc cntDBConnection,
118       String JavaDoc strUserName
119    ) throws SQLException JavaDoc, OSSException
120    {
121       Statement JavaDoc stmQuery = null;
122       try
123       {
124          stmQuery = cntDBConnection.createStatement();
125
126          if (stmQuery.execute(constructSQL(
127                                 "ID INTEGER IDENTITY,", "INTEGER",
128                                 "VARCHAR(" + MAPPING_CUSTOM_DATA_MAXLENGTH + ")" + NL,
129                                 "TIMESTAMP", m_strConstraintBody, true, false)))
130          {
131             // Close any results
132
stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS);
133          }
134          s_logger.log(Level.FINEST, "Table " + getSchemaPrefix() +
135                                     m_strMapTableName + " created.");
136
137          if (stmQuery.execute("grant all on " + getSchemaPrefix() +
138                   m_strMapTableName + " to " + strUserName))
139          {
140             // Close any results
141
stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS);
142          }
143          s_logger.log(Level.FINEST, "Access for table " + getSchemaPrefix() +
144                                     m_strMapTableName + " set for user " + strUserName);
145       }
146       catch (SQLException JavaDoc sqleExc)
147       {
148          s_logger.log(Level.WARNING, "Failed to create schema " + MAPPING_SCHEMA_NAME, sqleExc);
149          throw sqleExc;
150       }
151       finally
152       {
153          DatabaseUtils.closeStatement(stmQuery);
154       }
155    }
156
157    /**
158     * {@inheritDoc}
159     */

160    public MappedData insertMappedData(
161       Connection JavaDoc dbConnection,
162       MappedData data
163    ) throws OSSException
164    {
165       PreparedStatement JavaDoc insertStatement = null;
166       
167       try
168       {
169          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
170          int iIndex = 1;
171       
172          buffer.append("insert into ");
173          buffer.append(getSchemaPrefix());
174          buffer.append(m_strMapTableName);
175          buffer.append(" (");
176          getColumns(false, MappedData.ALL_MAPPEDDATA_COLUMNS, null, null, buffer);
177          buffer.append(") values (null, ?, ?, ?, ?, now, now)");
178
179          insertStatement = dbConnection.prepareStatement(buffer.toString());
180          insertStatement.setInt(iIndex++, data.getMappedId1());
181          insertStatement.setInt(iIndex++, data.getMappedId2());
182          insertStatement.setInt(iIndex++, data.getMappingType());
183          insertStatement.setString(iIndex++, data.getCustomData());
184
185          HsqlDBDataUtils.insertAndFetchGeneratedValues(dbConnection,
186             insertStatement, isInDomain(), getSchemaPrefix() + m_strMapTableName, data);
187       }
188       catch (SQLException JavaDoc eExc)
189       {
190          handleInsertMappedDataException(eExc);
191       }
192       finally
193       {
194          DatabaseUtils.closeStatement(insertStatement);
195       }
196
197       return data;
198    }
199
200    /**
201     * {@inheritDoc}
202     */

203    public MappedData updateMappedData(
204       Connection JavaDoc dbConnection,
205       MappedData data
206    ) throws OSSException
207    {
208       PreparedStatement JavaDoc updateStatement = null;
209       
210       try
211       {
212          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
213          int iIndex = 1;
214          
215          buffer.append("update ");
216          buffer.append(getSchemaPrefix());
217          buffer.append(m_strMapTableName);
218          buffer.append(" set ID1 = ?, ID2 = ?, MAPPING_TYPE = ?, " +
219                        "CUSTOM_DATA = ?, MODIFICATION_DATE = now where ID = ? " +
220                        "and MODIFICATION_DATE = ?");
221
222          updateStatement = dbConnection.prepareStatement(buffer.toString());
223          updateStatement.setInt(iIndex++, data.getMappedId1());
224          updateStatement.setInt(iIndex++, data.getMappedId2());
225          updateStatement.setInt(iIndex++, data.getMappingType());
226          updateStatement.setString(iIndex++, data.getCustomData());
227          updateStatement.setInt(iIndex++, data.getId());
228          updateStatement.setTimestamp(iIndex++, data.getModificationTimestamp());
229
230          HsqlDBDataUtils.updatedAndFetchGeneratedValues("Mapped Data", dbConnection,
231             updateStatement, isInDomain(), getSchemaPrefix() + m_strMapTableName, data);
232       }
233       catch (SQLException JavaDoc eExc)
234       {
235          handleUpdateMappedDataException(eExc, dbConnection, data);
236       }
237       finally
238       {
239          DatabaseUtils.closeStatement(updateStatement);
240       }
241
242       return data;
243    }
244 }
245
Popular Tags