-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJoinTest.java
More file actions
126 lines (115 loc) · 4.2 KB
/
Copy pathJoinTest.java
File metadata and controls
126 lines (115 loc) · 4.2 KB
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
package jaskell.sql;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static jaskell.sql.SQL.count;
import static jaskell.sql.SQL.func;
import static jaskell.sql.SQL.insert;
import static jaskell.sql.SQL.l;
import static jaskell.sql.SQL.n;
import static jaskell.sql.SQL.p;
import static jaskell.sql.SQL.select;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
public class JoinTest {
private static final String url = "jdbc:sqlite::memory:";
private static final String table = "test";
static private Connection conn;
static private Statement ist = insert().into("test", "pid, content")
.values(p("pid", int.class),
p("content", String.class));
@BeforeAll
static public void init() {
try {
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established.");
conn.prepareStatement(
"create table test(id integer primary key autoincrement, " +
"pid integer references test(id), " +
"content text)")
.execute();
Statement ins = ist.cache();
Query queryLastId = select(func("last_insert_rowid"));
int last = 1;
ins.setParameter("content", "one line.");
for(int i = 0; i < 10; i++){
ins.setParameter("pid", last);
ins.execute(conn);
last = queryLastId.scalar(conn, Integer.class).orElse(0);
if(last == 0){
throw new IllegalStateException("can't get new id which insert");
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
@AfterAll
static public void close(){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void initTest(){
try {
Query q = select(count()).from("test");
Optional<Integer> c = q.scalar(conn, Integer.class);
Assertions.assertTrue(c.isPresent());
Assertions.assertEquals( 10, c.get().intValue(), "expect 10 lines of data");
} catch (SQLException e) {
Assertions.fail(e.getMessage());
}
}
@Test
public void joinSelfTest(){
Query q = select("l.id, r.id, l.content, r.content")
.from(n("test").as("l"))
.join(n("test").as("r")).on(l("l.id").eq(l("r.pid")))
.where(l("l.id").ne(l("r.id")));
try(PreparedStatement statement = q.prepare(conn);
ResultSet rs = q.query(statement)){
while (rs.next()){
Assertions.assertEquals(rs.getString(3), rs.getString(4));
Assertions.assertEquals(rs.getInt(1)+1, rs.getInt(2));
}
} catch (SQLException e) {
Assertions.fail(e.getMessage());
}
}
@Test
public void leftJoinTest0(){
Query q = select("l.id, r.id, l.content, r.content")
.from(n("test").as("l")
.left().join(n("test").as("r")).on(l("l.id=r.pid")))
.where(l("r.id").isNull());
try(PreparedStatement statement = q.prepare(conn);
ResultSet rs = q.query(statement)){
while (rs.next()){
Assertions.assertNull(rs.getObject(2));
}
} catch (SQLException e) {
Assertions.fail(e.getMessage());
}
}
@Test
public void leftJoinTest1(){
Query q = select("r.id")
.from(n("test").as("l")
.left().join(n("test").as("r")).on(l("l.id=r.pid")))
.where(l("r.id").isNull());
try{
Assertions.assertFalse(q.scalar(conn).isPresent());
} catch (SQLException e) {
Assertions.fail(e.getMessage());
}
}
}