Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}
92 changes: 3 additions & 89 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.33</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spekframework.spek2</groupId>
<artifactId>spek-dsl-jvm</artifactId>
<version>${spek_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spekframework.spek2</groupId>
<artifactId>spek-runner-junit5</artifactId>
<version>${spek_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.amshove.kluent</groupId>
<artifactId>kluent</artifactId>
<version>1.68</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand All @@ -121,6 +75,7 @@
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -130,6 +85,7 @@
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
Expand All @@ -143,6 +99,7 @@
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
Expand Down Expand Up @@ -178,40 +135,6 @@
</configuration>
</plugin>

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
Expand All @@ -227,15 +150,6 @@
<style>GOOGLE</style>
</googleJavaFormat>
</java>
<kotlin>
<includes>
<include>src/test/kotlin/**/*.kt</include>
</includes>
<ktfmt>
<version>0.39</version>
<style>GOOGLE</style>
</ktfmt>
</kotlin>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public DialectConfig dialectConfig() {
.indexedPlaceholderTypes(Collections.singletonList("?"))
.namedPlaceholderTypes(Collections.emptyList())
.lineCommentTypes(Arrays.asList("--"))
.operators(Collections.singletonList("||"))
.operators(List.of("||", "!="))
.build();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.github.vertical_blank.sqlformatter;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.vertical_blank.sqlformatter.languages.Dialect;
import org.junit.jupiter.api.Test;

public class MariaDbFormatterTest {

private final SqlFormatter.Formatter formatter = SqlFormatter.of(Dialect.MariaDb);

@Test
public void testSupportsCase() {
String result = formatter.format("CASE WHEN a THEN b ELSE c END");
String expected = "CASE\n WHEN a THEN b\n ELSE c\nEND";
assertEquals(expected, result);
}

@Test
public void testSupportsCreateTable() {
String result = formatter.format("CREATE TABLE foo (id INT PRIMARY KEY, name VARCHAR(100))");
String expected = "CREATE TABLE foo (id INT PRIMARY KEY, name VARCHAR(100))";
assertEquals(expected, result);
}

@Test
public void testSupportsAlterTable() {
String result = formatter.format("ALTER TABLE foo ADD COLUMN bar INT");
String expected = "ALTER TABLE\n foo\nADD\n COLUMN bar INT";
assertEquals(expected, result);
}

@Test
public void testSupportsStrings() {
String result = formatter.format("SELECT \"foo\", 'bar', `baz` FROM table");
String expected = "SELECT\n \"foo\",\n 'bar',\n `baz`\nFROM\n table";
assertEquals(expected, result);
}

@Test
public void testSupportsBetween() {
String result = formatter.format("SELECT * FROM table WHERE a BETWEEN 1 AND 10");
String expected = "SELECT\n *\nFROM\n table\nWHERE\n a BETWEEN 1 AND 10";
assertEquals(expected, result);
}

@Test
public void testSupportsOperators() {
String result =
formatter.format("SELECT a % b & c | d ^ e ~ f != g ! h <=> i << j >> k && l || m := n");
String expected = "SELECT\n a % b & c | d ^ e ~ f != g ! h <=> i << j >> k && l || m := n";
assertEquals(expected, result);
}

@Test
public void testSupportsJoin() {
String result =
formatter.format("SELECT * FROM table1 STRAIGHT_JOIN table2 NATURAL LEFT JOIN table3");
String expected =
"SELECT\n *\nFROM\n table1\n STRAIGHT_JOIN table2\n NATURAL LEFT JOIN table3";
assertEquals(expected, result);
}

@Test
public void testSupportsHashComments() {
String result = formatter.format("SELECT a # comment\nFROM b # comment");
String expected = "SELECT\n a # comment\nFROM\n b # comment";
assertEquals(expected, result);
}

@Test
public void testSupportsVariables() {
String result = formatter.format("SELECT @foo, @bar");
String expected = "SELECT\n @foo,\n @bar";
assertEquals(expected, result);
}

@Test
public void testSupportsSettingVariables() {
String result = formatter.format("SET @foo := (SELECT * FROM tbl);");
String expected = "SET\n @foo := (\n SELECT\n *\n FROM\n tbl\n );";
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.github.vertical_blank.sqlformatter;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.github.vertical_blank.sqlformatter.languages.Dialect;
import java.util.Map;
import org.junit.jupiter.api.Test;

public class PostgreSqlFormatterTest {

private final SqlFormatter.Formatter formatter = SqlFormatter.of(Dialect.PostgreSql);

@Test
public void testSupportsCase() {
String result = formatter.format("CASE WHEN a THEN b ELSE c END");
String expected = "CASE\n WHEN a THEN b\n ELSE c\nEND";
assertEquals(expected, result);
}

@Test
public void testSupportsCreateTable() {
String result = formatter.format("CREATE TABLE foo (id INT PRIMARY KEY, name VARCHAR(100))");
String expected = "CREATE TABLE foo (id INT PRIMARY KEY, name VARCHAR(100))";
assertEquals(expected, result);
}

@Test
public void testSupportsAlterTable() {
String result = formatter.format("ALTER TABLE foo ADD COLUMN bar INT");
String expected = "ALTER TABLE\n foo\nADD\n COLUMN bar INT";
assertEquals(expected, result);
}

@Test
public void testSupportsStrings() {
String result = formatter.format("SELECT \"foo\", 'bar', $$baz$$ FROM table");
String expected = "SELECT\n \"foo\",\n 'bar',\n $$baz$$\nFROM\n table";
assertEquals(expected, result);
}

@Test
public void testSupportsBetween() {
String result = formatter.format("SELECT * FROM table WHERE a BETWEEN 1 AND 10");
String expected = "SELECT\n *\nFROM\n table\nWHERE\n a BETWEEN 1 AND 10";
assertEquals(expected, result);
}

@Test
public void testSupportsSchema() {
String result = formatter.format("SELECT * FROM schema.table");
String expected = "SELECT\n *\nFROM\n schema.table";
assertEquals(expected, result);
}

@Test
public void testSupportsOperators() {
String result =
formatter.format(
"SELECT a % b ^ c ! d !! e @ f != g & h | i ~ j # k << l >> m ||/ n |/ o :: p ->> q -> r ~~* s ~~ t !~~* u !~~ v ~* w !~* x !~ y @@ z @@@ aa");
String expected =
"SELECT\n a % b ^ c ! d !! e @ f != g & h | i ~ j # k << l >> m ||/ n |/ o :: p ->> q -> r ~~* s ~~ t !~~* u !~~ v ~* w !~* x !~ y @@ z @@@ aa";
assertEquals(expected, result);
}

@Test
public void testSupportsJoin() {
String result = formatter.format("SELECT * FROM table1 JOIN table2 ON table1.id = table2.id");
String expected = "SELECT\n *\nFROM\n table1\n JOIN table2 ON table1.id = table2.id";
assertEquals(expected, result);
}

@Test
public void testSupportsDollarPlaceholders() {
String result = formatter.format("SELECT $1, $2 FROM tbl");
String expected = "SELECT\n $1,\n $2\nFROM\n tbl";
assertEquals(expected, result);
}

@Test
public void testReplacesDollarPlaceholdersWithParamValues() {
String result =
formatter.format(
"SELECT $1, $2 FROM tbl", Map.of("1", "\"variable value\"", "2", "\"blah\""));
String expected = "SELECT\n \"variable value\",\n \"blah\"\nFROM\n tbl";
assertEquals(expected, result);
}

@Test
public void testSupportsNamePlaceholders() {
String result = formatter.format("foo = :bar");
String expected = "foo = :bar";
assertEquals(expected, result);
}

@Test
public void testReplacesNamePlaceholdersWithParamValues() {
String result =
formatter.format(
"foo = :bar AND :\"field\" = 10 OR col = :'val'",
Map.of("bar", "'Hello'", "field", "some_col", "val", 7));
String expected = "foo = 'Hello'\nAND some_col = 10\nOR col = 7";
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ public void withLambdasParams() {
format,
"SELECT\n" + " aggregate(array(1, 2, 3), 0, (acc, x) -> acc + x, acc -> acc * 10);");
}

@Test
public void withNotEquals() {
final String format = SqlFormatter.format("SELECT * FROM TEST WHERE ABC != '4'");
assertEquals(format, "SELECT\n" + " *\n" + "FROM\n" + " TEST\n" + "WHERE\n" + " ABC != '4'");
}
}
Loading