View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.database.data;
22  
23  import goldengate.common.database.DbPreparedStatement;
24  import goldengate.common.database.DbSession;
25  import goldengate.common.database.exception.GoldenGateDatabaseException;
26  import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
27  
28  /**
29   * Abstract database table implementation with explicit COMMIT.<br><br>
30   * 
31   * If the connection is in autocommit, this abstract should not be used.<br>
32   * If the connection is not in autocommit, one could use this implementation to implicitly 
33   * commit when needed automatically as should do an autocommit connection.
34   *
35   * @author Frederic Bregier
36   *
37   */
38  public abstract class AbstractDbDataWithCommit extends AbstractDbData {
39      /**
40       *  To be implemented
41       */
42      //public static String table;
43      //public static final int NBPRKEY;
44      //protected static String selectAllFields;
45      //protected static String updateAllFields;
46      //protected static String insertAllValues;
47      //protected DbValue[] primaryKey;
48      //protected DbValue[] otherFields;
49      //protected DbValue[] allFields;
50      
51      /**
52       * Abstract constructor to set the DbSession to use
53       * @param dbSession
54       */
55      public AbstractDbDataWithCommit(DbSession dbSession) {
56          super(dbSession);
57          initObject();
58      }
59     
60      /**
61       * Insert object into table
62       * @throws GoldenGateDatabaseException
63       */
64      public void insert() throws GoldenGateDatabaseException {
65          if (isSaved) {
66              return;
67          }
68          if (dbSession == null) {
69              isSaved = true;
70              return;
71          }
72          setToArray();
73          DbPreparedStatement preparedStatement = new DbPreparedStatement(
74                  dbSession);
75          try {
76              preparedStatement.createPrepareStatement("INSERT INTO " + getTable() +
77                      " (" + getSelectAllFields() + ") VALUES " + getInsertAllValues());
78              setValues(preparedStatement, allFields);
79              int count = preparedStatement.executeUpdate();
80              if (count <= 0) {
81                  throw new GoldenGateDatabaseNoDataException("No row found");
82              }
83              dbSession.commit();
84              isSaved = true;
85          } finally {
86              preparedStatement.realClose();
87          }
88      }
89      /**
90       * Update object to table
91       * @throws GoldenGateDatabaseException
92       */
93      public void update() throws GoldenGateDatabaseException {
94          if (isSaved) {
95              return;
96          }
97          if (dbSession == null) {
98              isSaved = true;
99              return;
100         }
101         setToArray();
102         DbPreparedStatement preparedStatement = new DbPreparedStatement(
103                 dbSession);
104         try {
105             preparedStatement.createPrepareStatement("UPDATE " + getTable() +
106                     " SET " + getUpdateAllFields() + " WHERE " +
107                     getWherePrimaryKey());
108             setValues(preparedStatement, allFields);
109             int count = preparedStatement.executeUpdate();
110             if (count <= 0) {
111                 throw new GoldenGateDatabaseNoDataException("No row found");
112             }
113             dbSession.commit();
114             isSaved = true;
115         } finally {
116             preparedStatement.realClose();
117         }
118     }
119     /**
120      * Delete object from table
121      * @throws GoldenGateDatabaseException
122      */
123     public void delete() throws GoldenGateDatabaseException {
124         if (dbSession == null) {
125             return;
126         }
127         DbPreparedStatement preparedStatement = new DbPreparedStatement(
128                 dbSession);
129         try {
130             preparedStatement.createPrepareStatement("DELETE FROM " + getTable() +
131                     " WHERE " + getWherePrimaryKey());
132             setPrimaryKey();
133             setValues(preparedStatement, primaryKey);
134             int count = preparedStatement.executeUpdate();
135             if (count <= 0) {
136                 throw new GoldenGateDatabaseNoDataException("No row found");
137             }
138             dbSession.commit();
139             isSaved = false;
140         } finally {
141             preparedStatement.realClose();
142         }
143     }
144 }