Skip to content

Commit 90bf807

Browse files
committed
create 'commons-sql'
1 parent 5f5a152 commit 90bf807

File tree

9 files changed

+469
-3
lines changed

9 files changed

+469
-3
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: 'Bug Report'
22
description: 'Select if you want to report commons issue.'
3-
title: "[BUG] "
43
labels: [ 'bug' ]
54
body:
65
- type: 'dropdown'
@@ -10,8 +9,9 @@ body:
109
description: 'With what element do you have problems?'
1110
multiple: false
1211
options:
13-
- 'Inject'
14-
- 'Reflection'
12+
- 'inject'
13+
- 'reflection'
14+
- 'sql'
1515
validations:
1616
required: true
1717
- type: 'input'

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ rootProject.name = "commons"
1414

1515
module("inject")
1616
module("reflection")
17+
module("sql")

sql/build.gradle.kts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import com.palantir.gradle.gitversion.VersionDetails
2+
import groovy.lang.Closure
3+
import java.lang.System.getenv
4+
5+
plugins {
6+
id("java")
7+
id("maven-publish")
8+
id("com.palantir.git-version") version "3.1.0"
9+
}
10+
11+
val versionDetails: Closure<VersionDetails> by extra
12+
13+
project.group = project.parent?.group!!
14+
project.version = project.parent?.version!!
15+
16+
java {
17+
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
18+
}
19+
20+
repositories {
21+
mavenCentral()
22+
}
23+
24+
dependencies {
25+
26+
/* HikariCP */
27+
implementation("com.zaxxer:HikariCP:${project.property("hikaricp.version")}")
28+
29+
/* JetBrains Annotations */
30+
compileOnly("org.jetbrains:annotations:${project.parent?.property("jetbrains.annotations.version")}")
31+
annotationProcessor("org.jetbrains:annotations:${project.parent?.property("jetbrains.annotations.version")}")
32+
33+
/* JUnit */
34+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
35+
testImplementation("org.junit.jupiter:junit-jupiter:${project.parent?.property("junit.version")}")
36+
37+
}
38+
39+
publishing {
40+
41+
publications {
42+
create<MavenPublication>("publish") {
43+
groupId = project.group as String
44+
artifactId = project.name
45+
version = project.version as String
46+
from(components["java"])
47+
}
48+
}
49+
50+
repositories {
51+
maven {
52+
url = uri("https://repo.mrstudios.pl/public/")
53+
credentials {
54+
username = getenv("REPOSITORY_USER")
55+
password = getenv("REPOSITORY_PASSWORD")
56+
}
57+
}
58+
}
59+
60+
}
61+
62+
tasks {
63+
64+
withType<JavaCompile> {
65+
options.encoding = "UTF-8"
66+
}
67+
68+
test {
69+
useJUnitPlatform()
70+
testLogging {
71+
events("passed")
72+
}
73+
}
74+
75+
build {
76+
dependsOn(test)
77+
if (versionDetails().branchName == "ver/latest")
78+
finalizedBy(publish)
79+
}
80+
81+
}

