Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added routine to read sql file.
  • Loading branch information
javadev committed Nov 30, 2021
commit 1a4b6167b6ee2d8477301f3410678518391117ca
6 changes: 2 additions & 4 deletions src/main/java/g0101_0200/s0175_combine_two_tables/script.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Write your MySQL query statement below
select distinct num as ConsecutiveNums from
(select num, lag(num,1) over() as l1, lag(num,2) over() as l2
from Logs) con_thr
where num = l1 and num = l2
SELECT FirstName, LastName, City, State
FROM Person LEFT JOIN Address USING (PersonId)
15 changes: 12 additions & 3 deletions src/test/java/g0101_0200/s0175_combine_two_tables/MysqlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.stream.Collectors;
import org.junit.Rule;
import org.junit.Test;
import org.zapodot.junit.db.EmbeddedDatabaseRule;
Expand All @@ -34,14 +38,19 @@ public class MysqlTest {
.build();

@Test
public void testScript() throws SQLException {
public void testScript() throws SQLException, FileNotFoundException {
try (final Connection connection =
DriverManager.getConnection(embeddedDatabaseRule.getConnectionJdbcUrl())) {
try (final Statement statement = connection.createStatement();
final ResultSet resultSet =
statement.executeQuery(
"SELECT FirstName, LastName, City, State\n"
+ "FROM Person LEFT JOIN Address USING (PersonId)")) {
new BufferedReader(
new FileReader(
"src/main/java/g0101_0200/"
+ "s0175_combine_two_tables/script.sql"))
.lines()
.collect(Collectors.joining("\n"))
.replaceAll("#.*?\\r?\\n", ""))) {
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("Allen"));
assertThat(resultSet.getNString(2), equalTo("Wang"));
Expand Down