├── .github
└── workflows
│ ├── publish.yml
│ └── test.yml
├── .gitignore
├── LICENCE
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── renovate.json
├── settings.gradle
└── src
├── main
├── java
│ └── me
│ │ └── mrnavastar
│ │ └── sqlib
│ │ ├── SQLib.java
│ │ ├── api
│ │ ├── DataContainer.java
│ │ ├── DataStore.java
│ │ ├── database
│ │ │ ├── AuthenticatedDatabase.java
│ │ │ ├── Database.java
│ │ │ ├── MySQL.java
│ │ │ ├── PostgreSQL.java
│ │ │ └── SQLite.java
│ │ └── types
│ │ │ ├── AdventureTypes.java
│ │ │ ├── GsonTypes.java
│ │ │ ├── JavaTypes.java
│ │ │ ├── MinecraftTypes.java
│ │ │ └── SQLibType.java
│ │ └── impl
│ │ ├── ByteParser.java
│ │ ├── SQLConnection.java
│ │ ├── SQLPrimitive.java
│ │ ├── SoundParser.java
│ │ ├── TextParser.java
│ │ └── config
│ │ ├── Config.java
│ │ ├── Fabric.java
│ │ ├── NonMinecraft.java
│ │ ├── Quilt.java
│ │ └── Velocity.java
└── resources
│ ├── assets
│ └── sqlib
│ │ └── icon.png
│ ├── fabric.mod.json
│ ├── sqlib.toml
│ └── velocity-plugin.json
└── testMod
├── java
└── me
│ └── mrnavastar
│ └── sqlib
│ └── TestMod.java
└── resources
└── fabric.mod.json
/.github/workflows/publish.yml:
--------------------------------------------------------------------------------
1 | name: Release New Version
2 | on:
3 | release:
4 | types: [published]
5 |
6 | jobs:
7 | build:
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/checkout@v4
11 | - uses: actions/setup-java@v4
12 | with:
13 | distribution: temurin
14 | java-version: 21
15 |
16 | - name: Set Version
17 | run: |
18 | sed -i 's/debug-build/${{ github.event.release.tag_name }}/g' src/main/resources/fabric.mod.json
19 | sed -i 's/debug-build/${{ github.event.release.tag_name }}/g' src/main/resources/velocity-plugin.json
20 |
21 | - name: Setup Gradle
22 | uses: gradle/gradle-build-action@v3
23 |
24 | - name: Execute Gradle build
25 | run: ./gradlew build
26 |
27 | - name: Rename Files
28 | run: |
29 | mv ${{ github.workspace }}/build/libs/sqlib-all.jar ${{ github.workspace }}/sqlib-${{ github.event.release.tag_name }}.jar;
30 | mv ${{ github.workspace }}/build/libs/sqlib-javadoc.jar ${{ github.workspace }}/sqlib-${{ github.event.release.tag_name }}-javadoc.jar;
31 | mv ${{ github.workspace }}/build/libs/sqlib-sources.jar ${{ github.workspace }}/sqlib-${{ github.event.release.tag_name }}-sources.jar;
32 |
33 | - name: Publish Release
34 | uses: Kir-Antipov/mc-publish@v3.3.0
35 | with:
36 | modrinth-token: ${{ secrets.MODRINTH_TOKEN }}
37 | modrinth-featured: true
38 | files: |
39 | ${{ github.workspace }}/sqlib-${{ github.event.release.tag_name }}.jar
40 | ${{ github.workspace }}/sqlib-${{ github.event.release.tag_name }}-javadoc.jar
41 | ${{ github.workspace }}/sqlib-${{ github.event.release.tag_name }}-sources.jar
42 | java: 21
43 | game-versions: |
44 | >=1.16.5
45 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: Run Tests
2 | on:
3 | push
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 | steps:
9 | - uses: actions/checkout@v4
10 | - uses: actions/setup-java@v4
11 | with:
12 | distribution: temurin
13 | java-version: 21
14 |
15 | - name: Setup Gradle
16 | uses: gradle/gradle-build-action@v3
17 |
18 | - name: Run Test Mod
19 | run: ./gradlew runTestMod
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # User-specific stuff
2 | .idea/
3 |
4 | *.iml
5 | *.ipr
6 | *.iws
7 |
8 | # IntelliJ
9 | out/
10 | # mpeltonen/sbt-idea plugin
11 | .idea_modules/
12 |
13 | # JIRA plugin
14 | atlassian-ide-plugin.xml
15 |
16 | # Compiled class file
17 | *.class
18 |
19 | # Log file
20 | *.log
21 |
22 | # BlueJ files
23 | *.ctxt
24 |
25 | # Package Files #
26 | *.jar
27 | *.war
28 | *.nar
29 | *.ear
30 | *.zip
31 | *.tar.gz
32 | *.rar
33 |
34 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
35 | hs_err_pid*
36 |
37 | *~
38 |
39 | # temporary files which can be created if a process still has a handle open of a deleted file
40 | .fuse_hidden*
41 |
42 | # KDE directory preferences
43 | .directory
44 |
45 | # Linux trash folder which might appear on any partition or disk
46 | .Trash-*
47 |
48 | # .nfs files are created when an open file is removed but is still being accessed
49 | .nfs*
50 |
51 | # General
52 | .DS_Store
53 | .AppleDouble
54 | .LSOverride
55 |
56 | # Icon must end with two \r
57 | Icon
58 |
59 | # Thumbnails
60 | ._*
61 |
62 | # Files that might appear in the root of a volume
63 | .DocumentRevisions-V100
64 | .fseventsd
65 | .Spotlight-V100
66 | .TemporaryItems
67 | .Trashes
68 | .VolumeIcon.icns
69 | .com.apple.timemachine.donotpresent
70 |
71 | # Directories potentially created on remote AFP share
72 | .AppleDB
73 | .AppleDesktop
74 | Network Trash Folder
75 | Temporary Items
76 | .apdisk
77 |
78 | # Windows thumbnail cache files
79 | Thumbs.db
80 | Thumbs.db:encryptable
81 | ehthumbs.db
82 | ehthumbs_vista.db
83 |
84 | # Dump file
85 | *.stackdump
86 |
87 | # Folder config file
88 | [Dd]esktop.ini
89 |
90 | # Recycle Bin used on file shares
91 | $RECYCLE.BIN/
92 |
93 | # Windows Installer files
94 | *.cab
95 | *.msi
96 | *.msix
97 | *.msm
98 | *.msp
99 |
100 | # Windows shortcuts
101 | *.lnk
102 |
103 | .gradle
104 | build/
105 |
106 | # Ignore Gradle GUI config
107 | gradle-app.setting
108 |
109 | # Cache of project
110 | .gradletasknamecache
111 |
112 | **/build/
113 |
114 | # Common working directory
115 | run/
116 |
117 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
118 | !gradle-wrapper.jar
119 |
--------------------------------------------------------------------------------
/LICENCE:
--------------------------------------------------------------------------------
1 | Creative Commons Legal Code
2 |
3 | CC0 1.0 Universal
4 |
5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12 | HEREUNDER.
13 |
14 | Statement of Purpose
15 |
16 | The laws of most jurisdictions throughout the world automatically confer
17 | exclusive Copyright and Related Rights (defined below) upon the creator
18 | and subsequent owner(s) (each and all, an "owner") of an original work of
19 | authorship and/or a database (each, a "Work").
20 |
21 | Certain owners wish to permanently relinquish those rights to a Work for
22 | the purpose of contributing to a commons of creative, cultural and
23 | scientific works ("Commons") that the public can reliably and without fear
24 | of later claims of infringement build upon, modify, incorporate in other
25 | works, reuse and redistribute as freely as possible in any form whatsoever
26 | and for any purposes, including without limitation commercial purposes.
27 | These owners may contribute to the Commons to promote the ideal of a free
28 | culture and the further production of creative, cultural and scientific
29 | works, or to gain reputation or greater distribution for their Work in
30 | part through the use and efforts of others.
31 |
32 | For these and/or other purposes and motivations, and without any
33 | expectation of additional consideration or compensation, the person
34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35 | is an owner of Copyright and Related Rights in the Work, voluntarily
36 | elects to apply CC0 to the Work and publicly distribute the Work under its
37 | terms, with knowledge of his or her Copyright and Related Rights in the
38 | Work and the meaning and intended legal effect of CC0 on those rights.
39 |
40 | 1. Copyright and Related Rights. A Work made available under CC0 may be
41 | protected by copyright and related or neighboring rights ("Copyright and
42 | Related Rights"). Copyright and Related Rights include, but are not
43 | limited to, the following:
44 |
45 | i. the right to reproduce, adapt, distribute, perform, display,
46 | communicate, and translate a Work;
47 | ii. moral rights retained by the original author(s) and/or performer(s);
48 | iii. publicity and privacy rights pertaining to a person's image or
49 | likeness depicted in a Work;
50 | iv. rights protecting against unfair competition in regards to a Work,
51 | subject to the limitations in paragraph 4(a), below;
52 | v. rights protecting the extraction, dissemination, use and reuse of data
53 | in a Work;
54 | vi. database rights (such as those arising under Directive 96/9/EC of the
55 | European Parliament and of the Council of 11 March 1996 on the legal
56 | protection of databases, and under any national implementation
57 | thereof, including any amended or successor version of such
58 | directive); and
59 | vii. other similar, equivalent or corresponding rights throughout the
60 | world based on applicable law or treaty, and any national
61 | implementations thereof.
62 |
63 | 2. Waiver. To the greatest extent permitted by, but not in contravention
64 | of, applicable law, Affirmer hereby overtly, fully, permanently,
65 | irrevocably and unconditionally waives, abandons, and surrenders all of
66 | Affirmer's Copyright and Related Rights and associated claims and causes
67 | of action, whether now known or unknown (including existing as well as
68 | future claims and causes of action), in the Work (i) in all territories
69 | worldwide, (ii) for the maximum duration provided by applicable law or
70 | treaty (including future time extensions), (iii) in any current or future
71 | medium and for any number of copies, and (iv) for any purpose whatsoever,
72 | including without limitation commercial, advertising or promotional
73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74 | member of the public at large and to the detriment of Affirmer's heirs and
75 | successors, fully intending that such Waiver shall not be subject to
76 | revocation, rescission, cancellation, termination, or any other legal or
77 | equitable action to disrupt the quiet enjoyment of the Work by the public
78 | as contemplated by Affirmer's express Statement of Purpose.
79 |
80 | 3. Public License Fallback. Should any part of the Waiver for any reason
81 | be judged legally invalid or ineffective under applicable law, then the
82 | Waiver shall be preserved to the maximum extent permitted taking into
83 | account Affirmer's express Statement of Purpose. In addition, to the
84 | extent the Waiver is so judged Affirmer hereby grants to each affected
85 | person a royalty-free, non transferable, non sublicensable, non exclusive,
86 | irrevocable and unconditional license to exercise Affirmer's Copyright and
87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the
88 | maximum duration provided by applicable law or treaty (including future
89 | time extensions), (iii) in any current or future medium and for any number
90 | of copies, and (iv) for any purpose whatsoever, including without
91 | limitation commercial, advertising or promotional purposes (the
92 | "License"). The License shall be deemed effective as of the date CC0 was
93 | applied by Affirmer to the Work. Should any part of the License for any
94 | reason be judged legally invalid or ineffective under applicable law, such
95 | partial invalidity or ineffectiveness shall not invalidate the remainder
96 | of the License, and in such case Affirmer hereby affirms that he or she
97 | will not (i) exercise any of his or her remaining Copyright and Related
98 | Rights in the Work or (ii) assert any associated claims and causes of
99 | action with respect to the Work, in either case contrary to Affirmer's
100 | express Statement of Purpose.
101 |
102 | 4. Limitations and Disclaimers.
103 |
104 | a. No trademark or patent rights held by Affirmer are waived, abandoned,
105 | surrendered, licensed or otherwise affected by this document.
106 | b. Affirmer offers the Work as-is and makes no representations or
107 | warranties of any kind concerning the Work, express, implied,
108 | statutory or otherwise, including without limitation warranties of
109 | title, merchantability, fitness for a particular purpose, non
110 | infringement, or the absence of latent or other defects, accuracy, or
111 | the present or absence of errors, whether or not discoverable, all to
112 | the greatest extent permissible under applicable law.
113 | c. Affirmer disclaims responsibility for clearing rights of other persons
114 | that may apply to the Work or any use thereof, including without
115 | limitation any person's Copyright and Related Rights in the Work.
116 | Further, Affirmer disclaims responsibility for obtaining any necessary
117 | consents, permissions or other rights required for any use of the
118 | Work.
119 | d. Affirmer understands and acknowledges that Creative Commons is not a
120 | party to this document and has no duty or obligation with respect to
121 | this CC0 or use of the Work.
122 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)
2 | [](http://makeapullrequest.com)
3 |
4 | [
](https://modrinth.com/plugin/sqlib)
5 |
6 | [](https://ko-fi.com/G2G4DZF4D)
7 |
8 |
9 |
10 | # SQLib
11 | SQLib is the easiest way to store data for all your minecraft needs! A simple sql wrapper made with a focus on minecraft use cases.
12 |
13 | # Important Note:
14 | This library is not a full-fledged sql wrapper, and does not provide full access to many sql features.
15 | The main focus of this library is to provide an easy and simple way to store data in your mods.
16 | If you are looking for a more advanced database I recommend taking a look at something like [Nitrite](https://github.com/nitrite/nitrite-java).
17 |
18 | # Config
19 | The mod generates a config on first time start that allows you to configure the database used by all mods relying on sqlib.
20 | The default database is a sqlite database running in the sqlib directory.
21 |
22 | # Datatypes
23 | The datatypes can be accessed via `JavaTpes`, `MinecraftTypes` or the `AdventureTypes` classes. I tend to add support for new types as I run into them in my projects. If you would like one added, pleade make an issue!
24 | | Standard | Minecraft | Adventure |
25 | |----------|-------------|-----------|
26 | | Byte | Vec3i | Key |
27 | | Byte[] | BlockPos | Component |
28 | | Bool | ChunkPos | |
29 | | Short | Text | |
30 | | Int | Identifier | |
31 | | Float | Sound | |
32 | | Double | Json | |
33 | | Long | NbtElement | |
34 | | String | | |
35 | | Char | | |
36 | | Date | | |
37 | | Color | | |
38 | | UUID | | |
39 | | URI | | |
40 | | URL | | |
41 |
42 | You can also add your own custom types as seen here:
43 |
44 | # Setup
45 | In your build.gradle include:
46 | ``` gradle
47 | repositories {
48 | maven { url "https://api.modrinth.com/maven" }
49 | }
50 |
51 | dependencies {
52 | modImplementation("maven.modrinth:sqlib:3.2.2")
53 | }
54 | ```
55 |
56 | # Developer Usage
57 | This example uses the built-in database managed by sqlib. for 99% of mods, using the built-in database is good, however
58 | further down are examples for custom database management.
59 | ```java
60 | // Do not call SQLib.getDatabase() in a early mod initializer. Doing so will likely crash your mod.
61 | // Calling in or after the regular mod initializer is ok.
62 | Database db = SQLib.getDatabase();
63 |
64 | DataStore store = db.dataStore("myModId", "userdata");
65 |
66 | DataContainer playerData = store.createDataContainer();
67 | playerData.put(JavaTypes.STRING, "username", "CoolGuy123");
68 | playerData.put(MinecraftTypes.BLOCKPOS, "home", new BlockPos(304, 62, 37));
69 | playerData.put(MinecraftTypes.NBT, "nbt", new NbtCompound());
70 |
71 | System.out.println(playerdata.get(JavaTypes.STRING, "username"));
72 | System.out.println(playerdata.get(MinecraftTypes.BLOCKPOS, "home"));
73 | System.out.println(playerdata.get(MinecraftTypes.NBT, "nbt"));
74 | ```
75 |
76 | # Custom Database Management
77 | ```java
78 | Postgres db = new Postgres("name", "192.168.1.69", "3306", "cooluser", "radman");
79 | // OR
80 | MySQL db = new MySQL( "name", "192.168.1.69", "3306", "cooluser", "radman");
81 | // OR
82 | SQLite db = new SQLite("name", "some/dir");
83 | ```
84 |
85 | # Transaction Support
86 | This approach will bach sql commands into one command for faster read/writes of large amounts of data.
87 | ```java
88 | DataStore store = db.dataStore("modId", "userdata");
89 |
90 | DataContainer playerData = table.createDataContainer();
91 | playerData.transaction().put("username", "CoolGuy123").put("home", new BlockPos(304, 62, 37).commit();
92 | ```
93 |
94 | # Custom Types
95 | You can add a custom type by following the implentations in `JavaTypes`, `MinecraftTypes` and `AdventureTypes`. Then you can use it like any other native SQLib type.
96 | ```java
97 | // The SQLPrimitive is the base type to serialize to, and the two function lambdas are to serialize and deserialize from it
98 | public static final SQLibType JSON = new SQLibType<>(SQLPrimitive.STRING, JsonElement::toString, JsonParser::parseString);
99 |
100 | // You can also extend a type like this:
101 | public static final SQLibType IDENTIFIER = new SQLibType<>(SQLPrimitive.STRING, Identifier::toString, Identifier::tryParse);
102 | public static final SQLibType SOUND = new SQLibType<>(IDENTIFIER, SoundEvent::getId, SoundEvent::of);
103 | ```
104 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'fabric-loom' version '1.10.5'
3 | id "xyz.jpenilla.run-velocity" version "2.3.1"
4 | id "io.github.goooler.shadow" version "8.1.8"
5 | id 'io.freefair.lombok' version '8.13.1'
6 | }
7 |
8 | def targetJavaVersion = 21
9 | group = project.maven_group
10 |
11 | sourceSets {
12 | testMod {
13 | compileClasspath += main.compileClasspath
14 | runtimeClasspath += main.runtimeClasspath
15 | }
16 | }
17 |
18 | loom {
19 | runs {
20 | testMod {
21 | server()
22 | ideConfigGenerated project.rootProject == project
23 | name = "TestMod"
24 | source sourceSets.testMod
25 | }
26 | }
27 | }
28 |
29 | loom.mods.register("testMod") {
30 | sourceSet sourceSets.testMod
31 | }
32 |
33 |
34 | repositories {
35 | maven { url "https://maven.quiltmc.org/repository/release" }
36 | maven { url "https://api.modrinth.com/maven" }
37 | maven { url "https://repo.papermc.io/repository/maven-public/" }
38 | maven {
39 | name = "sonatype-oss-snapshots1"
40 | url = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
41 | mavenContent { snapshotsOnly() }
42 | }
43 | }
44 |
45 | dependencies {
46 | testModImplementation sourceSets.main.output
47 |
48 | // Fabric
49 | minecraft "com.mojang:minecraft:${project.minecraft_version}"
50 | mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
51 | modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
52 | modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
53 | // Quilt
54 | modCompileOnly "org.quiltmc:quilt-loader:0.28.1"
55 | //Velocity
56 | compileOnly 'com.velocitypowered:velocity-api:3.4.0-SNAPSHOT'
57 | annotationProcessor 'com.velocitypowered:velocity-api:3.4.0-SNAPSHOT'
58 | // Adventure
59 | modImplementation "net.kyori:adventure-platform-fabric:${project.adventure_version}"
60 | // Config
61 | shadow implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-toml:${project.jackson_version}")
62 | // SQL
63 | shadow implementation("com.zaxxer:HikariCP:${project.hikari_version}")
64 | shadow implementation("org.jdbi:jdbi3-core:${project.jdbi_version}")
65 | shadow implementation("org.xerial:sqlite-jdbc:${project.sqlite_version}")
66 | shadow implementation("org.mariadb.jdbc:mariadb-java-client:${project.mariadb_version}")
67 | shadow implementation("org.postgresql:postgresql:${project.postgres_version}")
68 | // Extra
69 | compileOnly "org.projectlombok:lombok:1.18.38"
70 | annotationProcessor "org.projectlombok:lombok:1.18.38"
71 | modImplementation "maven.modrinth:easy-eula:1.1.1-fabric"
72 | }
73 |
74 | shadowJar {
75 | archiveBaseName.set('sqlib')
76 | configurations = [project.configurations.shadow]
77 |
78 | relocate 'com.fasterxml', 'me.mrnavastar.sqlib.libs.com.fasterxml'
79 | relocate 'com.github', 'me.mrnavastar.sqlib.libs.com.github'
80 | relocate 'com.google.errorprone', 'me.mrnavastar.sqlib.libs.com.google.errorprone'
81 | relocate 'com.zaxxer', 'me.mrnavastar.sqlib.libs.com.zaxxer'
82 |
83 | relocate 'io', 'me.mrnavastar.sqlib.libs.io'
84 | relocate 'waffle', 'me.mrnavastar.sqlib.libs.waffle'
85 | relocate 'org.checkerframework', 'me.mrnavastar.sqlib.libs.org.checkerframework'
86 | relocate 'org.jdbi', 'me.mrnavastar.sqlib.libs.org.jdbi'
87 | relocate 'me.mrnavastar.r', 'me.mrnavastar.sqlib.libs.me.mrnavastar.r'
88 | //relocate 'org.sqlite', 'me.mrnavastar.sqlib.libs.org.sqlite'
89 | //relocate 'org.mariadb', 'me.mrnavastar.sqlib.libs.org.mariadb'
90 | //relocate 'org.postgresql', 'me.mrnavastar.sqlib.libs.org.postgresql'
91 |
92 | dependencies {
93 | mergeServiceFiles()
94 | exclude(dependency('org.slf4j:.*:.*'))
95 | exclude(dependency('net.java.dev.jna:.*:.*'))
96 | }
97 | }
98 | jar.finalizedBy('shadowJar')
99 |
100 | tasks.withType(JavaCompile).configureEach {
101 | it.options.encoding = "UTF-8"
102 | }
103 |
104 | java {
105 | def javaVersion = JavaVersion.toVersion(targetJavaVersion)
106 | if (JavaVersion.current() < javaVersion) {
107 | toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
108 | }
109 | archivesBaseName = project.archives_base_name
110 |
111 | javadoc.options.addStringOption('Xdoclint:none', '-quiet')
112 | withJavadocJar()
113 | withSourcesJar()
114 | }
115 |
116 | sourcesJar {
117 | exclude {
118 | sourceSets.main.allSource.contains it.file
119 | }
120 | from delombok
121 | }
122 |
123 | jar {
124 | from("LICENSE") {
125 | rename { "${it}_${project.archivesBaseName}" }
126 | }
127 | }
128 |
129 | tasks {
130 | runVelocity {
131 | velocityVersion(project.velocity_version)
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | org.gradle.jvmargs=-Xmx4G
2 |
3 | # Fabric
4 | # check these on https://fabricmc.net/develop/
5 | minecraft_version=1.21.4
6 | yarn_mappings=1.21.4+build.2
7 | loader_version=0.16.14
8 | fabric_version=0.112.2+1.21.4
9 |
10 | velocity_version=3.3.0-SNAPSHOT
11 | adventure_version=6.2.0
12 |
13 | # Mod Properties
14 | maven_group=mrnavastar
15 | archives_base_name=sqlib
16 |
17 | # Dependencies
18 | # https://central.sonatype.com/artifact/com.zaxxer/HikariCP
19 | hikari_version=6.3.0
20 | # https://github.com/zsoltherpai/fluent-jdbc
21 | jdbi_version=3.49.4
22 | # https://github.com/xerial/sqlite-jdbc/releases
23 | sqlite_version=3.49.1.0
24 | # https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
25 | mariadb_version=3.5.3
26 | # https://github.com/pgjdbc/pgjdbc/releases
27 | postgres_version=42.7.6
28 | # https://github.com/FasterXML/jackson-dataformats-text
29 | jackson_version=2.19.0
30 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrNavaStar/SQLib/88e7b33555d1f978e67445577c512074196846a2/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
4 | networkTimeout=10000
5 | validateDistributionUrl=true
6 | zipStoreBase=GRADLE_USER_HOME
7 | zipStorePath=wrapper/dists
8 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | #
4 | # Copyright © 2015-2021 the original authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 | # SPDX-License-Identifier: Apache-2.0
19 | #
20 |
21 | ##############################################################################
22 | #
23 | # Gradle start up script for POSIX generated by Gradle.
24 | #
25 | # Important for running:
26 | #
27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
28 | # noncompliant, but you have some other compliant shell such as ksh or
29 | # bash, then to run this script, type that shell name before the whole
30 | # command line, like:
31 | #
32 | # ksh Gradle
33 | #
34 | # Busybox and similar reduced shells will NOT work, because this script
35 | # requires all of these POSIX shell features:
36 | # * functions;
37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
39 | # * compound commands having a testable exit status, especially «case»;
40 | # * various built-in commands including «command», «set», and «ulimit».
41 | #
42 | # Important for patching:
43 | #
44 | # (2) This script targets any POSIX shell, so it avoids extensions provided
45 | # by Bash, Ksh, etc; in particular arrays are avoided.
46 | #
47 | # The "traditional" practice of packing multiple parameters into a
48 | # space-separated string is a well documented source of bugs and security
49 | # problems, so this is (mostly) avoided, by progressively accumulating
50 | # options in "$@", and eventually passing that to Java.
51 | #
52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
54 | # see the in-line comments for details.
55 | #
56 | # There are tweaks for specific operating systems such as AIX, CygWin,
57 | # Darwin, MinGW, and NonStop.
58 | #
59 | # (3) This script is generated from the Groovy template
60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
61 | # within the Gradle project.
62 | #
63 | # You can find Gradle at https://github.com/gradle/gradle/.
64 | #
65 | ##############################################################################
66 |
67 | # Attempt to set APP_HOME
68 |
69 | # Resolve links: $0 may be a link
70 | app_path=$0
71 |
72 | # Need this for daisy-chained symlinks.
73 | while
74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
75 | [ -h "$app_path" ]
76 | do
77 | ls=$( ls -ld "$app_path" )
78 | link=${ls#*' -> '}
79 | case $link in #(
80 | /*) app_path=$link ;; #(
81 | *) app_path=$APP_HOME$link ;;
82 | esac
83 | done
84 |
85 | # This is normally unused
86 | # shellcheck disable=SC2034
87 | APP_BASE_NAME=${0##*/}
88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
90 |
91 | # Use the maximum available, or set MAX_FD != -1 to use that value.
92 | MAX_FD=maximum
93 |
94 | warn () {
95 | echo "$*"
96 | } >&2
97 |
98 | die () {
99 | echo
100 | echo "$*"
101 | echo
102 | exit 1
103 | } >&2
104 |
105 | # OS specific support (must be 'true' or 'false').
106 | cygwin=false
107 | msys=false
108 | darwin=false
109 | nonstop=false
110 | case "$( uname )" in #(
111 | CYGWIN* ) cygwin=true ;; #(
112 | Darwin* ) darwin=true ;; #(
113 | MSYS* | MINGW* ) msys=true ;; #(
114 | NONSTOP* ) nonstop=true ;;
115 | esac
116 |
117 | CLASSPATH="\\\"\\\""
118 |
119 |
120 | # Determine the Java command to use to start the JVM.
121 | if [ -n "$JAVA_HOME" ] ; then
122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
123 | # IBM's JDK on AIX uses strange locations for the executables
124 | JAVACMD=$JAVA_HOME/jre/sh/java
125 | else
126 | JAVACMD=$JAVA_HOME/bin/java
127 | fi
128 | if [ ! -x "$JAVACMD" ] ; then
129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
130 |
131 | Please set the JAVA_HOME variable in your environment to match the
132 | location of your Java installation."
133 | fi
134 | else
135 | JAVACMD=java
136 | if ! command -v java >/dev/null 2>&1
137 | then
138 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
139 |
140 | Please set the JAVA_HOME variable in your environment to match the
141 | location of your Java installation."
142 | fi
143 | fi
144 |
145 | # Increase the maximum file descriptors if we can.
146 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
147 | case $MAX_FD in #(
148 | max*)
149 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
150 | # shellcheck disable=SC2039,SC3045
151 | MAX_FD=$( ulimit -H -n ) ||
152 | warn "Could not query maximum file descriptor limit"
153 | esac
154 | case $MAX_FD in #(
155 | '' | soft) :;; #(
156 | *)
157 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
158 | # shellcheck disable=SC2039,SC3045
159 | ulimit -n "$MAX_FD" ||
160 | warn "Could not set maximum file descriptor limit to $MAX_FD"
161 | esac
162 | fi
163 |
164 | # Collect all arguments for the java command, stacking in reverse order:
165 | # * args from the command line
166 | # * the main class name
167 | # * -classpath
168 | # * -D...appname settings
169 | # * --module-path (only if needed)
170 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
171 |
172 | # For Cygwin or MSYS, switch paths to Windows format before running java
173 | if "$cygwin" || "$msys" ; then
174 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
175 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
176 |
177 | JAVACMD=$( cygpath --unix "$JAVACMD" )
178 |
179 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
180 | for arg do
181 | if
182 | case $arg in #(
183 | -*) false ;; # don't mess with options #(
184 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
185 | [ -e "$t" ] ;; #(
186 | *) false ;;
187 | esac
188 | then
189 | arg=$( cygpath --path --ignore --mixed "$arg" )
190 | fi
191 | # Roll the args list around exactly as many times as the number of
192 | # args, so each arg winds up back in the position where it started, but
193 | # possibly modified.
194 | #
195 | # NB: a `for` loop captures its iteration list before it begins, so
196 | # changing the positional parameters here affects neither the number of
197 | # iterations, nor the values presented in `arg`.
198 | shift # remove old arg
199 | set -- "$@" "$arg" # push replacement arg
200 | done
201 | fi
202 |
203 |
204 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
205 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
206 |
207 | # Collect all arguments for the java command:
208 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
209 | # and any embedded shellness will be escaped.
210 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
211 | # treated as '${Hostname}' itself on the command line.
212 |
213 | set -- \
214 | "-Dorg.gradle.appname=$APP_BASE_NAME" \
215 | -classpath "$CLASSPATH" \
216 | -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
217 | "$@"
218 |
219 | # Stop when "xargs" is not available.
220 | if ! command -v xargs >/dev/null 2>&1
221 | then
222 | die "xargs is not available"
223 | fi
224 |
225 | # Use "xargs" to parse quoted args.
226 | #
227 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
228 | #
229 | # In Bash we could simply go:
230 | #
231 | # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
232 | # set -- "${ARGS[@]}" "$@"
233 | #
234 | # but POSIX shell has neither arrays nor command substitution, so instead we
235 | # post-process each arg (as a line of input to sed) to backslash-escape any
236 | # character that might be a shell metacharacter, then use eval to reverse
237 | # that process (while maintaining the separation between arguments), and wrap
238 | # the whole thing up as a single "set" statement.
239 | #
240 | # This will of course break if any of these variables contains a newline or
241 | # an unmatched quote.
242 | #
243 |
244 | eval "set -- $(
245 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
246 | xargs -n1 |
247 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
248 | tr '\n' ' '
249 | )" '"$@"'
250 |
251 | exec "$JAVACMD" "$@"
252 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 | @rem SPDX-License-Identifier: Apache-2.0
17 | @rem
18 |
19 | @if "%DEBUG%"=="" @echo off
20 | @rem ##########################################################################
21 | @rem
22 | @rem Gradle startup script for Windows
23 | @rem
24 | @rem ##########################################################################
25 |
26 | @rem Set local scope for the variables with windows NT shell
27 | if "%OS%"=="Windows_NT" setlocal
28 |
29 | set DIRNAME=%~dp0
30 | if "%DIRNAME%"=="" set DIRNAME=.
31 | @rem This is normally unused
32 | set APP_BASE_NAME=%~n0
33 | set APP_HOME=%DIRNAME%
34 |
35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
37 |
38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
40 |
41 | @rem Find java.exe
42 | if defined JAVA_HOME goto findJavaFromJavaHome
43 |
44 | set JAVA_EXE=java.exe
45 | %JAVA_EXE% -version >NUL 2>&1
46 | if %ERRORLEVEL% equ 0 goto execute
47 |
48 | echo. 1>&2
49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
50 | echo. 1>&2
51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
52 | echo location of your Java installation. 1>&2
53 |
54 | goto fail
55 |
56 | :findJavaFromJavaHome
57 | set JAVA_HOME=%JAVA_HOME:"=%
58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
59 |
60 | if exist "%JAVA_EXE%" goto execute
61 |
62 | echo. 1>&2
63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
64 | echo. 1>&2
65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
66 | echo location of your Java installation. 1>&2
67 |
68 | goto fail
69 |
70 | :execute
71 | @rem Setup the command line
72 |
73 | set CLASSPATH=
74 |
75 |
76 | @rem Execute Gradle
77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
78 |
79 | :end
80 | @rem End local scope for the variables with windows NT shell
81 | if %ERRORLEVEL% equ 0 goto mainEnd
82 |
83 | :fail
84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
85 | rem the _cmd.exe /c_ return code!
86 | set EXIT_CODE=%ERRORLEVEL%
87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1
88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
89 | exit /b %EXIT_CODE%
90 |
91 | :mainEnd
92 | if "%OS%"=="Windows_NT" endlocal
93 |
94 | :omega
95 |
--------------------------------------------------------------------------------
/renovate.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3 | "extends": [
4 | "config:base", ":disableDependencyDashboard"
5 | ]
6 | }
7 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | maven {
4 | name = 'Fabric'
5 | url = 'https://maven.fabricmc.net/'
6 | }
7 | gradlePluginPortal()
8 | mavenCentral()
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/SQLib.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib;
2 |
3 | import me.mrnavastar.sqlib.api.database.Database;
4 | import me.mrnavastar.sqlib.impl.config.Config;
5 |
6 | import java.util.List;
7 |
8 | public class SQLib {
9 |
10 | protected static Database database;
11 |
12 | public static Database getDatabase() {
13 | Config.load();
14 | return database;
15 | }
16 |
17 | public static List getDatabases() {
18 | return Database.getDatabases();
19 | }
20 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/DataContainer.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api;
2 |
3 | import lombok.*;
4 | import me.mrnavastar.sqlib.api.types.SQLibType;
5 | import me.mrnavastar.sqlib.impl.SQLConnection;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 | import java.util.Optional;
10 |
11 | @AllArgsConstructor
12 | @EqualsAndHashCode
13 | public class DataContainer {
14 |
15 | @Getter
16 | private final DataStore store;
17 | @Getter
18 | private final int id;
19 | private final SQLConnection connection;
20 |
21 | @AllArgsConstructor
22 | public static class Transaction {
23 |
24 | public record Put(SQLibType> type, String field, Object value) {}
25 |
26 | private final DataContainer container;
27 | private final SQLConnection connection;
28 | private final ArrayList puts = new ArrayList<>();
29 |
30 | public Transaction put(SQLibType type, @NonNull String field, T value) {
31 | puts.add(new Put(type, field, type.serialize(value)));
32 | return this;
33 | }
34 |
35 | public DataContainer commit() {
36 | connection.writeField(container.store, container.id, puts);
37 | return container;
38 | }
39 | }
40 |
41 | public Transaction transaction() {
42 | return new Transaction(this, connection);
43 | }
44 |
45 | public void put(SQLibType type, @NonNull String field, T value) {
46 | connection.writeField(store, id, List.of(new Transaction.Put(type, field, type.serialize(value))));
47 | }
48 |
49 | public Optional get(SQLibType type, @NonNull String field) {
50 | Object value = connection.readField(store, id, field, type.getType().getClazz());
51 | if (value == null) return Optional.empty();
52 | return Optional.ofNullable(type.deserialize(value));
53 | }
54 |
55 | public void clear(@NonNull String field) {
56 | connection.clearField(store, id, field);
57 | }
58 |
59 | /**
60 | * Delete the {@link DataContainer} from the database
61 | */
62 | public void delete() {
63 | connection.deleteRow(store, id);
64 | }
65 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/DataStore.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api;
2 |
3 | import lombok.EqualsAndHashCode;
4 | import lombok.Getter;
5 | import lombok.NonNull;
6 | import lombok.SneakyThrows;
7 | import me.mrnavastar.sqlib.api.database.Database;
8 | import me.mrnavastar.sqlib.api.types.SQLibType;
9 | import me.mrnavastar.sqlib.impl.SQLConnection;
10 |
11 | import java.util.*;
12 | import java.util.function.Consumer;
13 |
14 | /**
15 | * This class represents a table in a {@link Database}.
16 | */
17 | @EqualsAndHashCode
18 | public class DataStore {
19 |
20 | @Getter
21 | private final String modId;
22 | @Getter
23 | private final String name;
24 | @Getter
25 | private final Database database;
26 | private final SQLConnection connection;
27 |
28 | public DataStore(@NonNull String modId, @NonNull String name, @NonNull Database database, @NonNull SQLConnection connection) {
29 | this.modId = modId;
30 | this.name = name;
31 | this.database = database;
32 | this.connection = connection;
33 | connection.createTable(this);
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return modId + "_" + name;
39 | }
40 |
41 | /**
42 | * Creates a new {@link DataContainer} with a unique id.
43 | */
44 | @SneakyThrows
45 | public DataContainer createContainer() {
46 | return new DataContainer(this, connection.createRow(this), connection);
47 | }
48 |
49 | /**
50 | * Tries to get a {@link DataContainer} or creates a new {@link DataContainer} if it is missing.
51 | * Note that the created {@link DataContainer} is not guaranteed to have the same id as the one passed in.
52 | */
53 | public DataContainer getOrCreateContainer(int id) {
54 | return getContainer(id).orElseGet(this::createContainer);
55 | }
56 |
57 | /**
58 | * Tries to get a {@link DataContainer} or creates a new {@link DataContainer} if it is missing.
59 | * Note that the created {@link DataContainer} is not guaranteed to have the same id as the one passed in.
60 | *
61 | * @param onCreate A function that gets run only when the container is created. This is useful for setting things
62 | * such as a container id or other elements that are only set once.
63 | */
64 | public DataContainer getOrCreateContainer(int id, Consumer onCreate) {
65 | return getContainer(id).orElseGet(() -> {
66 | DataContainer newContainer = createContainer();
67 | onCreate.accept(newContainer);
68 | return newContainer;
69 | });
70 | }
71 |
72 | /**
73 | * Tries to get a {@link DataContainer} with a matching key value pair or creates a new {@link DataContainer} if it is missing.
74 | */
75 | public DataContainer getOrCreateContainer(@NonNull String field, @NonNull Object value) {
76 | return getContainer(field, value).orElseGet(this::createContainer);
77 | }
78 |
79 | /**
80 | * Tries to get a {@link DataContainer} with a matching key value pair or creates a new {@link DataContainer} if it is missing.
81 | *
82 | * @param onCreate A function that gets run only when the container is created. This is useful for setting things
83 | * such as a container id or other elements that are only set once.
84 | */
85 | public DataContainer getOrCreateContainer(@NonNull String field, @NonNull Object value, Consumer onCreate) {
86 | return getContainer(field, value).orElseGet(() -> {
87 | DataContainer newContainer = createContainer();
88 | onCreate.accept(newContainer);
89 | return newContainer;
90 | });
91 | }
92 |
93 | /**
94 | * Tries to get a {@link DataContainer} with a matching key value pair or creates a new {@link DataContainer} with
95 | * a matching key value pair if it is missing.
96 | */
97 | public DataContainer getOrCreateDefaultContainer(@NonNull SQLibType type, @NonNull String field, @NonNull T value) {
98 | return getOrCreateContainer(field, value, c -> c.put(type, field, value));
99 | }
100 |
101 | /**
102 | * @return A {@link DataContainer}'s with a matching key value pair or null if one does not exist.
103 | */
104 | @SneakyThrows
105 | public Optional getContainer(@NonNull String field, @NonNull Object value) {
106 | return getContainers(field, value).stream().findFirst();
107 | }
108 |
109 | /**
110 | * @return A {@link DataContainer} or null if it is missing.
111 | */
112 | @SneakyThrows
113 | public Optional getContainer(int id) {
114 | return connection.rowExists(this, id) ? Optional.of(new DataContainer(this, id, connection)) : Optional.empty();
115 | }
116 |
117 | /**
118 | * @return A list of all the {@link DataContainer}'s in this table.
119 | */
120 | @SneakyThrows
121 | public List getContainers() {
122 | // Done this way to ensure that we always match the true state of the database in the case that the database is modified externally
123 | return connection.listIds(this).stream().map(id -> new DataContainer(this, id, connection)).toList();
124 | }
125 |
126 | /**
127 | * @return A list of all the {@link DataContainer}'s with a matching key value pair.
128 | */
129 | @SneakyThrows
130 | public List getContainers(@NonNull String field, @NonNull Object value) {
131 | // Done this way to ensure that we always match the true state of the database in the case that the database is modified externally
132 | return connection.findRows(this, field, value).stream().map(id -> new DataContainer(this, id, connection)).toList();
133 | }
134 |
135 | /**
136 | * @return A list of every key present in this {@link DataStore}.
137 | */
138 | public List getKeys() {
139 | return connection.listColumns(this);
140 | }
141 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/database/AuthenticatedDatabase.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.database;
2 |
3 | import lombok.Getter;
4 |
5 | import java.util.Properties;
6 |
7 | @Getter
8 | public abstract class AuthenticatedDatabase extends Database {
9 |
10 | protected final String address;
11 | protected final String port;
12 | protected final String username;
13 | protected final String password;
14 |
15 | public AuthenticatedDatabase(String name, String address, String port, String username, String password) {
16 | super(name);
17 | this.address = address;
18 | this.port = port;
19 | this.username = username;
20 | this.password = password;
21 | connect();
22 | }
23 |
24 | @Override
25 | public Properties getConnectionProperties() {
26 | Properties properties = new Properties();
27 | properties.setProperty("user", username);
28 | properties.setProperty("password", password);
29 | properties.setProperty("ssl", "true");
30 | properties.put("config_timeout", true);
31 | return properties;
32 | }
33 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/database/Database.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.database;
2 |
3 | import lombok.Getter;
4 | import lombok.NonNull;
5 | import me.mrnavastar.sqlib.api.DataStore;
6 | import me.mrnavastar.sqlib.impl.SQLConnection;
7 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
8 | import me.mrnavastar.sqlib.impl.config.Config;
9 |
10 | import java.util.*;
11 |
12 | /**
13 | * This class can be extended to allow for new database implementations
14 | */
15 | public abstract class Database {
16 |
17 | private static final HashSet databases = new HashSet<>();
18 |
19 | static {
20 | Runtime.getRuntime().addShutdownHook(new Thread(() -> databases.forEach(Database::close)));
21 | Config.load();
22 | }
23 |
24 | public static List getDatabases() {
25 | return List.copyOf(databases);
26 | }
27 |
28 | @Getter
29 | protected final String name;
30 | protected SQLConnection connection;
31 |
32 | public Database(@NonNull String name) {
33 | this.name = name;
34 | databases.add(this);
35 | }
36 |
37 | protected abstract String getConnectionUrl();
38 |
39 | protected Properties getConnectionProperties() {
40 | return new Properties();
41 | }
42 |
43 | public abstract String getTableCreationQuery(String tableName);
44 |
45 | public String getRowCreationQuery(String rowName) {
46 | return "INSERT INTO %s DEFAULT VALUES RETURNING SQLIB_AUTO_ID".formatted(rowName);
47 | }
48 |
49 | public abstract String getColumnListQuery(String tableName);
50 |
51 | public abstract String getDataType(SQLPrimitive> dataType);
52 |
53 | protected void connect() {
54 | connection = new SQLConnection(getConnectionUrl(), getConnectionProperties());
55 | }
56 |
57 | public void close() {
58 | connection.close();
59 | }
60 |
61 | public DataStore dataStore(String modId, String name) {
62 | return new DataStore(modId, name, this, connection);
63 | }
64 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/database/MySQL.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.database;
2 |
3 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
4 |
5 | public class MySQL extends AuthenticatedDatabase {
6 |
7 | public MySQL(String name, String address, String port, String username, String password) {
8 | super(name, address, port, username, password);
9 | }
10 |
11 | @Override
12 | public String getConnectionUrl() {
13 | return "jdbc:mariadb://%s:%s/%s".formatted(address, port, name);
14 | }
15 |
16 | @Override
17 | public String getTableCreationQuery(String tableName) {
18 | return "CREATE TABLE IF NOT EXISTS %s (SQLIB_AUTO_ID INT AUTO_INCREMENT, PRIMARY KEY (SQLIB_AUTO_ID))".formatted(tableName);
19 | }
20 |
21 | @Override
22 | public String getRowCreationQuery(String rowName) {
23 | return "INSERT INTO %s () VALUES () RETURNING SQLIB_AUTO_ID".formatted(rowName);
24 | }
25 |
26 | @Override
27 | public String getColumnListQuery(String tableName) {
28 | return "SELECT * FROM %s WHERE 1 = 0".formatted(tableName);
29 | }
30 |
31 | @Override
32 | public String getDataType(SQLPrimitive> type) {
33 | return switch (type.getType()) {
34 | case BYTE, BOOL -> "TINYINT";
35 | case BYTES -> "LONGBLOB";
36 | case SHORT -> "SMALLINT";
37 | case LONG -> "BIGINT";
38 | case STRING -> "LONGTEXT";
39 | case CHAR -> "CHAR(2)";
40 | default -> type.getType().name();
41 | };
42 | }
43 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/database/PostgreSQL.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.database;
2 |
3 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
4 |
5 | public class PostgreSQL extends AuthenticatedDatabase {
6 |
7 | public PostgreSQL(String name, String address, String port, String username, String password) {
8 | super(name, address, port, username, password);
9 | }
10 |
11 | @Override
12 | public String getConnectionUrl() {
13 | return "jdbc:postgresql://%s:%s/%s".formatted(address, port, name);
14 | }
15 |
16 | @Override
17 | public String getTableCreationQuery(String tableName) {
18 | return "CREATE TABLE IF NOT EXISTS %s (SQLIB_AUTO_ID BIGSERIAL PRIMARY KEY)".formatted(tableName);
19 | }
20 |
21 | @Override
22 | public String getRowCreationQuery(String rowName) {
23 | return "INSERT INTO %s VALUES(DEFAULT) RETURNING SQLIB_AUTO_ID".formatted(rowName);
24 | }
25 |
26 | @Override
27 | public String getColumnListQuery(String tableName) {
28 | return "SELECT * FROM %s WHERE 1 = 0".formatted(tableName);
29 | }
30 |
31 | @Override
32 | public String getDataType(SQLPrimitive> type) {
33 | return switch (type.getType()) {
34 | case BYTE, BOOL -> "TINYINT";
35 | case BYTES -> "BYTEA";
36 | case SHORT -> "SMALLINT";
37 | case INT -> "INT";
38 | case DOUBLE -> "DOUBLE PRECISION";
39 | case LONG -> "BIGINT";
40 | case STRING -> "TEXT";
41 | case CHAR -> "CHAR(2)";
42 | default -> type.getType().name();
43 | };
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/database/SQLite.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.database;
2 |
3 | import lombok.Getter;
4 | import lombok.NonNull;
5 | import lombok.SneakyThrows;
6 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
7 |
8 | import java.io.File;
9 |
10 | @Getter
11 | public class SQLite extends Database {
12 |
13 | private final String directory;
14 | private Mode mode = Mode.WAL2;
15 |
16 | public enum Mode {
17 | DELETE,
18 | TRUNCATE,
19 | PERSIST,
20 | MEMORY,
21 | WAL,
22 | WAL2,
23 | OFF
24 | }
25 |
26 | public SQLite(@NonNull String name, @NonNull String directory) {
27 | super(name);
28 | this.directory = directory;
29 | connect();
30 | connection.getSql().useHandle(h -> h.execute("PRAGMA journal_mode = %s;".formatted(mode)));
31 | }
32 |
33 | @Override
34 | public String getConnectionUrl() {
35 | return "jdbc:sqlite:" + new File(directory + "/" + name + ".db");
36 | }
37 |
38 | @Override
39 | public String getTableCreationQuery(String tableName) {
40 | return "CREATE TABLE IF NOT EXISTS %s (SQLIB_AUTO_ID INTEGER PRIMARY KEY UNIQUE)".formatted(tableName);
41 | }
42 |
43 | @Override
44 | public String getColumnListQuery(String tableName) {
45 | return "SELECT NAME FROM PRAGMA_TABLE_INFO('%s')".formatted(tableName);
46 | }
47 |
48 | @Override
49 | public String getDataType(SQLPrimitive> type) {
50 | return switch (type.getType()) {
51 | case BYTE, BOOL -> "TINYINT";
52 | case BYTES -> "BLOB";
53 | case SHORT -> "SMALLINT";
54 | case LONG -> "BIGINT";
55 | case STRING -> "TEXT";
56 | case CHAR -> "CHARACTER";
57 | default -> type.getType().name();
58 | };
59 | }
60 |
61 | @SneakyThrows
62 | public void setMode(@NonNull Mode mode) {
63 | this.mode = mode;
64 | connection.getSql().useHandle(h -> h.execute("PRAGMA journal_mode = %s;".formatted(mode)));
65 | }
66 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/types/AdventureTypes.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.types;
2 |
3 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
4 | import net.kyori.adventure.key.Key;
5 | import net.kyori.adventure.text.Component;
6 | import net.kyori.adventure.text.minimessage.MiniMessage;
7 |
8 | public class AdventureTypes {
9 | public static final SQLibType KEY = new SQLibType<>(SQLPrimitive.STRING, Key::asMinimalString, Key::key);
10 | public static final SQLibType COMPONENT = new SQLibType<>(SQLPrimitive.STRING, v -> MiniMessage.miniMessage().serialize(v), v -> MiniMessage.miniMessage().deserialize(v));
11 | }
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/types/GsonTypes.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.types;
2 |
3 | import com.google.gson.*;
4 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
5 |
6 | public class GsonTypes {
7 | public static final SQLibType ELEMENT = new SQLibType<>(SQLPrimitive.STRING, JsonElement::toString, JsonParser::parseString);
8 | public static final SQLibType OBJECT = new SQLibType<>(ELEMENT, v -> v, v -> (JsonObject) v);
9 | public static final SQLibType ARRAY = new SQLibType<>(ELEMENT, v -> v, v -> (JsonArray) v);
10 | public static final SQLibType PRIMITIVE = new SQLibType<>(ELEMENT, v -> v, v -> (JsonPrimitive) v);
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/types/JavaTypes.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.types;
2 |
3 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
4 |
5 | import java.awt.*;
6 | import java.net.*;
7 | import java.util.Date;
8 |
9 | public class JavaTypes {
10 | // Primitives
11 | public static final SQLibType BYTE = new SQLibType<>(SQLPrimitive.INT, Byte::intValue, Integer::byteValue);
12 | public static final SQLibType BYTES = new SQLibType<>(SQLPrimitive.BYTES, v -> v, v -> v);
13 | public static final SQLibType BOOL = new SQLibType<>(SQLPrimitive.INT, v -> v ? 1 : 0, v -> v == 1);
14 | public static final SQLibType SHORT = new SQLibType<>(SQLPrimitive.SHORT, v -> v, v -> v);
15 | public static final SQLibType INT = new SQLibType<>(SQLPrimitive.INT, v -> v, v -> v);
16 | public static final SQLibType FLOAT = new SQLibType<>(SQLPrimitive.FLOAT, v -> v, v -> v);
17 | public static final SQLibType DOUBLE = new SQLibType<>(SQLPrimitive.DOUBLE, v -> v, v -> v);
18 | public static final SQLibType LONG = new SQLibType<>(SQLPrimitive.LONG, v -> v, v -> v);
19 | public static final SQLibType STRING = new SQLibType<>(SQLPrimitive.STRING, v -> v, v -> v);
20 | public static final SQLibType CHAR = new SQLibType<>(SQLPrimitive.CHAR, v -> v, v -> v);
21 | // Java Data Types
22 | public static final SQLibType DATE = new SQLibType<>(SQLPrimitive.LONG, Date::getTime, Date::new);
23 | public static final SQLibType COLOR = new SQLibType<>(SQLPrimitive.INT, Color::getRGB, Color::new);
24 | public static final SQLibType UUID = new SQLibType<>(SQLPrimitive.STRING, java.util.UUID::toString, java.util.UUID::fromString);
25 | public static final SQLibType URI = new SQLibType<>(SQLPrimitive.STRING, java.net.URI::toString, java.net.URI::create);
26 | public static final SQLibType URL = new SQLibType<>(URI, v -> {
27 | try {
28 | return v.toURI();
29 | } catch (URISyntaxException e) {
30 | throw new RuntimeException(e);
31 | }
32 | }, v -> {
33 | try {
34 | return v.toURL();
35 | } catch (MalformedURLException e) {
36 | throw new RuntimeException(e);
37 | }
38 | });
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/types/MinecraftTypes.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.types;
2 |
3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
4 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
5 | import me.mrnavastar.sqlib.impl.SoundParser;
6 | import me.mrnavastar.sqlib.impl.TextParser;
7 | import net.minecraft.nbt.NbtElement;
8 | import net.minecraft.nbt.StringNbtReader;
9 | import net.minecraft.sound.SoundEvent;
10 | import net.minecraft.text.Text;
11 | import net.minecraft.util.Identifier;
12 | import net.minecraft.util.math.BlockPos;
13 | import net.minecraft.util.math.ChunkPos;
14 | import net.minecraft.util.math.Vec3i;
15 |
16 | public class MinecraftTypes {
17 | public static final SQLibType VEC3I = new SQLibType<>(SQLPrimitive.LONG, v -> BlockPos.asLong(v.getX(), v.getY(), v.getZ()), v -> new Vec3i(BlockPos.unpackLongX(v), BlockPos.unpackLongY(v), BlockPos.unpackLongZ(v)));
18 | public static final SQLibType BLOCKPOS = new SQLibType<>(SQLPrimitive.LONG, BlockPos::asLong, BlockPos::fromLong);
19 | public static final SQLibType CHUNKPOS = new SQLibType<>(SQLPrimitive.LONG, ChunkPos::toLong, ChunkPos::new);
20 |
21 | public static final SQLibType TEXT = new SQLibType<>(SQLPrimitive.STRING, TextParser::textToString, TextParser::stringToText);
22 | public static final SQLibType IDENTIFIER = new SQLibType<>(SQLPrimitive.STRING, Identifier::toString, Identifier::tryParse);
23 | public static final SQLibType SOUND = new SQLibType<>(IDENTIFIER, SoundParser::getId, SoundEvent::of);
24 |
25 | public static final SQLibType NBT = new SQLibType<>(SQLPrimitive.STRING, NbtElement::toString, v -> {
26 | try {
27 | return StringNbtReader.parse(v);
28 | } catch (CommandSyntaxException e) {
29 | throw new RuntimeException(e);
30 | }
31 | });
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/me/mrnavastar/sqlib/api/types/SQLibType.java:
--------------------------------------------------------------------------------
1 | package me.mrnavastar.sqlib.api.types;
2 |
3 | import lombok.Getter;
4 | import me.mrnavastar.sqlib.impl.SQLPrimitive;
5 |
6 | import java.util.function.Function;
7 |
8 | public class SQLibType {
9 | @Getter
10 | private final SQLPrimitive> type;
11 | private final Function serializer;
12 | private final Function