001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: TableManager.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.db;
025:
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import java.sql.Statement;
031:
032: import javax.naming.NamingException;
033: import javax.sql.DataSource;
034:
035: import org.ow2.easybeans.server.Embedded;
036: import org.ow2.easybeans.tests.common.helper.DBHelper;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039:
040: /**
041: * Manages a table in the database.
042: * @author Gisele Pinheiro Souza
043: * @author Eduardo Studzinski Estima de Castro
044: */
045: public class TableManager {
046:
047: /**
048: * The primary key inserted in the table.
049: */
050: protected static final int PRIMARY_KEY = 1;
051:
052: /**
053: * Logger.
054: */
055: private static Log logger = LogFactory.getLog(Embedded.class);
056:
057: /**
058: * Link to a datasource.
059: */
060: private DataSource ds = null;
061:
062: /**
063: * Creates a new instance of TableManager.
064: * @param dbName the database name in the registry.
065: * @throws NamingException if a lookup error occurs.
066: */
067: public TableManager(final String dbName) throws NamingException {
068: this (DBHelper.getDataSource(dbName));
069: }
070:
071: /**
072: * Creates a new instance of TableManager.
073: * @param ds the datasource.
074: */
075: public TableManager(final DataSource ds) {
076: this .ds = ds;
077: if (ds == null) {
078: throw new IllegalArgumentException("DataSource is null");
079: }
080: }
081:
082: /**
083: * Creates the table in the database done without make the commit after the
084: * insertion.
085: * @param tableName the table name.
086: * @throws SQLException if a database error occurs.
087: */
088: public void insertTable(final String tableName) throws SQLException {
089: Connection connection = null;
090: try {
091: logger.debug("Before insert table.");
092: connection = ds.getConnection();
093: logger.debug("Connection opened.");
094:
095: PreparedStatement stmUpdate = null;
096: try {
097: stmUpdate = connection.prepareStatement("CREATE TABLE "
098: + tableName
099: + " (CodeTest integer, NameTest varchar(30), "
100: + "PRIMARY KEY (CodeTest))");
101: stmUpdate.executeUpdate();
102: } finally {
103: if (stmUpdate != null) {
104: stmUpdate.close();
105: }
106: }
107: logger.debug("Table created.");
108:
109: //creates a line in the table
110: PreparedStatement stmUpdateField = null;
111: try {
112: stmUpdateField = connection
113: .prepareStatement("INSERT INTO " + tableName
114: + " (CodeTest) VALUES (" + PRIMARY_KEY
115: + ")");
116: stmUpdateField.executeUpdate();
117: } finally {
118: if (stmUpdateField != null) {
119: stmUpdateField.close();
120: }
121: }
122: } finally {
123: if (connection != null) {
124: connection.close();
125: logger.debug("Connection closed.");
126: }
127: }
128: }
129:
130: /**
131: * Deletes the table test. This methods does not make the commit after
132: * delete.
133: * @param tableName the table name.
134: * @throws SQLException if a database error occurs.
135: */
136: public void deleteTable(final String tableName) throws SQLException {
137: Connection connection = null;
138: try {
139: connection = ds.getConnection();
140: // deletes the table
141: PreparedStatement stmUpdate = null;
142: try {
143: stmUpdate = connection.prepareStatement("DROP TABLE "
144: + tableName + " CASCADE");
145: stmUpdate.executeUpdate();
146: } finally {
147: if (stmUpdate != null) {
148: stmUpdate.close();
149: }
150: }
151: } finally {
152: if (connection != null) {
153: connection.close();
154: }
155: }
156: }
157:
158: /**
159: * Verifies if the table was created.
160: * @param tableName the table name.
161: * @throws SQLException if a database error occurs or the table not exists.
162: */
163: public void verifyTable(final String tableName) throws SQLException {
164: Connection connection = null;
165: try {
166: connection = ds.getConnection();
167: Statement stmt = null;
168: try {
169: stmt = connection.createStatement();
170: ResultSet rs = stmt.executeQuery("SELECT * FROM "
171: + tableName + " WHERE CodeTest = "
172: + PRIMARY_KEY);
173: if (!rs.next()) {
174: throw new SQLException(
175: "There are not values in the table");
176: }
177: } finally {
178: if (stmt != null) {
179: stmt.close();
180: }
181: }
182: } finally {
183: if (connection != null) {
184: connection.close();
185: }
186: }
187: }
188:
189: /**
190: * Creates and deletes a table.
191: * @param tableName the table name.
192: * @throws SQLException if a database error occurs or the table not exists.
193: */
194: public void test(final String tableName) throws SQLException {
195: insertTable(tableName);
196: deleteTable(tableName);
197: }
198: }
|