Giter VIP home page Giter VIP logo

sadu's Introduction

GitHub Workflow Status GitHub Workflow Status

Sonatype Nexus (Releases) Sonatype Nexus (Snapshots)

SADU - Sql and damn utilitites

This project contains serveral things I use for working with sql.

The project is divided in several subprojects which allow to only import what you need.

It is by far not a replacement for large Frameworks like Hibernate, but a solid ground to reduce boilerplate code when you work with plain SQL like I do most of the time.

Dependency

If you want to use all projects simply import the whole thing.

implementation("de.chojo.sadu", "sadu", "version")

Database dependencies

SADU offers support for four different databases at the moment. To use them simply import:

  • sadu-postgresql
  • sadu-mariadb
  • sadu-mysql
  • sadu-sqlite

Querybuilder

SADU offers a query builder to manage resources, error handling, result set reading and dispatching of queries.

to use it import: sadu-queries

Learn how to use the query builder here

But why should I use it?

Before I give you a long talk about how much nicer the syntax and code is let me simple show you a comparison.

Without the query builder your code would ideally look like this:

class MyQueries {

    DataSource dataSource;

    MyQueries(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public CompletableFuture<Optional<Result>> getResultOld(int id) {
        return CompletableFuture.supplyAsync(() -> {
            try (Connection conn = source().getConnection(); PreparedStatement stmt = conn.prepareStatement("SELECT result FROM results WHERE id = ?")) {
                stmt.setInt(id);
                ResultSet rs = stmt.executeQuery();
                if (rs.next()) {
                    return Optional.of(new Result(rs.getString("result")));
                }
            } catch (SQLException e) {
                logger.error("Something went wrong", e);
            }
            return Optional.empty();
        });
    }
}

But using the query builder your code becomes this:

class MyQueries {
    public Optional<Result> getResultNew(int id) {
        return Query.query("SELECT result FROM results WHERE id = ?")
                .single(Call.of().bind(id))
                .map(row -> new Result(rs.getString("result")))
                .first();
    }
}

Beautiful isnt it? The query builder will enforce try with resources, set parameters in the order defined by you, read the result set and additionally handle the exceptions for you.

How does it work?

Datasource Builder

SADU offsers a data source builder to create data sources for the databases listed above.

to use it import: sadu-datasource

Note that in order to use this, you need at least one of the listed databases from above.

Learn how to use the datasource builder here

Updater

SADU offers a simple sql updater which deploys upgrade and migration scripts to your database.

to use it import: sadu-updater

Learn how to use it here

sadu's People

Contributors

aerulion avatar amejonah1200 avatar baaasty avatar balikfromua avatar dominik48n avatar doriancodes avatar goldmensch avatar rainbowdashlabs avatar renovate[bot] avatar richardwhateverr avatar simzahn001 avatar taucher2003 avatar yannicklamprecht avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sadu's Issues

Replace VersionInfo with SqlVersion

We have two very similar classes called VersionInfo and SqlVersion. Both are used in the SqlUpdater. While SqlVersion is used in external calls by other users, the VersionInfo exists purely for internal use.

Replace usage of VersionInfo with SqlVersion and remove the class.

Affected Modules:

  • sadu-updater

Implement simple Object mapper

Implement a simple object mapper which allows to map columns on an object based on the entered class.

Object mappers are registered at a central placed and will be picked based on the column names in the result set and the expected class.

The new method will be simply called map and will be in the result state as an alternative to readRow

Implement pre and post update hooks

Implement pre and post update hooks for the updater. Each version should be able to have an update hook. Those updates hook are performed via code.

Special Characters break url encoding in database creator

Characters like !, ?, &, = break the url and prevent the password from being resolved correctly. Confirmed with MariaDB at least.
Further investigation on other database types is required. It is suspected that everything is affected that uses the password inside the url.

Switch to GNU LGPLv3 license

In order to use sadu in a wider variety of projects we should switch to GNU LGPLv3 license

SPDX: LGPL-3.0-only

Add consumer for hikari config to data source creator

The DataSourceCreator allows to call hikari config methods via a delegate.
However we lag a direct access to the config itself, which limits the general availability of methods without a delegate.

Add a method which allows accessing the config via a consumer.

Affected Modules:

  • sadu-datasource
  • sadu-postgres
  • sadu-mysql
  • sadu-mariadb
  • sadu-sqlite
  • sadu-core

SQL Updater cannot execute function creations

If you try to execute a patch file (e.g. patch_2.sql), which contains a CREATE FUNCTION statement, it does not work. An SQL Syntax Error is thrown.
When the statement is executed via the command line, it does work perfectly fine.
Neither when running with using other Delimiters, nor without them does work.

Here is the statement I ran into this issue with: GIST
Setup when the issue first occurred:

