1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
30
31
32
33
34
35
36
37
38 public abstract class AbstractDbDataWithCommit extends AbstractDbData {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public AbstractDbDataWithCommit(DbSession dbSession) {
56 super(dbSession);
57 initObject();
58 }
59
60
61
62
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
91
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
121
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 }