-
Notifications
You must be signed in to change notification settings - Fork 221
/
TestTablesService.java
214 lines (190 loc) · 9.46 KB
/
TestTablesService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.test;
import java.sql.Types;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.DatabaseNamesConstants;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.sql.Row;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.service.impl.AbstractService;
import org.junit.Ignore;
@Ignore
public class TestTablesService extends AbstractService {
public TestTablesService(ISymmetricEngine engine) {
super(engine.getParameterService(), engine.getSymmetricDialect());
setSqlMap(new TestTablesServiceSqlMap(platform, createSqlReplacementTokens()));
}
// TODO support insert of blob test for postgres and informix
public boolean insertIntoTestUseStreamLob(int id, String tableName, String lobValue) {
if (symmetricDialect.isBlobSyncSupported()) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
boolean updated = transaction.prepareAndExecute(String.format(
"insert into %s (test_id, test_blob) values(?, ?)", tableName),
new Object[] { id, lobValue.getBytes() }, new int[] { Types.INTEGER,
Types.BLOB }) > 0;
transaction.commit();
return updated;
} finally {
if (transaction != null) {
transaction.close();
}
}
} else {
return false;
}
}
// TODO support insert of blob test for postgres and informix
public boolean updateTestUseStreamLob(int id, String tableName, String lobValue) {
if (symmetricDialect.isBlobSyncSupported()) {
if (!DatabaseNamesConstants.INFORMIX.equals(platform.getName())) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
boolean updated = transaction.prepareAndExecute(
String.format("update %s set test_blob=? where test_id=?", tableName),
new Object[] { lobValue.getBytes(), id }, new int[] { Types.BLOB,
Types.INTEGER }) > 0;
transaction.commit();
return updated;
} finally {
if (transaction != null) {
transaction.close();
}
}
} else {
return false;
}
} else {
return false;
}
}
public void assertTestBlobIsInDatabase(int id, String tableName, String expected) {
if (symmetricDialect.isBlobSyncSupported()) {
int rowCount = sqlTemplate.queryForInt("select count(*) from "
+ tableName + " where test_id=?", id);
Assert.assertEquals("The " + id + " row for table " + tableName + " did not exist", 1, rowCount);
Map<String, Object> values = sqlTemplate.queryForMap("select test_blob from "
+ tableName + " where test_id=?", id);
Assert.assertEquals(
"The blob column for test_use_stream_lob was not loaded into the client database",
expected, values != null && values.get("TEST_BLOB") != null ? new String(
(byte[]) values.get("TEST_BLOB")) : null);
}
}
public void insertOrder(Order order) {
sqlTemplate.update(getSql("insertOrderSql"), order.getOrderId(), order.getCustomerId(),
order.getStatus(), order.getDeliverDate());
List<OrderDetail> details = order.getOrderDetails();
for (OrderDetail orderDetail : details) {
sqlTemplate.update(
getSql("insertOrderDetailSql"),
new Object[] { orderDetail.getOrderId(), orderDetail.getLineNumber(),
orderDetail.getItemType(), orderDetail.getItemId(),
orderDetail.getQuantity(),
(DatabaseNamesConstants.SQLITE.equals(platform.getName())?
orderDetail.getPrice().doubleValue():
orderDetail.getPrice())
}, new int[] {
Types.VARCHAR, Types.NUMERIC, Types.CHAR, Types.VARCHAR, Types.NUMERIC,
Types.NUMERIC });
}
}
public Order getOrder(String id) {
return sqlTemplate.queryForObject(getSql("selectOrderSql"), new ISqlRowMapper<Order>() {
public Order mapRow(Row rs) {
return new Order(rs.getString("order_id"), rs.getInt("customer_id"), rs
.getString("status"), rs.getDateTime("deliver_date"));
}
}, id);
}
public void insertCustomer(Customer customer) {
int blobType = symmetricDialect.getPlatform().getTableFromCache("test_customer", false)
.getColumn(11).getMappedTypeCode();
sqlTemplate.update(getSql("insertCustomerSql"),
new Object[] { customer.getCustomerId(), customer.getName(),
customer.isActive() ? "1" : "0", customer.getAddress(), customer.getCity(),
customer.getState(), customer.getZip(), customer.getEntryTimestamp(),
customer.getEntryTime(), customer.getNotes(), customer.getIcon() },
new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP,
Types.TIMESTAMP, Types.CLOB, blobType });
}
public int updateCustomer(int id, String column, Object value, int type) {
return sqlTemplate.update(
String.format("update test_customer set %s=? where customer_id=?", column),
new Object[] { value, id }, new int[] { type, Types.NUMERIC });
}
public Customer getCustomer(int id) {
return sqlTemplate.queryForObject("select * from test_customer where customer_id=?",
new ISqlRowMapper<Customer>() {
public Customer mapRow(Row rs) {
return new Customer(rs.getInt("customer_id"), rs.getString("name"), rs
.getBoolean("is_active"), rs.getString("address"), rs
.getString("city"), rs.getString("state"), rs.getInt("zip"), rs
.getDateTime("entry_timestamp"), rs.getDateTime("entry_time"), rs
.getString("notes"), rs.getBytes("icon"));
}
}, id);
}
public int count(String table) {
return sqlTemplate.queryForInt(String.format("select count(*) from %s", table));
}
public boolean doesCustomerExist(int id) {
return sqlTemplate
.queryForInt("select count(*) from test_customer where customer_id=?", id) > 0;
}
public String getCustomerNotes(int id) {
return sqlTemplate
.queryForString("select notes from test_customer where customer_id=?", id);
}
public byte[] getCustomerIcon(int id) {
return sqlTemplate.queryForObject("select icon from test_customer where customer_id=?",
new ISqlRowMapper<byte[]>() {
public byte[] mapRow(org.jumpmind.db.sql.Row rs) {
return rs.bytesValue();
}
}, id);
}
public void insertIntoTestTriggerTable(Object[] values) {
Table testTriggerTable = platform
.getTableFromCache(null, null, "test_triggers_table", true);
String quote = getSymmetricDialect().getPlatform().getDatabaseInfo().getDelimiterToken();
ISqlTransaction transaction = sqlTemplate.startSqlTransaction();
try {
transaction.allowInsertIntoAutoIncrementColumns(true, testTriggerTable, quote);
transaction.prepareAndExecute(getSql("insertIntoTestTriggersTableSql"), values);
transaction.commit();
} finally {
transaction.allowInsertIntoAutoIncrementColumns(false, testTriggerTable, quote);
transaction.close();
}
}
public int countTestTriggersTable() {
return sqlTemplate.queryForInt("select count(*) from test_triggers_table");
}
}