KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > DeleteRequest


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DeleteRequest.java,v 1.6 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import com.triactive.jdo.StateManager;
15 import java.sql.Connection JavaDoc;
16 import java.sql.PreparedStatement JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import org.apache.log4j.Category;
19
20
21 class DeleteRequest extends RequestUsingFields
22 {
23     private static final Category LOG = Category.getInstance(DeleteRequest.class);
24
25     private final String JavaDoc textStmt;
26
27     public DeleteRequest(ClassBaseTable table)
28     {
29         super(table);
30
31         textStmt = "DELETE FROM " + table.getName() + " WHERE " + idMapping.getColumn().getName() + " = ?";
32     }
33
34     public void execute(StateManager sm)
35     {
36         if (cpxFields != null)
37         {
38             for (int i = 0; i < cpxFields.length; ++i)
39                 cpxFieldMappings[cpxFields[i]].deleteObject(sm);
40         }
41
42         PersistenceManager pm = sm.getPersistenceManager();
43
44         try
45         {
46             Connection JavaDoc conn = pm.getConnection(true);
47
48             try
49             {
50                 PreparedStatement JavaDoc ps = conn.prepareStatement(textStmt);
51
52                 try
53                 {
54                     idMapping.setObject(pm, ps, 1, sm.getObjectId());
55
56                     long startTime = System.currentTimeMillis();
57
58                     ps.executeUpdate();
59
60                     if (LOG.isDebugEnabled())
61                         LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + textStmt);
62                 }
63                 finally
64                 {
65                     ps.close();
66                 }
67             }
68             finally
69             {
70                 pm.releaseConnection(conn);
71             }
72         }
73         catch (SQLException JavaDoc e)
74         {
75             throw pm.getStoreManager().getDatabaseAdapter().newDataStoreException("Delete request failed: " + textStmt, e);
76         }
77     }
78 }
79
Popular Tags