Skip to content

Commit cb48e0f

Browse files
committed
#2 - Support for java.sql.Connection
1 parent d444de4 commit cb48e0f

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.project
33
/.settings/
44
/target/
5+
/.factorypath

pom.xml

+33-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
<slf4j.version>1.7.30</slf4j.version>
5656
<logback.version>1.2.3</logback.version>
5757
<assertj.version>3.11.1</assertj.version>
58-
<junit.version>5.3.2</junit.version>
58+
<junit.version>5.5.2</junit.version>
59+
<powermock.version>2.0.2</powermock.version>
60+
<easymock.version>4.0.2</easymock.version>
5961
<lombok.version>1.18.10</lombok.version>
6062
<jacoco.version>0.8.5</jacoco.version>
6163
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -91,6 +93,36 @@
9193
<version>${junit.version}</version>
9294
<scope>test</scope>
9395
</dependency>
96+
<dependency>
97+
<groupId>org.junit.vintage</groupId>
98+
<artifactId>junit-vintage-engine</artifactId>
99+
<version>${junit.version}</version>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>org.easymock</groupId>
104+
<artifactId>easymock</artifactId>
105+
<version>${easymock.version}</version>
106+
<scope>test</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>org.powermock</groupId>
110+
<artifactId>powermock-api-easymock</artifactId>
111+
<version>${powermock.version}</version>
112+
<scope>test</scope>
113+
</dependency>
114+
<dependency>
115+
<groupId>org.powermock</groupId>
116+
<artifactId>powermock-module-junit4</artifactId>
117+
<version>${powermock.version}</version>
118+
<scope>test</scope>
119+
<exclusions>
120+
<exclusion>
121+
<groupId>junit</groupId>
122+
<artifactId>junit</artifactId>
123+
</exclusion>
124+
</exclusions>
125+
</dependency>
94126
<dependency>
95127
<groupId>org.jacoco</groupId>
96128
<artifactId>org.jacoco.agent</artifactId>

src/main/java/de/jaggl/sqlbuilder/queries/Query.java

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import static de.jaggl.sqlbuilder.utils.Indentation.disabled;
44

55
import java.io.PrintStream;
6+
import java.sql.Connection;
7+
import java.sql.PreparedStatement;
8+
import java.sql.SQLException;
69

710
import de.jaggl.sqlbuilder.dialect.Dialect;
811
import de.jaggl.sqlbuilder.utils.Indentation;
@@ -155,4 +158,19 @@ default void println(PrintStream printStream, String dialectName, Indentation in
155158
{
156159
println(printStream, Dialect.forName(dialectName), indentation);
157160
}
161+
162+
default PreparedStatement prepare(Connection connection) throws SQLException
163+
{
164+
return connection.prepareStatement(build());
165+
}
166+
167+
default PreparedStatement prepare(Connection connection, Dialect dialect) throws SQLException
168+
{
169+
return connection.prepareStatement(build(dialect));
170+
}
171+
172+
default PreparedStatement prepare(Connection connection, String dialectName) throws SQLException
173+
{
174+
return connection.prepareStatement(build(dialectName));
175+
}
158176
}

src/test/java/de/jaggl/sqlbuilder/queries/QueryTest.java

+63-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
import static de.jaggl.sqlbuilder.queries.Queries.select;
55
import static de.jaggl.sqlbuilder.schema.Table.create;
66
import static de.jaggl.sqlbuilder.utils.Indentation.enabled;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.easymock.EasyMock.expect;
9+
import static org.powermock.api.easymock.PowerMock.createStrictMock;
10+
import static org.powermock.api.easymock.PowerMock.replayAll;
11+
import static org.powermock.api.easymock.PowerMock.verifyAll;
712

8-
import org.junit.jupiter.api.Test;
13+
import java.sql.Connection;
14+
import java.sql.PreparedStatement;
15+
import java.sql.SQLException;
916

10-
class QueryTest
17+
import org.junit.Test;
18+
19+
public class QueryTest
1120
{
1221
@Test
13-
void testPrintAndPrintln()
22+
public void testPrintAndPrintln()
1423
{
1524
var query = select().from(create("table"));
1625

@@ -52,4 +61,55 @@ void testPrintAndPrintln()
5261
System.out.println();
5362
query.println(System.out, "MYSQL", enabled());
5463
}
64+
65+
@Test
66+
public void testPrepare() throws SQLException
67+
{
68+
var query = select().from(create("table"));
69+
70+
try (var connection = createStrictMock(Connection.class))
71+
{
72+
expect(connection.prepareStatement("SELECT * FROM `table`")).andReturn(createStrictMock(PreparedStatement.class));
73+
connection.close();
74+
75+
replayAll();
76+
assertThat(query.prepare(connection)).isInstanceOf(PreparedStatement.class);
77+
}
78+
79+
verifyAll();
80+
}
81+
82+
@Test
83+
public void testPrepareWithDialect() throws SQLException
84+
{
85+
var query = select().from(create("table"));
86+
87+
try (var connection = createStrictMock(Connection.class))
88+
{
89+
expect(connection.prepareStatement("SELECT * FROM `table`")).andReturn(createStrictMock(PreparedStatement.class));
90+
connection.close();
91+
92+
replayAll();
93+
assertThat(query.prepare(connection, MYSQL)).isInstanceOf(PreparedStatement.class);
94+
}
95+
96+
verifyAll();
97+
}
98+
99+
@Test
100+
public void testPrepareWithDialectName() throws SQLException
101+
{
102+
var query = select().from(create("table"));
103+
104+
try (var connection = createStrictMock(Connection.class))
105+
{
106+
expect(connection.prepareStatement("SELECT * FROM `table`")).andReturn(createStrictMock(PreparedStatement.class));
107+
connection.close();
108+
109+
replayAll();
110+
assertThat(query.prepare(connection, "MYSQL")).isInstanceOf(PreparedStatement.class);
111+
}
112+
113+
verifyAll();
114+
}
55115
}

0 commit comments

Comments
 (0)