sql/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Dependency Properties
2+
hikaricp.version=5.1.0
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package pl.mrstudios.commons.sql;
2+
3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
import org.jetbrains.annotations.NotNull;
6+
import pl.mrstudios.commons.sql.result.SqlEntry;
7+
import pl.mrstudios.commons.sql.result.SqlResult;
8+
import pl.mrstudios.commons.sql.statement.SqlStatement;
9+
10+
import java.sql.Connection;
11+
import java.sql.PreparedStatement;
12+
import java.sql.ResultSet;
13+
import java.sql.ResultSetMetaData;
14+
import java.util.Collection;
15+
import java.util.LinkedList;
16+
17+
import static java.lang.Class.forName;
18+
19+
public class SqlConnection {
20+
21+
private final HikariConfig config;
22+
private HikariDataSource dataSource;
23+
24+
public SqlConnection(
25+
@NotNull HikariConfig hikariConfig
26+
) {
27+
this.config = hikariConfig;
28+
this.dataSource = new HikariDataSource(this.config);
29+
}
30+
31+
@SuppressWarnings("all")
32+
public @NotNull Collection<SqlResult> fetch(
33+
@NotNull SqlStatement statement
34+
) {
35+
36+
Collection<SqlResult> result = new LinkedList<>();
37+
38+
if (this.dataSource.isClosed())
39+
this.dataSource = new HikariDataSource(this.config);
40+
41+
try (
42+
Connection connection = this.dataSource.getConnection();
43+
PreparedStatement preparedStatement = connection.prepareStatement(statement.query())
44+
) {
45+
46+
statement.prepare(preparedStatement);
47+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
48+
while (resultSet.next())
49+
result.add(handleReceivedResultSet(resultSet));
50+
}
51+
52+
} catch (@NotNull Exception exception) {
53+
throw new RuntimeException("Unable to fetch entries from database due to exception.", exception);
54+
}
55+
56+
return result;
57+
58+
}
59+
60+
@SuppressWarnings("all")
61+
public void execute(
62+
@NotNull SqlStatement statement
63+
) {
64+
65+
try (
66+
Connection connection = this.dataSource.getConnection();
67+
PreparedStatement preparedStatement = connection.prepareStatement(statement.query())
68+
) {
69+
statement.prepare(preparedStatement);
70+
preparedStatement.execute();
71+
} catch (@NotNull Exception exception) {
72+
throw new RuntimeException("Unable to execute statement due to exception.", exception);
73+
}
74+
75+
}
76+
77+
protected static @NotNull SqlResult handleReceivedResultSet(
78+
@NotNull ResultSet resultSet
79+
) {
80+
81+
try {
82+
83+
SqlResult result = new SqlResult();
84+
ResultSetMetaData metaData = resultSet.getMetaData();
85+
86+
for (int i = 1; i <= metaData.getColumnCount(); i++)
87+
result.add(new SqlEntry(metaData.getColumnName(i), forName(metaData.getColumnClassName(i)), resultSet.getObject(i)));
88+
89+
return result;
90+
91+
} catch (@NotNull Exception exception) {
92+
throw new RuntimeException("Unable to handle received result due to exception.", exception);
93+
}
94+
95+
}
96+
97+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pl.mrstudios.commons.sql.result;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public record SqlEntry(
6+
@NotNull String key,
7+
@NotNull Class<?> type,
8+
@NotNull Object object
9+
) {
10+
11+
public @NotNull Long asLong() {
12+
return (Long) this.object;
13+
}
14+
15+
public @NotNull String asString() {
16+
return (String) this.object;
17+
}
18+
19+
public @NotNull Integer asInteger() {
20+
return (Integer) this.object;
21+
}
22+
23+
public @NotNull Double asDouble() {
24+
return (Double) this.object;
25+
}
26+
27+
public @NotNull Object asObject() {
28+
return this.object;
29+
}
30+
31+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package pl.mrstudios.commons.sql.result;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.Collection;
6+
import java.util.Iterator;
7+
import java.util.LinkedList;
8+
import java.util.function.Consumer;
9+
import java.util.stream.Stream;
10+
11+
import static java.util.List.copyOf;
12+
13+
public class SqlResult implements Collection<SqlEntry> {
14+
15+
private final Collection<SqlEntry> entries; {
16+
this.entries = new LinkedList<>();
17+
}
18+
19+
public @NotNull SqlEntry entry(
20+
@NotNull String key
21+
) {
22+
return this.entries.stream()
23+
.filter((entry) -> entry.key().equalsIgnoreCase(key))
24+
.findFirst().orElseThrow();
25+
}
26+
27+
public @NotNull Collection<SqlEntry> entries() {
28+
return copyOf(this.entries);
29+
}
30+
31+
@Override
32+
public int size() {
33+
return this.entries.size();
34+
}
35+
36+
@Override
37+
public boolean isEmpty() {
38+
return this.entries.isEmpty();
39+
}
40+
41+
@Override
42+
public boolean contains(
43+
@NotNull Object object
44+
) {
45+
return this.entries.contains(object);
46+
}
47+
48+
@Override
49+
public @NotNull Iterator<SqlEntry> iterator() {
50+
return this.entries.iterator();
51+
}
52+
53+
@Override
54+
public void forEach(
55+
@NotNull Consumer<? super SqlEntry> action
56+
) {
57+
this.entries.forEach(action);
58+
}
59+
60+
@Override
61+
public @NotNull Object[] toArray() {
62+
return this.entries.toArray();
63+
}
64+
65+
@Override
66+
public @NotNull <T> T[] toArray(
67+
@NotNull T[] array
68+
) {
69+
return this.entries.toArray(array);
70+
}
71+
72+
@Override
73+
public boolean add(
74+
@NotNull SqlEntry sqlEntry
75+
) {
76+
return this.entries.add(sqlEntry);
77+
}
78+
79+
@Override
80+
public boolean remove(
81+
@NotNull Object object
82+
) {
83+
return this.entries.remove(object);
84+
}
85+
86+
@Override
87+
public boolean containsAll(
88+
@NotNull Collection<?> collection
89+
) {
90+
return this.entries.containsAll(collection);
91+
}
92+
93+
@Override
94+
public boolean addAll(
95+
@NotNull Collection<? extends SqlEntry> collection
96+
) {
97+
return this.entries.addAll(collection);
98+
}
99+
100+
@Override
101+
public boolean removeAll(
102+
@NotNull Collection<?> collection
103+
) {
104+
return this.entries.removeAll(collection);
105+
}
106+
107+
@Override
108+
public boolean retainAll(
109+
@NotNull Collection<?> collection
110+
) {
111+
return this.entries.retainAll(collection);
112+
}
113+
114+
@Override
115+
public void clear() {
116+
throw new UnsupportedOperationException("Unable to clear entities due to limited access.");
117+
}
118+
119+
@Override
120+
public @NotNull Stream<SqlEntry> stream() {
121+
return this.entries.stream();
122+
}
123+
124+
@Override
125+
public @NotNull Stream<SqlEntry> parallelStream() {
126+
return this.entries.parallelStream();
127+
}
128+
129+
}

0 commit comments

Comments
 (0)