  • Database: MariaDB
  • Program, which uses the SADU Updater: A PaperMC plugin executed on MC version 1.19.4; Java version 17
    Tested with:
  • Standalone java application on java 17, java 20, java 8
  • Simpler CREATE FUNCTIONS with just a simple statement inside
    All Tests performed on Server running on Debian 12.0 (Debian 12.1 not tested)

Solution I use for now:
Just remove the CREATE FUNCTION statements from the patch file and add them manually xD

Replace Runtime exteptions with proper implementation

In DataSourceCreater#loadDriverClass we wrap a exceptions in a runtime exception. This should be replaced by an own exception since usage of the RuntimeException class is not a good practice.

Affected Modules:

  • sadu-datasource

Add support for java date time api

Add support for the java date time api introduced in java 8 in the Row and ParamBuilder.

At least for:

  • LocalDate
  • LocalTime
  • LocalDateTime
  • Instant (Make sure that it is used as a unix timestamp)
  • ZonedDateTime
  • OffsetDateTime
  • OffsetTime
  • Unix Timestamp (Important for databases without a date or timestamp type like SqLite)

Additionally add support to add a current timestamp. Can be simply a delegate to the instant call.

Affected Modules:

  • sadu-mapper
  • sadu-queries

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/javadocs.yml
  • actions/checkout v4
  • actions/setup-java v4
  • JamesIves/github-pages-deploy-action v4
.github/workflows/publish.yml
  • actions/checkout v4
  • actions/setup-java v4
.github/workflows/verify.yml
  • actions/checkout v4
  • actions/setup-java v4
gradle
settings.gradle.kts
  • org.gradle.toolchains.foojay-resolver-convention 0.8.0
  • com.diffplug.spotless 6.25.0
  • com.github.johnrengelman.shadow 8.1.1
  • net.kyori.indra 3.1.3
  • net.kyori.indra.publishing 3.1.3
  • net.kyori.indra.publishing.sonatype 3.1.3
  • org.slf4j:slf4j-api 2.0.13
  • org.junit.jupiter:junit-jupiter 5.10.3
  • org.junit.jupiter:junit-jupiter-params 5.10.3
  • org.testcontainers:postgresql 1.19.8
  • org.testcontainers:mariadb 1.19.8
  • org.testcontainers:mysql 1.19.8
  • org.testcontainers:testcontainers 1.19.8
  • org.testcontainers:junit-jupiter 1.19.8
  • org.slf4j:slf4j-nop 2.0.13
  • org.postgresql:postgresql 42.7.3
  • org.mariadb.jdbc:mariadb-java-client 3.4.0
  • org.xerial:sqlite-jdbc 3.46.0.0
  • com.mysql:mysql-connector-j 9.0.0
build.gradle.kts
  • de.chojo.publishdata 1.4.0
  • org.junit.jupiter:junit-jupiter-api 5.10.3
  • org.junit.jupiter:junit-jupiter-engine 5.10.3
  • org.mockito:mockito-core 5.+
sadu-core/build.gradle.kts
  • org.jetbrains:annotations 24.1.0
sadu-datasource/build.gradle.kts
  • com.zaxxer:HikariCP 5.1.0
sadu-examples/build.gradle.kts
  • org.xerial:sqlite-jdbc 3.46.0.0
  • org.postgresql:postgresql 42.7.3
  • org.mariadb.jdbc:mariadb-java-client 3.4.0
  • mysql:mysql-connector-java 8.0.33
sadu-mapper/build.gradle.kts
sadu-mariadb/build.gradle.kts
sadu-mysql/build.gradle.kts
sadu-postgresql/build.gradle.kts
  • org.postgresql:postgresql 42.7.3
sadu-queries/build.gradle.kts
  • org.postgresql:postgresql 42.7.3
sadu-sqlite/build.gradle.kts
sadu-testing/build.gradle.kts
  • org.junit:junit-bom 5.10.3
  • org.junit:junit-bom 5.10.3
sadu-updater/build.gradle.kts
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.9

  • Check this box to trigger a request for Renovate to run again on this repository

Context Sensitive SqlUpdateBuilder

Currently we use the same builder for updates of every database.
The builder supports setting a schema, however only one of our databases even support creating a schema.

Implement a similar approach to the JDBC builder for each database type. Implementing one base builder which just gets extended by postgres might be enough already.

  • Implement Base Builder for databases without a schema
  • Implement a builder for Postgres
  • Return the builder based on the entered database type.
  • Add a method to the database type which returns a SqlUpdateBuilder implementation

Affected Modules:

  • sadu-updater (Primarily)
  • sadu-postgres (Secondary)
  • sadu-mysql
  • sadu-mariadb
  • sadu-sqlite
  • sadu-core

Assistance Needed with Upgrading from SADU v1.4.1

Hello,

I've been using SADU v1.4.1 for a while now and noticed that there are new updates available. However, I'm a bit confused about the changes. I appreciated the use of CompletableFutures in the previous version, but it seems that this feature has been removed in the newer versions.

Could you please clarify whether it is still beneficial to use CompletableFutures in combination with something like Query#query in the latest version? If not, what is the recommended approach for handling asynchronous operations now?

Thank you for your assistance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.