001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestDelete.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database.queries;
009:
010: import com.uwyn.rife.database.BeanImpl;
011: import com.uwyn.rife.database.Datasource;
012: import com.uwyn.rife.database.DbPreparedStatementHandler;
013: import com.uwyn.rife.database.DbQueryManager;
014: import com.uwyn.rife.database.exceptions.DatabaseException;
015:
016: public abstract class TestDelete extends TestQuery {
017: public TestDelete(String name) {
018: super (name);
019: }
020:
021: public DbQueryManager setupQuery(Datasource datasource) {
022: DbQueryManager manager = new DbQueryManager(datasource);
023:
024: CreateTable createtable = new CreateTable(datasource);
025: createtable.table("tablename").columns(BeanImpl.class)
026: .precision("propertyBigDecimal", 18, 9).precision(
027: "propertyChar", 1).precision("propertyDouble",
028: 12, 3).precision("propertyFloat", 13, 2)
029: .precision("propertyString", 255).precision(
030: "propertyStringbuffer", 100);
031:
032: try {
033: // prepare table and data
034: manager.executeUpdate(createtable);
035:
036: createtable.table("table2");
037: manager.executeUpdate(createtable);
038:
039: Insert insert = new Insert(datasource);
040: insert.into("tablename")
041: .fields(BeanImpl.getPopulatedBean());
042: manager.executeUpdate(insert);
043:
044: insert.into("table2");
045: manager.executeUpdate(insert);
046:
047: insert.clear();
048: insert.into("tablename").fields(BeanImpl.getNullBean());
049: manager.executeUpdate(insert);
050:
051: insert.into("table2");
052: manager.executeUpdate(insert);
053: } catch (DatabaseException e) {
054: cleanupQuery(manager);
055: throw new RuntimeException(e);
056: }
057:
058: return manager;
059: }
060:
061: private void cleanupQuery(DbQueryManager manager) {
062: // clean up nicely
063: DropTable drop_table = new DropTable(manager.getDatasource());
064: try {
065: drop_table.table("tablename");
066: manager.executeUpdate(drop_table);
067:
068: drop_table.clear();
069: drop_table.table("table2");
070: manager.executeUpdate(drop_table);
071: } catch (DatabaseException e) {
072: System.out.println(e.toString());
073: }
074: }
075:
076: public boolean execute(Delete query) {
077: boolean success = false;
078: DbQueryManager manager = setupQuery(query.getDatasource());
079:
080: try {
081: // try to execute insert statement
082: if (manager.executeUpdate(query) > 0) {
083: success = true;
084: }
085: } catch (DatabaseException e) {
086: throw new RuntimeException(e);
087: } finally {
088: cleanupQuery(manager);
089: }
090:
091: return success;
092: }
093:
094: public boolean execute(Delete query,
095: DbPreparedStatementHandler handler) {
096: boolean success = false;
097: DbQueryManager manager = setupQuery(query.getDatasource());
098:
099: try {
100: if (manager.executeUpdate(query, handler) > 0) {
101: success = true;
102: }
103: } catch (DatabaseException e) {
104: throw new RuntimeException(e);
105: } finally {
106: cleanupQuery(manager);
107: }
108:
109: return success;
110: }
111: }
|