Last active
March 12, 2019 13:40
-
-
Save horitaku1124/bab01b783821b78244dc9fffb88115e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import java.sql.*; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
import static java.lang.Class.forName; | |
public class DBLoader { | |
@Test | |
public void test1() throws ClassNotFoundException, SQLException { | |
forName("oracle.jdbc.driver.OracleDriver"); | |
try(Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@host:orcl","user","pass")) { | |
List<List<String>> member = allData(conn, "member"); | |
List<String> keys = primaryKey(conn, "member"); | |
Map<String, List<String>> uniqueMap = toUnique(keys, member); | |
System.out.println(uniqueMap); | |
} | |
} | |
@Test | |
public void test2() throws ClassNotFoundException, SQLException { | |
List<List<String>> member1 = new ArrayList<List<String>>() {{ | |
add(Arrays.asList("ID", "NAME", "SALARY")); | |
add(Arrays.asList("A001", "Tom", "100")); | |
add(Arrays.asList("A002", "Jane", "80")); | |
}}; | |
List<List<String>> member2 = new ArrayList<List<String>>() {{ | |
add(Arrays.asList("ID", "NAME", "SALARY")); | |
add(Arrays.asList("A001", "Tom", "100")); | |
add(Arrays.asList("A002", "Jane", "80")); | |
add(Arrays.asList("A003", "Bob", "250")); | |
}}; | |
List<String> keys = Arrays.asList("ID"); | |
Map<String, List<String>> memberMap1 = toUnique(keys, member1); | |
Map<String, List<String>> memberMap2 = toUnique(keys, member2); | |
List<Update> diffs = diff(memberMap1, memberMap2); | |
System.out.println(diffs); | |
} | |
enum Update { | |
Create, Updated, Deleted, Same | |
} | |
private List<Update> diff(Map<String, List<String>> origin, Map<String, List<String>> modified) { | |
List<Update> diff = new ArrayList<>(); | |
int size = modified.size(); | |
int index = 0; | |
for (String key: modified.keySet()) { | |
if (origin.containsKey(key)) { | |
diff.add(Update.Same); | |
} else { | |
diff.add(Update.Create); | |
} | |
} | |
return diff; | |
} | |
public Map<String, List<String>> toUnique(List<String> keys, List<List<String>> tableData) { | |
Map<String, List<String>> map = new LinkedHashMap(); | |
List<Integer> keyIndexes = new ArrayList<>(); | |
for (String key: keys) { | |
List<String> header = tableData.get(0); | |
for (int i = 0;i < header.size();i++) { | |
if (key.equals(header.get(i))) { | |
keyIndexes.add(i); | |
} | |
} | |
} | |
for (int i = 1;i < tableData.size();i++) { | |
List<String> row = tableData.get(i); | |
String pKey = keyIndexes.stream().map(row::get).collect(Collectors.joining("_")); | |
map.put(pKey, row); | |
} | |
return map; | |
} | |
public List<List<String>> allData(Connection conn, String table) throws SQLException { | |
try (Statement stmt = conn.createStatement()) { | |
List<List<String>> allData = new ArrayList<>(); | |
ResultSet rs = stmt.executeQuery("select * from " + table); | |
int columnCount = rs.getMetaData().getColumnCount(); | |
List<String> header = new ArrayList<>(); | |
for (int i = 1;i <= columnCount;i++) { | |
header.add(rs.getMetaData().getColumnName(i)); | |
} | |
allData.add(header); | |
while(rs.next()) { | |
List<String> cols = new ArrayList<>(); | |
for (int i = 1;i <= columnCount;i++) { | |
cols.add(rs.getString(i)); | |
} | |
allData.add(cols); | |
} | |
return allData; | |
} | |
} | |
public List<String> primaryKey(Connection conn, String table) throws SQLException { | |
List<String> keys = new ArrayList<>(); | |
try (Statement stmt = conn.createStatement()) { | |
String sql = "SELECT column_name FROM all_cons_columns WHERE constraint_name = (\n" + | |
" SELECT constraint_name FROM user_constraints \n" + | |
" WHERE UPPER(table_name) = UPPER('" + table + "') AND CONSTRAINT_TYPE = 'P'\n" + | |
")"; | |
ResultSet rs = stmt.executeQuery(sql); | |
while(rs.next()) { | |
keys.add(rs.getString(1)); | |
} | |
return keys; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import java.sql.*; | |
import static java.lang.Class.forName; | |
public class test1 { | |
@Test | |
public void tst1() throws ClassNotFoundException, SQLException { | |
forName("oracle.jdbc.driver.OracleDriver"); | |
Connection connection = null; | |
connection = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:orcl","user","pass"); | |
Statement stmt; | |
stmt = connection.createStatement(); | |
ResultSet rs = stmt.executeQuery("select * from member"); | |
while(rs.next()) { | |
int columnCount = rs.getMetaData().getColumnCount(); | |
for (int i = 1;i <= columnCount;i++) { | |
String name = rs.getMetaData().getColumnName(i); | |
String value = rs.getString(i); | |
System.out.println(name + "=" + value); | |
} | |
} | |
stmt.close(); | |
String sql = "SELECT column_name FROM all_cons_columns WHERE constraint_name = (\n" + | |
" SELECT constraint_name FROM user_constraints \n" + | |
" WHERE UPPER(table_name) = UPPER('member') AND CONSTRAINT_TYPE = 'P'\n" + | |
")"; | |
stmt = connection.createStatement(); | |
System.out.println(sql); | |
rs = stmt.executeQuery(sql); | |
while(rs.next()) { | |
System.out.println(rs.getString(1)); | |
} | |
stmt.close(); | |
connection.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TestSpec { | |
static class TestModel { | |
Method beforeClass = null; | |
Method afterClass = null; | |
Method before = null; | |
Method after = null; | |
List<Method> tests = new ArrayList<>(); | |
} | |
public static TestModel check(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { | |
return check(Class.forName(className)); | |
} | |
public static TestModel check(Class target) { | |
TestModel model = new TestModel(); | |
Method[] methods = target.getMethods(); | |
for (int i = 0; i < methods.length; i++) { | |
Method m = methods[i]; | |
Test testAnnotation = m.getAnnotation(Test.class); | |
if (testAnnotation != null) { | |
model.tests.add(m); | |
} | |
Before beforeAnnotation = m.getAnnotation(Before.class); | |
if (beforeAnnotation != null) { | |
model.before = m; | |
} | |
After afterAnnotation = m.getAnnotation(After.class); | |
if (afterAnnotation != null) { | |
model.after = m; | |
} | |
BeforeClass beforeClassAnnotation = m.getAnnotation(BeforeClass.class); | |
if (beforeClassAnnotation != null) { | |
model.beforeClass = m; | |
} | |
AfterClass afterClassAnnotation = m.getAnnotation(AfterClass.class); | |
if (afterClassAnnotation != null) { | |
model.afterClass = m; | |
} | |
} | |
return model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment