Revision 30601
Added by Alessia Bardi about 10 years ago
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/cnr-enabling-database-service/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "cnr-enabling-database-service"} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/utils/DatabaseUtilsTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database.utils; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertNotNull; |
|
5 |
import static org.junit.Assert.assertTrue; |
|
6 |
import static org.mockito.Matchers.any; |
|
7 |
import static org.mockito.Matchers.anyObject; |
|
8 |
import static org.mockito.Matchers.anyString; |
|
9 |
import static org.mockito.Mockito.atLeastOnce; |
|
10 |
import static org.mockito.Mockito.never; |
|
11 |
import static org.mockito.Mockito.times; |
|
12 |
import static org.mockito.Mockito.verify; |
|
13 |
import static org.mockito.Mockito.when; |
|
14 |
|
|
15 |
import java.io.StringReader; |
|
16 |
import java.sql.Date; |
|
17 |
import java.util.Arrays; |
|
18 |
import java.util.HashMap; |
|
19 |
import java.util.List; |
|
20 |
import java.util.Map; |
|
21 |
|
|
22 |
import javax.sql.DataSource; |
|
23 |
|
|
24 |
import org.apache.velocity.app.VelocityEngine; |
|
25 |
import org.dom4j.Document; |
|
26 |
import org.dom4j.DocumentHelper; |
|
27 |
import org.dom4j.Element; |
|
28 |
import org.dom4j.io.SAXReader; |
|
29 |
import org.junit.Before; |
|
30 |
import org.junit.Ignore; |
|
31 |
import org.junit.Test; |
|
32 |
import org.junit.runner.RunWith; |
|
33 |
import org.mockito.Mock; |
|
34 |
import org.mockito.runners.MockitoJUnitRunner; |
|
35 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
36 |
import org.springframework.transaction.support.TransactionCallback; |
|
37 |
import org.springframework.transaction.support.TransactionTemplate; |
|
38 |
|
|
39 |
import com.google.common.collect.Lists; |
|
40 |
import com.google.common.collect.Maps; |
|
41 |
|
|
42 |
import eu.dnetlib.enabling.database.DataSourceFactory; |
|
43 |
import eu.dnetlib.enabling.database.TransactionTemplateFactory; |
|
44 |
import eu.dnetlib.enabling.database.objects.DnetDatabase; |
|
45 |
|
|
46 |
@RunWith(MockitoJUnitRunner.class) |
|
47 |
public class DatabaseUtilsTest { |
|
48 |
|
|
49 |
private static final String DB = "THE_DB"; |
|
50 |
private static final String TABLE = "THE_TABLE"; |
|
51 |
private static final String QUERY = "SELECT age FROM persons"; |
|
52 |
private static final String RESOURCE_ID = "id1234"; |
|
53 |
private static final String DB_PREFIX = "THE_"; |
|
54 |
public static final String DNET_RESOURCE_ID_FIELD = "_dnet_resource_identifier_"; |
|
55 |
private static final String DEFAULT_DB = "postgres"; |
|
56 |
|
|
57 |
// Class Under Test |
|
58 |
private DatabaseUtils dbUtils; |
|
59 |
|
|
60 |
private final List<Map<String, Object>> RESULTS = Lists.newArrayList(); |
|
61 |
|
|
62 |
private Map<String, Object> MAP; |
|
63 |
|
|
64 |
private final DnetDatabase DNET_DB = new DnetDatabase(); |
|
65 |
|
|
66 |
@Mock |
|
67 |
private JdbcTemplateFactory jdbcTemplateFactory; |
|
68 |
@Mock |
|
69 |
private JdbcTemplate jdbcTemplate; |
|
70 |
@Mock |
|
71 |
private DataSource dataSource; |
|
72 |
@Mock |
|
73 |
private VelocityEngine velocityEngine; |
|
74 |
@Mock |
|
75 |
private DataSourceFactory dataSourceFactory; |
|
76 |
@Mock |
|
77 |
private TransactionTemplateFactory transactionTemplateFactory; |
|
78 |
@Mock |
|
79 |
private TransactionTemplate transactionTemplate; |
|
80 |
|
|
81 |
@SuppressWarnings("unchecked") |
|
82 |
@Before |
|
83 |
public void setUp() throws Exception { |
|
84 |
dbUtils = new DatabaseUtils(); |
|
85 |
|
|
86 |
DNET_DB.setDbName(DB); |
|
87 |
|
|
88 |
MAP = new HashMap<String, Object>(); |
|
89 |
MAP.put("k1", "v1"); |
|
90 |
MAP.put("k2", "v2"); |
|
91 |
MAP.put("k3", "v3"); |
|
92 |
|
|
93 |
dbUtils.setJdbcTemplateFactory(jdbcTemplateFactory); |
|
94 |
dbUtils.setDataSourceFactory(dataSourceFactory); |
|
95 |
dbUtils.setTransactionTemplateFactory(transactionTemplateFactory); |
|
96 |
dbUtils.setVelocityEngine(velocityEngine); |
|
97 |
dbUtils.setDbPrefix(DB_PREFIX); |
|
98 |
dbUtils.setNumbersOfRecordsForTransaction(20); |
|
99 |
|
|
100 |
when(jdbcTemplateFactory.createJdbcTemplate(dataSource)).thenReturn(jdbcTemplate); |
|
101 |
when(jdbcTemplateFactory.createJdbcTemplate(DB)).thenReturn(jdbcTemplate); |
|
102 |
|
|
103 |
when(jdbcTemplate.queryForList(QUERY, Integer.class)).thenReturn(Arrays.asList(1, 2, 3, 4)); |
|
104 |
|
|
105 |
for (int i = 0; i < 4; i++) { |
|
106 |
RESULTS.add(MAP); |
|
107 |
} |
|
108 |
|
|
109 |
when(jdbcTemplate.queryForList(QUERY)).thenReturn(RESULTS); |
|
110 |
when(jdbcTemplate.queryForList("SELECT * FROM " + TABLE)).thenReturn(Arrays.asList(MAP, MAP, MAP)); |
|
111 |
|
|
112 |
when(jdbcTemplate.queryForList("SELECT * FROM information_schema.columns WHERE table_name = ?", new Object[] { TABLE })).thenReturn( |
|
113 |
Arrays.asList(MAP, MAP, MAP)); |
|
114 |
when(jdbcTemplate.queryForList("SELECT profileid FROM dnet_tables WHERE table_name='THE_TABLE' AND database_db_name='THE_DB'", String.class)) |
|
115 |
.thenReturn(Arrays.asList("1234")); |
|
116 |
|
|
117 |
when(dataSourceFactory.createDataSource(anyString())).thenReturn(dataSource); |
|
118 |
when(jdbcTemplate.getDataSource()).thenReturn(dataSource); |
|
119 |
|
|
120 |
when(transactionTemplateFactory.createTransactionTemplate(dataSource)).thenReturn(transactionTemplate); |
|
121 |
|
|
122 |
} |
|
123 |
|
|
124 |
@Test |
|
125 |
public void testListCommonDBTables() throws Exception { |
|
126 |
dbUtils.listCommonDBTables(DB); |
|
127 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
128 |
verify(jdbcTemplate).queryForList( |
|
129 |
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type != 'VIEW' AND table_name NOT LIKE '%_log'", |
|
130 |
String.class); |
|
131 |
} |
|
132 |
|
|
133 |
@Test |
|
134 |
public void testListCommonDBViews() throws Exception { |
|
135 |
dbUtils.listCommonDBViews(DB); |
|
136 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
137 |
verify(jdbcTemplate).queryForList( |
|
138 |
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'VIEW' AND table_name NOT LIKE '%_log'", |
|
139 |
String.class); |
|
140 |
} |
|
141 |
|
|
142 |
@Ignore |
|
143 |
@Test |
|
144 |
public void testListAllDatabases() throws Exception { |
|
145 |
dbUtils.listAllDatabases(); |
|
146 |
verify(jdbcTemplateFactory).createJdbcTemplate(DEFAULT_DB); |
|
147 |
verify(jdbcTemplate).queryForList("SELECT datname FROM pg_database WHERE datname LIKE '" + DB_PREFIX + "%'", String.class); |
|
148 |
} |
|
149 |
|
|
150 |
@Test |
|
151 |
public void testGetTypedListFromSql() throws Exception { |
|
152 |
List<Integer> list = dbUtils.getTypedListFromSql(DB, QUERY, Integer.class); |
|
153 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
154 |
verify(jdbcTemplate).queryForList(QUERY, Integer.class); |
|
155 |
assertEquals(4, list.size()); |
|
156 |
} |
|
157 |
|
|
158 |
@Test |
|
159 |
public void testGetSimpleListFromSql() throws Exception { |
|
160 |
List<String> list = dbUtils.getSimpleListFromSql(DB, QUERY); |
|
161 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
162 |
verify(jdbcTemplate).queryForList(QUERY); |
|
163 |
assertEquals(4, list.size()); |
|
164 |
} |
|
165 |
|
|
166 |
@Test |
|
167 |
public void testExecuteSql() throws Exception { |
|
168 |
dbUtils.executeSql(DB, QUERY, null); |
|
169 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
170 |
verify(jdbcTemplate).update(QUERY); |
|
171 |
} |
|
172 |
|
|
173 |
@Test |
|
174 |
public void testDescribeTable() throws Exception { |
|
175 |
List<Map<?, ?>> res = dbUtils.describeTable(DB, TABLE); |
|
176 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
177 |
assertEquals(3, res.size()); |
|
178 |
assertEquals(3, res.get(0).size()); |
|
179 |
assertEquals(3, res.get(1).size()); |
|
180 |
assertEquals(3, res.get(2).size()); |
|
181 |
} |
|
182 |
|
|
183 |
@Test |
|
184 |
public void testDumpTableAsXML() throws Exception { |
|
185 |
String xml = dbUtils.dumpTableAsXML(DB, TABLE); |
|
186 |
|
|
187 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
188 |
assertNotNull(xml); |
|
189 |
assertTrue(xml.contains(TABLE)); |
|
190 |
assertTrue(xml.contains(DB)); |
|
191 |
assertTrue(xml.contains("ROW")); |
|
192 |
} |
|
193 |
|
|
194 |
@Test |
|
195 |
public void testDumpTableAsList() throws Exception { |
|
196 |
List<Document> list = dbUtils.dumpTableAsList(DB, TABLE); |
|
197 |
|
|
198 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
199 |
verify(jdbcTemplate).queryForList("SELECT * FROM " + TABLE); |
|
200 |
assertTrue(list.size() > 0); |
|
201 |
} |
|
202 |
|
|
203 |
@Test |
|
204 |
public void testGetRowByResourceId() throws Exception { |
|
205 |
dbUtils.getRowByResourceId(DB, TABLE, RESOURCE_ID); |
|
206 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
207 |
verify(jdbcTemplate).queryForMap("SELECT * FROM " + TABLE + " WHERE " + DatabaseUtils.DNET_RESOURCE_ID_FIELD + "=?", new Object[] { RESOURCE_ID }); |
|
208 |
} |
|
209 |
|
|
210 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
|
211 |
@Test |
|
212 |
public void testImportFromIterable() throws Exception { |
|
213 |
List<String> iterable = Lists.newArrayList("<ROWS><ROW table='t1' /></ROWS>", "<ROWS><ROW table='t2' /></ROWS>"); |
|
214 |
|
|
215 |
GenericRow row1 = new GenericRow(TABLE, MAP, true); |
|
216 |
GenericRow row2 = new GenericRow(TABLE, MAP, false);; |
|
217 |
|
|
218 |
when(transactionTemplate.execute((TransactionCallback) anyObject())).thenReturn(Lists.newArrayList(row1, row2, row1, row2)); |
|
219 |
|
|
220 |
dbUtils.importFromIterable(DB, iterable); |
|
221 |
|
|
222 |
verify(jdbcTemplateFactory).createJdbcTemplate(dataSource); |
|
223 |
verify(transactionTemplateFactory).createTransactionTemplate(dataSource); |
|
224 |
|
|
225 |
verify(transactionTemplate, times(1)).execute((TransactionCallback) anyObject()); |
|
226 |
} |
|
227 |
|
|
228 |
@Test |
|
229 |
public void testDeleteRowByResourceId() throws Exception { |
|
230 |
dbUtils.deleteRowByResourceId(DB, TABLE, RESOURCE_ID); |
|
231 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
232 |
verify(jdbcTemplate).update("DELETE FROM " + TABLE + " WHERE " + DatabaseUtils.DNET_RESOURCE_ID_FIELD + "=?", new Object[] { RESOURCE_ID }); |
|
233 |
} |
|
234 |
|
|
235 |
@Test |
|
236 |
public void testClearTable() throws Exception { |
|
237 |
dbUtils.clearTable(DB, TABLE); |
|
238 |
verify(jdbcTemplateFactory).createJdbcTemplate(DB); |
|
239 |
verify(jdbcTemplate).update("DELETE FROM " + TABLE); |
|
240 |
} |
|
241 |
|
|
242 |
@Ignore |
|
243 |
@SuppressWarnings("unchecked") |
|
244 |
@Test |
|
245 |
public void testPrepareManagementOfTable() throws Exception { |
|
246 |
when(jdbcTemplate.queryForObject(anyString(), any(Class.class), (Object[]) anyObject())).thenReturn(1); |
|
247 |
|
|
248 |
dbUtils.prepareManagementOfTable(DB, TABLE); |
|
249 |
verify(jdbcTemplateFactory, atLeastOnce()).createJdbcTemplate(DB); |
|
250 |
verify(jdbcTemplate, atLeastOnce()).queryForObject(anyString(), Integer.class, (Object[]) anyObject()); |
|
251 |
verify(jdbcTemplate, never()).update(anyString()); |
|
252 |
} |
|
253 |
|
|
254 |
@Ignore |
|
255 |
@SuppressWarnings("unchecked") |
|
256 |
@Test |
|
257 |
public void testPrepareManagementOfTable_2() throws Exception { |
|
258 |
when(jdbcTemplate.queryForObject(anyString(), any(Class.class), (Object[]) anyObject())).thenReturn(0); |
|
259 |
|
|
260 |
dbUtils.prepareManagementOfTable(DB, TABLE); |
|
261 |
verify(jdbcTemplateFactory, atLeastOnce()).createJdbcTemplate(DB); |
|
262 |
verify(jdbcTemplate, atLeastOnce()).queryForObject(anyString(), Integer.class, (Object[]) anyObject()); |
|
263 |
verify(jdbcTemplate, atLeastOnce()).update(anyString()); |
|
264 |
} |
|
265 |
|
|
266 |
@Ignore |
|
267 |
@SuppressWarnings("unchecked") |
|
268 |
@Test |
|
269 |
public void testClearManagementOfTable() throws Exception { |
|
270 |
when(jdbcTemplate.queryForObject(anyString(), any(Class.class), (Object[]) anyObject())).thenReturn(0); |
|
271 |
|
|
272 |
dbUtils.removeManagementOfTable(DB, TABLE); |
|
273 |
verify(jdbcTemplateFactory, atLeastOnce()).createJdbcTemplate(DB); |
|
274 |
|
|
275 |
verify(jdbcTemplate, never()).update(anyString()); |
|
276 |
} |
|
277 |
|
|
278 |
@Ignore |
|
279 |
@Test |
|
280 |
public void testClearManagementOfTable_2() throws Exception { |
|
281 |
when(jdbcTemplate.queryForObject(anyString(), any(Class.class), (Object[]) anyObject())).thenReturn(1); |
|
282 |
dbUtils.removeManagementOfTable(DB, TABLE); |
|
283 |
verify(jdbcTemplateFactory, atLeastOnce()).createJdbcTemplate(DB); |
|
284 |
|
|
285 |
verify(jdbcTemplate, atLeastOnce()).update(anyString()); |
|
286 |
} |
|
287 |
|
|
288 |
@Test |
|
289 |
public void testParseDate() throws Exception { |
|
290 |
Date d = dbUtils.parseDate("31-05-2011", "dd-MM-yyyy"); |
|
291 |
assertEquals("2011-05-31", d.toString()); |
|
292 |
} |
|
293 |
|
|
294 |
@Test |
|
295 |
public void testEscape() throws Exception { |
|
296 |
Map<String, String> m = Maps.newHashMap(); |
|
297 |
|
|
298 |
m.put("url", "http://www.google.com/set?a=val1&b=val2"); |
|
299 |
m.put("title", "My favourite movie title"); |
|
300 |
|
|
301 |
Document doc = dbUtils.rowToDocument(m); |
|
302 |
assertNotNull(doc); |
|
303 |
|
|
304 |
Document doc2 = DocumentHelper.createDocument(); |
|
305 |
Element elem = doc2.addElement("a"); |
|
306 |
elem.setText(doc.asXML()); |
|
307 |
doc2.setRootElement(elem); |
|
308 |
|
|
309 |
String tempXML = doc2.asXML(); |
|
310 |
|
|
311 |
SAXReader reader = new SAXReader(); |
|
312 |
Document doc3 = reader.read(new StringReader(tempXML)); |
|
313 |
|
|
314 |
System.out.println(doc.asXML());; |
|
315 |
System.out.println(doc3.getRootElement().getText());; |
|
316 |
|
|
317 |
assertEquals(doc.asXML(), doc3.getRootElement().getText()); |
|
318 |
|
|
319 |
} |
|
320 |
|
|
321 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/utils/JdbcTemplateFactoryTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database.utils; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.mockito.Mockito.verify; |
|
5 |
import static org.mockito.Mockito.when; |
|
6 |
|
|
7 |
import javax.sql.DataSource; |
|
8 |
|
|
9 |
import org.junit.Before; |
|
10 |
import org.junit.Test; |
|
11 |
import org.junit.runner.RunWith; |
|
12 |
import org.mockito.Mock; |
|
13 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
14 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
15 |
|
|
16 |
import eu.dnetlib.enabling.database.DataSourceFactory; |
|
17 |
|
|
18 |
@RunWith(MockitoJUnit44Runner.class) |
|
19 |
public class JdbcTemplateFactoryTest { |
|
20 |
|
|
21 |
private static final String VALID_DB = "db01"; |
|
22 |
|
|
23 |
// Class under test |
|
24 |
private JdbcTemplateFactory factory; |
|
25 |
|
|
26 |
@Mock |
|
27 |
private DataSourceFactory dataSourceFactory; |
|
28 |
|
|
29 |
@Mock |
|
30 |
private DataSource dataSource; |
|
31 |
|
|
32 |
@Before |
|
33 |
public void setUp() throws Exception { |
|
34 |
factory = new JdbcTemplateFactory(); |
|
35 |
factory.setDataSourceFactory(dataSourceFactory); |
|
36 |
when(dataSourceFactory.createDataSource(VALID_DB)).thenReturn(dataSource); |
|
37 |
} |
|
38 |
|
|
39 |
@Test |
|
40 |
public void testCreateJdbcTemplate() { |
|
41 |
JdbcTemplate t = factory.createJdbcTemplate(VALID_DB); |
|
42 |
verify(dataSourceFactory).createDataSource(VALID_DB); |
|
43 |
assertEquals(dataSource, t.getDataSource()); |
|
44 |
} |
|
45 |
|
|
46 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/DataSourceFactoryImplTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; |
|
4 |
|
|
5 |
|
|
6 |
import java.sql.SQLException; |
|
7 |
|
|
8 |
import javax.sql.DataSource; |
|
9 |
|
|
10 |
import org.junit.Before; |
|
11 |
import org.junit.Ignore; |
|
12 |
import org.junit.Test; |
|
13 |
import org.junit.runner.RunWith; |
|
14 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
15 |
|
|
16 |
@RunWith(MockitoJUnit44Runner.class) |
|
17 |
@Ignore |
|
18 |
public class DataSourceFactoryImplTest { |
|
19 |
|
|
20 |
// Class under test |
|
21 |
private DataSourceFactoryImpl factory; |
|
22 |
|
|
23 |
@Before |
|
24 |
public void setUp() throws Exception { |
|
25 |
factory = new DataSourceFactoryImpl(); |
|
26 |
factory.setBaseUrl("jdbc:postgresql://localhost:5432"); |
|
27 |
factory.setDriverClassName("org.postgresql.Driver"); |
|
28 |
factory.setUsername("dnet"); |
|
29 |
factory.setPassword("dnetPwd"); |
|
30 |
} |
|
31 |
|
|
32 |
@Test |
|
33 |
public void testCreateDataSource_1() throws SQLException { |
|
34 |
DataSource ds = factory.createDataSource(""); |
|
35 |
assertNotNull(ds); |
|
36 |
assertNotNull(ds.getConnection()); |
|
37 |
} |
|
38 |
@Test |
|
39 |
public void testCreateDataSource_2() throws SQLException { |
|
40 |
DataSource ds = factory.createDataSource("postgres"); |
|
41 |
assertNotNull(ds); |
|
42 |
assertNotNull(ds.getConnection()); |
|
43 |
} |
|
44 |
|
|
45 |
@Test(expected=SQLException.class) |
|
46 |
public void testCreateDataSource_error() throws SQLException { |
|
47 |
DataSource ds = factory.createDataSource("_INVALID_DATABASE_"); |
|
48 |
assertNotNull(ds); |
|
49 |
assertNotNull(ds.getConnection()); |
|
50 |
} |
|
51 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/DatabaseServiceImplTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database; |
|
2 |
|
|
3 |
import static org.mockito.Mockito.verify; |
|
4 |
|
|
5 |
import java.util.Date; |
|
6 |
|
|
7 |
import org.junit.Before; |
|
8 |
import org.junit.Test; |
|
9 |
import org.junit.runner.RunWith; |
|
10 |
import org.mockito.Mock; |
|
11 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
12 |
|
|
13 |
@RunWith(MockitoJUnit44Runner.class) |
|
14 |
public class DatabaseServiceImplTest { |
|
15 |
|
|
16 |
private static final String DBNAME = "THE_DB"; |
|
17 |
private static final String TABLENAME = "THE_TABLE"; |
|
18 |
|
|
19 |
|
|
20 |
// Class under test |
|
21 |
private DatabaseServiceImpl impl; |
|
22 |
@Mock |
|
23 |
private DatabaseServiceCore core; |
|
24 |
|
|
25 |
|
|
26 |
@Before |
|
27 |
public void setUp() throws Exception { |
|
28 |
impl = new DatabaseServiceImpl(); |
|
29 |
impl.setCore(core); |
|
30 |
} |
|
31 |
|
|
32 |
@Test |
|
33 |
public void testDumpTable() { |
|
34 |
impl.dumpTable(DBNAME, TABLENAME); |
|
35 |
verify(core).generateResultSet(DBNAME, TABLENAME, null); |
|
36 |
} |
|
37 |
|
|
38 |
@Test |
|
39 |
public void testDumpTableAndLogs() { |
|
40 |
Date d1 = new Date(100000); |
|
41 |
Date d2 = new Date(200000); |
|
42 |
impl.dumpTableAndLogs(DBNAME, TABLENAME, d1, d2); |
|
43 |
verify(core).generateResultSet(DBNAME, TABLENAME, d1, d2); |
|
44 |
} |
|
45 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/resultset/SQLResultSetListenerTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database.resultset; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.mockito.Matchers.anyMap; |
|
5 |
import static org.mockito.Matchers.anyString; |
|
6 |
import static org.mockito.Matchers.eq; |
|
7 |
import static org.mockito.Mockito.times; |
|
8 |
import static org.mockito.Mockito.verify; |
|
9 |
import static org.mockito.Mockito.when; |
|
10 |
|
|
11 |
import java.util.List; |
|
12 |
|
|
13 |
import org.dom4j.Document; |
|
14 |
import org.junit.Before; |
|
15 |
import org.junit.Test; |
|
16 |
import org.junit.runner.RunWith; |
|
17 |
import org.mockito.Mock; |
|
18 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
19 |
import org.springframework.jdbc.support.rowset.SqlRowSet; |
|
20 |
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; |
|
21 |
|
|
22 |
import eu.dnetlib.enabling.database.rmi.DatabaseException; |
|
23 |
import eu.dnetlib.enabling.database.utils.DatabaseUtils; |
|
24 |
|
|
25 |
|
|
26 |
@RunWith(MockitoJUnit44Runner.class) |
|
27 |
public class SQLResultSetListenerTest { |
|
28 |
|
|
29 |
|
|
30 |
// Class under test |
|
31 |
private SQLResultSetListener listener; |
|
32 |
|
|
33 |
private static final int SIZE = 123; |
|
34 |
private static final int FROM = 10; |
|
35 |
private static final int TO = 12; |
|
36 |
private static final String DB = "THE_DB"; |
|
37 |
private static final String SQL = "SELECT * FROM test"; |
|
38 |
private static final String SQL_FROM_TO = SQL + " OFFSET " + ( FROM - 1 ); |
|
39 |
private static final String XML = "<AAAA />"; |
|
40 |
|
|
41 |
@Mock |
|
42 |
private DatabaseUtils dbUtils; |
|
43 |
|
|
44 |
|
|
45 |
@Mock |
|
46 |
private Document simpleResponseDoc; |
|
47 |
@Mock |
|
48 |
private SqlRowSet ROWSET; |
|
49 |
@Mock |
|
50 |
private SqlRowSetMetaData ROWMETADATA; |
|
51 |
|
|
52 |
|
|
53 |
@Before |
|
54 |
public void setUp() throws Exception { |
|
55 |
listener = new SQLResultSetListener(); |
|
56 |
listener.setDb(DB); |
|
57 |
listener.setDbUtils(dbUtils); |
|
58 |
listener.setSql(SQL); |
|
59 |
|
|
60 |
when(dbUtils.executeSql(eq(DB), anyString(), eq(Integer.class))).thenReturn(SIZE); |
|
61 |
when(dbUtils.executeSql(DB, SQL, SqlRowSet.class)).thenReturn(ROWSET); |
|
62 |
when(dbUtils.executeSql(DB, SQL_FROM_TO, SqlRowSet.class)).thenReturn(ROWSET); |
|
63 |
when(ROWSET.next()).thenReturn(true); |
|
64 |
when(ROWSET.getMetaData()).thenReturn(ROWMETADATA); |
|
65 |
when(ROWMETADATA.getColumnNames()).thenReturn(new String[] {"col1,col2,col3"}); |
|
66 |
when(ROWSET.getObject(anyString())).thenReturn("val_XXX"); |
|
67 |
when(dbUtils.rowToDocument(anyMap())).thenReturn(simpleResponseDoc); |
|
68 |
when(simpleResponseDoc.asXML()).thenReturn(XML); |
|
69 |
} |
|
70 |
|
|
71 |
@Test |
|
72 |
public void testGetResult_1() throws DatabaseException { |
|
73 |
listener.getResult(1, FROM - 1); |
|
74 |
listener.getResult(FROM, TO); |
|
75 |
|
|
76 |
verify(dbUtils).executeSql(DB, SQL, SqlRowSet.class); |
|
77 |
verify(ROWSET, times(TO + 2)).next(); |
|
78 |
verify(ROWSET, times(1)).previous(); |
|
79 |
} |
|
80 |
|
|
81 |
@Test |
|
82 |
public void testGetResult_3() throws DatabaseException { |
|
83 |
List<String> response = listener.getResult(FROM, TO); |
|
84 |
|
|
85 |
verify(dbUtils).executeSql(DB, SQL_FROM_TO, SqlRowSet.class); |
|
86 |
|
|
87 |
assertEquals(3, response.size()); |
|
88 |
for (String s : response) { |
|
89 |
assertEquals(XML, s); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
@Test(expected=IllegalArgumentException.class) |
|
94 |
public void testGetResult_err1() { |
|
95 |
listener.getResult(0, 0); |
|
96 |
} |
|
97 |
|
|
98 |
@Test(expected=IllegalArgumentException.class) |
|
99 |
public void testGetResult_err2() { |
|
100 |
listener.getResult(-4, 2); |
|
101 |
} |
|
102 |
|
|
103 |
@Test(expected=IllegalArgumentException.class) |
|
104 |
public void testGetResult_err3() { |
|
105 |
listener.getResult(1, -1); |
|
106 |
} |
|
107 |
|
|
108 |
@Test(expected=IllegalArgumentException.class) |
|
109 |
public void testGetResult_err4() { |
|
110 |
listener.getResult(10, 4); |
|
111 |
} |
|
112 |
|
|
113 |
@Test |
|
114 |
public void testGetSize() { |
|
115 |
assertEquals(SIZE, listener.getSize()); |
|
116 |
} |
|
117 |
|
|
118 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/resultset/SQLResultSetListenerFactoryTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database.resultset; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertNotNull; |
|
5 |
import static org.mockito.Matchers.anyMap; |
|
6 |
import static org.mockito.Matchers.eq; |
|
7 |
import static org.mockito.Mockito.when; |
|
8 |
|
|
9 |
import java.util.Date; |
|
10 |
import java.util.Map; |
|
11 |
import java.util.concurrent.BlockingQueue; |
|
12 |
|
|
13 |
import org.junit.Before; |
|
14 |
import org.junit.Test; |
|
15 |
import org.junit.runner.RunWith; |
|
16 |
import org.mockito.Mock; |
|
17 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
18 |
|
|
19 |
import eu.dnetlib.enabling.database.rmi.DatabaseException; |
|
20 |
import eu.dnetlib.enabling.database.utils.DatabaseUtils; |
|
21 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
22 |
|
|
23 |
@RunWith(MockitoJUnit44Runner.class) |
|
24 |
public class SQLResultSetListenerFactoryTest { |
|
25 |
|
|
26 |
// Class under test. |
|
27 |
private SQLResultSetListenerFactory factory; |
|
28 |
|
|
29 |
private static final String DB = "THE_DB"; |
|
30 |
private static final String TABLE = "THE_TABLE"; |
|
31 |
|
|
32 |
@Mock |
|
33 |
private DatabaseUtils dbUtils; |
|
34 |
|
|
35 |
@Mock |
|
36 |
private BlockingQueue<Map<String, Object>> queue; |
|
37 |
|
|
38 |
@Mock |
|
39 |
private Date fromDATE; |
|
40 |
@Mock |
|
41 |
private Date untilDATE; |
|
42 |
|
|
43 |
@Before |
|
44 |
public void setUp() throws Exception { |
|
45 |
factory = new SQLResultSetListenerFactory(); |
|
46 |
factory.setDbUtils(dbUtils); |
|
47 |
} |
|
48 |
|
|
49 |
@Test |
|
50 |
public void testCreateSQLListener() throws DatabaseException { |
|
51 |
String query = "SELECT * FROM test "; |
|
52 |
String countquery = "SELECT 100"; |
|
53 |
|
|
54 |
when(dbUtils.executeSql(DB, countquery, Integer.class)).thenReturn(100); |
|
55 |
when(dbUtils.executeSql(DB, query, BlockingQueue.class)).thenReturn(queue); |
|
56 |
|
|
57 |
ResultSetListener listener = factory.createSQLListenerWithSize(DB, query, countquery); |
|
58 |
|
|
59 |
assertNotNull(listener); |
|
60 |
} |
|
61 |
|
|
62 |
@Test |
|
63 |
public void testCreateTableListener() { |
|
64 |
SQLResultSetListener listener = (SQLResultSetListener) factory.createTableListener(DB, TABLE); |
|
65 |
|
|
66 |
assertEquals(listener.getSql(), "SELECT * FROM " + TABLE); |
|
67 |
assertEquals(dbUtils, listener.getDbUtils()); |
|
68 |
assertEquals(DB, listener.getDb()); |
|
69 |
} |
|
70 |
|
|
71 |
@Test |
|
72 |
public void testCreateCondTableListener() { |
|
73 |
String cond = "title='XYZ'"; |
|
74 |
|
|
75 |
SQLResultSetListener listener = (SQLResultSetListener) factory.createCondTableListener(DB, TABLE, cond); |
|
76 |
|
|
77 |
assertEquals(listener.getSql(), "SELECT * FROM " + TABLE + " WHERE " + cond); |
|
78 |
assertEquals(dbUtils, listener.getDbUtils()); |
|
79 |
assertEquals(DB, listener.getDb()); |
|
80 |
} |
|
81 |
|
|
82 |
@SuppressWarnings("unchecked") |
|
83 |
@Test |
|
84 |
public void testCreateLoggedTableListener() { |
|
85 |
String sql = "SELECT * FROM ..."; |
|
86 |
|
|
87 |
when(dbUtils.getSQLFromTemplate(eq("loggedTable"), eq(DB), eq(TABLE), anyMap())).thenReturn(sql); |
|
88 |
|
|
89 |
SQLResultSetListener listener = (SQLResultSetListener) factory.createLoggedTableListener(DB, TABLE, fromDATE, untilDATE); |
|
90 |
|
|
91 |
assertEquals(listener.getSql(), sql); |
|
92 |
assertEquals(dbUtils, listener.getDbUtils()); |
|
93 |
assertEquals(DB, listener.getDb()); |
|
94 |
} |
|
95 |
|
|
96 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/test/java/eu/dnetlib/enabling/database/DatabaseServiceCoreTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertFalse; |
|
5 |
import static org.junit.Assert.assertTrue; |
|
6 |
import static org.mockito.Mockito.verify; |
|
7 |
import static org.mockito.Mockito.when; |
|
8 |
|
|
9 |
import java.util.Arrays; |
|
10 |
import java.util.Date; |
|
11 |
import java.util.HashMap; |
|
12 |
import java.util.List; |
|
13 |
import java.util.Map; |
|
14 |
|
|
15 |
import org.junit.Before; |
|
16 |
import org.junit.Ignore; |
|
17 |
import org.junit.Test; |
|
18 |
import org.junit.runner.RunWith; |
|
19 |
import org.mockito.Mock; |
|
20 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
21 |
|
|
22 |
import eu.dnetlib.enabling.database.objects.DnetDatabase; |
|
23 |
import eu.dnetlib.enabling.database.resultset.SQLResultSetListenerFactory; |
|
24 |
import eu.dnetlib.enabling.database.utils.DatabaseUtils; |
|
25 |
import eu.dnetlib.enabling.database.utils.GenericRow; |
|
26 |
import eu.dnetlib.enabling.resultset.ResultSetFactory; |
|
27 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
28 |
|
|
29 |
@RunWith(MockitoJUnit44Runner.class) |
|
30 |
public class DatabaseServiceCoreTest { |
|
31 |
|
|
32 |
private static final String DB_NAME = "cinema"; |
|
33 |
private static final String TABLE_NAME = "films"; |
|
34 |
private static final String TABLE_WHERE = "year=1980"; |
|
35 |
private static final Date TABLE_FROM = new Date(100000); |
|
36 |
private static final Date TABLE_UNTIL = new Date(200000); |
|
37 |
private static final String SQL = "SELECT * from films"; |
|
38 |
|
|
39 |
// Class under test |
|
40 |
private DatabaseServiceCore core; |
|
41 |
|
|
42 |
@Mock |
|
43 |
private DatabaseUtils dbUtils; |
|
44 |
|
|
45 |
@Mock |
|
46 |
private ResultSetFactory resultSetFactory; |
|
47 |
@Mock |
|
48 |
private SQLResultSetListenerFactory resultSetListenerFactory; |
|
49 |
@Mock |
|
50 |
private ResultSetListener listener_where; |
|
51 |
@Mock |
|
52 |
private ResultSetListener listener_from_until; |
|
53 |
@Mock |
|
54 |
private ResultSetListener listener_sql; |
|
55 |
@Mock |
|
56 |
private GenericRow ROW; |
|
57 |
|
|
58 |
private final Map<String, Object> ROW_FIELDS = new HashMap<String, Object>(); |
|
59 |
|
|
60 |
@Before |
|
61 |
public void setUp() throws Exception { |
|
62 |
core = new DatabaseServiceCore(); |
|
63 |
core.setDbUtils(dbUtils); |
|
64 |
|
|
65 |
core.setResultSetFactory(resultSetFactory); |
|
66 |
core.setResultSetListenerFactory(resultSetListenerFactory); |
|
67 |
|
|
68 |
ROW_FIELDS.put("name", "Pippo"); |
|
69 |
ROW_FIELDS.put("age", 36); |
|
70 |
|
|
71 |
when(dbUtils.listCommonDBTables(DB_NAME)).thenReturn(Arrays.asList("T1", "T2", "T3"));; |
|
72 |
|
|
73 |
when(resultSetListenerFactory.createCondTableListener(DB_NAME, TABLE_NAME, TABLE_WHERE)).thenReturn(listener_where); |
|
74 |
when(resultSetListenerFactory.createLoggedTableListener(DB_NAME, TABLE_NAME, TABLE_FROM, TABLE_UNTIL)).thenReturn(listener_from_until); |
|
75 |
when(resultSetListenerFactory.createSQLListener(DB_NAME, SQL)).thenReturn(listener_sql); |
|
76 |
|
|
77 |
when(ROW.getTable()).thenReturn(TABLE_NAME); |
|
78 |
when(ROW.getFields()).thenReturn(ROW_FIELDS); |
|
79 |
} |
|
80 |
|
|
81 |
@Ignore |
|
82 |
@Test |
|
83 |
public void testListDatabases() throws Exception { |
|
84 |
List<DnetDatabase> list = core.listDatabases(); |
|
85 |
verify(dbUtils).listAllDatabases(); |
|
86 |
assertEquals("List size invalid", 5, list.size()); |
|
87 |
|
|
88 |
for (DnetDatabase db : list) { |
|
89 |
if (db.getDbName().startsWith("M")) { |
|
90 |
assertTrue("Db should be managed", db.isManaged()); |
|
91 |
} else { |
|
92 |
assertFalse("Db should be not managed", db.isManaged()); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
} |
|
97 |
|
|
98 |
@Test |
|
99 |
public void testChangeDatabaseStatus_manage() throws Exception { |
|
100 |
core.changeDatabaseStatus(DB_NAME, true); |
|
101 |
verify(dbUtils).listCommonDBTables(DB_NAME); |
|
102 |
verify(dbUtils).setManaged(DB_NAME, true); |
|
103 |
} |
|
104 |
|
|
105 |
@Test |
|
106 |
public void testChangeDatabaseStatus_unmanage() throws Exception { |
|
107 |
core.changeDatabaseStatus(DB_NAME, false); |
|
108 |
verify(dbUtils).listCommonDBTables(DB_NAME); |
|
109 |
verify(dbUtils).setManaged(DB_NAME, false); |
|
110 |
} |
|
111 |
|
|
112 |
@Test |
|
113 |
public void testGenerateResultSet_1() throws Exception { |
|
114 |
core.generateResultSet(DB_NAME, SQL); |
|
115 |
verify(resultSetFactory).createResultSet(listener_sql); |
|
116 |
} |
|
117 |
|
|
118 |
@Test |
|
119 |
public void testGenerateResultSet_2() throws Exception { |
|
120 |
core.generateResultSet(DB_NAME, TABLE_NAME, TABLE_WHERE); |
|
121 |
verify(resultSetFactory).createResultSet(listener_where); |
|
122 |
} |
|
123 |
|
|
124 |
@Test |
|
125 |
public void testGenerateResultSet_3() throws Exception { |
|
126 |
core.generateResultSet(DB_NAME, TABLE_NAME, TABLE_FROM, TABLE_UNTIL); |
|
127 |
verify(resultSetFactory).createResultSet(listener_from_until); |
|
128 |
} |
|
129 |
|
|
130 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/main/java/eu/dnetlib/enabling/database/utils/GenericRow.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database.utils; |
|
2 |
|
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
public class GenericRow { |
|
6 |
private String table; |
|
7 |
private Map<String, Object> fields; |
|
8 |
private boolean toDelete = false; |
|
9 |
|
|
10 |
|
|
11 |
public GenericRow(String table, Map<String, Object> fields, boolean toDelete) { |
|
12 |
super(); |
|
13 |
this.table = table; |
|
14 |
this.fields = fields; |
|
15 |
this.toDelete = toDelete; |
|
16 |
} |
|
17 |
|
|
18 |
public String getTable() { |
|
19 |
return table; |
|
20 |
} |
|
21 |
|
|
22 |
public void setTable(String table) { |
|
23 |
this.table = table; |
|
24 |
} |
|
25 |
|
|
26 |
public Map<String, Object> getFields() { |
|
27 |
return fields; |
|
28 |
} |
|
29 |
|
|
30 |
public void setFields(Map<String, Object> fields) { |
|
31 |
this.fields = fields; |
|
32 |
} |
|
33 |
|
|
34 |
public boolean isToDelete() { |
|
35 |
return toDelete; |
|
36 |
} |
|
37 |
|
|
38 |
public void setToDelete(boolean toDelete) { |
|
39 |
this.toDelete = toDelete; |
|
40 |
} |
|
41 |
|
|
42 |
} |
modules/cnr-enabling-database-service/tags/cnr-enabling-database-service-1.0.0/src/main/java/eu/dnetlib/enabling/database/utils/DatabaseUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.database.utils; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.sql.Array; |
|
5 |
import java.sql.Date; |
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.ResultSetMetaData; |
|
8 |
import java.sql.SQLException; |
|
9 |
import java.text.ParseException; |
|
10 |
import java.text.SimpleDateFormat; |
|
11 |
import java.util.ArrayList; |
|
12 |
import java.util.HashMap; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Map; |
|
15 |
import java.util.concurrent.BlockingQueue; |
|
16 |
import java.util.concurrent.Executors; |
|
17 |
import java.util.concurrent.LinkedBlockingQueue; |
|
18 |
import java.util.concurrent.TimeUnit; |
|
19 |
import java.util.regex.Pattern; |
|
20 |
|
|
21 |
import javax.sql.DataSource; |
|
22 |
|
|
23 |
import org.apache.commons.logging.Log; |
|
24 |
import org.apache.commons.logging.LogFactory; |
|
25 |
import org.apache.velocity.app.VelocityEngine; |
|
26 |
import org.dom4j.Document; |
|
27 |
import org.dom4j.DocumentHelper; |
|
28 |
import org.dom4j.Element; |
|
29 |
import org.dom4j.Node; |
|
30 |
import org.dom4j.io.SAXReader; |
|
31 |
import org.joda.time.DateTime; |
|
32 |
import org.joda.time.format.ISODateTimeFormat; |
|
33 |
import org.springframework.beans.factory.annotation.Required; |
|
34 |
import org.springframework.dao.DataAccessException; |
|
35 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
36 |
import org.springframework.jdbc.core.RowCallbackHandler; |
|
37 |
import org.springframework.jdbc.support.rowset.SqlRowSet; |
|
38 |
import org.springframework.transaction.TransactionStatus; |
|
39 |
import org.springframework.transaction.support.TransactionCallback; |
|
40 |
import org.springframework.transaction.support.TransactionTemplate; |
|
41 |
import org.springframework.ui.velocity.VelocityEngineUtils; |
|
42 |
|
|
43 |
import com.google.common.collect.Lists; |
|
44 |
|
|
45 |
import eu.dnetlib.enabling.database.DataSourceFactory; |
|
46 |
import eu.dnetlib.enabling.database.TransactionTemplateFactory; |
|
47 |
import eu.dnetlib.enabling.database.objects.DnetDatabase; |
|
48 |
import eu.dnetlib.enabling.database.rmi.DatabaseException; |
|
49 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
50 |
import eu.dnetlib.miscutils.functional.string.Sanitizer; |
|
51 |
|
|
52 |
public class DatabaseUtils { |
|
53 |
|
|
54 |
private static final String SQL_DATE_FORMAT = "yyyy-MM-dd"; |
|
55 |
|
|
56 |
private DataSourceFactory dataSourceFactory; |
|
57 |
private JdbcTemplateFactory jdbcTemplateFactory; |
|
58 |
private TransactionTemplateFactory transactionTemplateFactory; |
|
59 |
private String defaultDB; |
|
60 |
|
|
61 |
private VelocityEngine velocityEngine; |
|
62 |
private String dbPrefix; |
|
63 |
private int numbersOfRecordsForTransaction; |
|
64 |
|
|
65 |
public static final String DNET_RESOURCE_ID_FIELD = "_dnet_resource_identifier_"; |
|
66 |
|
|
67 |
private static final Log log = LogFactory.getLog(DatabaseUtils.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
68 |
|
|
69 |
private static final int BLOCKING_QUEUE_SIZE = 200; |
|
70 |
public static final int BLOCKING_QUEUE_TIMEOUT = 300; |
|
71 |
|
|
72 |
public class TableDates { |
|
73 |
|
|
74 |
private Date lastInsert; |
|
75 |
private Date lastUpdate; |
|
76 |
private Date lastDelete; |
|
77 |
|
|
78 |
public Date getLastInsert() { |
|
79 |
return lastInsert; |
|
80 |
} |
|
81 |
|
|
82 |
public void setLastInsert(final Date lastInsert) { |
|
83 |
this.lastInsert = lastInsert; |
|
84 |
} |
|
85 |
|
|
86 |
public Date getLastUpdate() { |
|
87 |
return lastUpdate; |
|
88 |
} |
|
89 |
|
|
90 |
public void setLastUpdate(final Date lastUpdate) { |
|
91 |
this.lastUpdate = lastUpdate; |
|
92 |
} |
|
93 |
|
|
94 |
public Date getLastDelete() { |
|
95 |
return lastDelete; |
|
96 |
} |
|
97 |
|
|
98 |
public void setLastDelete(final Date lastDelete) { |
|
99 |
this.lastDelete = lastDelete; |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
public List<String> listCommonDBTables(final String database) throws DatabaseException { |
|
104 |
String query = "SELECT table_name FROM information_schema.tables " + "WHERE table_schema = 'public' " + "AND table_type != 'VIEW' " |
|
105 |
+ "AND table_name NOT LIKE '%_log'"; |
|
106 |
return getTypedListFromSql(database, query, String.class); |
|
107 |
} |
|
108 |
|
|
109 |
public List<String> listCommonDBViews(final String database) throws DatabaseException { |
|
110 |
String query = "SELECT table_name FROM information_schema.tables " + "WHERE table_schema = 'public' " + "AND table_type = 'VIEW' " |
|
111 |
+ "AND table_name NOT LIKE '%_log'"; |
|
112 |
return getTypedListFromSql(database, query, String.class); |
|
113 |
} |
|
114 |
|
|
115 |
public Map<String, TableDates> getTableDatesForDB(final String db) throws DatabaseException { |
|
116 |
Map<String, TableDates> res = new HashMap<String, TableDates>(); |
|
117 |
|
|
118 |
for (String table : listCommonDBTables(db)) { |
|
119 |
try { |
|
120 |
TableDates dates = new TableDates(); |
|
121 |
|
|
122 |
String query = "select lastinsert, lastupdate, lastdelete from " + "(select max(date) as lastinsert from " + table |
|
123 |
+ "_log where operation='insert') as t1, " + "(select max(date) as lastupdate from " + table + "_log where operation='update') as t2, " |
|
124 |
+ "(select max(date) as lastdelete from " + table + "_log where operation='delete') as t3"; |
|
125 |
|
|
126 |
SqlRowSet srs = executeSql(db, query, SqlRowSet.class); |
|
127 |
if (srs.next()) { |
|
128 |
dates.setLastInsert(srs.getDate("lastinsert")); |
|
129 |
dates.setLastUpdate(srs.getDate("lastupdate")); |
|
130 |
dates.setLastDelete(srs.getDate("lastdelete")); |
|
131 |
} |
|
132 |
res.put(table, dates); |
|
133 |
} catch (Exception e) { |
|
134 |
log.warn("Error obtaing dates for table " + table, e); |
|
135 |
} |
|
136 |
} |
|
137 |
return res; |
|
138 |
} |
|
139 |
|
|
140 |
public List<DnetDatabase> listAllDatabases() throws DatabaseException { |
|
141 |
final String query = "SELECT d.datname AS db, COALESCE(dsc.description,'')='isManaged' AS managed FROM pg_database d LEFT OUTER JOIN pg_shdescription dsc ON (d.oid = dsc.objoid) WHERE d.datname LIKE '" + dbPrefix + "%' ORDER BY d.datname DESC"; |
|
142 |
final JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(defaultDB); |
|
143 |
|
|
144 |
final List<DnetDatabase> list = Lists.newArrayList(); |
|
145 |
for (Map<String,Object> map : jdbcTemplate.queryForList(query)) { |
|
146 |
list.add(new DnetDatabase(map.get("db").toString(), Boolean.parseBoolean(map.get("managed").toString()))); |
|
147 |
} |
|
148 |
return list; |
|
149 |
|
|
150 |
} |
|
151 |
|
|
152 |
public <T> List<T> getTypedListFromSql(final String dbName, final String query, final Class<T> clazz) throws DatabaseException { |
|
153 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(dbName); |
|
154 |
|
|
155 |
try { |
|
156 |
List<T> list = new ArrayList<T>(); |
|
157 |
for (Object obj : jdbcTemplate.queryForList(query, clazz)) { |
|
158 |
list.add(clazz.cast(obj)); |
|
159 |
} |
|
160 |
return list; |
|
161 |
} catch (DataAccessException e) { |
|
162 |
throw new DatabaseException(e); |
|
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
public List<String> getSimpleListFromSql(final String dbName, final String query) throws DatabaseException { |
|
167 |
|
|
168 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(dbName); |
|
169 |
|
|
170 |
try { |
|
171 |
List<String> list = new ArrayList<String>(); |
|
172 |
for (Object obj : jdbcTemplate.queryForList(query)) { |
|
173 |
list.add(obj.toString()); |
|
174 |
} |
|
175 |
return list; |
|
176 |
} catch (DataAccessException e) { |
|
177 |
throw new DatabaseException(e); |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
public void executeSql(final String db, final String query) throws DatabaseException { |
|
182 |
executeSql(db, query, Void.class); |
|
183 |
} |
|
184 |
|
|
185 |
@SuppressWarnings("unchecked") |
|
186 |
public <T> T executeSql(final String dbName, final String query, final Class<T> clazz) throws DatabaseException { |
|
187 |
|
|
188 |
final JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(dbName); |
|
189 |
try { |
|
190 |
if (clazz == Integer.class) return (T) jdbcTemplate.queryForObject(query, Integer.class); |
|
191 |
else if (clazz == List.class) return (T) jdbcTemplate.queryForList(query); |
|
192 |
else if (clazz == Map.class) return (T) jdbcTemplate.queryForMap(query); |
|
193 |
else if (clazz == SqlRowSet.class) return (T) jdbcTemplate.queryForRowSet(query); |
|
194 |
|
|
195 |
else if (clazz == BlockingQueue.class) { |
|
196 |
log.info("Creating Queue"); |
|
197 |
|
|
198 |
final LinkedBlockingQueue<Map<String, Object>> q = new LinkedBlockingQueue<Map<String, Object>>(BLOCKING_QUEUE_SIZE); |
|
199 |
|
|
200 |
Runnable run = new Runnable() { |
|
201 |
|
|
202 |
@Override |
|
203 |
public void run() { |
|
204 |
try { |
|
205 |
jdbcTemplate.query(query, new RowCallbackHandler() { |
|
206 |
|
|
207 |
@Override |
|
208 |
public void processRow(final ResultSet rs) throws SQLException { |
|
209 |
|
|
210 |
ResultSetMetaData md = rs.getMetaData(); |
|
211 |
Map<String, Object> row = new HashMap<String, Object>(); |
|
212 |
for (int i = 1; i <= md.getColumnCount(); i++) { |
|
213 |
row.put(md.getColumnName(i), rs.getObject(i)); |
|
214 |
} |
|
215 |
try { |
|
216 |
if (!q.offer(row, BLOCKING_QUEUE_TIMEOUT, TimeUnit.SECONDS)) { |
|
217 |
log.info("The consumer doesn't consume my queue, I stop"); |
|
218 |
rs.close(); |
|
219 |
Thread.currentThread().interrupt(); |
|
220 |
return; |
|
221 |
} |
|
222 |
log.debug("Putted element in queue"); |
|
223 |
} catch (InterruptedException e) { |
|
224 |
log.error("Error putting element in queue"); |
|
225 |
} |
|
226 |
} |
|
227 |
}); |
|
228 |
} catch (Throwable e) { |
|
229 |
log.info("Exception executing SQL", e); |
|
230 |
} |
|
231 |
try { |
|
232 |
// An empty Map indicates the end of the resultset |
|
233 |
q.offer(new HashMap<String, Object>(), BLOCKING_QUEUE_TIMEOUT, TimeUnit.SECONDS); |
|
234 |
} catch (InterruptedException e) { |
|
235 |
log.error("Error putting LAST element in queue"); |
|
236 |
} |
|
237 |
log.info(" -- End of Sql Resultset"); |
|
238 |
} |
|
239 |
}; |
|
240 |
Executors.newSingleThreadExecutor().submit(run); |
|
241 |
|
|
242 |
log.info("Returned Queue"); |
|
243 |
|
|
244 |
return (T) q; |
|
245 |
} else { |
|
246 |
jdbcTemplate.update(query); |
|
247 |
return null; |
|
248 |
} |
|
249 |
} catch (DataAccessException e) { |
|
250 |
throw new DatabaseException(e); |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
public boolean contains(final String db, final String table, final String column, final String value) { |
|
255 |
String query = ""; |
|
256 |
try { |
|
257 |
verifyParameters(db, table, column); |
|
258 |
query = "SELECT " + column + " FROM " + table + " WHERE " + column + " = '" + value + "'"; |
|
259 |
List<String> res = getSimpleListFromSql(db, query); |
|
260 |
return res != null ? res.size() > 0 : false; |
|
261 |
} catch (DatabaseException e) { |
|
262 |
throw new RuntimeException("Error performing SQL: " + query, e); |
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
public List<Map<?, ?>> describeTable(final String database, final String table) throws DatabaseException { |
|
267 |
verifyParameters(database, table); |
|
268 |
|
|
269 |
try { |
|
270 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
271 |
List<Map<?, ?>> response = new ArrayList<Map<?, ?>>(); |
|
272 |
String query = "SELECT * FROM information_schema.columns WHERE table_name = ?"; |
|
273 |
|
|
274 |
for (Object o : jdbcTemplate.queryForList(query, new Object[] { table })) { |
|
275 |
if (o instanceof Map<?, ?>) { |
|
276 |
response.add((Map<?, ?>) o); |
|
277 |
} |
|
278 |
} |
|
279 |
return response; |
|
280 |
} catch (DataAccessException e) { |
|
281 |
throw new DatabaseException(e); |
|
282 |
} |
|
283 |
} |
|
284 |
|
|
285 |
public String dumpTableAsXML(final String db, final String t) throws DatabaseException { |
|
286 |
return dumpTableAsDoc(db, t).asXML(); |
|
287 |
} |
|
288 |
|
|
289 |
public Document dumpTableAsDoc(final String db, final String t) throws DatabaseException { |
|
290 |
Document doc = DocumentHelper.createDocument(); |
|
291 |
|
|
292 |
Element root = doc.addElement("DB_TABLE"); |
|
293 |
Element head = root.addElement("HEADER"); |
|
294 |
|
|
295 |
head.addElement("DATABASE").addAttribute("value", db); |
|
296 |
head.addElement("TABLE").addAttribute("value", t); |
|
297 |
head.addElement("DATE").addAttribute("value", DateUtils.now_ISO8601()); |
|
298 |
|
|
299 |
Element body = root.addElement("BODY"); |
|
300 |
for (Document d : dumpTableAsList(db, t)) { |
|
301 |
body.add(d.getRootElement()); |
|
302 |
} |
|
303 |
return doc; |
|
304 |
} |
|
305 |
|
|
306 |
public List<Document> dumpTableAsList(final String db, final String t) throws DatabaseException { |
|
307 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(db); |
|
308 |
|
|
309 |
List<Document> list = new ArrayList<Document>(); |
|
310 |
for (Object o : jdbcTemplate.queryForList("SELECT * FROM " + t)) { |
|
311 |
if (o instanceof Map<?, ?>) { |
|
312 |
list.add(rowToDocument((Map<?, ?>) o)); |
|
313 |
} |
|
314 |
} |
|
315 |
return list; |
|
316 |
} |
|
317 |
|
|
318 |
public Document rowToDocument(final Map<?, ?> map) throws DatabaseException { |
|
319 |
Document doc = DocumentHelper.createDocument(); |
|
320 |
|
|
321 |
Element row = doc.addElement("ROW"); |
|
322 |
for (Map.Entry<?, ?> entry : map.entrySet()) { |
|
323 |
Element col = row.addElement("FIELD"); |
|
324 |
col.addAttribute("name", "" + entry.getKey()); |
|
325 |
addValue(col, entry.getValue()); |
|
326 |
} |
|
327 |
return doc; |
|
328 |
} |
|
329 |
|
|
330 |
public Document getRowByResourceId(final String database, final String table, final String resourceId) throws DatabaseException { |
|
331 |
verifyParameters(database, table); |
|
332 |
|
|
333 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
334 |
String query = "SELECT * FROM " + table + " WHERE " + DNET_RESOURCE_ID_FIELD + "=?"; |
|
335 |
|
|
336 |
Map<?, ?> map = jdbcTemplate.queryForMap(query, new Object[] { resourceId }); |
|
337 |
Document doc = DocumentHelper.createDocument(); |
|
338 |
|
|
339 |
Element root = doc.addElement("DB_RECORD"); |
|
340 |
Element head = root.addElement("HEADER"); |
|
341 |
head.addElement("RESOURCE_IDENTIFIER").addAttribute("value", resourceId); |
|
342 |
head.addElement("DATABASE").addAttribute("value", database); |
|
343 |
head.addElement("TABLE").addAttribute("value", table); |
|
344 |
head.addElement("DATE").addAttribute("value", DateUtils.now_ISO8601()); |
|
345 |
|
|
346 |
Element body = root.addElement("BODY"); |
|
347 |
|
|
348 |
Element row = body.addElement("ROW"); |
|
349 |
|
|
350 |
for (Map.Entry<?, ?> entry : map.entrySet()) { |
|
351 |
Element col = row.addElement("FIELD"); |
|
352 |
col.addAttribute("name", "" + entry.getKey()); |
|
353 |
addValue(col, entry.getValue()); |
|
354 |
} |
|
355 |
|
|
356 |
return doc; |
|
357 |
} |
|
358 |
|
|
359 |
private void addValue(final Element elem, final Object value) throws DatabaseException { |
|
360 |
if (value instanceof Array) { |
|
361 |
try { |
|
362 |
for (Object o : (Object[]) ((Array) value).getArray()) { |
|
363 |
addValue(elem.addElement("ITEM"), o); |
|
364 |
} |
|
365 |
} catch (Exception e) { |
|
366 |
throw new DatabaseException("Error procsessing a Array", e); |
|
367 |
} |
|
368 |
} else if (value != null) { |
|
369 |
elem.addText(Sanitizer.sanitize(value.toString())); |
|
370 |
} else { |
|
371 |
elem.addAttribute("isNull", "true"); |
|
372 |
} |
|
373 |
} |
|
374 |
|
|
375 |
private void verifyParameters(final String... params) throws DatabaseException { |
|
376 |
Pattern pattern = Pattern.compile("\\w{1,128}"); |
|
377 |
|
|
378 |
for (String p : params) { |
|
379 |
log.debug("TESTING SQL PARAM:" + p); |
|
380 |
if (p == null) throw new DatabaseException("Parameter is null"); |
|
381 |
else if (!pattern.matcher(p).matches()) throw new DatabaseException("Parameter [" + p + "] contains an invalid character"); |
|
382 |
else { |
|
383 |
log.debug("TEST OK"); |
|
384 |
} |
|
385 |
} |
|
386 |
} |
|
387 |
|
|
388 |
public void importFromIterable(final String db, final Iterable<String> iterable) throws DatabaseException { |
|
389 |
verifyParameters(db); |
|
390 |
|
|
391 |
final DataSource dataSource = dataSourceFactory.createDataSource(db); |
|
392 |
final JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(dataSource); |
|
393 |
final TransactionTemplate transactionTemplate = transactionTemplateFactory.createTransactionTemplate(dataSource); |
|
394 |
|
|
395 |
int counterDone = 0; |
|
396 |
int counterTotal = 0; |
|
397 |
|
|
398 |
long start = DateUtils.now(); |
|
399 |
|
|
400 |
List<GenericRow> rows = new ArrayList<GenericRow>(); |
|
401 |
for (String prof : iterable) { |
|
402 |
rows.addAll(obtainListOfRows(prof)); |
|
403 |
if (rows.size() > numbersOfRecordsForTransaction) { |
|
404 |
counterTotal += rows.size(); |
|
405 |
counterDone += importTransaction(jdbcTemplate, transactionTemplate, rows); |
|
406 |
rows.clear(); |
|
407 |
} |
|
408 |
} |
|
409 |
counterTotal += rows.size(); |
|
410 |
counterDone += importTransaction(jdbcTemplate, transactionTemplate, rows); |
|
411 |
|
|
412 |
long end = DateUtils.now(); |
|
413 |
|
|
414 |
log.info("**********************************************************"); |
|
415 |
log.info("Processed " + counterDone + "/" + counterTotal + " rows in " + ((end - start) / 1000) + " seconds"); |
|
416 |
log.info("**********************************************************"); |
|
417 |
} |
|
418 |
|
|
419 |
private int importTransaction(final JdbcTemplate jdbcTemplate, final TransactionTemplate transactionTemplate, List<GenericRow> rows) { |
|
420 |
if (rows == null) return 0; |
|
421 |
|
|
422 |
int res = rows.size(); |
|
423 |
|
|
424 |
while (!rows.isEmpty()) { |
|
425 |
List<GenericRow> ok = importTransactionInternal(jdbcTemplate, transactionTemplate, rows); |
|
426 |
|
|
427 |
if (ok.size() < rows.size()) { |
|
428 |
importTransactionInternal(jdbcTemplate, transactionTemplate, ok); |
|
429 |
res--; |
|
430 |
if ((ok.size() + 1) < rows.size()) { |
|
431 |
rows = rows.subList(ok.size() + 1, rows.size()); |
|
432 |
} else { |
|
433 |
rows.clear(); |
|
434 |
} |
|
435 |
} else { |
|
436 |
rows.clear(); |
|
437 |
} |
|
438 |
} |
|
439 |
return res; |
|
440 |
} |
|
441 |
|
|
442 |
private List<GenericRow> importTransactionInternal(final JdbcTemplate jdbcTemplate, |
|
443 |
final TransactionTemplate transactionTemplate, |
|
444 |
final List<GenericRow> rows) { |
|
445 |
return transactionTemplate.execute(new TransactionCallback<List<GenericRow>>() { |
|
446 |
|
|
447 |
@Override |
|
448 |
public List<GenericRow> doInTransaction(final TransactionStatus status) { |
|
449 |
final List<GenericRow> ok = Lists.newArrayList(); |
|
450 |
try { |
|
451 |
for (GenericRow row : rows) { |
|
452 |
if (row.isToDelete()) { |
|
453 |
deleteRow(jdbcTemplate, row.getTable(), row.getFields()); |
|
454 |
} else { |
|
455 |
addOrUpdateRow(jdbcTemplate, row.getTable(), row.getFields()); |
|
456 |
} |
|
457 |
ok.add(row); |
|
458 |
} |
|
459 |
} catch (DatabaseException e) { |
|
460 |
log.warn("Transaction failed", e); |
|
461 |
status.setRollbackOnly(); |
|
462 |
} |
|
463 |
return ok; |
|
464 |
} |
|
465 |
}); |
|
466 |
} |
|
467 |
|
|
468 |
protected void addOrUpdateRow(final JdbcTemplate jdbcTemplate, final String table, final Map<String, Object> rowFields) throws DatabaseException { |
|
469 |
try { |
|
470 |
|
|
471 |
if (log.isDebugEnabled()) { |
|
472 |
log.debug("Adding or updating element to table " + table); |
|
473 |
} |
|
474 |
verifyParameters(table); |
|
475 |
verifyParameters(rowFields.keySet().toArray(new String[rowFields.size()])); |
|
476 |
|
|
477 |
String fields = ""; |
|
478 |
String values = ""; |
|
479 |
List<Object> list = new ArrayList<Object>(); |
|
480 |
|
|
481 |
for (Map.Entry<String, Object> e : rowFields.entrySet()) { |
|
482 |
if (!fields.isEmpty()) { |
|
483 |
fields += ","; |
|
484 |
} |
|
485 |
fields += e.getKey(); |
|
486 |
if (!values.isEmpty()) { |
|
487 |
values += ","; |
|
488 |
} |
|
489 |
values += "?"; |
|
490 |
list.add(e.getValue()); |
|
491 |
} |
|
492 |
|
|
493 |
int count = 0; |
|
494 |
if (rowFields.containsKey(DNET_RESOURCE_ID_FIELD)) { |
|
495 |
List<Object> list2 = new ArrayList<Object>(); |
|
496 |
list2.addAll(list); |
|
497 |
list2.add(rowFields.get(DNET_RESOURCE_ID_FIELD)); |
|
498 |
count = jdbcTemplate.update("UPDATE " + table + " SET (" + fields + ") = (" + values + ") WHERE " + DNET_RESOURCE_ID_FIELD + "=?", |
|
499 |
list2.toArray()); |
|
500 |
} |
|
501 |
if (count == 0) { |
|
502 |
jdbcTemplate.update("INSERT INTO " + table + " (" + fields + ") VALUES (" + values + ")", list.toArray()); |
|
503 |
} |
|
504 |
} catch (final Exception e) { |
|
505 |
throw new DatabaseException("Error adding or updating record", e); |
|
506 |
} |
|
507 |
} |
|
508 |
|
|
509 |
protected void deleteRow(final JdbcTemplate jdbcTemplate, final String table, final Map<String, Object> rowFields) throws DatabaseException { |
|
510 |
if (log.isDebugEnabled()) { |
|
511 |
log.debug("Deleting element from table " + table); |
|
512 |
} |
|
513 |
verifyParameters(table); |
|
514 |
verifyParameters(rowFields.keySet().toArray(new String[rowFields.size()])); |
|
515 |
|
|
516 |
List<Object> list = new ArrayList<Object>(); |
|
517 |
|
|
518 |
String where = ""; |
|
519 |
|
|
520 |
for (Map.Entry<String, Object> e : rowFields.entrySet()) { |
|
521 |
if (!where.isEmpty()) { |
|
522 |
where += " AND "; |
|
523 |
} |
|
524 |
where += e.getKey() + "=?"; |
|
525 |
list.add(e.getValue()); |
|
526 |
} |
|
527 |
|
|
528 |
if (where.isEmpty()) throw new DatabaseException("Delete condition is empty"); |
|
529 |
int n = jdbcTemplate.update("DELETE FROM " + table + " WHERE " + where, list.toArray()); |
|
530 |
|
|
531 |
if (log.isDebugEnabled()) { |
|
532 |
log.debug("Number of Deleted records: " + n); |
|
533 |
} |
|
534 |
} |
|
535 |
|
|
536 |
public void deleteRowByResourceId(final String database, final String table, final String resourceIdentifier) throws DatabaseException { |
|
537 |
verifyParameters(database, table, resourceIdentifier); |
|
538 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
539 |
jdbcTemplate.update("DELETE FROM " + table + " WHERE " + DNET_RESOURCE_ID_FIELD + "=?", new Object[] { resourceIdentifier }); |
|
540 |
} |
|
541 |
|
|
542 |
public void clearTable(final String database, final String table) throws DatabaseException { |
|
543 |
verifyParameters(database, table); |
|
544 |
|
|
545 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
546 |
jdbcTemplate.update("DELETE FROM " + table); |
|
547 |
} |
|
548 |
|
|
549 |
public void prepareManagementOfTable(final String database, final String table) throws DatabaseException { |
|
550 |
verifyParameters(database, table); |
|
551 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
552 |
|
|
553 |
if (!isManagedTable(jdbcTemplate, table)) { |
|
554 |
jdbcTemplate.update(getSQLFromTemplate("manageTable", database, table, null)); |
|
555 |
log.info("Added management of table " + table); |
|
556 |
} |
|
557 |
|
|
558 |
addLogTable(database, table); |
|
559 |
} |
|
560 |
|
|
561 |
public void addLogTable(final String database, final String table) throws DatabaseException { |
|
562 |
verifyParameters(database, table); |
|
563 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
564 |
|
|
565 |
if (!isLoggedTable(jdbcTemplate, table)) { |
|
566 |
jdbcTemplate.update(getSQLFromTemplate("removeLogTable", database, table, null)); |
|
567 |
jdbcTemplate.update(getSQLFromTemplate("addLogTable", database, table, null)); |
|
568 |
log.info("Added logs of table " + table); |
|
569 |
} |
|
570 |
} |
|
571 |
|
|
572 |
public void removeManagementOfTable(final String database, final String table) throws DatabaseException { |
|
573 |
verifyParameters(database, table); |
|
574 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
575 |
|
|
576 |
if (isManagedTable(jdbcTemplate, table)) { |
|
577 |
jdbcTemplate.update(getSQLFromTemplate("unmanageTable", database, table, null)); |
|
578 |
log.info("Removed management of table " + table); |
|
579 |
} |
|
580 |
removeLogTable(database, table); |
|
581 |
} |
|
582 |
|
|
583 |
public void removeLogTable(final String database, final String table) throws DatabaseException { |
|
584 |
verifyParameters(database, table); |
|
585 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
586 |
|
|
587 |
if (isLoggedTable(jdbcTemplate, table)) { |
|
588 |
jdbcTemplate.update(getSQLFromTemplate("removeLogTable", database, table, null)); |
|
589 |
log.info("Removed logs of table " + table); |
|
590 |
} |
|
591 |
} |
|
592 |
|
|
593 |
public boolean isManagedTable(final String database, final String table) throws DatabaseException { |
|
594 |
verifyParameters(database, table); |
|
595 |
JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(database); |
|
596 |
return isManagedTable(jdbcTemplate, table); |
|
597 |
} |
|
598 |
|
|
599 |
private boolean isManagedTable(final JdbcTemplate jdbcTemplate, final String table) { |
|
600 |
return jdbcTemplate.queryForObject("SELECT count(*) FROM information_schema.columns WHERE table_name = ? AND column_name = ?", Integer.class, |
Also available in: Unified diff
[maven-release-plugin] copy for tag cnr-enabling-database-service-1.0.0