Skip to content

Commit

Permalink
create 'commons-sql'
Browse files Browse the repository at this point in the history
  • Loading branch information
SfenKer committed Aug 5, 2024
1 parent 5f5a152 commit 90bf807
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: 'Bug Report'
description: 'Select if you want to report commons issue.'
title: "[BUG] "
labels: [ 'bug' ]
body:
- type: 'dropdown'
Expand All @@ -10,8 +9,9 @@ body:
description: 'With what element do you have problems?'
multiple: false
options:
- 'Inject'
- 'Reflection'
- 'inject'
- 'reflection'
- 'sql'
validations:
required: true
- type: 'input'
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rootProject.name = "commons"

module("inject")
module("reflection")
module("sql")
81 changes: 81 additions & 0 deletions sql/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import com.palantir.gradle.gitversion.VersionDetails
import groovy.lang.Closure
import java.lang.System.getenv

plugins {
id("java")
id("maven-publish")
id("com.palantir.git-version") version "3.1.0"
}

val versionDetails: Closure<VersionDetails> by extra

project.group = project.parent?.group!!
project.version = project.parent?.version!!

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

repositories {
mavenCentral()
}

dependencies {

/* HikariCP */
implementation("com.zaxxer:HikariCP:${project.property("hikaricp.version")}")

/* JetBrains Annotations */
compileOnly("org.jetbrains:annotations:${project.parent?.property("jetbrains.annotations.version")}")
annotationProcessor("org.jetbrains:annotations:${project.parent?.property("jetbrains.annotations.version")}")

/* JUnit */
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.junit.jupiter:junit-jupiter:${project.parent?.property("junit.version")}")

}

publishing {

publications {
create<MavenPublication>("publish") {
groupId = project.group as String
artifactId = project.name
version = project.version as String
from(components["java"])
}
}

repositories {
maven {
url = uri("https://repo.mrstudios.pl/public/")
credentials {
username = getenv("REPOSITORY_USER")
password = getenv("REPOSITORY_PASSWORD")
}
}
}

}

tasks {

withType<JavaCompile> {
options.encoding = "UTF-8"
}

test {
useJUnitPlatform()
testLogging {
events("passed")
}
}

build {
dependsOn(test)
if (versionDetails().branchName == "ver/latest")
finalizedBy(publish)
}

}
2 changes: 2 additions & 0 deletions sql/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Dependency Properties
hikaricp.version=5.1.0
97 changes: 97 additions & 0 deletions sql/src/main/java/pl/mrstudios/commons/sql/SqlConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package pl.mrstudios.commons.sql;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.jetbrains.annotations.NotNull;
import pl.mrstudios.commons.sql.result.SqlEntry;
import pl.mrstudios.commons.sql.result.SqlResult;
import pl.mrstudios.commons.sql.statement.SqlStatement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Collection;
import java.util.LinkedList;

import static java.lang.Class.forName;

public class SqlConnection {

private final HikariConfig config;
private HikariDataSource dataSource;

public SqlConnection(
@NotNull HikariConfig hikariConfig
) {
this.config = hikariConfig;
this.dataSource = new HikariDataSource(this.config);
}

@SuppressWarnings("all")
public @NotNull Collection<SqlResult> fetch(
@NotNull SqlStatement statement
) {

Collection<SqlResult> result = new LinkedList<>();

if (this.dataSource.isClosed())
this.dataSource = new HikariDataSource(this.config);

try (
Connection connection = this.dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(statement.query())
) {

statement.prepare(preparedStatement);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next())
result.add(handleReceivedResultSet(resultSet));
}

} catch (@NotNull Exception exception) {
throw new RuntimeException("Unable to fetch entries from database due to exception.", exception);
}

return result;

}

@SuppressWarnings("all")
public void execute(
@NotNull SqlStatement statement
) {

try (
Connection connection = this.dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(statement.query())
) {
statement.prepare(preparedStatement);
preparedStatement.execute();
} catch (@NotNull Exception exception) {
throw new RuntimeException("Unable to execute statement due to exception.", exception);
}

}

protected static @NotNull SqlResult handleReceivedResultSet(
@NotNull ResultSet resultSet
) {

try {

SqlResult result = new SqlResult();
ResultSetMetaData metaData = resultSet.getMetaData();

for (int i = 1; i <= metaData.getColumnCount(); i++)
result.add(new SqlEntry(metaData.getColumnName(i), forName(metaData.getColumnClassName(i)), resultSet.getObject(i)));

return result;

} catch (@NotNull Exception exception) {
throw new RuntimeException("Unable to handle received result due to exception.", exception);
}

}

}
31 changes: 31 additions & 0 deletions sql/src/main/java/pl/mrstudios/commons/sql/result/SqlEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pl.mrstudios.commons.sql.result;

import org.jetbrains.annotations.NotNull;

public record SqlEntry(
@NotNull String key,
@NotNull Class<?> type,
@NotNull Object object
) {

public @NotNull Long asLong() {
return (Long) this.object;
}

public @NotNull String asString() {
return (String) this.object;
}

public @NotNull Integer asInteger() {
return (Integer) this.object;
}

public @NotNull Double asDouble() {
return (Double) this.object;
}

public @NotNull Object asObject() {
return this.object;
}

}
129 changes: 129 additions & 0 deletions sql/src/main/java/pl/mrstudios/commons/sql/result/SqlResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package pl.mrstudios.commons.sql.result;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static java.util.List.copyOf;

public class SqlResult implements Collection<SqlEntry> {

private final Collection<SqlEntry> entries; {
this.entries = new LinkedList<>();
}

public @NotNull SqlEntry entry(
@NotNull String key
) {
return this.entries.stream()
.filter((entry) -> entry.key().equalsIgnoreCase(key))
.findFirst().orElseThrow();
}

public @NotNull Collection<SqlEntry> entries() {
return copyOf(this.entries);
}

@Override
public int size() {
return this.entries.size();
}

@Override
public boolean isEmpty() {
return this.entries.isEmpty();
}

@Override
public boolean contains(
@NotNull Object object
) {
return this.entries.contains(object);
}

@Override
public @NotNull Iterator<SqlEntry> iterator() {
return this.entries.iterator();
}

@Override
public void forEach(
@NotNull Consumer<? super SqlEntry> action
) {
this.entries.forEach(action);
}

@Override
public @NotNull Object[] toArray() {
return this.entries.toArray();
}

@Override
public @NotNull <T> T[] toArray(
@NotNull T[] array
) {
return this.entries.toArray(array);
}

@Override
public boolean add(
@NotNull SqlEntry sqlEntry
) {
return this.entries.add(sqlEntry);
}

@Override
public boolean remove(
@NotNull Object object
) {
return this.entries.remove(object);
}

@Override
public boolean containsAll(
@NotNull Collection<?> collection
) {
return this.entries.containsAll(collection);
}

@Override
public boolean addAll(
@NotNull Collection<? extends SqlEntry> collection
) {
return this.entries.addAll(collection);
}

@Override
public boolean removeAll(
@NotNull Collection<?> collection
) {
return this.entries.removeAll(collection);
}

@Override
public boolean retainAll(
@NotNull Collection<?> collection
) {
return this.entries.retainAll(collection);
}

@Override
public void clear() {
throw new UnsupportedOperationException("Unable to clear entities due to limited access.");
}

@Override
public @NotNull Stream<SqlEntry> stream() {
return this.entries.stream();
}

@Override
public @NotNull Stream<SqlEntry> parallelStream() {
return this.entries.parallelStream();
}

}
Loading

0 comments on commit 90bf807

Please sign in to comment.