Compare commits
140 Commits
jdbc
...
compiled_s
| Author | SHA1 | Date | |
|---|---|---|---|
| 817912dd5b | |||
| 96aaa1ada9 | |||
| 3e3a72306b | |||
| f4e88d385b | |||
| 5c37481368 | |||
| f228ec8da2 | |||
| 7c26670a1b | |||
| aa3ca82b39 | |||
| 67fae02cb3 | |||
| 88d79b9609 | |||
| 0702e7dca8 | |||
| 9cba119337 | |||
| 1def5ae095 | |||
| ad657a6136 | |||
| aa1435bb4e | |||
| 691fd888f5 | |||
| 6f6e9f6560 | |||
| 9f6c32375e | |||
| eac77d7419 | |||
| 75066d977c | |||
| 9fc6a23ac1 | |||
| c268efb44f | |||
| f8f3273f18 | |||
| b31c8c87f1 | |||
| 56c67981db | |||
| bd473daa6c | |||
| 1af34366fc | |||
| a7e66ab8b5 | |||
| 7d39dcf248 | |||
| b96ffcc5ea | |||
| 074228807e | |||
| ee89ffcb1d | |||
| d5b8472304 | |||
| 2cd69d4501 | |||
| 79d2e52978 | |||
| 90d5f6e0d7 | |||
| 39bae86307 | |||
| 72a7aa7273 | |||
| 8da69d1288 | |||
| dab7358e39 | |||
| a2b8483967 | |||
| f835685c30 | |||
| 18e9bddff5 | |||
| 0491a444aa | |||
| 6522818cde | |||
| 4392f1ff8f | |||
| 927eff7172 | |||
| f06bdd732e | |||
| 1b3616df59 | |||
| 38170246dd | |||
| c5c271506f | |||
| c5a9efe8df | |||
| 0f10b6308e | |||
| d6d79ab4d7 | |||
| 29986895cf | |||
| b94a9ec5a7 | |||
| 4e4816eb77 | |||
| ac2afbcf93 | |||
| 2b89fee027 | |||
| 7b962a779d | |||
| a970a5b885 | |||
| 5ebcba73f4 | |||
| 05759c9b47 | |||
| bd88e7938d | |||
| 0288a57ed9 | |||
| 11cff350ed | |||
| ab1422ec16 | |||
| a4a4ef5f6f | |||
| 35d43abc4b | |||
| 71742c0c4f | |||
| 6e9175d84f | |||
| 7a0ef83211 | |||
| 23b4a17505 | |||
| fa66114d01 | |||
| a2f265d227 | |||
| aa70ea9170 | |||
| fdc6681f27 | |||
| 95e7a32747 | |||
| 69f85b5bf5 | |||
| eb3a732440 | |||
| 8a5697cd31 | |||
| b70230fd4c | |||
| 682fd5caba | |||
| 486b038ad2 | |||
| 4ad69238c7 | |||
| 4ff30e1201 | |||
| 8a848fd3dc | |||
| 414dec1f98 | |||
| b8331e7420 | |||
| 6957a325fe | |||
| 9804bf5246 | |||
| 58bb85ff1a | |||
| f98b26b22c | |||
| b06a92209c | |||
| 6aa7f5568b | |||
| a839370b27 | |||
| 6b5323f70d | |||
| 0de6f13eee | |||
| c6da94a6e5 | |||
| 93ce3831f4 | |||
| 4b28f64c43 | |||
| 35a8712eef | |||
| b200b3579a | |||
| a13ca2d954 | |||
| 36ed678c5a | |||
| c2c8b91dc5 | |||
| 9d208292a5 | |||
| 8bb3b61f2d | |||
| 5023f229ca | |||
| 55b547db75 | |||
| 377459f430 | |||
| df99bf4743 | |||
| fc4fde490f | |||
| a6d2a4d34f | |||
| 232158f85d | |||
| 3c998edb3c | |||
| 92722b0fef | |||
| 265c586198 | |||
| 440ecc8193 | |||
| 1bae960139 | |||
| 1dec96551d | |||
| dbf5fbcfc5 | |||
| 8eeb1e9e9d | |||
| c4125f1429 | |||
| 21c05f72f5 | |||
| 2a738e0595 | |||
| 6641ed78e7 | |||
| 8810c1f08d | |||
| 88c45aaf77 | |||
| 2f9f397f50 | |||
| 215c054f27 | |||
| 65d8e00902 | |||
| 2922c9309c | |||
| ae96bfd737 | |||
| e42f3b07cb | |||
| 19e98250b7 | |||
| f56c84c675 | |||
| 13f0f12c1b | |||
| 01a0000b15 | |||
| 24349fc71f |
76
.github/workflows/gradle.yml
vendored
Normal file
76
.github/workflows/gradle.yml
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
name: Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'master'
|
||||
pull_request:
|
||||
branches:
|
||||
- 'master'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 11
|
||||
|
||||
- name: Cache Gradle packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: |
|
||||
~/.gradle/caches
|
||||
~/.gradle/wrapper
|
||||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-gradle-
|
||||
|
||||
- name: Build
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: build -x test -x ktlintKotlinScriptCheck -x ktlintTestSourceSetCheck -x ktlintMainSourceSetCheck
|
||||
- name: Cleanup Gradle Cache
|
||||
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
|
||||
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
|
||||
run: |
|
||||
rm -f ~/.gradle/caches/modules-2/modules-2.lock
|
||||
rm -f ~/.gradle/caches/modules-2/gc.properties
|
||||
|
||||
- name: Lint
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: ktlintCheck
|
||||
|
||||
- name: Test
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: test
|
||||
|
||||
- name: Coverage
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: jacocoTestReport
|
||||
|
||||
- name: Cache SonarCloud packages
|
||||
uses: actions/cache@v1
|
||||
with:
|
||||
path: ~/.sonar/cache
|
||||
key: ${{ runner.os }}-sonar
|
||||
restore-keys: ${{ runner.os }}-sonar
|
||||
|
||||
- name: Push analyse to sonarqube
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: sonarqube --info
|
||||
53
.github/workflows/publish.yml
vendored
Normal file
53
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
name: Publish
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [ published ]
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 11
|
||||
|
||||
- name: Cache Gradle packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: |
|
||||
~/.gradle/caches
|
||||
~/.gradle/wrapper
|
||||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-gradle-
|
||||
|
||||
- name: Build
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: build -x test -x ktlintKotlinScriptCheck -x ktlintTestSourceSetCheck -x ktlintMainSourceSetCheck
|
||||
- name: Cleanup Gradle Cache
|
||||
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
|
||||
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
|
||||
run: |
|
||||
rm -f ~/.gradle/caches/modules-2/modules-2.lock
|
||||
rm -f ~/.gradle/caches/modules-2/gc.properties
|
||||
|
||||
- name: Test
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: test
|
||||
|
||||
- name: Publish
|
||||
uses: eskatos/gradle-command-action@v1
|
||||
with:
|
||||
gradle-version: '7.1'
|
||||
arguments: publish
|
||||
env:
|
||||
GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@
|
||||
/out/
|
||||
/build/
|
||||
.gradle
|
||||
/var/log/
|
||||
/var/log/
|
||||
/.idea/*
|
||||
4
.idea/.gitignore
generated
vendored
4
.idea/.gitignore
generated
vendored
@@ -5,5 +5,5 @@
|
||||
/dataSources.local.xml
|
||||
/compiler.xml
|
||||
/uiDesigner.xml
|
||||
/dataSources.xml
|
||||
/sonarlint/
|
||||
/sonarlint/
|
||||
/jarRepositories.xml
|
||||
22
.idea/codeStyles/Project.xml
generated
22
.idea/codeStyles/Project.xml
generated
@@ -1,9 +1,31 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
||||
<value />
|
||||
</option>
|
||||
<option name="PACKAGES_IMPORT_LAYOUT">
|
||||
<value>
|
||||
<package name="" alias="false" withSubpackages="true" />
|
||||
<package name="" alias="true" withSubpackages="true" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="SPACE_BEFORE_EXTEND_COLON" value="false" />
|
||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
|
||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
|
||||
<option name="IMPORT_NESTED_CLASSES" value="true" />
|
||||
<option name="ALLOW_TRAILING_COMMA" value="true" />
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</JetCodeStyleSettings>
|
||||
<SqlCodeStyleSettings version="6">
|
||||
<option name="KEYWORD_CASE" value="1" />
|
||||
<option name="IDENTIFIER_CASE" value="1" />
|
||||
<option name="TYPE_CASE" value="1" />
|
||||
<option name="CUSTOM_TYPE_CASE" value="1" />
|
||||
<option name="ALIAS_CASE" value="1" />
|
||||
<option name="BUILT_IN_CASE" value="1" />
|
||||
<option name="QUOTE_IDENTIFIER" value="1" />
|
||||
</SqlCodeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
<indentOptions>
|
||||
|
||||
12
.idea/dataSources.xml
generated
Normal file
12
.idea/dataSources.xml
generated
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="PostgreSQL - json_test@localhost" uuid="1191ff9a-6823-4b18-af90-483ddf0e4b69">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://localhost:35555/json_test</jdbc-url>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
2
.idea/gradle.xml
generated
2
.idea/gradle.xml
generated
@@ -8,13 +8,11 @@
|
||||
<option name="testRunner" value="PLATFORM" />
|
||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleHome" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="useAutoImport" value="true" />
|
||||
<option name="useQualifiedModuleNames" value="true" />
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
|
||||
9
.idea/kotlinc.xml
generated
Normal file
9
.idea/kotlinc.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Kotlin2JvmCompilerArguments">
|
||||
<option name="jvmTarget" value="11" />
|
||||
</component>
|
||||
<component name="KotlinJpsPluginSettings">
|
||||
<option name="version" value="1.7.20" />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/misc.xml
generated
6
.idea/misc.xml
generated
@@ -1,7 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="TaskProjectConfiguration">
|
||||
|
||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/postgres-json.iml" filepath="$PROJECT_DIR$/.idea/postgres-json.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
11
.idea/postgres-json.iml
generated
11
.idea/postgres-json.iml
generated
@@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/var" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
.idea/runConfigurations/Check.xml
generated
Normal file
8
.idea/runConfigurations/Check.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Check" type="CompoundRunConfigurationType">
|
||||
<toRun name="run DB" type="docker-deploy" />
|
||||
<toRun name="Lint" type="GradleRunConfiguration" />
|
||||
<toRun name="tests" type="JUnit" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
31
.idea/runConfigurations/Lint.xml
generated
Normal file
31
.idea/runConfigurations/Lint.xml
generated
Normal file
@@ -0,0 +1,31 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Lint" type="GradleRunConfiguration" factoryName="Gradle" singleton="false">
|
||||
<ExternalSystemSettings>
|
||||
<option name="executionName" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="externalSystemIdString" value="GRADLE" />
|
||||
<option name="scriptParameters" value="" />
|
||||
<option name="taskDescriptions">
|
||||
<list />
|
||||
</option>
|
||||
<option name="taskNames">
|
||||
<list>
|
||||
<option value="ktlintCheck" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="vmOptions" value="" />
|
||||
</ExternalSystemSettings>
|
||||
<extension name="net.ashald.envfile">
|
||||
<option name="IS_ENABLED" value="false" />
|
||||
<option name="IS_SUBST" value="false" />
|
||||
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||
<ENTRIES>
|
||||
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||
</ENTRIES>
|
||||
</extension>
|
||||
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
31
.idea/runConfigurations/Publish_To_MavenLocal.xml
generated
Normal file
31
.idea/runConfigurations/Publish_To_MavenLocal.xml
generated
Normal file
@@ -0,0 +1,31 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Publish To MavenLocal" type="GradleRunConfiguration" factoryName="Gradle" singleton="true">
|
||||
<ExternalSystemSettings>
|
||||
<option name="executionName" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="externalSystemIdString" value="GRADLE" />
|
||||
<option name="scriptParameters" value="" />
|
||||
<option name="taskDescriptions">
|
||||
<list />
|
||||
</option>
|
||||
<option name="taskNames">
|
||||
<list>
|
||||
<option value="publishToMavenLocal" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="vmOptions" value="" />
|
||||
</ExternalSystemSettings>
|
||||
<extension name="net.ashald.envfile">
|
||||
<option name="IS_ENABLED" value="false" />
|
||||
<option name="IS_SUBST" value="false" />
|
||||
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||
<ENTRIES>
|
||||
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||
</ENTRIES>
|
||||
</extension>
|
||||
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
13
.idea/runConfigurations/tests.xml
generated
13
.idea/runConfigurations/tests.xml
generated
@@ -1,6 +1,13 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="tests" type="JUnit" factoryName="JUnit">
|
||||
<module name="postgresjson.test" />
|
||||
<configuration default="false" name="tests" type="JUnit" factoryName="JUnit" singleton="false">
|
||||
<module name="postgres-json.test" />
|
||||
<useClassPathOnly />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="fr.postgresjson.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
<extension name="net.ashald.envfile">
|
||||
<option name="IS_ENABLED" value="false" />
|
||||
<option name="IS_SUBST" value="false" />
|
||||
@@ -12,7 +19,7 @@
|
||||
</ENTRIES>
|
||||
</extension>
|
||||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="11" />
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="corretto-11" />
|
||||
<option name="MAIN_CLASS_NAME" value="" />
|
||||
<option name="METHOD_NAME" value="" />
|
||||
<option name="TEST_OBJECT" value="directory" />
|
||||
|
||||
39
README.md
Normal file
39
README.md
Normal file
@@ -0,0 +1,39 @@
|
||||
PostgresJson
|
||||
============
|
||||
_Kotlin library to request postgres with native SQL queries_
|
||||
|
||||
[](https://github.com/flecomte/postgres-json/actions/workflows/gradle.yml)
|
||||
[](https://sonarcloud.io/dashboard?id=postgres-json)
|
||||
|
||||
[](https://sonarcloud.io/dashboard?id=postgres-json)
|
||||
[](https://sonarcloud.io/dashboard?id=postgres-json)
|
||||
|
||||
---
|
||||
|
||||
## What is this lib for?
|
||||
This library allows you to make sql requests and return the result in json format, then deserialize it into an entity.
|
||||
It also allows you to save an entity (INSERT) by serializing it and sending the json to the database, allowing you to insert several entities with their children, in a single request.
|
||||
|
||||
It also manages the migrations of the schema of tables and stored procedures.
|
||||
|
||||
All sql requests are handled manually for full control over what you do.
|
||||
|
||||
---
|
||||
|
||||
## The best benefits
|
||||
|
||||
* Total control of all Postgresql features and SQL language
|
||||
* More speed and flexible than an ORM
|
||||
* [Multi level request](./docs/usage/multi-level.md) (Can return multiple tables and these children in a single request)
|
||||
* Queries are written in separate native `.sql` files
|
||||
* Unit testing of SQL queries
|
||||
* Migrations are written in separate native `.sql` files
|
||||
* Automatic tested database migration and rollback
|
||||
---
|
||||
## Documentation: Table of Contents
|
||||
|
||||
* [Installation](./docs/installation.md)
|
||||
* [Migrations](./docs/migrations/migrations.md)
|
||||
* [Usage](./docs/usage/usage.md)
|
||||
* [How that works](./docs/call%20function.png) (Diagram)
|
||||
* [How to begin](./docs/checklist.md)
|
||||
127
build.gradle.kts
127
build.gradle.kts
@@ -1,41 +1,124 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
val containerAlwaysOn: String by project
|
||||
val disableLint: String by project
|
||||
val projectName: String by project
|
||||
|
||||
plugins {
|
||||
jacoco
|
||||
|
||||
id("maven-publish")
|
||||
id("org.jetbrains.kotlin.jvm") version "1.3.31"
|
||||
kotlin("jvm") version "1.7.20"
|
||||
|
||||
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
|
||||
id("org.owasp.dependencycheck") version "7.2.0"
|
||||
id("fr.coppernic.versioning") version "3.2.1"
|
||||
id("com.avast.gradle.docker-compose") version "0.16.9"
|
||||
id("org.sonarqube") version "+"
|
||||
}
|
||||
|
||||
apply(plugin = "kotlin")
|
||||
|
||||
group = "fr.postgresjson"
|
||||
version = "0.1"
|
||||
group = "io.github.flecomte"
|
||||
version = versioning.info.tag
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions {
|
||||
jvmTarget = "11"
|
||||
}
|
||||
}
|
||||
|
||||
val compileKotlin: KotlinCompile by tasks
|
||||
compileKotlin.kotlinOptions {
|
||||
jvmTarget = "11"
|
||||
}
|
||||
val compileTestKotlin: KotlinCompile by tasks
|
||||
compileTestKotlin.kotlinOptions {
|
||||
jvmTarget = "11"
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnit()
|
||||
useJUnitPlatform()
|
||||
systemProperty("junit.jupiter.execution.parallel.enabled", true)
|
||||
if (!disableLint.toBoolean()) {
|
||||
finalizedBy(tasks.ktlintCheck)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.jacocoTestReport {
|
||||
dependsOn(tasks.test)
|
||||
reports {
|
||||
xml.required.set(true)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.sonarqube.configure {
|
||||
dependsOn(tasks.jacocoTestReport)
|
||||
}
|
||||
|
||||
tasks.publishToMavenLocal {
|
||||
dependsOn(tasks.test)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.31")
|
||||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9")
|
||||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-joda:2.9.9")
|
||||
implementation("org.slf4j:slf4j-api:1.7.26")
|
||||
implementation("org.postgresql:postgresql:42.2.6")
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.20")
|
||||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.0-rc1")
|
||||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-joda:2.14.0-rc1")
|
||||
implementation("com.github.jasync-sql:jasync-postgresql:2.1.7")
|
||||
implementation("org.slf4j:slf4j-api:2.0.3")
|
||||
|
||||
testImplementation("ch.qos.logback:logback-classic:1.2.3")
|
||||
testImplementation("ch.qos.logback:logback-core:1.2.3")
|
||||
testImplementation("io.mockk:mockk:1.9")
|
||||
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
|
||||
testImplementation("org.amshove.kluent:kluent:1.47")
|
||||
testImplementation("ch.qos.logback:logback-classic:1.4.3")
|
||||
testImplementation("ch.qos.logback:logback-core:1.4.3")
|
||||
testImplementation("io.mockk:mockk:1.13.2")
|
||||
testImplementation("org.junit.jupiter:junit-jupiter:5.9.0")
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.7.20")
|
||||
testImplementation("org.amshove.kluent:kluent:1.68")
|
||||
testImplementation("io.kotest:kotest-runner-junit5:5.5.5")
|
||||
testImplementation("io.kotest:kotest-property:5.5.5")
|
||||
}
|
||||
|
||||
val sourcesJar by tasks.creating(Jar::class) {
|
||||
archiveClassifier.set("sources")
|
||||
from(sourceSets.getByName("main").allSource)
|
||||
}
|
||||
|
||||
apply(plugin = "docker-compose")
|
||||
dockerCompose {
|
||||
setProjectName(projectName.toString())
|
||||
setProperty("useComposeFiles", listOf("docker-compose.yml"))
|
||||
setProperty("stopContainers", !containerAlwaysOn.toBoolean())
|
||||
isRequiredBy(project.tasks.test)
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
groupId = "fr.postgresjson"
|
||||
artifactId = "postgresjson-jdbc"
|
||||
version = "0.1"
|
||||
repositories {
|
||||
maven {
|
||||
name = projectName.toString()
|
||||
url = uri("https://maven.pkg.github.com/flecomte/postgres-json")
|
||||
credentials {
|
||||
username = System.getenv("GITHUB_USERNAME")
|
||||
password = System.getenv("GITHUB_TOKEN")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
publications {
|
||||
create<MavenPublication>(projectName.toString()) {
|
||||
from(components["java"])
|
||||
artifact(sourcesJar)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType<PublishToMavenRepository>().configureEach {
|
||||
onlyIf {
|
||||
versioning.info.run {
|
||||
!dirty && tag != null && tag.matches("""[0-9]+\.[0-9]+\.[0-9]+""".toRegex())
|
||||
}
|
||||
}
|
||||
|
||||
dependsOn(tasks.test)
|
||||
}
|
||||
|
||||
15
docker-compose.yml
Normal file
15
docker-compose.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
# To execute this docker-compose yml file use docker-compose -f <file_name> up
|
||||
# Add the "-d" flag at the end for detached execution
|
||||
version: '3.8'
|
||||
services:
|
||||
db:
|
||||
container_name: postgres_json
|
||||
build:
|
||||
context: docker/postgresql
|
||||
restart: always
|
||||
ports:
|
||||
- "35555:5432"
|
||||
environment:
|
||||
POSTGRES_DB: json_test
|
||||
POSTGRES_USER: test
|
||||
POSTGRES_PASSWORD: test
|
||||
9
docker/postgresql/Dockerfile
Normal file
9
docker/postgresql/Dockerfile
Normal file
@@ -0,0 +1,9 @@
|
||||
FROM postgres:15
|
||||
|
||||
COPY postgresql.conf /tmp/postgresql.conf
|
||||
COPY extension.sh /docker-entrypoint-initdb.d/000-extension.sh
|
||||
COPY setup.sh /docker-entrypoint-initdb.d/100-setup.sh
|
||||
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
EXPOSE 5432
|
||||
CMD ["postgres"]
|
||||
7
docker/postgresql/extension.sh
Normal file
7
docker/postgresql/extension.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
psql -v ON_ERROR_STOP=1 --username "test" --dbname "json_test" <<-EOSQL
|
||||
create extension if not exists plpgsql;
|
||||
create extension if not exists "uuid-ossp";
|
||||
EOSQL
|
||||
666
docker/postgresql/postgresql.conf
Normal file
666
docker/postgresql/postgresql.conf
Normal file
@@ -0,0 +1,666 @@
|
||||
# -----------------------------
|
||||
# PostgreSQL configuration file
|
||||
# -----------------------------
|
||||
#
|
||||
# This file consists of lines of the form:
|
||||
#
|
||||
# name = value
|
||||
#
|
||||
# (The "=" is optional.) Whitespace may be used. Comments are introduced with
|
||||
# "#" anywhere on a line. The complete list of parameter names and allowed
|
||||
# values can be found in the PostgreSQL documentation.
|
||||
#
|
||||
# The commented-out settings shown in this file represent the default values.
|
||||
# Re-commenting a setting is NOT sufficient to revert it to the default value;
|
||||
# you need to reload the server.
|
||||
#
|
||||
# This file is read on server startup and when the server receives a SIGHUP
|
||||
# signal. If you edit the file on a running system, you have to SIGHUP the
|
||||
# server for the changes to take effect, run "pg_ctl reload", or execute
|
||||
# "SELECT pg_reload_conf()". Some parameters, which are marked below,
|
||||
# require a server shutdown and restart to take effect.
|
||||
#
|
||||
# Any parameter can also be given as a command-line option to the server, e.g.,
|
||||
# "postgres -c log_connections=on". Some parameters can be changed at run time
|
||||
# with the "SET" SQL command.
|
||||
#
|
||||
# Memory units: kB = kilobytes Time units: ms = milliseconds
|
||||
# MB = megabytes s = seconds
|
||||
# GB = gigabytes min = minutes
|
||||
# TB = terabytes h = hours
|
||||
# d = days
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# FILE LOCATIONS
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# The default values of these variables are driven from the -D command-line
|
||||
# option or PGDATA environment variable, represented here as ConfigDir.
|
||||
|
||||
#data_directory = 'ConfigDir' # use data in another directory
|
||||
# (change requires restart)
|
||||
#hba_file = 'ConfigDir/pg_hba.conf' # host-based authentication file
|
||||
# (change requires restart)
|
||||
#ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file
|
||||
# (change requires restart)
|
||||
|
||||
# If external_pid_file is not explicitly set, no extra PID file is written.
|
||||
#external_pid_file = '' # write an extra PID file
|
||||
# (change requires restart)
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# CONNECTIONS AND AUTHENTICATION
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Connection Settings -
|
||||
|
||||
listen_addresses = '*'
|
||||
# comma-separated list of addresses;
|
||||
# defaults to 'localhost'; use '*' for all
|
||||
# (change requires restart)
|
||||
#port = 5432 # (change requires restart)
|
||||
#max_connections = 100 # (change requires restart)
|
||||
#superuser_reserved_connections = 3 # (change requires restart)
|
||||
#unix_socket_directories = '/tmp' # comma-separated list of directories
|
||||
# (change requires restart)
|
||||
#unix_socket_group = '' # (change requires restart)
|
||||
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
|
||||
# (change requires restart)
|
||||
#bonjour = off # advertise server via Bonjour
|
||||
# (change requires restart)
|
||||
#bonjour_name = '' # defaults to the computer name
|
||||
# (change requires restart)
|
||||
|
||||
# - Security and Authentication -
|
||||
|
||||
#authentication_timeout = 1min # 1s-600s
|
||||
#ssl = off
|
||||
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
|
||||
#ssl_prefer_server_ciphers = on
|
||||
#ssl_ecdh_curve = 'prime256v1'
|
||||
#ssl_dh_params_file = ''
|
||||
#ssl_cert_file = 'server.crt'
|
||||
#ssl_key_file = 'server.key'
|
||||
#ssl_ca_file = ''
|
||||
#ssl_crl_file = ''
|
||||
#password_encryption = md5 # md5 or scram-sha-256
|
||||
#db_user_namespace = off
|
||||
#row_security = on
|
||||
|
||||
# GSSAPI using Kerberos
|
||||
#krb_server_keyfile = ''
|
||||
#krb_caseins_users = off
|
||||
|
||||
# - TCP Keepalives -
|
||||
# see "man 7 tcp" for details
|
||||
|
||||
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
|
||||
# 0 selects the system default
|
||||
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
|
||||
# 0 selects the system default
|
||||
#tcp_keepalives_count = 0 # TCP_KEEPCNT;
|
||||
# 0 selects the system default
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# RESOURCE USAGE (except WAL)
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Memory -
|
||||
|
||||
shared_buffers = 1GB # min 128kB
|
||||
# (change requires restart)
|
||||
#huge_pages = try # on, off, or try
|
||||
# (change requires restart)
|
||||
#temp_buffers = 8MB # min 800kB
|
||||
#max_prepared_transactions = 0 # zero disables the feature
|
||||
# (change requires restart)
|
||||
# Caution: it is not advisable to set max_prepared_transactions nonzero unless
|
||||
# you actively intend to use prepared transactions.
|
||||
work_mem = 256MB # min 64kB
|
||||
#maintenance_work_mem = 64MB # min 1MB
|
||||
#replacement_sort_tuples = 150000 # limits use of replacement selection sort
|
||||
#autovacuum_work_mem = -1 # min 1MB, or -1 to use maintenance_work_mem
|
||||
#max_stack_depth = 2MB # min 100kB
|
||||
#dynamic_shared_memory_type = posix # the default is the first option
|
||||
# supported by the operating system:
|
||||
# posix
|
||||
# sysv
|
||||
# windows
|
||||
# mmap
|
||||
# use none to disable dynamic shared memory
|
||||
# (change requires restart)
|
||||
|
||||
# - Disk -
|
||||
|
||||
#temp_file_limit = -1 # limits per-process temp file space
|
||||
# in kB, or -1 for no limit
|
||||
|
||||
# - Kernel Resource Usage -
|
||||
|
||||
#max_files_per_process = 1000 # min 25
|
||||
# (change requires restart)
|
||||
shared_preload_libraries = 'pg_stat_statements'
|
||||
pg_stat_statements.max = 10000
|
||||
pg_stat_statements.track = all
|
||||
|
||||
# - Cost-Based Vacuum Delay -
|
||||
|
||||
#vacuum_cost_delay = 0 # 0-100 milliseconds
|
||||
#vacuum_cost_page_hit = 1 # 0-10000 credits
|
||||
#vacuum_cost_page_miss = 10 # 0-10000 credits
|
||||
#vacuum_cost_page_dirty = 20 # 0-10000 credits
|
||||
#vacuum_cost_limit = 200 # 1-10000 credits
|
||||
|
||||
# - Background Writer -
|
||||
|
||||
#bgwriter_delay = 200ms # 10-10000ms between rounds
|
||||
#bgwriter_lru_maxpages = 100 # 0-1000 max buffers written/round
|
||||
#bgwriter_lru_multiplier = 2.0 # 0-10.0 multiplier on buffers scanned/round
|
||||
#bgwriter_flush_after = 0 # measured in pages, 0 disables
|
||||
|
||||
# - Asynchronous Behavior -
|
||||
|
||||
effective_io_concurrency = 100 # 1-1000; 0 disables prefetching
|
||||
#max_worker_processes = 8 # (change requires restart)
|
||||
max_parallel_workers_per_gather = 4 # taken from max_parallel_workers
|
||||
#max_parallel_workers = 8 # maximum number of max_worker_processes that
|
||||
# can be used in parallel queries
|
||||
#old_snapshot_threshold = -1 # 1min-60d; -1 disables; 0 is immediate
|
||||
# (change requires restart)
|
||||
#backend_flush_after = 0 # measured in pages, 0 disables
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# WRITE AHEAD LOG
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Settings -
|
||||
|
||||
#wal_level = replica # minimal, replica, or logical
|
||||
# (change requires restart)
|
||||
#fsync = on # flush data to disk for crash safety
|
||||
# (turning this off can cause
|
||||
# unrecoverable data corruption)
|
||||
#synchronous_commit = on # synchronization level;
|
||||
# off, local, remote_write, remote_apply, or on
|
||||
#wal_sync_method = fsync # the default is the first option
|
||||
# supported by the operating system:
|
||||
# open_datasync
|
||||
# fdatasync (default on Linux)
|
||||
# fsync
|
||||
# fsync_writethrough
|
||||
# open_sync
|
||||
#full_page_writes = on # recover from partial page writes
|
||||
#wal_compression = off # enable compression of full-page writes
|
||||
#wal_log_hints = off # also do full page writes of non-critical updates
|
||||
# (change requires restart)
|
||||
#wal_buffers = -1 # min 32kB, -1 sets based on shared_buffers
|
||||
# (change requires restart)
|
||||
#wal_writer_delay = 200ms # 1-10000 milliseconds
|
||||
#wal_writer_flush_after = 1MB # measured in pages, 0 disables
|
||||
|
||||
#commit_delay = 0 # range 0-100000, in microseconds
|
||||
#commit_siblings = 5 # range 1-1000
|
||||
|
||||
# - Checkpoints -
|
||||
|
||||
#checkpoint_timeout = 5min # range 30s-1d
|
||||
#max_wal_size = 1GB
|
||||
#min_wal_size = 80MB
|
||||
#checkpoint_completion_target = 0.5 # checkpoint target duration, 0.0 - 1.0
|
||||
#checkpoint_flush_after = 0 # measured in pages, 0 disables
|
||||
#checkpoint_warning = 30s # 0 disables
|
||||
|
||||
# - Archiving -
|
||||
|
||||
#archive_mode = off # enables archiving; off, on, or always
|
||||
# (change requires restart)
|
||||
#archive_command = '' # command to use to archive a logfile segment
|
||||
# placeholders: %p = path of file to archive
|
||||
# %f = file name only
|
||||
# e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
|
||||
#archive_timeout = 0 # force a logfile segment switch after this
|
||||
# number of seconds; 0 disables
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# REPLICATION
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Sending Server(s) -
|
||||
|
||||
# Set these on the master and on any standby that will send replication data.
|
||||
|
||||
#max_wal_senders = 10 # max number of walsender processes
|
||||
# (change requires restart)
|
||||
#wal_keep_segments = 0 # in logfile segments, 16MB each; 0 disables
|
||||
#wal_sender_timeout = 60s # in milliseconds; 0 disables
|
||||
|
||||
#max_replication_slots = 10 # max number of replication slots
|
||||
# (change requires restart)
|
||||
#track_commit_timestamp = off # collect timestamp of transaction commit
|
||||
# (change requires restart)
|
||||
|
||||
# - Master Server -
|
||||
|
||||
# These settings are ignored on a standby server.
|
||||
|
||||
#synchronous_standby_names = '' # standby servers that provide sync rep
|
||||
# method to choose sync standbys, number of sync standbys,
|
||||
# and comma-separated list of application_name
|
||||
# from standby(s); '*' = all
|
||||
#vacuum_defer_cleanup_age = 0 # number of xacts by which cleanup is delayed
|
||||
|
||||
# - Standby Servers -
|
||||
|
||||
# These settings are ignored on a master server.
|
||||
|
||||
#hot_standby = on # "off" disallows queries during recovery
|
||||
# (change requires restart)
|
||||
#max_standby_archive_delay = 30s # max delay before canceling queries
|
||||
# when reading WAL from archive;
|
||||
# -1 allows indefinite delay
|
||||
#max_standby_streaming_delay = 30s # max delay before canceling queries
|
||||
# when reading streaming WAL;
|
||||
# -1 allows indefinite delay
|
||||
#wal_receiver_status_interval = 10s # send replies at least this often
|
||||
# 0 disables
|
||||
#hot_standby_feedback = off # send info from standby to prevent
|
||||
# query conflicts
|
||||
#wal_receiver_timeout = 60s # time that receiver waits for
|
||||
# communication from master
|
||||
# in milliseconds; 0 disables
|
||||
#wal_retrieve_retry_interval = 5s # time to wait before retrying to
|
||||
# retrieve WAL after a failed attempt
|
||||
|
||||
# - Subscribers -
|
||||
|
||||
# These settings are ignored on a publisher.
|
||||
|
||||
#max_logical_replication_workers = 4 # taken from max_worker_processes
|
||||
# (change requires restart)
|
||||
#max_sync_workers_per_subscription = 2 # taken from max_logical_replication_workers
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# QUERY TUNING
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Planner Method Configuration -
|
||||
|
||||
#enable_bitmapscan = on
|
||||
#enable_hashagg = on
|
||||
#enable_hashjoin = on
|
||||
#enable_indexscan = on
|
||||
#enable_indexonlyscan = on
|
||||
#enable_material = on
|
||||
#enable_mergejoin = on
|
||||
#enable_nestloop = on
|
||||
#enable_seqscan = on
|
||||
#enable_sort = on
|
||||
#enable_tidscan = on
|
||||
|
||||
# - Planner Cost Constants -
|
||||
|
||||
#seq_page_cost = 1.0 # measured on an arbitrary scale
|
||||
#random_page_cost = 4.0 # same scale as above
|
||||
#cpu_tuple_cost = 0.01 # same scale as above
|
||||
#cpu_index_tuple_cost = 0.005 # same scale as above
|
||||
#cpu_operator_cost = 0.0025 # same scale as above
|
||||
#parallel_tuple_cost = 0.1 # same scale as above
|
||||
#parallel_setup_cost = 1000.0 # same scale as above
|
||||
#min_parallel_table_scan_size = 8MB
|
||||
#min_parallel_index_scan_size = 512kB
|
||||
#effective_cache_size = 4GB
|
||||
|
||||
# - Genetic Query Optimizer -
|
||||
|
||||
#geqo = on
|
||||
#geqo_threshold = 12
|
||||
#geqo_effort = 5 # range 1-10
|
||||
#geqo_pool_size = 0 # selects default based on effort
|
||||
#geqo_generations = 0 # selects default based on effort
|
||||
#geqo_selection_bias = 2.0 # range 1.5-2.0
|
||||
#geqo_seed = 0.0 # range 0.0-1.0
|
||||
|
||||
# - Other Planner Options -
|
||||
|
||||
#default_statistics_target = 100 # range 1-10000
|
||||
#constraint_exclusion = partition # on, off, or partition
|
||||
#cursor_tuple_fraction = 0.1 # range 0.0-1.0
|
||||
#from_collapse_limit = 8
|
||||
#join_collapse_limit = 8 # 1 disables collapsing of explicit
|
||||
# JOIN clauses
|
||||
#force_parallel_mode = off
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# ERROR REPORTING AND LOGGING
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Where to Log -
|
||||
|
||||
#log_destination = 'stderr' # Valid values are combinations of
|
||||
# stderr, csvlog, syslog, and eventlog,
|
||||
# depending on platform. csvlog
|
||||
# requires logging_collector to be on.
|
||||
|
||||
# This is used when logging to stderr:
|
||||
logging_collector = on
|
||||
# Enable capturing of stderr and csvlog
|
||||
# into log files. Required to be on for
|
||||
# csvlogs.
|
||||
# (change requires restart)
|
||||
|
||||
# These are only used if logging_collector is on:
|
||||
log_directory = '/var/log/postgresql'
|
||||
# directory where log files are written,
|
||||
# can be absolute or relative to PGDATA
|
||||
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
|
||||
# log file name pattern,
|
||||
# can include strftime() escapes
|
||||
#log_file_mode = 0600 # creation mode for log files,
|
||||
# begin with 0 to use octal notation
|
||||
log_file_mode = 0660
|
||||
#log_truncate_on_rotation = off # If on, an existing log file with the
|
||||
# same name as the new log file will be
|
||||
# truncated rather than appended to.
|
||||
# But such truncation only occurs on
|
||||
# time-driven rotation, not on restarts
|
||||
# or size-driven rotation. Default is
|
||||
# off, meaning append to existing files
|
||||
# in all cases.
|
||||
log_rotation_age = 1d
|
||||
# Automatic rotation of logfiles will
|
||||
# happen after that time. 0 disables.
|
||||
log_rotation_size = 100MB
|
||||
# Automatic rotation of logfiles will
|
||||
# happen after that much log output.
|
||||
# 0 disables.
|
||||
|
||||
# These are relevant when logging to syslog:
|
||||
#syslog_facility = 'LOCAL0'
|
||||
#syslog_ident = 'postgres'
|
||||
#syslog_sequence_numbers = on
|
||||
#syslog_split_messages = on
|
||||
|
||||
# This is only relevant when logging to eventlog (win32):
|
||||
# (change requires restart)
|
||||
#event_source = 'PostgreSQL'
|
||||
|
||||
# - When to Log -
|
||||
|
||||
#client_min_messages = notice # values in order of decreasing detail:
|
||||
# debug5
|
||||
# debug4
|
||||
# debug3
|
||||
# debug2
|
||||
# debug1
|
||||
# log
|
||||
# notice
|
||||
# warning
|
||||
# error
|
||||
|
||||
#log_min_messages = warning # values in order of decreasing detail:
|
||||
# debug5
|
||||
# debug4
|
||||
# debug3
|
||||
# debug2
|
||||
# debug1
|
||||
# info
|
||||
# notice
|
||||
# warning
|
||||
# error
|
||||
# log
|
||||
# fatal
|
||||
# panic
|
||||
|
||||
#log_min_error_statement = error # values in order of decreasing detail:
|
||||
# debug5
|
||||
# debug4
|
||||
# debug3
|
||||
# debug2
|
||||
# debug1
|
||||
# info
|
||||
# notice
|
||||
# warning
|
||||
# error
|
||||
# log
|
||||
# fatal
|
||||
# panic (effectively off)
|
||||
|
||||
#log_min_duration_statement = -1 # -1 is disabled, 0 logs all statements
|
||||
# and their durations, > 0 logs only
|
||||
# statements running at least this number
|
||||
# of milliseconds
|
||||
|
||||
|
||||
# - What to Log -
|
||||
|
||||
#debug_print_parse = off
|
||||
#debug_print_rewritten = off
|
||||
#debug_print_plan = off
|
||||
#debug_pretty_print = on
|
||||
#log_checkpoints = off
|
||||
#log_connections = off
|
||||
#log_disconnections = off
|
||||
#log_duration = off
|
||||
#log_error_verbosity = default # terse, default, or verbose messages
|
||||
#log_hostname = off
|
||||
#log_line_prefix = '%m [%p] ' # special values:
|
||||
# %a = application name
|
||||
# %u = user name
|
||||
# %d = database name
|
||||
# %r = remote host and port
|
||||
# %h = remote host
|
||||
# %p = process ID
|
||||
# %t = timestamp without milliseconds
|
||||
# %m = timestamp with milliseconds
|
||||
# %n = timestamp with milliseconds (as a Unix epoch)
|
||||
# %i = command tag
|
||||
# %e = SQL state
|
||||
# %c = session ID
|
||||
# %l = session line number
|
||||
# %s = session start timestamp
|
||||
# %v = virtual transaction ID
|
||||
# %x = transaction ID (0 if none)
|
||||
# %q = stop here in non-session
|
||||
# processes
|
||||
# %% = '%'
|
||||
# e.g. '<%u%%%d> '
|
||||
#log_lock_waits = off # log lock waits >= deadlock_timeout
|
||||
#log_statement = 'none' # none, ddl, mod, all
|
||||
#log_replication_commands = off
|
||||
#log_temp_files = -1 # log temporary files equal or larger
|
||||
# than the specified size in kilobytes;
|
||||
# -1 disables, 0 logs all temp files
|
||||
#log_timezone = 'GMT'
|
||||
|
||||
|
||||
# - Process Title -
|
||||
|
||||
#cluster_name = '' # added to process titles if nonempty
|
||||
# (change requires restart)
|
||||
#update_process_title = on
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# RUNTIME STATISTICS
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Query/Index Statistics Collector -
|
||||
|
||||
#track_activities = on
|
||||
#track_counts = on
|
||||
#track_io_timing = off
|
||||
#track_functions = none # none, pl, all
|
||||
#track_activity_query_size = 1024 # (change requires restart)
|
||||
#stats_temp_directory = 'pg_stat_tmp'
|
||||
|
||||
|
||||
# - Statistics Monitoring -
|
||||
|
||||
#log_parser_stats = off
|
||||
#log_planner_stats = off
|
||||
#log_executor_stats = off
|
||||
#log_statement_stats = off
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# AUTOVACUUM PARAMETERS
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
#autovacuum = on # Enable autovacuum subprocess? 'on'
|
||||
# requires track_counts to also be on.
|
||||
#log_autovacuum_min_duration = -1 # -1 disables, 0 logs all actions and
|
||||
# their durations, > 0 logs only
|
||||
# actions running at least this number
|
||||
# of milliseconds.
|
||||
#autovacuum_max_workers = 3 # max number of autovacuum subprocesses
|
||||
# (change requires restart)
|
||||
#autovacuum_naptime = 1min # time between autovacuum runs
|
||||
#autovacuum_vacuum_threshold = 50 # min number of row updates before
|
||||
# vacuum
|
||||
#autovacuum_analyze_threshold = 50 # min number of row updates before
|
||||
# analyze
|
||||
#autovacuum_vacuum_scale_factor = 0.2 # fraction of table size before vacuum
|
||||
#autovacuum_analyze_scale_factor = 0.1 # fraction of table size before analyze
|
||||
#autovacuum_freeze_max_age = 200000000 # maximum XID age before forced vacuum
|
||||
# (change requires restart)
|
||||
#autovacuum_multixact_freeze_max_age = 400000000 # maximum multixact age
|
||||
# before forced vacuum
|
||||
# (change requires restart)
|
||||
#autovacuum_vacuum_cost_delay = 20ms # default vacuum cost delay for
|
||||
# autovacuum, in milliseconds;
|
||||
# -1 means use vacuum_cost_delay
|
||||
#autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for
|
||||
# autovacuum, -1 means use
|
||||
# vacuum_cost_limit
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# CLIENT CONNECTION DEFAULTS
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Statement Behavior -
|
||||
|
||||
#search_path = '"$user", public' # schema names
|
||||
#default_tablespace = '' # a tablespace name, '' uses the default
|
||||
#temp_tablespaces = '' # a list of tablespace names, '' uses
|
||||
# only default tablespace
|
||||
#check_function_bodies = on
|
||||
#default_transaction_isolation = 'read committed'
|
||||
#default_transaction_read_only = off
|
||||
#default_transaction_deferrable = off
|
||||
#session_replication_role = 'origin'
|
||||
#statement_timeout = 0 # in milliseconds, 0 is disabled
|
||||
#lock_timeout = 0 # in milliseconds, 0 is disabled
|
||||
#idle_in_transaction_session_timeout = 0 # in milliseconds, 0 is disabled
|
||||
#vacuum_freeze_min_age = 50000000
|
||||
#vacuum_freeze_table_age = 150000000
|
||||
#vacuum_multixact_freeze_min_age = 5000000
|
||||
#vacuum_multixact_freeze_table_age = 150000000
|
||||
#bytea_output = 'hex' # hex, escape
|
||||
#xmlbinary = 'base64'
|
||||
#xmloption = 'content'
|
||||
#gin_fuzzy_search_limit = 0
|
||||
#gin_pending_list_limit = 4MB
|
||||
|
||||
# - Locale and Formatting -
|
||||
|
||||
#datestyle = 'iso, mdy'
|
||||
#intervalstyle = 'postgres'
|
||||
#timezone = 'GMT'
|
||||
#timezone_abbreviations = 'Default' # Select the set of available time zone
|
||||
# abbreviations. Currently, there are
|
||||
# Default
|
||||
# Australia (historical usage)
|
||||
# India
|
||||
# You can create your own file in
|
||||
# share/timezonesets/.
|
||||
#extra_float_digits = 0 # min -15, max 3
|
||||
#client_encoding = sql_ascii # actually, defaults to database
|
||||
# encoding
|
||||
|
||||
# These settings are initialized by initdb, but they can be changed.
|
||||
#lc_messages = 'C' # locale for system error message
|
||||
# strings
|
||||
#lc_monetary = 'C' # locale for monetary formatting
|
||||
#lc_numeric = 'C' # locale for number formatting
|
||||
#lc_time = 'C' # locale for time formatting
|
||||
|
||||
# default configuration for text search
|
||||
#default_text_search_config = 'pg_catalog.simple'
|
||||
|
||||
# - Other Defaults -
|
||||
|
||||
#dynamic_library_path = '$libdir'
|
||||
#local_preload_libraries = ''
|
||||
#session_preload_libraries = ''
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# LOCK MANAGEMENT
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
#deadlock_timeout = 1s
|
||||
#max_locks_per_transaction = 64 # min 10
|
||||
# (change requires restart)
|
||||
#max_pred_locks_per_transaction = 64 # min 10
|
||||
# (change requires restart)
|
||||
#max_pred_locks_per_relation = -2 # negative values mean
|
||||
# (max_pred_locks_per_transaction
|
||||
# / -max_pred_locks_per_relation) - 1
|
||||
#max_pred_locks_per_page = 2 # min 0
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# VERSION/PLATFORM COMPATIBILITY
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# - Previous PostgreSQL Versions -
|
||||
|
||||
#array_nulls = on
|
||||
#backslash_quote = safe_encoding # on, off, or safe_encoding
|
||||
#default_with_oids = off
|
||||
#escape_string_warning = on
|
||||
#lo_compat_privileges = off
|
||||
#operator_precedence_warning = off
|
||||
#quote_all_identifiers = off
|
||||
#standard_conforming_strings = on
|
||||
#synchronize_seqscans = on
|
||||
|
||||
# - Other Platforms and Clients -
|
||||
|
||||
#transform_null_equals = off
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# ERROR HANDLING
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
#exit_on_error = off # terminate session on any error?
|
||||
#restart_after_crash = on # reinitialize after backend crash?
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# CONFIG FILE INCLUDES
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# These options allow settings to be loaded from files other than the
|
||||
# default postgresql.conf.
|
||||
|
||||
#include_dir = 'conf.d' # include files ending in '.conf' from
|
||||
# directory 'conf.d'
|
||||
#include_if_exists = 'exists.conf' # include file only if it exists
|
||||
#include = 'special.conf' # include file
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# CUSTOMIZED OPTIONS
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# Add settings for extensions here
|
||||
4
docker/postgresql/setup.sh
Normal file
4
docker/postgresql/setup.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
cat /tmp/postgresql.conf > /var/lib/postgresql/data/postgresql.conf
|
||||
BIN
docs/call function.png
Normal file
BIN
docs/call function.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 89 KiB |
52
docs/call function.puml
Normal file
52
docs/call function.puml
Normal file
@@ -0,0 +1,52 @@
|
||||
@startuml
|
||||
|
||||
participant Gradle
|
||||
participant App
|
||||
control Migration
|
||||
control Repository
|
||||
control Requester
|
||||
control Function
|
||||
control Connection
|
||||
database Database
|
||||
|
||||
== Migration ==
|
||||
|
||||
[-> Gradle++ : run gradle
|
||||
Gradle -> Migration** : Init Migration
|
||||
Migration -> Migration : Read Schemas
|
||||
Migration -> Migration : Read SQL Function
|
||||
Gradle -> Migration++ : Run Migrations
|
||||
Migration -> Migration : Run Migrations
|
||||
return
|
||||
Gradle -> Migration!!
|
||||
return
|
||||
|
||||
== Application ==
|
||||
|
||||
[-> App : run app
|
||||
App -> Connection** : Create Connection
|
||||
Connection -> Database : Connect to the database
|
||||
App -> Requester** : Create Requester
|
||||
Requester -> Requester: Read all Function file
|
||||
Requester -> Function** : Create Function object
|
||||
Function -> Function : Parse function
|
||||
Function -> Function : Store parameters and function name
|
||||
App -> Repository** : Create Repository
|
||||
|
||||
... wait request ...
|
||||
|
||||
[-> Repository++ : call repository with args
|
||||
Repository -> Repository : Define function name
|
||||
Repository -> Requester++ : get function
|
||||
Requester -> Function++ : select()
|
||||
Function -> Function : Compile args
|
||||
Function -> Function : Compile SQL
|
||||
Function -> Connection++ : select()
|
||||
Connection -> Database++ : Send Prepared Statement
|
||||
return
|
||||
Connection -> Connection : Convert json to Entity
|
||||
return entity
|
||||
return entity
|
||||
return entity
|
||||
return entity
|
||||
@enduml
|
||||
9
docs/checklist.md
Normal file
9
docs/checklist.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Checklist
|
||||
|
||||
- [ ] Define schemas with migrations files (*.up.sql & *.down.sql)
|
||||
- [ ] Define query with sql function (*.sql)
|
||||
- [ ] Create Entity (*.kt)
|
||||
- [ ] Create repository (*.kt)
|
||||
- [ ] Configure migrations in gradle
|
||||
- [ ] Execute migrations functions
|
||||
- [ ] Call Repository
|
||||
9
docs/installation.md
Normal file
9
docs/installation.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Installation
|
||||
|
||||
## Gradle
|
||||
```kotlin
|
||||
// build.gradle.kts
|
||||
dependencies {
|
||||
implementation("com.github.flecomte:postgres-json:+")
|
||||
}
|
||||
```
|
||||
16
docs/migrations/migrations-application.md
Normal file
16
docs/migrations/migrations-application.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# Execute migration in application
|
||||
```kotlin
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
val conn: Connection = TODO()
|
||||
val migrations = Migrations(
|
||||
conn,
|
||||
this::class.java.getResource("/sql/migrations")?.toURI() ?: error("No migrations found"),
|
||||
this::class.java.getResource("/sql/functions")?.toURI() ?: error("No sql function found")
|
||||
)
|
||||
|
||||
migrations.status() // Show executed and not executed migrations
|
||||
migrations.runDry() // Execute migration in transaction and rollback at the end
|
||||
migrations.run() // Execute migration in transaction and commit if no error
|
||||
```
|
||||
37
docs/migrations/migrations-gradle.md
Normal file
37
docs/migrations/migrations-gradle.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Execute Migrations with Gradle
|
||||
|
||||
You can execute migration with a Gradle task like this:
|
||||
|
||||
```kotlin
|
||||
// build.gradle.kts
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.migration.Migrations
|
||||
|
||||
buildscript {
|
||||
dependencies {
|
||||
classpath("com.github.flecomte:postgres-json:+")
|
||||
}
|
||||
}
|
||||
|
||||
val migration by tasks.registering {
|
||||
doLast {
|
||||
val connection = Connection(
|
||||
host = "localhost",
|
||||
port = 5432,
|
||||
database = "database",
|
||||
username = "username",
|
||||
password = "password"
|
||||
)
|
||||
Migrations(
|
||||
connection,
|
||||
file("$buildDir/resources/main/sql/migrations").toURI(),
|
||||
file("$buildDir/resources/main/sql/functions").toURI()
|
||||
).run()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```shell
|
||||
$ gradle migration
|
||||
```
|
||||
71
docs/migrations/migrations.md
Normal file
71
docs/migrations/migrations.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Migration
|
||||
## Schemas migration
|
||||
Migrations are just manually written `*.sql` files that represent the database schemas.
|
||||
Each file is executed one after the other in alphabetical order.
|
||||
Each execution is stored in the `migration.history` table.
|
||||
|
||||
A migration contains a `*.up.sql` file and a `*.down.sql` file to rollback the migration.
|
||||
The content of the `*.down.sql` file is also stored in the database.
|
||||
This allows the `*.down.sql` to be executed even if the code is already rollback.
|
||||
|
||||
Example:
|
||||
```postgresql
|
||||
-- resources/sql/migrations/0000-init_schema.up.sql
|
||||
create table "user"
|
||||
(
|
||||
id uuid default uuid_generate_v4() not null primary key,
|
||||
created_at timestamptz default now() not null,
|
||||
blocked_at timestamptz default null null,
|
||||
username varchar(64) not null check ( username != '' and lower(username) = username) unique,
|
||||
password text not null check ( password != '' ),
|
||||
roles text[] default '{}' not null
|
||||
);
|
||||
```
|
||||
|
||||
```postgresql
|
||||
-- resources/sql/migrations/0000-init_schema.down.sql
|
||||
drop table if exists "user";
|
||||
```
|
||||
## Stored procedure migrations
|
||||
|
||||
Migrations are also stored procedures and other functions.
|
||||
Each function is updated with each migration.
|
||||
|
||||
Example:
|
||||
```postgresql
|
||||
-- resources/sql/functions/insert_user.sql
|
||||
create or replace function insert_user(inout resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into "user" (id, username, password, blocked_at, roles)
|
||||
select
|
||||
coalesce(t.id, uuid_generate_v4()),
|
||||
t.username,
|
||||
crypt(resource->>'password', gen_salt('bf', 8)),
|
||||
case when t.blocked_at is not null then now() else null end,
|
||||
t.roles
|
||||
from json_populate_record(null::"user", resource) t
|
||||
returning id into new_id;
|
||||
|
||||
select find_user_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
```
|
||||
|
||||
```postgresql
|
||||
-- resources/sql/functions/find_user_by_id.sql
|
||||
create or replace function find_user_by_id(in _id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
select to_jsonb(u) - 'password' into resource
|
||||
from "user" as u
|
||||
where u.id = _id;
|
||||
end;
|
||||
$$;
|
||||
```
|
||||
|
||||
* [Execute migrations in application](./migrations-application.md)
|
||||
* [Execute migrations with gradle](./migrations-gradle.md)
|
||||
|
||||
14
docs/usage/init-connection.md
Normal file
14
docs/usage/init-connection.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Init connection
|
||||
|
||||
Before execute any query you must instantiate the connection.
|
||||
```kotlin
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
val connection = Connection(
|
||||
host = "localhost",
|
||||
port = 5432,
|
||||
database = "mydb",
|
||||
username = "john",
|
||||
password = "azerty"
|
||||
)
|
||||
```
|
||||
93
docs/usage/multi-level.md
Normal file
93
docs/usage/multi-level.md
Normal file
@@ -0,0 +1,93 @@
|
||||
Multi Level Queries
|
||||
===================
|
||||
|
||||
## Define schema, query and kotlin object
|
||||
1. Schema
|
||||
```postgresql
|
||||
create table parent (
|
||||
id uuid primary key,
|
||||
name text not null
|
||||
);
|
||||
|
||||
create table child (
|
||||
id uuid primary key,
|
||||
name text not null,
|
||||
parent_id uuid not null references parent
|
||||
)
|
||||
```
|
||||
2. Insert some data for tests
|
||||
```postgresql
|
||||
insert into parent (id, name) VALUES ('379e0687-9e4a-4781-b0e9-d94a62e4261f', 'Bernard');
|
||||
insert into child (id, name, parent_id) VALUES (uuid_generate_v4(), 'Noé', '379e0687-9e4a-4781-b0e9-d94a62e4261f');
|
||||
insert into child (id, name, parent_id) VALUES (uuid_generate_v4(), 'John', '379e0687-9e4a-4781-b0e9-d94a62e4261f');
|
||||
```
|
||||
3. Define Model
|
||||
```kotlin
|
||||
import java.util.UUID
|
||||
|
||||
class Parent(val id: UUID, val name: String, val children: List<Child>)
|
||||
class Child(val id: UUID, val name: String)
|
||||
```
|
||||
|
||||
4. Define request function
|
||||
```postgresql
|
||||
-- resource/sql/functions/find_parent_by_id.sql
|
||||
create or replace function find_parent_by_id(in _id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
select to_json(t) into resource
|
||||
from (
|
||||
select
|
||||
p.*,
|
||||
json_agg(to_jsonb(c) - 'parent_id') as children
|
||||
from parent p
|
||||
join child c on c.parent_id = p.id
|
||||
where p.id = _id
|
||||
group by p.id
|
||||
) t;
|
||||
end;
|
||||
$$;
|
||||
```
|
||||
|
||||
## Execute the function
|
||||
|
||||
You just to use `Requester` and set the sql function name, then pass arguments.
|
||||
|
||||
If you need to return more than one entry, use `.select()` instead of `.selecteOne()`
|
||||
|
||||
See the [Paginated example](./paginated.md)
|
||||
```kotlin
|
||||
import fr.postgresjson.connexion.Requester
|
||||
|
||||
val requester: Requester = TODO()
|
||||
val result: Parent = requester
|
||||
.getFunction("find_parent_by_id")
|
||||
.execute("id" to "379e0687-9e4a-4781-b0e9-d94a62e4261f")
|
||||
```
|
||||
|
||||
The requester create dynamically this request
|
||||
```postgresql
|
||||
select * from find_parent_by_id(_id => '379e0687-9e4a-4781-b0e9-d94a62e4261f');
|
||||
```
|
||||
*Watch the underscore as prefix is added if necessary.
|
||||
The requester known the parameters because it parses all SQL functions and reads the names of the parameters from them.*
|
||||
|
||||
|
||||
And the SQL return is a JSON like follow:
|
||||
```json
|
||||
{
|
||||
"id": "379e0687-9e4a-4781-b0e9-d94a62e4261f",
|
||||
"name": "Bernard",
|
||||
"child": [
|
||||
{
|
||||
"id": "c2d0ec81-7cac-4689-8086-2644a3b309b5",
|
||||
"name": "Noé"
|
||||
},
|
||||
{
|
||||
"id": "255d911a-0cbc-4156-bf8c-0204e89494d9",
|
||||
"name": "John"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
But the requester deserialize the result automatically into a Kotlin object with their children objects. **And do that in only one request**.
|
||||
86
docs/usage/raw-request.md
Normal file
86
docs/usage/raw-request.md
Normal file
@@ -0,0 +1,86 @@
|
||||
# Raw request
|
||||
You can execute query directly from the code like this:
|
||||
(*see [Init connection](./init-connection.md) before*)
|
||||
|
||||
```kotlin
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
val connection: Connection = TODO()
|
||||
|
||||
val result: QueryResult = connection.exec(
|
||||
"SELECT id FROM inventor WHERE name = :name",
|
||||
mapOf("name" to "Nikola Tesla")
|
||||
)
|
||||
val id: String = result.rows[0].getString(0)
|
||||
```
|
||||
|
||||
And if you must map the query result with an entity, you can do it like this:
|
||||
```kotlin
|
||||
import java.util.UUID
|
||||
import fr.postgresjson.entity.Serializable
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
val connection: Connection = TODO()
|
||||
|
||||
data class Inventor(
|
||||
val id: UUID = UUID.randomUUID(),
|
||||
val name: String,
|
||||
val roles: List<String> = listOf(),
|
||||
)
|
||||
|
||||
// Select one entity
|
||||
val result: Inventor = connection.execute(
|
||||
"""
|
||||
SELECT json_build_object(
|
||||
'id', '9e65de49-712e-47ce-8bf2-dfffae53a82e',
|
||||
'name', :name
|
||||
)
|
||||
""",
|
||||
mapOf("name" to "Nikola Tesla")
|
||||
)
|
||||
|
||||
// Select multiple entities
|
||||
val result = connection.execute<List<Inventor>>(
|
||||
"""
|
||||
SELECT json_build_array(
|
||||
json_build_object(
|
||||
'id', '9e65de49-712e-47ce-8bf2-dfffae53a82e',
|
||||
'name', :name
|
||||
),
|
||||
json_build_object(
|
||||
'id', '32f67ed3-af6d-403b-a3b9-5fe3540c3412',
|
||||
'name', :name2
|
||||
)
|
||||
)
|
||||
""",
|
||||
mapOf(
|
||||
"name" to "Nikola Tesla",
|
||||
"name2" to "Albert Einstein",
|
||||
)
|
||||
)
|
||||
|
||||
// Select multiple with real query
|
||||
val result: List<Inventor> = connection.execute(
|
||||
"""
|
||||
select json_agg(i)
|
||||
from inventor i
|
||||
where roles @> ARRAY[:role];
|
||||
""",
|
||||
mapOf("role" to "ADMIN")
|
||||
)
|
||||
|
||||
|
||||
// Select multiple with only some rows
|
||||
val result: List<Inventor> = connection.execute(
|
||||
"""
|
||||
select json_agg(i)
|
||||
from (
|
||||
select id, name
|
||||
from inventor
|
||||
) i;
|
||||
"""
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
See [ConnectionTest.kt](/src/test/kotlin/fr/postgresjson/ConnectionTest.kt) for more examples.
|
||||
80
docs/usage/stored-procedure.md
Normal file
80
docs/usage/stored-procedure.md
Normal file
@@ -0,0 +1,80 @@
|
||||
# Stored Procedure
|
||||
*Execute stored procedure with requester*
|
||||
|
||||
You can execute a stored procedure (previously defined in a migration) via the Requester
|
||||
|
||||
To do that:
|
||||
|
||||
1. First, instantiate the requester
|
||||
```kotlin
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
val connection: Connection = TODO()
|
||||
|
||||
val requester = Requester(
|
||||
connection = connection,
|
||||
functionsDirectory = this::class.java.getResource("/sql/functions")?.toURI() ?: error("No sql function found")
|
||||
).createRequester()
|
||||
```
|
||||
|
||||
2. then, define Entities
|
||||
```kotlin
|
||||
import java.util.UUID
|
||||
import org.joda.time.DateTime
|
||||
|
||||
enum class Roles { ROLE_USER, ROLE_ADMIN }
|
||||
|
||||
class User(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
override var username: String,
|
||||
var blockedAt: DateTime? = null,
|
||||
var roles: List<Roles> = emptyList()
|
||||
)
|
||||
|
||||
@SqlSerializable
|
||||
class UserForCreate(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
username: String,
|
||||
val password: String,
|
||||
blockedAt: DateTime? = null,
|
||||
roles: List<Roles> = emptyList()
|
||||
)
|
||||
```
|
||||
3. and, define Repositories
|
||||
[See SQL function](../migrations/migrations.md)
|
||||
|
||||
```kotlin
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.repository.RepositoryI
|
||||
import java.util.UUID
|
||||
|
||||
class UserRepository(override var requester: Requester): RepositoryI {
|
||||
fun findById(id: UUID): User {
|
||||
return requester
|
||||
.getFunction("find_user_by_id") // Use the name of the function
|
||||
.execute("id" to id) // You can pass parameters by their names. The underscore prefix on parameters is not required to be mapped.
|
||||
// Throw exception if user not found
|
||||
}
|
||||
|
||||
fun insert(user: UserForCreate): User {
|
||||
return requester
|
||||
.getFunction("insert_user")
|
||||
.execute("resource" to user)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4. And at last, execute queries
|
||||
```kotlin
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import java.util.UUID
|
||||
|
||||
val requester: Requester = TODO()
|
||||
val userRepo = UserRepository(requester)
|
||||
|
||||
val user: User = userRepo.findById(UUID.fromString(id))
|
||||
|
||||
val newUser: UserForCreate = TODO()
|
||||
val userInserted: User = userRepo.insert(newUser)
|
||||
```
|
||||
6
docs/usage/usage.md
Normal file
6
docs/usage/usage.md
Normal file
@@ -0,0 +1,6 @@
|
||||
## Usage
|
||||
|
||||
1. [Init connection](./init-connection.md)
|
||||
2. [Raw request](./raw-request.md)
|
||||
3. [Stored Procedure](./stored-procedure.md)
|
||||
4. [Multi level request](./multi-level.md)
|
||||
@@ -1,2 +1,11 @@
|
||||
kotlin.code.style=official
|
||||
kotlin_version=1.3.31
|
||||
systemProp.sonar.host.url=https://sonarcloud.io
|
||||
systemProp.sonar.projectKey=postgres-json
|
||||
systemProp.sonar.projectName=PostgresJson
|
||||
systemProp.sonar.organization=flecomte
|
||||
systemProp.sonar.java.coveragePlugin=jacoco
|
||||
systemProp.sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
|
||||
org.gradle.jvmargs=-Xmx4096M
|
||||
containerAlwaysOn=false
|
||||
disableLint=false
|
||||
projectName=postgres-json
|
||||
5
gradle/wrapper/gradle-wrapper.properties
vendored
5
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,5 @@
|
||||
#Wed May 29 17:08:27 CEST 2019
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
||||
2
jitpack.yml
Normal file
2
jitpack.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
jdk:
|
||||
- openjdk11
|
||||
@@ -1 +1 @@
|
||||
rootProject.name = 'postgresjson'
|
||||
rootProject.name = 'postgres-json'
|
||||
|
||||
9
src/.editorconfig
Normal file
9
src/.editorconfig
Normal file
@@ -0,0 +1,9 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
tab_width = 4
|
||||
@@ -1,17 +1,18 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import com.github.jasync.sql.db.ResultSet
|
||||
import com.github.jasync.sql.db.pool.ConnectionPool
|
||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
||||
import com.github.jasync.sql.db.util.length
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
import org.slf4j.Logger
|
||||
import java.sql.DriverManager
|
||||
import java.sql.ResultSet
|
||||
import java.sql.Connection as JDBCConnection
|
||||
|
||||
typealias SelectOneCallback<T> = ResultSet.(T?) -> Unit
|
||||
typealias SelectCallback<T> = ResultSet.(List<T>) -> Unit
|
||||
typealias SelectPaginatedCallback<T> = ResultSet.(Paginated<T>) -> Unit
|
||||
import kotlin.jvm.Throws
|
||||
import kotlin.random.Random
|
||||
import kotlin.reflect.full.hasAnnotation
|
||||
|
||||
class Connection(
|
||||
private val database: String,
|
||||
@@ -19,194 +20,105 @@ class Connection(
|
||||
private val password: String,
|
||||
private val host: String = "localhost",
|
||||
private val port: Int = 5432
|
||||
): Executable {
|
||||
private lateinit var connection: JDBCConnection
|
||||
) : ExecutableRaw {
|
||||
private var connectionPool: ConnectionPool<PostgreSQLConnection>? = null
|
||||
private val serializer = Serializer()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
|
||||
internal fun connect(): JDBCConnection {
|
||||
if (!::connection.isInitialized || connection.isClosed) {
|
||||
connection = DriverManager.getConnection("jdbc:postgresql://$host:$port/$database", username, password)
|
||||
internal fun connect(): ConnectionPool<PostgreSQLConnection> {
|
||||
return connectionPool.let { connectionPool ->
|
||||
if (connectionPool == null || !connectionPool.isConnected()) {
|
||||
PostgreSQLConnectionBuilder.createConnectionPool(
|
||||
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
|
||||
).also {
|
||||
this.connectionPool = it
|
||||
}
|
||||
} else {
|
||||
connectionPool
|
||||
}
|
||||
}
|
||||
return connection
|
||||
}
|
||||
|
||||
fun <T> inTransaction(f: (Connection) -> T) {
|
||||
fun disconnect() {
|
||||
connectionPool?.disconnect()
|
||||
}
|
||||
|
||||
fun <A> inTransaction(block: Connection.() -> A?): A? = connect().run {
|
||||
sendQuery("BEGIN")
|
||||
f(this)
|
||||
sendQuery("COMMIT")
|
||||
try {
|
||||
block().apply { sendQuery("COMMIT") }
|
||||
} catch (e: Throwable) {
|
||||
sendQuery("ROLLBACK")
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
/**
|
||||
* Select with unnamed parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
override fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (ResultSet, R?) -> Unit
|
||||
block: SelectCallback<R>
|
||||
): R? {
|
||||
val primaryObject = values.firstOrNull {
|
||||
it is EntityI<*> && typeReference.type.typeName == it::class.java.name
|
||||
} as R?
|
||||
val result = exec(sql, compileArgs(values))
|
||||
val json = result.getString(1)
|
||||
val result: QueryResult = exec(sql, compileArgs(values))
|
||||
if (result.rows.size == 0) throw DataNotFoundException(sql)
|
||||
|
||||
val json: String? = result.rows.firstOrNull()?.getString(0)
|
||||
return if (json === null) {
|
||||
null
|
||||
} else {
|
||||
if (primaryObject != null) {
|
||||
serializer.deserialize(json, primaryObject)
|
||||
} else {
|
||||
serializer.deserialize(json, typeReference)
|
||||
}
|
||||
serializer.deserialize(json, typeReference)
|
||||
}.also {
|
||||
block(result, it)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
/**
|
||||
* Select with named parameters
|
||||
*/
|
||||
override fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, R?) -> Unit
|
||||
block: SelectCallback<R>
|
||||
): R? {
|
||||
return replaceArgs(sql, values) {
|
||||
select(this.sql, typeReference, this.parameters, block)
|
||||
execute(this.sql, typeReference, parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(sql, object: TypeReference<R>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (ResultSet, List<R>) -> Unit
|
||||
): List<R> {
|
||||
val result = exec(sql, compileArgs(values))
|
||||
val json = result.getString(1)
|
||||
return if (json === null) {
|
||||
listOf<EntityI<*>>() as List<R>
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
}.also {
|
||||
block(result, it)
|
||||
override fun exec(sql: String, values: List<Any?>): QueryResult {
|
||||
val compiledValues = compileArgs(values)
|
||||
return stopwatchQuery(sql, compiledValues) {
|
||||
connect().sendPreparedStatement(replaceNamedArgByQuestionMark(sql), compiledValues).join()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, Paginated<R>) -> Unit
|
||||
): Paginated<R> {
|
||||
val offset = (page - 1) * limit
|
||||
val newValues = values
|
||||
.plus("offset" to offset)
|
||||
.plus("limit" to limit)
|
||||
|
||||
val line = replaceArgs(sql, newValues) {
|
||||
override fun exec(sql: String, values: Map<String, Any?>): QueryResult {
|
||||
return replaceArgs(sql, values) {
|
||||
exec(this.sql, this.parameters)
|
||||
}
|
||||
}
|
||||
|
||||
return line.run {
|
||||
val json = getString(1)
|
||||
val entities = if (json === null) {
|
||||
listOf<EntityI<*>>() as List<R>
|
||||
} else {
|
||||
serializer.deserializeList(json, typeReference)
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
override fun sendQuery(sql: String, values: List<Any?>): QueryResult {
|
||||
val compiledValues = compileArgs(values)
|
||||
return stopwatchQuery(sql, compiledValues) {
|
||||
replaceArgsIntoSql(sql, compiledValues) {
|
||||
connect().sendQuery(it).join()
|
||||
}
|
||||
Paginated(
|
||||
entities,
|
||||
offset,
|
||||
limit,
|
||||
getInt("total")
|
||||
)
|
||||
}.also {
|
||||
block(line, it)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(sql, page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, List<R>) -> Unit
|
||||
): List<R> {
|
||||
return replaceArgs(sql, values) {
|
||||
select(this.sql, typeReference, this.parameters, block)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(sql, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun exec(sql: String, values: List<Any?>): ResultSet {
|
||||
return stopwatchQuery(sql, values) {
|
||||
connect().prepareStatement(sql).apply {
|
||||
compileArgs(values).forEachIndexed { i, v ->
|
||||
when (v) {
|
||||
is String -> setString(i+1, v)
|
||||
is Int -> setInt(i+1, v)
|
||||
else -> setString(i+1, v.toString())
|
||||
}
|
||||
}
|
||||
}.executeQuery().apply { next() }
|
||||
}
|
||||
}
|
||||
|
||||
override fun exec(sql: String, values: Map<String, Any?>): ResultSet {
|
||||
return replaceArgs(sql, values) {
|
||||
exec(this.sql, this.parameters)
|
||||
}
|
||||
}
|
||||
|
||||
override fun sendQuery(sql: String, values: List<Any?>): Int {
|
||||
return stopwatchQuery(sql, values) {
|
||||
connect().prepareStatement(sql).apply {
|
||||
compileArgs(values).forEachIndexed { i, v ->
|
||||
when (v) {
|
||||
is String -> setString(i+1, v)
|
||||
is Int -> setInt(i+1, v)
|
||||
else -> setString(i+1, v.toString())
|
||||
}
|
||||
}
|
||||
}.executeUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
override fun sendQuery(sql: String, values: Map<String, Any?>): Int {
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
override fun sendQuery(sql: String, values: Map<String, Any?>): QueryResult {
|
||||
return replaceArgs(sql, values) {
|
||||
sendQuery(this.sql, this.parameters)
|
||||
}
|
||||
@@ -214,46 +126,148 @@ class Connection(
|
||||
|
||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||
return values.map {
|
||||
if (it is EntityI<*>) {
|
||||
serializer.serialize(it).apply {
|
||||
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
|
||||
}
|
||||
} else {
|
||||
it
|
||||
when {
|
||||
it == null -> it
|
||||
it is List<*> && it.isEmpty() -> it
|
||||
it is List<*> && it.first()!!::class.hasAnnotation<SqlSerializable>() -> serializer.serialize(it)
|
||||
it::class.hasAnnotation<SqlSerializable>() -> serializer.serialize(it)
|
||||
else -> it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> replaceArgs(sql: String, values: Map<String, Any?>, block: ParametersQuery.() -> T): T {
|
||||
val paramRegex = "(?<!:):([a-zA-Z0-9_-]+)".toRegex(RegexOption.IGNORE_CASE)
|
||||
val newArgs = paramRegex.findAll(sql).map { match ->
|
||||
val paramRegex = "(?<!:):([a-z0-9_-]+)".toRegex(RegexOption.IGNORE_CASE)
|
||||
val orderedArgs = paramRegex.findAll(sql).map { match ->
|
||||
val name = match.groups[1]!!.value
|
||||
values[name] ?: error("Parameter $name missing")
|
||||
values[name] ?: values[name.trimStart('_')] ?: queryError("""Parameter "$name" missing""", sql, values)
|
||||
}.toList()
|
||||
|
||||
var newSql = sql
|
||||
values.forEach { (key, _) ->
|
||||
val regex = ":$key".toRegex()
|
||||
newSql = newSql.replace(regex, "?")
|
||||
}
|
||||
return block(ParametersQuery(replaceNamedArgByQuestionMark(sql), orderedArgs))
|
||||
}
|
||||
|
||||
return block(ParametersQuery(newSql, newArgs))
|
||||
private fun replaceNamedArgByQuestionMark(sql: String): String =
|
||||
"(?<!:):([a-z0-9_-]+)"
|
||||
.toRegex(RegexOption.IGNORE_CASE)
|
||||
.replace(sql, "?")
|
||||
|
||||
private fun insertArgsValuesIntoSql(sql: String, values: List<Any?>): String {
|
||||
var i = 0
|
||||
|
||||
/* The regular expression matches a question mark "?" alone, not preceded or followed by another question mark */
|
||||
return """(?<!\?)(\?)(?!\?)"""
|
||||
.toRegex(RegexOption.IGNORE_CASE)
|
||||
.replace(sql) {
|
||||
values.getOrNull(i)
|
||||
?.toString()
|
||||
?.also { ++i }
|
||||
?.let(this::escapeParameter)
|
||||
?: queryError("Parameter $i missing", sql, values)
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> replaceArgsIntoSql(sql: String, values: List<Any?>, block: (String) -> T): T {
|
||||
return if (values.isNotEmpty()) {
|
||||
sql
|
||||
.let(this::replaceNamedArgByQuestionMark)
|
||||
.let { insertArgsValuesIntoSql(it, values) }
|
||||
.let(block)
|
||||
} else block(sql)
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape parameter by generate a random tag to prevent SQL injection
|
||||
*/
|
||||
private fun escapeParameter(parameter: String): String {
|
||||
val escapeTag = escapeTag().let {
|
||||
if (parameter.indexOf(it) >= 0) escapeParameter(parameter) else it
|
||||
}
|
||||
return """$escapeTag$parameter$escapeTag"""
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random alphaNum tag of 8 characters
|
||||
*/
|
||||
private fun escapeTag(): String {
|
||||
val charPool: List<Char> = ('a'..'z') + ('A'..'Z')
|
||||
val tagName = (1..8)
|
||||
.map { _ -> Random.nextInt(0, charPool.size) }
|
||||
.map(charPool::get)
|
||||
.joinToString("")
|
||||
return "\$$tagName\$"
|
||||
}
|
||||
|
||||
data class ParametersQuery(val sql: String, val parameters: List<Any?>)
|
||||
|
||||
private fun <T> stopwatchQuery(sql: String, values: List<Any?> = emptyList(), callback: () -> T): T {
|
||||
val sqlForLog = "\n${sql.prependIndent()}"
|
||||
try {
|
||||
val start = System.currentTimeMillis()
|
||||
val result = callback()
|
||||
val duration = (System.currentTimeMillis() - start)
|
||||
logger?.debug("$duration ms for query: $sqlForLog", values)
|
||||
val resultText = when (result) {
|
||||
null -> "with no result"
|
||||
is QueryResult -> result.rows.firstOrNull()?.joinToString(", ")?.let { text ->
|
||||
if (text.length > 100) "${text.take(100)}... (size: ${text.length})" else text
|
||||
} ?: "with no result"
|
||||
else -> "unknown"
|
||||
}
|
||||
val args = """
|
||||
|Query ($duration ms):
|
||||
|${sql.trimIndent().prependIndent()}
|
||||
|Arguments (${values.length}):
|
||||
|${values.joinToString("\n").ifBlank { "No arguments" }.prependIndent()}
|
||||
|Result:
|
||||
|${resultText.trimIndent().prependIndent()}
|
||||
""".trimMargin().prependIndent(" > ")
|
||||
logger?.debug("Query executed in $duration ms \n{}", args)
|
||||
return result
|
||||
} catch (e: Throwable) {
|
||||
logger?.info("Query Error: $sqlForLog, $values", e)
|
||||
logger?.info(
|
||||
"""
|
||||
|Query Error:
|
||||
|$sql,
|
||||
|Arguments (${values.length}):
|
||||
|${values.joinToString(", ").ifBlank { "No arguments" }.prependIndent()}
|
||||
""".trimMargin().prependIndent(" > "),
|
||||
e
|
||||
)
|
||||
throw e
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class QueryError(msg: String) : Exception(msg)
|
||||
|
||||
private fun queryError(
|
||||
msg: String,
|
||||
sql: String,
|
||||
parameters: List<Any?>,
|
||||
result: ResultSet? = null
|
||||
): Nothing = throw QueryError(
|
||||
"""
|
||||
|$msg
|
||||
|
|
||||
|${parameters.joinToString(", ") { it.toString() }.prependIndent(" > ")}
|
||||
|${sql.prependIndent(" > ")}
|
||||
|${result?.let { "-----" }?.prependIndent(" > ") ?: ""}
|
||||
|${result?.columnNames()?.joinToString(" | ")?.prependIndent(" > ") ?: ""}
|
||||
|${result?.map { it.joinToString(" | ") }?.joinToString("\n")?.prependIndent(" > ") ?: ""}
|
||||
""".trimMargin().trim(' ', '\n')
|
||||
)
|
||||
|
||||
private fun queryError(
|
||||
msg: String,
|
||||
sql: String,
|
||||
parameters: Map<String, Any?>,
|
||||
result: ResultSet? = null
|
||||
): Nothing = throw QueryError(
|
||||
"""
|
||||
|$msg
|
||||
|
|
||||
|${parameters.map { ":" + it.key + " = " + it.value }.joinToString(", ").prependIndent(" > ")}
|
||||
|${sql.prependIndent(" > ")}
|
||||
|${result?.let { "-----" }?.prependIndent(" > ") ?: ""}
|
||||
|${result?.columnNames()?.joinToString(" | ")?.prependIndent(" > ") ?: ""}
|
||||
|${result?.map { it.joinToString(" | ") }?.joinToString("\n")?.prependIndent(" > ") ?: ""}
|
||||
""".trimMargin().trim(' ', '\n')
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
class DataNotFoundException(val queryExecuted: String) : Exception() {
|
||||
override val message: String
|
||||
get() = "No data return for the query"
|
||||
}
|
||||
@@ -1,54 +1,46 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import java.sql.ResultSet
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
interface EmbedExecutable {
|
||||
sealed interface EmbedExecutable : Executable {
|
||||
val connection: Connection
|
||||
override fun toString(): String
|
||||
val name: String
|
||||
|
||||
/* Select One */
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
* Select with unnamed parameters
|
||||
*/
|
||||
fun <R: EntityI<*>> select(
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectOneCallback<R> = {}
|
||||
values: List<Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
/**
|
||||
* Select with named parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/* Select Miltiples */
|
||||
fun <R: EntityI<*>> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
/**
|
||||
* Select with named parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
vararg values: Pair<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
): R? =
|
||||
execute(typeReference, values.toMap(), block)
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
/* Select Paginated */
|
||||
fun <R: EntityI<*>> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R>
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): ResultSet
|
||||
fun exec(values: Map<String, Any?>): ResultSet
|
||||
fun sendQuery(values: List<Any?> = emptyList()): Int
|
||||
fun sendQuery(values: Map<String, Any?>): Int
|
||||
}
|
||||
fun exec(values: List<Any?>): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
fun exec(vararg values: Pair<String, Any?>): QueryResult = exec(values.toMap())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> EmbedExecutable.execute(
|
||||
values: List<Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> EmbedExecutable.execute(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(object : TypeReference<R>() {}, values, block)
|
||||
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> EmbedExecutable.execute(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(object : TypeReference<R>() {}, values = values, block)
|
||||
@@ -1,55 +1,3 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import java.sql.ResultSet
|
||||
|
||||
interface Executable {
|
||||
/* Select One */
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectOneCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/* Select Miltiples */
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): List<R>
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
fun <R: EntityI<*>> select(
|
||||
sql: String,
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R>
|
||||
|
||||
fun exec(sql: String, values: List<Any?> = emptyList()): ResultSet
|
||||
fun exec(sql: String, values: Map<String, Any?>): ResultSet
|
||||
fun sendQuery(sql: String, values: List<Any?> = emptyList()): Int
|
||||
fun sendQuery(sql: String, values: Map<String, Any?>): Int
|
||||
}
|
||||
sealed interface Executable
|
||||
|
||||
64
src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt
Normal file
64
src/main/kotlin/fr/postgresjson/connexion/ExecutableRaw.kt
Normal file
@@ -0,0 +1,64 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
typealias SelectCallback<R> = QueryResult.(R?) -> Unit
|
||||
|
||||
sealed interface ExecutableRaw : Executable {
|
||||
/**
|
||||
* Select with [List] of parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?> = emptyList(),
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select with [Map] of parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): R?
|
||||
|
||||
/**
|
||||
* Select with multiple [Pair] of parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
fun <R : Any> execute(
|
||||
sql: String,
|
||||
typeReference: TypeReference<R>,
|
||||
vararg values: Pair<String, Any?>,
|
||||
block: SelectCallback<R> = {}
|
||||
): R? = execute(sql, typeReference, values.toMap(), block)
|
||||
|
||||
fun <R : Any?> exec(sql: String, value: R): QueryResult = exec(sql, listOf(value))
|
||||
fun exec(sql: String, values: List<Any?>): QueryResult
|
||||
fun exec(sql: String, values: Map<String, Any?>): QueryResult
|
||||
fun exec(sql: String, vararg values: Pair<String, Any?>): QueryResult = exec(sql, values.toMap())
|
||||
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun <R : Any?> sendQuery(sql: String, value: R): QueryResult = sendQuery(sql, listOf(value))
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, values: List<Any?>): QueryResult
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, values: Map<String, Any?>): QueryResult
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(sql: String, vararg values: Pair<String, Any?>): QueryResult = sendQuery(sql, values.toMap())
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
/**
|
||||
* Select with unnamed parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> ExecutableRaw.execute(
|
||||
sql: String,
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select with named parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> ExecutableRaw.execute(
|
||||
sql: String,
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(sql, object : TypeReference<R>() {}, values, block)
|
||||
|
||||
/**
|
||||
* Select with named parameters
|
||||
*/
|
||||
@Throws(DataNotFoundException::class)
|
||||
inline fun <reified R : Any> ExecutableRaw.execute(
|
||||
sql: String,
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): R? =
|
||||
execute(sql, object : TypeReference<R>() {}, values = values, block)
|
||||
@@ -1,185 +1,52 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
import fr.postgresjson.definition.Function
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import java.sql.ResultSet
|
||||
|
||||
class Function(val definition: Function, override val connection: Connection): EmbedExecutable {
|
||||
class Function(val definition: Function, override val connection: Connection) : EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return definition.name
|
||||
}
|
||||
|
||||
override val name: String = definition.name
|
||||
|
||||
/* Select One */
|
||||
|
||||
/**
|
||||
* Select One entity with list of parameters
|
||||
* Select with [List] of parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (ResultSet, R?) -> Unit
|
||||
): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
value: R,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, listOf(value), block)
|
||||
connection.execute(compileSql(values), typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select One entity with named parameters
|
||||
* Select with named parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, R?) -> Unit
|
||||
): R? {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
): R? =
|
||||
selectOne(values.toMap(), block)
|
||||
|
||||
/* Select Multiples */
|
||||
connection.execute(compileSql(values), typeReference, values, block)
|
||||
|
||||
/**
|
||||
* Select list of entities with list of parameters
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (ResultSet, List<R>) -> Unit
|
||||
): List<R> {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
override fun exec(values: List<Any?>): QueryResult = connection.exec(compileSql(values), values)
|
||||
|
||||
/**
|
||||
* Select list of entities with named parameters
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, List<R>) -> Unit
|
||||
): List<R> {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(compileSql(values), values)
|
||||
|
||||
return connection.select(sql, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(values.toMap(), block)
|
||||
|
||||
/* Select Paginated */
|
||||
private fun <A : Any?> compileParameters(value: A): String = compileParameters(listOf(value))
|
||||
|
||||
/**
|
||||
* Select Multiple with pagination
|
||||
* Add cast to all parameters
|
||||
*/
|
||||
override fun <R: EntityI<*>> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, Paginated<R>) -> Unit
|
||||
): Paginated<R> {
|
||||
val offset = (page - 1) * limit
|
||||
val newValues = values
|
||||
.plus("offset" to offset)
|
||||
.plus("limit" to limit)
|
||||
|
||||
val args = compileArgs(newValues)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.select(sql, page, limit, typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
vararg values: Pair<String, Any?>,
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values.toMap(), block)
|
||||
|
||||
/* Execute function without traitements */
|
||||
|
||||
override fun exec(values: List<Any?>): ResultSet {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
|
||||
override fun exec(values: Map<String, Any?>): ResultSet {
|
||||
val args = compileArgs(values)
|
||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
||||
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
|
||||
override fun sendQuery(values: List<Any?>): Int {
|
||||
exec(values)
|
||||
return 0
|
||||
}
|
||||
|
||||
override fun sendQuery(values: Map<String, Any?>): Int {
|
||||
exec(values)
|
||||
return 0
|
||||
}
|
||||
|
||||
private fun compileArgs(values: List<Any?>): String {
|
||||
private fun compileParameters(values: List<Any?>): String {
|
||||
val placeholders = values
|
||||
.filterIndexed { index, value ->
|
||||
definition.parameters[index].default === null || value != null
|
||||
@@ -191,18 +58,34 @@ class Function(val definition: Function, override val connection: Connection): E
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
|
||||
private fun compileArgs(values: Map<String, Any?>): String {
|
||||
/**
|
||||
* Cast and add named parameters
|
||||
*/
|
||||
private fun compileParameters(values: Map<String, Any?>): String {
|
||||
val parameters = definition.getParametersIndexedByName()
|
||||
val placeholders = values
|
||||
.filter { entry ->
|
||||
val parameter = parameters[entry.key] ?: error("Parameter ${entry.key} not exist")
|
||||
val parameter = parameters[entry.key] ?: parameters["_" + entry.key] ?: error("Parameter ${entry.key} of function ${definition.name} not exist")
|
||||
parameter.default === null || entry.value !== null
|
||||
}
|
||||
.map { entry ->
|
||||
val parameter = parameters[entry.key]!!
|
||||
val parameter = parameters[entry.key] ?: parameters["_" + entry.key] ?: error("Parameter ${entry.key} of function ${definition.name} not exist")
|
||||
""""${parameter.name}" := :${parameter.name}::${parameter.type}"""
|
||||
}
|
||||
|
||||
return placeholders.joinToString(separator = ", ")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create SQL to call the function
|
||||
*/
|
||||
private fun <A : Any?> compileSql(value: A): String = "SELECT * FROM ${definition.name} (${compileParameters(value)})"
|
||||
/**
|
||||
* Create SQL to call the function
|
||||
*/
|
||||
private fun compileSql(values: List<Any?>): String = "SELECT * FROM ${definition.name} (${compileParameters(values)})"
|
||||
/**
|
||||
* Create SQL to call the function
|
||||
*/
|
||||
private fun compileSql(values: Map<String, Any?>): String = "SELECT * FROM ${definition.name} (${compileParameters(values)})"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
fun DefinitionFunction.toRunnable(connection: Connection): Function = Function(this, connection)
|
||||
|
||||
fun Sequence<DefinitionFunction>.toRunnable(connection: Connection): Sequence<Function> = map { it.toRunnable(connection) }
|
||||
|
||||
fun Sequence<Function>.toMutableMap(): MutableMap<String, Function> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
internal fun URI.toFunction(connection: Connection): MutableMap<String, Function> = searchSqlFiles()
|
||||
.filterIsInstance(DefinitionFunction::class.java)
|
||||
.toRunnable(connection)
|
||||
.toMutableMap()
|
||||
@@ -1,19 +0,0 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
data class Paginated<T: EntityI<*>>(
|
||||
val result: List<T>,
|
||||
val offset: Int,
|
||||
val limit: Int,
|
||||
val total: Int
|
||||
) {
|
||||
val currentPage: Int = (offset / limit) + 1
|
||||
val count: Int = result.size
|
||||
|
||||
init {
|
||||
if (offset < 0) error("offset must be greather or equal than 0")
|
||||
if (limit < 1) error("limit must be greather or equal than 1")
|
||||
if (total < 0) error("total must be greather or equal than 0")
|
||||
}
|
||||
}
|
||||
@@ -1,110 +1,55 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import java.sql.ResultSet
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
|
||||
|
||||
class Query(override val name: String, private val sql: String, override val connection: Connection): EmbedExecutable {
|
||||
class Query(override val name: String, private val sql: String, override val connection: Connection) : EmbedExecutable {
|
||||
override fun toString(): String {
|
||||
return sql
|
||||
}
|
||||
|
||||
/* Select One */
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
/**
|
||||
* Select with unnamed of parameters
|
||||
*/
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: List<Any?>,
|
||||
block: (ResultSet, R?) -> Unit
|
||||
): R? {
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
connection.execute(sql, typeReference, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
/**
|
||||
* Select with named parameters
|
||||
*/
|
||||
override fun <R : Any> execute(
|
||||
typeReference: TypeReference<R>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, R?) -> Unit
|
||||
): R? {
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
|
||||
inline fun <reified R: EntityI<*>> selectOne(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectOneCallback<R> = {}
|
||||
block: SelectCallback<R>
|
||||
): R? =
|
||||
select(object: TypeReference<R>() {}, values, block)
|
||||
connection.execute(sql, typeReference, values, block)
|
||||
|
||||
/* Select Multiples */
|
||||
/**
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun exec(values: List<Any?>): QueryResult = connection.exec(sql, values)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: List<Any?>,
|
||||
block: (ResultSet, List<R>) -> Unit
|
||||
): List<R> {
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
/**
|
||||
* Execute function without treatments
|
||||
*/
|
||||
override fun exec(values: Map<String, Any?>): QueryResult = connection.exec(sql, values)
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
values: List<Any?> = emptyList(),
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(values: List<Any?>): QueryResult = connection.sendQuery(sql, values)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, List<R>) -> Unit
|
||||
): List<R> {
|
||||
return connection.select(this.toString(), typeReference, values, block)
|
||||
}
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(values: Map<String, Any?>): QueryResult = connection.sendQuery(sql, values)
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
values: Map<String, Any?>,
|
||||
noinline block: SelectCallback<R> = {}
|
||||
): List<R> =
|
||||
select(object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
override fun <R: EntityI<*>> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
typeReference: TypeReference<List<R>>,
|
||||
values: Map<String, Any?>,
|
||||
block: (ResultSet, Paginated<R>) -> Unit
|
||||
): Paginated<R> {
|
||||
return connection.select(this.toString(), page, limit, typeReference, values, block)
|
||||
}
|
||||
|
||||
/* Select Paginated */
|
||||
|
||||
inline fun <reified R: EntityI<*>> select(
|
||||
page: Int,
|
||||
limit: Int,
|
||||
values: Map<String, Any?> = emptyMap(),
|
||||
noinline block: SelectPaginatedCallback<R> = {}
|
||||
): Paginated<R> =
|
||||
select(page, limit, object: TypeReference<List<R>>() {}, values, block)
|
||||
|
||||
/* Execute function without traitements */
|
||||
|
||||
override fun exec(values: List<Any?>): ResultSet {
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
|
||||
override fun exec(values: Map<String, Any?>): ResultSet {
|
||||
return connection.exec(sql, values)
|
||||
}
|
||||
|
||||
override fun sendQuery(values: List<Any?>): Int {
|
||||
return connection.sendQuery(sql, values)
|
||||
}
|
||||
|
||||
override fun sendQuery(values: Map<String, Any?>): Int {
|
||||
return connection.sendQuery(sql, values)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Warning: this method not use prepared statement
|
||||
*/
|
||||
fun sendQuery(vararg values: Pair<String, Any?>): QueryResult = sendQuery(values.toMap())
|
||||
}
|
||||
|
||||
28
src/main/kotlin/fr/postgresjson/connexion/QueryConverter.kt
Normal file
28
src/main/kotlin/fr/postgresjson/connexion/QueryConverter.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Query as QueryDefinition
|
||||
|
||||
/**
|
||||
* Convert [QueryDefinition], to runnable [Query]
|
||||
*/
|
||||
fun QueryDefinition.toRunnable(connection: Connection): Query = Query(name, script, connection)
|
||||
|
||||
/**
|
||||
* Convert Sequence of [QueryDefinition], to runnable Sequence of [Query]
|
||||
*/
|
||||
fun Sequence<QueryDefinition>.toRunnable(connection: Connection): Sequence<Query> = map { it.toRunnable(connection) }
|
||||
|
||||
/**
|
||||
* Convert Sequence of [Query], to [Map] of [Query] with name as key
|
||||
*/
|
||||
fun Sequence<Query>.toMutableMap(): MutableMap<String, Query> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
/**
|
||||
* Create a [Map] of [Query] from a [URI] pointing to the queries folder
|
||||
*/
|
||||
internal fun URI.toQuery(connection: Connection): MutableMap<String, Query> = searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.toRunnable(connection)
|
||||
.toMutableMap()
|
||||
@@ -1,105 +1,69 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import java.io.File
|
||||
import fr.postgresjson.definition.parse.parseFunction
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
import fr.postgresjson.definition.Query as QueryDefinition
|
||||
|
||||
class Requester(
|
||||
private val connection: Connection,
|
||||
private val queries: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
) {
|
||||
fun addQuery(query: Query): Requester {
|
||||
constructor(connection: Connection) : this(connection, mutableMapOf(), mutableMapOf())
|
||||
|
||||
constructor(
|
||||
connection: Connection,
|
||||
queriesDirectory: URI? = null,
|
||||
functionsDirectory: URI? = null
|
||||
) : this(
|
||||
connection = connection,
|
||||
queries = queriesDirectory?.toQuery(connection) ?: mutableMapOf(),
|
||||
functions = functionsDirectory?.toFunction(connection) ?: mutableMapOf(),
|
||||
)
|
||||
|
||||
fun addQuery(query: Query) {
|
||||
queries[query.name] = query
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(name: String, sql: String): Requester {
|
||||
fun addQuery(query: QueryDefinition) = addQuery(query.toRunnable(connection))
|
||||
|
||||
fun addQuery(name: String, sql: String) {
|
||||
addQuery(Query(name, sql, connection))
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(queriesDirectory: File): Requester {
|
||||
queriesDirectory.walk()
|
||||
.filter { it.isFile && it.extension == "sql" }
|
||||
.forEach {
|
||||
val path = it.parentFile.nameWithoutExtension
|
||||
addQuery("$path/${it.nameWithoutExtension}", it.readText())
|
||||
}
|
||||
return this
|
||||
fun addQuery(queriesDirectory: URI) {
|
||||
queriesDirectory
|
||||
.searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.forEach(this::addQuery)
|
||||
}
|
||||
|
||||
fun getQueries(): List<Query> {
|
||||
return queries.map { it.value }
|
||||
fun getQueries(): List<Query> = queries.map { it.value }
|
||||
|
||||
fun addFunction(definition: DefinitionFunction) {
|
||||
definition
|
||||
.run { toRunnable(connection) }
|
||||
.run { functions[name] = this }
|
||||
}
|
||||
|
||||
fun addFunction(definition: DefinitionFunction): Requester {
|
||||
functions[definition.name] = Function(definition, connection)
|
||||
return this
|
||||
fun addFunction(sql: String) {
|
||||
parseFunction(sql)
|
||||
.run { toRunnable(connection) }
|
||||
.run { functions[name] = this }
|
||||
}
|
||||
|
||||
fun addFunction(sql: String): Requester {
|
||||
DefinitionFunction(sql).let {
|
||||
functions[it.name] = Function(it, connection)
|
||||
}
|
||||
return this
|
||||
fun addFunctions(functionsDirectory: URI) {
|
||||
functionsDirectory.searchSqlFiles()
|
||||
.filterIsInstance(DefinitionFunction::class.java)
|
||||
.forEach(this::addFunction)
|
||||
}
|
||||
|
||||
fun addFunction(functionsDirectory: File): Requester {
|
||||
functionsDirectory.walk()
|
||||
.filter { it.isFile && it.extension == "sql" }
|
||||
.forEach {
|
||||
addFunction(it.readText())
|
||||
}
|
||||
return this
|
||||
}
|
||||
fun getFunction(name: String): Function = functions[name] ?: throw NoFunctionDefined(name)
|
||||
|
||||
fun getFunction(name: String): Function {
|
||||
if (functions[name] === null) {
|
||||
throw Exception("No function defined for $name")
|
||||
}
|
||||
return functions[name]!!
|
||||
}
|
||||
fun getQuery(path: String): Query = queries[path] ?: throw NoQueryDefined(path)
|
||||
|
||||
fun getQuery(path: String): Query {
|
||||
if (queries[path] === null) {
|
||||
throw Exception("No query defined in $path")
|
||||
}
|
||||
return queries[path]!!
|
||||
}
|
||||
|
||||
class RequesterFactory(
|
||||
private val connection: Connection,
|
||||
private val queriesDirectory: File? = null,
|
||||
private val functionsDirectory: File? = null
|
||||
) {
|
||||
constructor(
|
||||
host: String = "localhost",
|
||||
port: Int = 5432,
|
||||
database: String = "dc-project",
|
||||
username: String = "dc-project",
|
||||
password: String = "dc-project",
|
||||
queriesDirectory: File? = null,
|
||||
functionsDirectory: File? = null
|
||||
): this(
|
||||
Connection(host = host, port = port, database = database, username = username, password = password),
|
||||
queriesDirectory,
|
||||
functionsDirectory
|
||||
)
|
||||
|
||||
fun createRequester(): Requester {
|
||||
return initRequester(Requester(connection))
|
||||
}
|
||||
|
||||
private fun initRequester(req: Requester): Requester {
|
||||
if (queriesDirectory !== null) {
|
||||
req.addQuery(queriesDirectory)
|
||||
}
|
||||
|
||||
if (functionsDirectory !== null) {
|
||||
req.addFunction(functionsDirectory)
|
||||
}
|
||||
|
||||
return req
|
||||
}
|
||||
}
|
||||
class NoFunctionDefined(name: String) : Exception("No function defined for $name")
|
||||
class NoQueryDefined(path: String) : Exception("No query defined in $path")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
annotation class SqlSerializable
|
||||
@@ -1,69 +1,26 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.io.File
|
||||
import fr.postgresjson.definition.Parameter.Direction.IN
|
||||
import java.nio.file.Path
|
||||
|
||||
class Function(
|
||||
override val name: String,
|
||||
override val parameters: List<Parameter>,
|
||||
val returns: Returns,
|
||||
override val script: String,
|
||||
override val source: Path? = null,
|
||||
) : Resource, ParametersInterface {
|
||||
|
||||
open class Function(
|
||||
override val script: String
|
||||
): Resource, ParametersInterface {
|
||||
val returns: String?
|
||||
override val name: String
|
||||
override val parameters: List<Parameter>
|
||||
override var source: File? = null
|
||||
fun getDefinition(): String = parameters
|
||||
.filter { it.direction == IN }
|
||||
.joinToString(", ") { it.type.toString() }
|
||||
.let { "$name ($it)" }
|
||||
|
||||
init {
|
||||
val functionRegex =
|
||||
"""create (or replace )?(procedure|function) *(?<name>[^(\s]+)\s*\((?<params>(\s*((IN|OUT|INOUT|VARIADIC)?\s+)?([^\s,)]+\s+)?([^\s,)]+)(\s+(?:default\s|=)\s*[^\s,)]+)?\s*(,|(?=\))))*)\) *(?<return>RETURNS *[^ ]+)?"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
fun getParametersIndexedByName(): Map<String, Parameter> = parameters
|
||||
.withIndex()
|
||||
.associate { (key, param) -> Pair(param.name ?: "${key + 1}", param) }
|
||||
|
||||
val paramsRegex =
|
||||
"""\s*(?<param>((?<direction>IN|OUT|INOUT|VARIADIC)?\s+)?("?(?<name>[^\s,")]+)"?\s+)?(?<type>[^\s,)]+)(\s+(?<default>default\s|=)\s*[^\s,)]+)?)\s*(,|$)"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
|
||||
val queryMatch = functionRegex.find(script)
|
||||
if (queryMatch !== null) {
|
||||
val functionName = queryMatch.groups.get("name")?.value?.trim()
|
||||
val functionParameters = queryMatch.groups["params"]?.value?.trim()
|
||||
this.returns = queryMatch.groups["return"]?.value?.trim()
|
||||
|
||||
/* Create parameters definition */
|
||||
val parameters = if (functionParameters !== null) {
|
||||
val matchesParams = paramsRegex.findAll(functionParameters)
|
||||
matchesParams.map { paramsMatch ->
|
||||
Parameter(
|
||||
paramsMatch.groups["name"]!!.value.trim(),
|
||||
paramsMatch.groups["type"]!!.value.trim(),
|
||||
paramsMatch.groups["direction"]?.value?.trim(),
|
||||
paramsMatch.groups["default"]?.value?.trim()
|
||||
)
|
||||
}.toList()
|
||||
} else {
|
||||
listOf()
|
||||
}
|
||||
this.name = functionName!!
|
||||
this.parameters = parameters
|
||||
} else {
|
||||
throw FunctionNotFound()
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ParseException(message: String, cause: Throwable? = null): Exception(message, cause)
|
||||
class FunctionNotFound(cause: Throwable? = null): ParseException("Function not found in script", cause)
|
||||
|
||||
fun getDefinition(): String {
|
||||
return parameters
|
||||
.filter { it.direction == Parameter.Direction.IN }
|
||||
.joinToString(", ") { "${it.name} ${it.type}" }.let {
|
||||
"$name ($it) $returns"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getParametersIndexedByName(): Map<String, Parameter> {
|
||||
return parameters.map {
|
||||
it.name to it
|
||||
}.toMap()
|
||||
}
|
||||
operator fun get(name: String): Parameter? = parameters.firstOrNull { it.name == name }
|
||||
|
||||
infix fun `has same definition`(other: Function): Boolean {
|
||||
return other.getDefinition() == this.getDefinition()
|
||||
@@ -72,21 +29,4 @@ open class Function(
|
||||
infix fun `is different from`(other: Function): Boolean {
|
||||
return other.script != this.script
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun build(source: File): List<Function> {
|
||||
return source.readText()
|
||||
.split(
|
||||
"CREATE +(OR REPLACE +)?(PROCEDURE|FUNCTION)".toRegex(
|
||||
setOf(
|
||||
RegexOption.IGNORE_CASE,
|
||||
RegexOption.MULTILINE
|
||||
)
|
||||
)
|
||||
)
|
||||
.map {
|
||||
Function("CREATE OR REPLACE FUNCTION $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
33
src/main/kotlin/fr/postgresjson/definition/Migration.kt
Normal file
33
src/main/kotlin/fr/postgresjson/definition/Migration.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
class Migration(
|
||||
override val script: String,
|
||||
override var source: Path
|
||||
) : Resource {
|
||||
override val name: String
|
||||
val direction: Direction
|
||||
|
||||
init {
|
||||
this.direction = source.fileName.toString()
|
||||
.let {
|
||||
when {
|
||||
it.endsWith(".down.sql") -> Direction.DOWN
|
||||
it.endsWith(".up.sql") -> Direction.UP
|
||||
else -> throw MigrationNotFound()
|
||||
}
|
||||
}
|
||||
this.name = source.fileName.toString()
|
||||
.substringAfterLast("/")
|
||||
.let {
|
||||
when (direction) {
|
||||
Direction.DOWN -> it.substringBefore(".down.sql")
|
||||
Direction.UP -> it.substringBefore(".up.sql")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MigrationNotFound(cause: Throwable? = null) : Resource.ParseException("Migration not found in script", cause)
|
||||
enum class Direction { UP, DOWN }
|
||||
}
|
||||
@@ -1,33 +1,66 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
interface ParameterI {
|
||||
val name: String
|
||||
val type: String
|
||||
val direction: Parameter.Direction
|
||||
val default: String
|
||||
import java.util.Locale
|
||||
|
||||
class ParameterType(
|
||||
val name: String,
|
||||
val precision: Int? = null,
|
||||
val scale: Int? = null,
|
||||
val array: String? = null,
|
||||
) {
|
||||
val isArray: Boolean
|
||||
get() = array != null
|
||||
|
||||
override fun toString(): String {
|
||||
val type = if (precision == null && scale == null) {
|
||||
name
|
||||
} else if (scale == null) {
|
||||
"""$name($precision)"""
|
||||
} else {
|
||||
"""$name($precision, $scale)"""
|
||||
}
|
||||
|
||||
return type+array
|
||||
}
|
||||
}
|
||||
|
||||
class Parameter(val name: String, val type: String, direction: Direction? = Direction.IN, val default: Any? = null) {
|
||||
val direction: Direction
|
||||
interface ParameterSimpleI {
|
||||
val name: String?
|
||||
val type: ParameterType
|
||||
}
|
||||
|
||||
init {
|
||||
if (direction === null) {
|
||||
this.direction = Direction.IN
|
||||
} else {
|
||||
this.direction = direction
|
||||
}
|
||||
}
|
||||
|
||||
constructor(name: String, type: String, direction: String? = "IN", default: Any? = null): this(
|
||||
class Parameter(
|
||||
override val name: String?,
|
||||
override val type: ParameterType,
|
||||
val direction: Direction = Direction.IN,
|
||||
val default: String? = null,
|
||||
) : ParameterSimpleI {
|
||||
constructor(name: String?, type: ParameterType, direction: String = "IN", default: String? = null) : this(
|
||||
name = name,
|
||||
type = type,
|
||||
direction = direction?.let { Direction.valueOf(direction.toUpperCase()) },
|
||||
direction = direction.let { Direction.valueOf(direction.uppercase(Locale.getDefault())) },
|
||||
default = default
|
||||
)
|
||||
|
||||
enum class Direction { IN, OUT, INOUT }
|
||||
|
||||
override fun toString(): String {
|
||||
return buildString {
|
||||
append(direction.name.lowercase())
|
||||
if (name?.isNotBlank() == true) {
|
||||
append(" ")
|
||||
append(name)
|
||||
}
|
||||
append(" ")
|
||||
append(type.toString())
|
||||
if (default?.isNotBlank() == true) {
|
||||
append(" ")
|
||||
append(default)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ParametersInterface {
|
||||
val parameters: List<Parameter>
|
||||
}
|
||||
}
|
||||
|
||||
24
src/main/kotlin/fr/postgresjson/definition/Query.kt
Normal file
24
src/main/kotlin/fr/postgresjson/definition/Query.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
class Query(
|
||||
override val script: String,
|
||||
override var source: Path
|
||||
) : Resource {
|
||||
override val name: String = getNameFromComment(script) ?: getNameFromFile(source)
|
||||
|
||||
/** Try to get name from comment in file */
|
||||
private fun getNameFromComment(script: String): String? =
|
||||
"""-- *name ?: ?(?<name>[^ \n]+)"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
.find(script)?.let {
|
||||
it.groups["name"]?.value?.trim()
|
||||
}
|
||||
|
||||
/** Try to get name from the filename */
|
||||
private fun getNameFromFile(source: Path): String = source
|
||||
.fileName.toString()
|
||||
.substringAfterLast("/")
|
||||
.substringBeforeLast(".sql")
|
||||
}
|
||||
@@ -1,13 +1,37 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import fr.postgresjson.definition.parse.parseFunction
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
|
||||
interface Resource {
|
||||
sealed interface Resource {
|
||||
val name: String
|
||||
val script: String
|
||||
var source: File?
|
||||
}
|
||||
val source: Path?
|
||||
|
||||
interface ResourceCollection {
|
||||
val parameters: List<Parameter>
|
||||
}
|
||||
open class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
|
||||
|
||||
companion object {
|
||||
fun build(file: File): Resource =
|
||||
build(file.readText(), Path.of(file.toURI()))
|
||||
|
||||
fun build(url: URL): Resource =
|
||||
build(url.readText(), Path.of(url.toURI()))
|
||||
|
||||
fun build(resource: String, path: Path): Resource =
|
||||
try {
|
||||
Migration(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
parseFunction(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Query(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
throw ParseException("No SQL resource found")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
45
src/main/kotlin/fr/postgresjson/definition/Returns.kt
Normal file
45
src/main/kotlin/fr/postgresjson/definition/Returns.kt
Normal file
@@ -0,0 +1,45 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
sealed class Returns(
|
||||
val definition: String,
|
||||
val isSetOf: Boolean,
|
||||
) {
|
||||
class Primitive(
|
||||
definition: String,
|
||||
isSetOf: Boolean,
|
||||
) : Returns(definition, isSetOf) {
|
||||
val name = definition
|
||||
.trim('"')
|
||||
}
|
||||
|
||||
class PrimitiveList(
|
||||
definition: String,
|
||||
isSetOf: Boolean,
|
||||
) : Returns(definition, isSetOf) {
|
||||
val name = definition
|
||||
.drop(2)
|
||||
.trim('"')
|
||||
}
|
||||
|
||||
class Table(
|
||||
definition: String,
|
||||
isSetOf: Boolean,
|
||||
val parameters: List<ParameterTable>,
|
||||
) : Returns(definition, isSetOf) {
|
||||
class ParameterTable(
|
||||
override val name: String,
|
||||
override val type: ParameterType,
|
||||
) : ParameterSimpleI
|
||||
}
|
||||
|
||||
class Any(
|
||||
isSetOf: Boolean,
|
||||
) : Returns("any", isSetOf)
|
||||
|
||||
class Unknown(
|
||||
definition: String,
|
||||
isSetOf: Boolean,
|
||||
) : Returns(definition, isSetOf)
|
||||
|
||||
class Void : Returns("void", false)
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
package fr.postgresjson.definition.parse
|
||||
|
||||
import fr.postgresjson.definition.Function
|
||||
import fr.postgresjson.definition.Parameter
|
||||
import fr.postgresjson.definition.Parameter.Direction
|
||||
import fr.postgresjson.definition.Parameter.Direction.IN
|
||||
import fr.postgresjson.definition.Parameter.Direction.INOUT
|
||||
import fr.postgresjson.definition.Parameter.Direction.OUT
|
||||
import fr.postgresjson.definition.ParameterType
|
||||
import fr.postgresjson.definition.Resource.ParseException
|
||||
import fr.postgresjson.definition.Returns
|
||||
import fr.postgresjson.definition.Returns.Primitive
|
||||
import fr.postgresjson.definition.Returns.Unknown
|
||||
import fr.postgresjson.definition.Returns.Void
|
||||
import java.nio.file.Path
|
||||
import kotlin.text.RegexOption.IGNORE_CASE
|
||||
|
||||
internal fun parseFunction(script: String, source: Path? = null): Function {
|
||||
val name: String
|
||||
val parameters: List<Parameter>
|
||||
val returns: Returns
|
||||
ScriptPart(script)
|
||||
.getFunctionOrProcedure().trimSpace().nextScriptPart
|
||||
.getFunctionName().apply { name = value }.nextScriptPart
|
||||
.getParameters().apply { parameters = value }.nextScriptPart
|
||||
.getReturns().apply { returns = value }
|
||||
|
||||
return Function(name, parameters, returns, script, source)
|
||||
}
|
||||
|
||||
@Throws(FunctionNameMalformed::class)
|
||||
internal fun ScriptPart.getFunctionName(): NextScript<String> {
|
||||
try {
|
||||
return getNextScript { status.isNotEscaped() && afterBeginBy("(", " ", "\n") }
|
||||
.changeValue(String::unescapeOrLowercase)
|
||||
} catch (e: ParseException) {
|
||||
throw FunctionNameMalformed(this, e)
|
||||
}
|
||||
}
|
||||
|
||||
internal class FunctionNameMalformed(val script: ScriptPart, cause: Throwable? = null) :
|
||||
ParseException("Function name is malformed", cause)
|
||||
|
||||
@Throws(FunctionNotFound::class)
|
||||
internal fun ScriptPart.getFunctionOrProcedure(): NextScript<String> {
|
||||
val result = """create\s+(?:or\s+replace\s+)?(procedure|function)\s+"""
|
||||
.toRegex()
|
||||
.find(restOfScript)
|
||||
?: throw FunctionNotFound(this)
|
||||
|
||||
val rest = result.range.last
|
||||
.let { cursor -> restOfScript.drop(cursor + 1) }
|
||||
|
||||
return NextScript(
|
||||
result.groups[1]!!.value,
|
||||
rest
|
||||
)
|
||||
}
|
||||
|
||||
internal class FunctionNotFound(val script: ScriptPart) :
|
||||
ParseException("Function not found in script")
|
||||
|
||||
internal fun ScriptPart.getParameters(): NextScript<List<Parameter>> {
|
||||
val allParametersScript = this.getNextScript {
|
||||
currentChar == ')' && status.isNotEscaped()
|
||||
}
|
||||
val parameterList: List<Parameter> = allParametersScript
|
||||
.valueAsScriptPart()
|
||||
.removeParentheses()
|
||||
.split(",")
|
||||
.map { it.toParameter() }
|
||||
|
||||
return NextScript(parameterList, allParametersScript.restOfScript)
|
||||
}
|
||||
|
||||
private fun ScriptPart.toParameter(): Parameter {
|
||||
var script: ScriptPart = this.trimSpace()
|
||||
return Parameter(
|
||||
direction = script.getParameterMode().apply { script = nextScriptPart }.value,
|
||||
name = script.getParameterName().trimSpace().apply { script = nextScriptPart }.value.trim(),
|
||||
type = script.getParameterType().trimSpace().apply { script = nextScriptPart }.value,
|
||||
default = script.getParameterDefault().trimSpace().apply { script = nextScriptPart }.value,
|
||||
)
|
||||
}
|
||||
|
||||
private fun ScriptPart.getParameterMode(): NextScript<Direction> {
|
||||
return when {
|
||||
restOfScript.startsWith("inout ", true) -> NextScript(INOUT, restOfScript.drop("inout ".length))
|
||||
restOfScript.startsWith("in ", true) -> NextScript(IN, restOfScript.drop("in ".length))
|
||||
restOfScript.startsWith("out ", true) -> NextScript(OUT, restOfScript.drop("out ".length))
|
||||
else -> NextScript(IN, restOfScript)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(ParameterNameMalformed::class)
|
||||
private fun ScriptPart.getParameterName(): NextScript<String> {
|
||||
try {
|
||||
return getNextScript { afterBeginBy(" ", "\n") && status.isNotEscaped() }
|
||||
.changeValue(String::unescapeOrLowercase)
|
||||
} catch (e: ParseException) {
|
||||
throw ParameterNameMalformed(this, e)
|
||||
}
|
||||
}
|
||||
|
||||
private class ParameterNameMalformed(val script: ScriptPart, cause: Throwable) :
|
||||
ParseException("Parameter name is malformed", cause)
|
||||
|
||||
@Throws(ParameterTypeMalformed::class)
|
||||
private fun ScriptPart.getParameterType(): NextScript<ParameterType> {
|
||||
val fullType = try {
|
||||
val endTextList = arrayOf(" default ", "=")
|
||||
getNextScript { afterBeginBy(texts = endTextList) }
|
||||
} catch (e: ParseError) {
|
||||
throw ParameterTypeMalformed(this, e)
|
||||
}
|
||||
|
||||
var rest: ScriptPart = fullType.valueAsScriptPart()
|
||||
|
||||
val name = rest
|
||||
.getNextScript { afterBeginBy("(", "[") }
|
||||
.apply { rest = nextScriptPart }
|
||||
rest = rest.trimStart(' ', '\n', '\t', ',', '(')
|
||||
val precision = rest
|
||||
.getNextInteger()
|
||||
.apply { rest = nextScriptPart }
|
||||
rest = rest.trimStart(' ', '\n', '\t', ',')
|
||||
val scale = rest
|
||||
.getNextInteger()
|
||||
.apply { rest = nextScriptPart }
|
||||
rest = rest.trimStart(' ', '\n', '\t', ')')
|
||||
|
||||
val arrayDef = rest.restOfScript.trim().takeIf(String::isNotBlank)
|
||||
|
||||
return NextScript(
|
||||
ParameterType(
|
||||
name = name.value.trim().trim('[', ']'),
|
||||
precision = precision.value,
|
||||
scale = scale.value,
|
||||
array = arrayDef
|
||||
),
|
||||
fullType.nextScriptPart.restOfScript
|
||||
)
|
||||
}
|
||||
|
||||
internal class ParameterTypeMalformed(val script: ScriptPart, cause: Throwable) :
|
||||
ParseException("Parameter type is malformed", cause)
|
||||
|
||||
@Throws(ParameterDefaultMalformed::class)
|
||||
private fun ScriptPart.getParameterDefault(): NextScript<String?> {
|
||||
return if (this.isEmpty() || this.restOfScript == ")") {
|
||||
NextScript(null, "")
|
||||
} else {
|
||||
"""^(\s*=\s*|\s+default\s+)(.+)\s*$"""
|
||||
.toRegex(IGNORE_CASE)
|
||||
.find(restOfScript)
|
||||
.let { it ?: throw ParameterDefaultMalformed(this) }
|
||||
.let { it.groups[2]!!.value }
|
||||
.let { NextScript(it.trim(), "") }
|
||||
}
|
||||
}
|
||||
|
||||
private class ParameterDefaultMalformed(val script: ScriptPart) :
|
||||
ParseException("Parameter default is malformed")
|
||||
|
||||
/**
|
||||
* TODO Finalize this
|
||||
*/
|
||||
internal fun ScriptPart.getReturns(): NextScript<Returns> {
|
||||
val rest = this.trimSpace()
|
||||
if (!rest.restOfScript.startsWith("returns")) {
|
||||
return NextScript(Void(), "")
|
||||
}
|
||||
var returns = ScriptPart(rest.restOfScript.drop("returns".length))
|
||||
.getNextScript { this.afterBeginBy(Regex("\\s+language\\s+", IGNORE_CASE), Regex("\\s+as\\s+", IGNORE_CASE)) }
|
||||
.trimSpace()
|
||||
.value
|
||||
.trimStart()
|
||||
|
||||
val isSetOf = returns.startsWith("SETOF", ignoreCase = true)
|
||||
|
||||
if (isSetOf) {
|
||||
returns = returns.drop("SETOF".length).trimStart()
|
||||
}
|
||||
|
||||
val returnsClass = if (returns.isBlank()) {
|
||||
Void()
|
||||
} else if (primitiveList.contains(ScriptPart(returns).getParameterType().value.name)) {
|
||||
Primitive(returns, isSetOf)
|
||||
} else {
|
||||
Unknown(returns, isSetOf)
|
||||
}
|
||||
|
||||
return NextScript(returnsClass, "")
|
||||
}
|
||||
|
||||
private val primitiveList = listOf(
|
||||
"text",
|
||||
"varchar",
|
||||
"character varying",
|
||||
"character",
|
||||
"char",
|
||||
"int",
|
||||
"smallint",
|
||||
"integer",
|
||||
"bigint",
|
||||
"decimal",
|
||||
"real",
|
||||
"double precision",
|
||||
"float",
|
||||
"numeric",
|
||||
"boolean",
|
||||
"json",
|
||||
"jsonb",
|
||||
)
|
||||
|
||||
class ParseError(message: String? = null, cause: Throwable? = null) :
|
||||
ParseException(message ?: "Parsing fail", cause)
|
||||
@@ -0,0 +1,203 @@
|
||||
package fr.postgresjson.definition.parse
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
|
||||
import kotlin.contracts.contract
|
||||
|
||||
@JvmInline
|
||||
internal value class ScriptPart(val restOfScript: String) {
|
||||
fun copy(block: (String) -> String): ScriptPart {
|
||||
return ScriptPart(block(restOfScript))
|
||||
}
|
||||
|
||||
fun isEmpty() = restOfScript.isEmpty()
|
||||
}
|
||||
|
||||
internal class NextScript<T>(val value: T, val restOfScript: String) {
|
||||
val nextScriptPart: ScriptPart = ScriptPart(restOfScript)
|
||||
fun isLast() = restOfScript == ""
|
||||
fun isEmptyValue() = value == "" || value == null
|
||||
}
|
||||
|
||||
internal fun ScriptPart.removeParentheses(): ScriptPart {
|
||||
return if (restOfScript.take(1) == "(" && restOfScript.takeLast(1) == ")") {
|
||||
this.copy {
|
||||
it.drop(1).dropLast(1)
|
||||
}
|
||||
} else {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next part of script.
|
||||
* You can define a list of characters that end the part of script. Like `(` or space.
|
||||
*/
|
||||
@Throws(ParseError::class)
|
||||
internal fun ScriptPart.getNextScript(isEnd: Context.() -> Boolean = { false }): NextScript<String> {
|
||||
val status = Status()
|
||||
|
||||
for ((index, c) in restOfScript.withIndex()) {
|
||||
val prevChar = restOfScript.getOrNull(index - 1)
|
||||
val nextChar = restOfScript.getOrNull(index + 1)
|
||||
val nestedChars = listOf(prevChar, nextChar)
|
||||
|
||||
if (c == '"' && nestedChars.none { c == it }) {
|
||||
status.doubleQuoted = !status.doubleQuoted
|
||||
} else if (c == '\'' && nestedChars.none { c == it }) {
|
||||
status.simpleQuoted = !status.simpleQuoted
|
||||
}
|
||||
|
||||
if (status.isNotQuoted()) {
|
||||
when (c) {
|
||||
'(' -> status.parentheses++
|
||||
')' -> status.parentheses--
|
||||
'[' -> status.brackets++
|
||||
']' -> status.brackets--
|
||||
'{' -> status.braces++
|
||||
'}' -> status.braces--
|
||||
}
|
||||
}
|
||||
|
||||
if (isEnd(Context(index, c, status.copy(), restOfScript))) {
|
||||
return NextScript(
|
||||
restOfScript.take(index + 1),
|
||||
restOfScript.drop(index + 1),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (status.isNotEscaped()) {
|
||||
return NextScript(
|
||||
restOfScript.trim(),
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
throw ParseError()
|
||||
}
|
||||
|
||||
internal fun ScriptPart.unescapeOrLowercase(): ScriptPart = restOfScript
|
||||
.run(String::unescapeOrLowercase)
|
||||
.let(::ScriptPart)
|
||||
|
||||
internal fun String.unescapeOrLowercase(): String {
|
||||
val first = take(1)
|
||||
val last = takeLast(1)
|
||||
return if (first == last && first == "'") {
|
||||
drop(1).dropLast(1).replace("$first$first", first).lowercase()
|
||||
} else if (first == last && first == "\"") {
|
||||
drop(1).dropLast(1).replace("$first$first", first)
|
||||
} else {
|
||||
this.lowercase()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <T> NextScript<T>.trimSpace(): NextScript<T> {
|
||||
val spaces = charArrayOf(' ', '\n', '\t')
|
||||
return trim(chars = spaces)
|
||||
}
|
||||
|
||||
internal fun ScriptPart.trimSpace(): ScriptPart {
|
||||
for ((n, char) in restOfScript.withIndex()) {
|
||||
if (char !in listOf(' ', '\n', '\t')) {
|
||||
return ScriptPart(
|
||||
restOfScript.drop(n)
|
||||
)
|
||||
}
|
||||
}
|
||||
return ScriptPart(restOfScript)
|
||||
}
|
||||
|
||||
internal fun <T> NextScript<T>.trim(vararg chars: Char): NextScript<T> {
|
||||
return NextScript(value, restOfScript.apply { dropWhile { it in chars } })
|
||||
}
|
||||
|
||||
internal fun ScriptPart.trimStart(vararg chars: Char): ScriptPart {
|
||||
return this.change { dropWhile { it in chars } }
|
||||
}
|
||||
|
||||
internal fun ScriptPart.trimEnd(vararg chars: Char): ScriptPart {
|
||||
return this.change { dropLastWhile { it in chars } }
|
||||
}
|
||||
|
||||
internal fun ScriptPart.split(delimiter: String): List<ScriptPart> {
|
||||
val parts: MutableList<ScriptPart> = mutableListOf()
|
||||
var rest: ScriptPart = this
|
||||
do {
|
||||
rest = rest.trimSpace()
|
||||
.getNextScript { status.isNotEscaped() && currentChar.toString() == delimiter }
|
||||
.trimSpace()
|
||||
.also { parts.add(it.valueAsScriptPart().trimSpace().trimEnd(',')) }
|
||||
.nextScriptPart
|
||||
} while (!rest.isEmpty())
|
||||
|
||||
return parts
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value as ScriptPart
|
||||
*/
|
||||
internal fun NextScript<String>.valueAsScriptPart(): ScriptPart = ScriptPart(value)
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
internal inline fun ScriptPart.change(block: String.() -> String): ScriptPart {
|
||||
contract {
|
||||
callsInPlace(block, EXACTLY_ONCE)
|
||||
}
|
||||
return ScriptPart(restOfScript.run(block))
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
internal inline fun <T> NextScript<T>.changeValue(block: (T) -> T): NextScript<T> {
|
||||
contract {
|
||||
callsInPlace(block, EXACTLY_ONCE)
|
||||
}
|
||||
return NextScript(value.run(block), nextScriptPart.restOfScript)
|
||||
}
|
||||
|
||||
internal fun <T> NextScript<T>.changeScript(block: (String) -> String): NextScript<T> {
|
||||
return NextScript(value, block(restOfScript))
|
||||
}
|
||||
|
||||
internal fun <T> NextScript<T>.dropOneOf(vararg endTextList: String): NextScript<T> {
|
||||
return changeScript { script ->
|
||||
endTextList
|
||||
.filter { script.startsWith(it) }
|
||||
.let { script.drop(it.size) }
|
||||
}
|
||||
}
|
||||
|
||||
internal fun ScriptPart.getNextInteger(): NextScript<Int?> {
|
||||
val digits = restOfScript.takeWhile { it.isDigit() }
|
||||
val restOfScript = restOfScript.trimStart { it.isDigit() }
|
||||
return NextScript(digits.toIntOrNull(), restOfScript).trimSpace()
|
||||
}
|
||||
|
||||
internal data class Status(
|
||||
var doubleQuoted: Boolean = false, // "
|
||||
var simpleQuoted: Boolean = false, // '
|
||||
var parentheses: Int = 0, // ()
|
||||
var brackets: Int = 0, // []
|
||||
var braces: Int = 0, // {}
|
||||
) {
|
||||
fun isQuoted(): Boolean = doubleQuoted || simpleQuoted
|
||||
fun isNotQuoted(): Boolean = !isQuoted()
|
||||
fun isNotEscaped(): Boolean = isNotQuoted() && parentheses == 0 && brackets == 0 && braces == 0
|
||||
}
|
||||
|
||||
internal data class Context(
|
||||
val index: Int,
|
||||
val currentChar: Char,
|
||||
val status: Status,
|
||||
val script: String,
|
||||
) {
|
||||
fun afterBeginBy(vararg texts: String): Boolean = texts.any {
|
||||
script.drop(index + 1).take(it.length).lowercase() == it.lowercase()
|
||||
}
|
||||
fun afterBeginBy(vararg texts: Regex): Boolean = texts.any {
|
||||
it.matchAt(script, index + 1) != null
|
||||
}
|
||||
|
||||
val nextChar: Char? get() = script.substring(index + 1).getOrNull(0)
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class EntitiesCollections {
|
||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||
|
||||
fun <I, R: EntityI<I?>> get(id: I, className: KClass<R>): R? {
|
||||
val collection = collections[className]
|
||||
val entity = collection?.get(id!!)
|
||||
return entity as R?
|
||||
}
|
||||
|
||||
inline fun <I, reified R: EntityI<I?>> get(id: I): R? {
|
||||
return get(id, R::class)
|
||||
}
|
||||
|
||||
fun <I, R: EntityI<out I?>> set(entity: R): EntitiesCollections {
|
||||
if (collections[entity.className] == null) {
|
||||
collections[entity.className] = EntityCollection()
|
||||
}
|
||||
|
||||
collections[entity.className]!!.set(entity as EntityI<Any?>)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
class EntityCollection<T, E: EntityI<T?>> {
|
||||
private var collection: MutableMap<T, E> = mutableMapOf()
|
||||
|
||||
fun get(id: T): E? {
|
||||
return collection[id]
|
||||
}
|
||||
|
||||
fun set(entity: E) {
|
||||
val id = entity.id
|
||||
if (id !== null) {
|
||||
collection[id] = entity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/* ID */
|
||||
interface EntityI<T> {
|
||||
var id: T?
|
||||
val className: KClass<EntityI<T?>>
|
||||
@JsonIgnore() get() = this::class as KClass<EntityI<T?>>
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override var id: T? = null): EntityI<T?>
|
||||
abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID?>(id)
|
||||
abstract class IdEntity(override var id: Int? = null): Entity<Int?>(id)
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<T> {
|
||||
var version: T
|
||||
}
|
||||
|
||||
interface EntityVersioningIncrement: EntityVersioning<Int?>
|
||||
class EntityVersioningIncrementImp: EntityVersioningIncrement {
|
||||
override var version: Int? = null
|
||||
}
|
||||
|
||||
interface EntityVersioningDate: EntityVersioning<DateTime?>
|
||||
class EntityVersioningDateImp: EntityVersioningDate {
|
||||
override var version: DateTime? = null
|
||||
}
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
var createdAt: DateTime?
|
||||
}
|
||||
|
||||
interface EntityUpdatedAt {
|
||||
var updatedAt: DateTime?
|
||||
}
|
||||
|
||||
class EntityCreatedAtImp: EntityCreatedAt {
|
||||
override var createdAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityUpdatedAtImp: EntityUpdatedAt {
|
||||
override var updatedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Author */
|
||||
interface CreatedBy<T: EntityI<*>> {
|
||||
var createdBy: T?
|
||||
}
|
||||
|
||||
interface UpdatedBy<T: EntityI<*>> {
|
||||
var updatedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT: EntityI<*>>: CreatedBy<UserT> {
|
||||
override var createdBy: UserT? = null
|
||||
}
|
||||
|
||||
class EntityUpdatedByImp<UserT: EntityI<*>>: UpdatedBy<UserT> {
|
||||
override var updatedBy: UserT? = null
|
||||
}
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT: EntityI<*>> {
|
||||
var publishedAt: DateTime?
|
||||
var publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT: EntityI<*>>: Published<UserT> {
|
||||
override var publishedAt: DateTime? = null
|
||||
override var publishedBy: UserT? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT: EntityI<*>>: Entity<T>(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(),
|
||||
CreatedBy<UserT> by EntityCreatedByImp(),
|
||||
UpdatedBy<UserT> by EntityUpdatedByImp()
|
||||
|
||||
abstract class EntityExtended<T, UserT: EntityI<*>>:
|
||||
EntityImp<T, UserT>(),
|
||||
EntityVersioningIncrement by EntityVersioningIncrementImp(),
|
||||
Published<UserT> by EntityPublishedImp()
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package fr.postgresjson.functionGenerator
|
||||
|
||||
import fr.postgresjson.definition.Function
|
||||
import fr.postgresjson.definition.Parameter
|
||||
import fr.postgresjson.definition.Parameter.Direction.IN
|
||||
import fr.postgresjson.definition.Parameter.Direction.INOUT
|
||||
import fr.postgresjson.definition.Parameter.Direction.OUT
|
||||
import fr.postgresjson.definition.Returns
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import fr.postgresjson.utils.toCamelCase
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.File
|
||||
import java.net.URI
|
||||
|
||||
class FunctionGenerator(private val functionsDirectories: List<URI>) {
|
||||
constructor(functionsDirectories: URI) : this(listOf(functionsDirectories))
|
||||
|
||||
private val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
||||
|
||||
private fun List<Parameter>.toKotlinArgs(): String {
|
||||
return filter { it.direction == IN || it.direction == INOUT }
|
||||
.mapIndexed { index, parameter -> index to parameter }
|
||||
.joinToString(", ") { (idx, param) ->
|
||||
val base = """${param.kotlinName ?: "arg$idx"}: ${param.kotlinType}"""
|
||||
val default = if (param.default == null) {
|
||||
""
|
||||
} else {
|
||||
when (param.kotlinType) {
|
||||
"String" -> """ = "${param.default.trim('\'')}""""
|
||||
"Int" -> """ = ${param.default}"""
|
||||
"Boolean" -> """ = ${param.default.lowercase()}"""
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
base + default
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<Parameter>.toMapOf(): String {
|
||||
return filter { it.direction == IN || it.direction == INOUT }
|
||||
.joinToString(", ", prefix = "mapOf(", postfix = ")") { """"${it.kotlinName}" to ${it.kotlinName}""" }
|
||||
}
|
||||
|
||||
private val Parameter.kotlinType: String
|
||||
get() {
|
||||
return when (type.name.lowercase()) {
|
||||
"text" -> "String"
|
||||
"varchar" -> "String"
|
||||
"character varying" -> "String"
|
||||
"character" -> "String"
|
||||
"char" -> "String"
|
||||
"int" -> "Int"
|
||||
"smallint" -> "Int"
|
||||
"integer" -> "Int"
|
||||
"bigint" -> "Int"
|
||||
"decimal" -> "Float"
|
||||
"real" -> "Float"
|
||||
"double precision" -> "Float"
|
||||
"float" -> "Float"
|
||||
"numeric" -> "Number"
|
||||
"boolean" -> "Boolean"
|
||||
"json" -> "S"
|
||||
"jsonb" -> "S"
|
||||
"any" -> "Any"
|
||||
"anyelement" -> "Any"
|
||||
"anyarray" -> "List<*>"
|
||||
else -> "String"
|
||||
}
|
||||
}
|
||||
|
||||
private val Parameter.kotlinName: String?
|
||||
get() {
|
||||
return name?.toCamelCase()?.trimStart('_')
|
||||
}
|
||||
|
||||
private val Function.kotlinName: String
|
||||
get() {
|
||||
return name.toCamelCase().trimStart('_')
|
||||
}
|
||||
|
||||
private val functions: List<Function>
|
||||
get() = functionsDirectories
|
||||
.flatMap { it.searchSqlFiles() }
|
||||
.filterIsInstance<Function>()
|
||||
|
||||
fun generate(outputDirectory: URI) {
|
||||
File(outputDirectory.path).apply {
|
||||
logger.debug("Create Directory: $absolutePath")
|
||||
mkdirs()
|
||||
}
|
||||
|
||||
functions
|
||||
.map { function ->
|
||||
File("${outputDirectory.path}${function.kotlinName}.kt").apply {
|
||||
writeText(generate(function))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun generate(functionName: String): String {
|
||||
return functions
|
||||
.first { it.name == functionName }
|
||||
.let { generate(it) }
|
||||
}
|
||||
|
||||
fun generate(function: Function): String = function.run {
|
||||
val args = parameters.toKotlinArgs()
|
||||
|
||||
val hasInputArgs: Boolean = parameters.filter { it.direction != OUT }.any { it.kotlinType == "S" }
|
||||
val hasReturn: Boolean = parameters.any { it.direction != IN } || (returns !is Returns.Void)
|
||||
|
||||
val generics = mutableListOf<String>()
|
||||
if (hasReturn) generics.add("reified E: Any")
|
||||
if (hasInputArgs) generics.add("S: Any?")
|
||||
|
||||
val functionDecl = if (generics.isNotEmpty()) "inline fun <${generics.joinToString(", ")}>" else "fun"
|
||||
|
||||
return if (hasReturn) {
|
||||
"""
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import com.fasterxml.jackson.core.type.TypeReference
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|
|
||||
|$functionDecl Requester.$kotlinName($args): E? {
|
||||
| return getFunction("$name")
|
||||
| .execute<E>(object : TypeReference<E>() {}, ${parameters.toMapOf()})
|
||||
|}
|
||||
""".trimMargin()
|
||||
} else {
|
||||
"""
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|
|
||||
|$functionDecl Requester.$kotlinName($args): Unit {
|
||||
| getFunction("$name")
|
||||
| .exec(${parameters.toMapOf()})
|
||||
|}
|
||||
""".trimMargin()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
package fr.postgresjson.migration
|
||||
|
||||
import com.github.jasync.sql.db.postgresql.exceptions.GenericDatabaseException
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.definition.parse.parseFunction
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import java.util.*
|
||||
import java.util.Date
|
||||
import java.util.concurrent.CompletionException
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
data class Function(
|
||||
@@ -11,7 +15,7 @@ data class Function(
|
||||
val down: DefinitionFunction,
|
||||
private val connection: Connection,
|
||||
override var executedAt: Date? = null
|
||||
): Migration {
|
||||
) : Migration {
|
||||
val name = up.name
|
||||
override var doExecute: Action? = null
|
||||
|
||||
@@ -26,61 +30,57 @@ data class Function(
|
||||
down: String,
|
||||
connection: Connection,
|
||||
executedAt: Date? = null
|
||||
): this(
|
||||
DefinitionFunction(up),
|
||||
DefinitionFunction(down),
|
||||
) : this(
|
||||
parseFunction(up),
|
||||
parseFunction(down),
|
||||
connection,
|
||||
executedAt
|
||||
)
|
||||
|
||||
override fun up(): Status {
|
||||
connection.sendQuery(up.script)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/insertFunction.sql")!!.readText().let {
|
||||
connection.selectOne<MigrationEntity>(it, listOf(up.name, up.getDefinition(), up.script, down.script))?.let { function ->
|
||||
executedAt = function.executedAt
|
||||
doExecute = Action.OK
|
||||
return try {
|
||||
try {
|
||||
connection.sendQuery(up.script)
|
||||
} catch (e: CompletionException) {
|
||||
val cause = e.cause
|
||||
if (cause is GenericDatabaseException && cause.errorMessage.fields['C'] == "42P13") {
|
||||
connection.sendQuery("drop function ${down.getDefinition()}")
|
||||
connection.sendQuery(up.script)
|
||||
}
|
||||
}
|
||||
|
||||
this::class.java.classLoader
|
||||
.getResource("sql/migration/insertFunction.sql")!!.readText()
|
||||
.let { connection.execute<MigrationEntity>(it, listOf(up.name, up.getDefinition(), up.script, down.script)) }
|
||||
?.let { migration: MigrationEntity ->
|
||||
executedAt = migration.executedAt
|
||||
doExecute = Action.OK
|
||||
} ?: error("No migration executed")
|
||||
|
||||
Status.OK
|
||||
} catch (e: Throwable) {
|
||||
Status.UP_FAIL
|
||||
}
|
||||
return Status.OK
|
||||
}
|
||||
|
||||
override fun down(): Status {
|
||||
connection.sendQuery(down.script)
|
||||
return try {
|
||||
connection.sendQuery(down.script)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
|
||||
connection.sendQuery(it, listOf(down))
|
||||
}
|
||||
return Status.OK
|
||||
}
|
||||
this::class.java.classLoader
|
||||
.getResource("sql/migration/deleteFunction.sql")!!
|
||||
.readText()
|
||||
.let { connection.sendQuery(it, listOf(down.name)) }
|
||||
|
||||
override fun test(): Status {
|
||||
connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
it.sendQuery("ROLLBACK")
|
||||
}
|
||||
|
||||
return Status.OK // TODO
|
||||
}
|
||||
|
||||
override fun status(): Status {
|
||||
val result = connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
it.sendQuery("ROLLBACK")
|
||||
}
|
||||
|
||||
return Status.OK // TODO
|
||||
}
|
||||
|
||||
fun copy(): Function {
|
||||
return this.copy(up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||
it.doExecute = this.doExecute
|
||||
Status.OK
|
||||
} catch (e: Throwable) {
|
||||
Status.DOWN_FAIL
|
||||
}
|
||||
}
|
||||
|
||||
infix fun `is different from`(other: DefinitionFunction): Boolean {
|
||||
return other.script != this.up.script
|
||||
}
|
||||
}
|
||||
fun copy(): Function = this
|
||||
.copy(up = up, down = down, connection = connection, executedAt = executedAt)
|
||||
.also { it.doExecute = this.doExecute }
|
||||
|
||||
infix fun `is different from`(other: DefinitionFunction): Boolean = other.script != this.up.script
|
||||
}
|
||||
|
||||
@@ -2,16 +2,17 @@ package fr.postgresjson.migration
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.definition.Function.FunctionNotFound
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.definition.parse.parseFunction
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import org.slf4j.Logger
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.util.*
|
||||
import java.net.URI
|
||||
import java.util.Date
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
import fr.postgresjson.definition.Migration as DefinitionMigration
|
||||
|
||||
class MigrationEntity(
|
||||
val filename: String,
|
||||
@@ -19,33 +20,42 @@ class MigrationEntity(
|
||||
val up: String,
|
||||
val down: String,
|
||||
val version: Int
|
||||
): Entity<String?>(filename)
|
||||
)
|
||||
|
||||
interface Migration {
|
||||
var executedAt: Date?
|
||||
var doExecute: Action?
|
||||
fun up(): Status
|
||||
fun down(): Status
|
||||
fun test(): Status
|
||||
fun status(): Status
|
||||
|
||||
enum class Status(i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) }
|
||||
enum class Status(val i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) }
|
||||
enum class Action { OK, UP, DOWN }
|
||||
}
|
||||
|
||||
data class Migrations private constructor(
|
||||
class MigrationExecutor private constructor(
|
||||
private val connection: Connection,
|
||||
private val queries: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val migrationsScripts: MutableMap<String, MigrationScript> = mutableMapOf(),
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
) {
|
||||
private var directories: List<URI> = emptyList()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
constructor(directory: File, connection: Connection): this(listOf(directory), connection)
|
||||
constructor(directory: URI, connection: Connection) : this(listOf(directory), connection)
|
||||
constructor(connection: Connection, vararg directory: URI) : this(directory.toList(), connection)
|
||||
|
||||
constructor(directories: List<File>, connection: Connection): this(connection) {
|
||||
constructor(directories: List<URI>, connection: Connection) : this(connection) {
|
||||
initDB()
|
||||
getMigrationFromDB()
|
||||
getMigrationFromDirectory(directories)
|
||||
queries.forEach { (_, query) ->
|
||||
this.directories = directories
|
||||
reset()
|
||||
}
|
||||
|
||||
private fun reset() {
|
||||
migrationsScripts.clear()
|
||||
functions.clear()
|
||||
|
||||
addMigrationFromDB()
|
||||
addMigrationFromDirectory(directories)
|
||||
|
||||
migrationsScripts.forEach { (_, query) ->
|
||||
if (query.doExecute === null) {
|
||||
query.doExecute = Action.DOWN
|
||||
}
|
||||
@@ -63,18 +73,18 @@ data class Migrations private constructor(
|
||||
/**
|
||||
* Get all migration from DB
|
||||
*/
|
||||
private fun getMigrationFromDB() {
|
||||
private fun addMigrationFromDB() {
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
|
||||
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
|
||||
.map { function ->
|
||||
connection.execute(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
?.map { function ->
|
||||
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
||||
}
|
||||
}
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
|
||||
.map { query ->
|
||||
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
connection.execute(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
?.map { query ->
|
||||
migrationsScripts[query.filename] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,38 +92,35 @@ data class Migrations private constructor(
|
||||
/**
|
||||
* Get all migration from multiples Directories
|
||||
*/
|
||||
private fun getMigrationFromDirectory(directory: List<File>) {
|
||||
directory.forEach {
|
||||
getMigrationFromDirectory(it)
|
||||
private fun addMigrationFromDirectory(directories: List<URI>) {
|
||||
directories.forEach {
|
||||
addMigrationFromDirectory(it)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all migration from Directory
|
||||
*/
|
||||
private fun getMigrationFromDirectory(directory: File) {
|
||||
directory.walk().filter {
|
||||
it.isFile
|
||||
}.forEach { file ->
|
||||
if (file.name.endsWith(".up.sql")) {
|
||||
file.path.substring(0, file.path.length - 7).let {
|
||||
try {
|
||||
val down = File("$it.down.sql").readText()
|
||||
val up = file.readText()
|
||||
val name = file.name.substring(0, file.name.length - 7)
|
||||
addQuery(name, up, down)
|
||||
} catch (e: FileNotFoundException) {
|
||||
throw DownMigrationNotDefined("$it.down.sql", e)
|
||||
}
|
||||
private fun addMigrationFromDirectory(directory: URI) {
|
||||
val downs: MutableMap<String, DefinitionMigration> = mutableMapOf()
|
||||
|
||||
directory.searchSqlFiles().apply {
|
||||
/* Set Down Migration */
|
||||
forEach { migration ->
|
||||
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.DOWN) {
|
||||
downs += migration.name to migration
|
||||
}
|
||||
} else if (file.name.endsWith(".down.sql")) {
|
||||
// Nothing
|
||||
} else {
|
||||
val fileContent = file.readText()
|
||||
try {
|
||||
addFunction(fileContent)
|
||||
} catch(e: FunctionNotFound) {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
/* Set up migrations and functions */
|
||||
forEach { migration ->
|
||||
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.UP) {
|
||||
val down = downs[migration.name] ?: throw DownMigrationNotDefined(migration.name + ".down.sql")
|
||||
downs -= migration.name
|
||||
|
||||
addMigrationScript(migration, down)
|
||||
} else if (migration is DefinitionFunction) {
|
||||
addFunction(migration)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,13 +128,14 @@ data class Migrations private constructor(
|
||||
|
||||
enum class Direction { UP, DOWN }
|
||||
|
||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException):
|
||||
Throwable("The file $path whas not found", cause)
|
||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException? = null) :
|
||||
Throwable("The file $path was not found", cause)
|
||||
|
||||
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
|
||||
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): MigrationExecutor {
|
||||
val currentFunction = functions[newDefinition.name]
|
||||
if (currentFunction === null || currentFunction `is different from` newDefinition) {
|
||||
functions[newDefinition.name] = Function(newDefinition, newDefinition, connection).apply {
|
||||
val oldDefinition = functions[newDefinition.name]?.up ?: newDefinition
|
||||
functions[newDefinition.name] = Function(newDefinition, oldDefinition, connection).apply {
|
||||
doExecute = Action.UP
|
||||
}
|
||||
} else {
|
||||
@@ -139,33 +147,36 @@ data class Migrations private constructor(
|
||||
return this
|
||||
}
|
||||
|
||||
fun addFunction(sql: String): Migrations {
|
||||
addFunction(DefinitionFunction(sql))
|
||||
fun addFunction(sql: String): MigrationExecutor {
|
||||
addFunction(parseFunction(sql))
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(name: String, up: String, down: String, callback: (Query) -> Unit = {}): Migrations {
|
||||
if (queries[name] === null) {
|
||||
queries[name] = Query(name, up, down, connection).apply {
|
||||
fun addMigrationScript(up: DefinitionMigration, down: DefinitionMigration, callback: (MigrationScript) -> Unit = {}): MigrationExecutor =
|
||||
addMigrationScript(up.name, up.script, down.script, callback)
|
||||
|
||||
fun addMigrationScript(name: String, up: String, down: String, callback: (MigrationScript) -> Unit = {}): MigrationExecutor {
|
||||
if (migrationsScripts[name] === null) {
|
||||
migrationsScripts[name] = MigrationScript(name, up, down, connection).apply {
|
||||
doExecute = Action.UP
|
||||
}
|
||||
} else {
|
||||
queries[name]!!.apply {
|
||||
migrationsScripts[name]!!.apply {
|
||||
doExecute = Action.OK
|
||||
}
|
||||
}
|
||||
|
||||
callback(queries[name]!!)
|
||||
callback(migrationsScripts[name]!!)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
private fun initDB() {
|
||||
if (!initialized) {
|
||||
this::class.java.classLoader.getResource("sql/migration/createHistoryShema.sql")!!.readText().let {
|
||||
this::class.java.classLoader.getResource("sql/migration/createHistorySchema.sql")!!.readText().let {
|
||||
connection.sendQuery(it)
|
||||
}
|
||||
this::class.java.classLoader.getResource("sql/migration/createFunctionShema.sql")!!.readText().let {
|
||||
this::class.java.classLoader.getResource("sql/migration/createFunctionSchema.sql")!!.readText().let {
|
||||
connection.sendQuery(it)
|
||||
}
|
||||
initialized = true
|
||||
@@ -180,7 +191,7 @@ data class Migrations private constructor(
|
||||
|
||||
internal fun up(): Map<String, Status> {
|
||||
val list: MutableMap<String, Status> = mutableMapOf()
|
||||
queries.forEach {
|
||||
migrationsScripts.forEach {
|
||||
it.value.let { query ->
|
||||
if (query.doExecute == Action.UP) {
|
||||
query.up().let { status ->
|
||||
@@ -205,7 +216,7 @@ data class Migrations private constructor(
|
||||
|
||||
internal fun down(force: Boolean = false): Map<String, Status> {
|
||||
val list: MutableMap<String, Status> = mutableMapOf()
|
||||
queries.forEach {
|
||||
migrationsScripts.forEach {
|
||||
it.value.let { query ->
|
||||
if (query.doExecute == Action.DOWN || force) {
|
||||
query.down().let { status ->
|
||||
@@ -243,6 +254,7 @@ data class Migrations private constructor(
|
||||
sendQuery("COMMIT")
|
||||
}
|
||||
logger?.info("Migration done")
|
||||
reset()
|
||||
|
||||
return list.toMap()
|
||||
}
|
||||
@@ -263,6 +275,7 @@ data class Migrations private constructor(
|
||||
sendQuery("COMMIT")
|
||||
}
|
||||
logger?.info("Migration DOWN done")
|
||||
reset()
|
||||
|
||||
return list.toMap()
|
||||
}
|
||||
@@ -283,8 +296,8 @@ data class Migrations private constructor(
|
||||
return list.toMap()
|
||||
}
|
||||
|
||||
fun copy(): Migrations {
|
||||
val queriesCopy = queries.map {
|
||||
private fun copy(): MigrationExecutor {
|
||||
val queriesCopy = migrationsScripts.map {
|
||||
it.key to it.value.copy()
|
||||
}.toMap().toMutableMap()
|
||||
|
||||
@@ -292,10 +305,6 @@ data class Migrations private constructor(
|
||||
it.key to it.value.copy()
|
||||
}.toMap().toMutableMap()
|
||||
|
||||
return Migrations(connection, queriesCopy, functionsCopy)
|
||||
return MigrationExecutor(connection, queriesCopy, functionsCopy)
|
||||
}
|
||||
|
||||
fun status(): Map<String, Int> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
50
src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt
Normal file
50
src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt
Normal file
@@ -0,0 +1,50 @@
|
||||
package fr.postgresjson.migration
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import java.util.Date
|
||||
|
||||
data class MigrationScript(
|
||||
val name: String,
|
||||
val up: String,
|
||||
val down: String,
|
||||
private val connection: Connection,
|
||||
override var executedAt: Date? = null
|
||||
) : Migration {
|
||||
override var doExecute: Action? = null
|
||||
|
||||
override fun up(): Status {
|
||||
return try {
|
||||
connection.sendQuery(up)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let { sqlScript ->
|
||||
connection.execute<MigrationEntity>(sqlScript, listOf(name, up, down))?.let { query ->
|
||||
executedAt = query.executedAt
|
||||
doExecute = Action.OK
|
||||
} ?: error("No migration executed")
|
||||
}
|
||||
|
||||
Status.OK
|
||||
} catch (e: Throwable) {
|
||||
Status.UP_FAIL
|
||||
}
|
||||
}
|
||||
|
||||
override fun down(): Status {
|
||||
connection.sendQuery(down)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
|
||||
connection.exec(it, listOf(name))
|
||||
}
|
||||
|
||||
return Status.OK
|
||||
}
|
||||
|
||||
fun copy(): MigrationScript {
|
||||
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||
it.doExecute = this.doExecute
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package fr.postgresjson.migration
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import java.util.*
|
||||
|
||||
data class Query(
|
||||
val name: String,
|
||||
val up: String,
|
||||
val down: String,
|
||||
private val connection: Connection,
|
||||
override var executedAt: Date? = null
|
||||
): Migration, Entity<String?>(name) {
|
||||
override var doExecute: Action? = null
|
||||
|
||||
override fun up(): Migration.Status {
|
||||
connection.sendQuery(up)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let {
|
||||
connection.selectOne<MigrationEntity>(it, listOf(name, up, down))?.let { query ->
|
||||
executedAt = query.executedAt
|
||||
doExecute = Action.OK
|
||||
}
|
||||
}
|
||||
|
||||
return Migration.Status.OK
|
||||
}
|
||||
|
||||
override fun down(): Migration.Status {
|
||||
connection.sendQuery(down)
|
||||
|
||||
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
|
||||
connection.sendQuery(it, listOf(name))
|
||||
}
|
||||
|
||||
return Migration.Status.OK
|
||||
}
|
||||
|
||||
override fun test(): Migration.Status {
|
||||
connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
it.sendQuery("ROLLBACK")
|
||||
}
|
||||
|
||||
return Migration.Status.OK // TODO
|
||||
}
|
||||
|
||||
override fun status(): Migration.Status {
|
||||
val result = connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
it.sendQuery("ROLLBACK")
|
||||
}
|
||||
|
||||
return Migration.Status.OK // TODO
|
||||
}
|
||||
|
||||
fun copy(): Query {
|
||||
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||
it.doExecute = this.doExecute
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package fr.postgresjson.repository
|
||||
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
interface RepositoryI<E: EntityI<*>> {
|
||||
val entityName: KClass<E>
|
||||
var requester: Requester
|
||||
fun getClassName(): String {
|
||||
return entityName.simpleName!!
|
||||
}
|
||||
}
|
||||
12
src/main/kotlin/fr/postgresjson/repository/RepositoryI.kt
Normal file
12
src/main/kotlin/fr/postgresjson/repository/RepositoryI.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package fr.postgresjson.repository
|
||||
|
||||
import fr.postgresjson.connexion.Requester
|
||||
|
||||
interface RepositoryI {
|
||||
val requester: Requester
|
||||
|
||||
enum class Direction {
|
||||
asc,
|
||||
desc
|
||||
}
|
||||
}
|
||||
@@ -1,102 +1,48 @@
|
||||
package fr.postgresjson.serializer
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.core.JsonProcessingException
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.fasterxml.jackson.databind.*
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule
|
||||
import com.fasterxml.jackson.datatype.joda.JodaModule
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import fr.postgresjson.entity.EntitiesCollections
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import com.github.jasync.sql.db.QueryResult
|
||||
|
||||
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
|
||||
var collection: EntitiesCollections = EntitiesCollections()
|
||||
|
||||
init {
|
||||
val module = SimpleModule()
|
||||
module.addDeserializer(UuidEntity::class.java, EntityUuidDeserializer(collection))
|
||||
module.addDeserializer(IdEntity::class.java, EntityIdDeserializer(collection))
|
||||
mapper.registerModule(module)
|
||||
mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
|
||||
mapper.propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE
|
||||
|
||||
mapper.registerModule(JodaModule())
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
}
|
||||
|
||||
fun <T> serialize(source: EntityI<T>, pretty: Boolean = false): String {
|
||||
fun serialize(source: Any, pretty: Boolean = false): String {
|
||||
return if (pretty) mapper.writerWithDefaultPrettyPrinter().writeValueAsString(source)
|
||||
else mapper.writeValueAsString(source)
|
||||
}
|
||||
|
||||
fun <E: EntityI<*>> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
return this.mapper.readValue(json, valueTypeRef)
|
||||
}
|
||||
|
||||
inline fun <reified E: EntityI<*>> deserialize(json: String): E? {
|
||||
return this.mapper.readValue(json)
|
||||
}
|
||||
|
||||
fun <E> deserializeList(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
fun <E> deserialize(json: String, valueTypeRef: TypeReference<E>): E {
|
||||
return mapper.readValue(json, valueTypeRef)
|
||||
}
|
||||
|
||||
inline fun <reified E> deserializeList(json: String): E {
|
||||
return deserializeList(json, object: TypeReference<E>() {})
|
||||
}
|
||||
|
||||
fun <E: EntityI<*>> deserialize(json: String, target: E): E {
|
||||
return mapper.readerForUpdating(target).readValue<E>(json)
|
||||
inline fun <reified E> deserialize(json: String): E? {
|
||||
return this.mapper.readValue(json)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> EntityI<T?>.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
inline fun <reified E: EntityI<*>> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
inline fun <reified E: EntityI<*>> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
|
||||
|
||||
class EntityUuidDeserializer<T: UuidEntity> @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer<T>(vc) {
|
||||
var collection: EntitiesCollections = EntitiesCollections()
|
||||
|
||||
constructor(collection: EntitiesCollections): this() {
|
||||
this.collection = collection
|
||||
}
|
||||
|
||||
@Throws(IOException::class, JsonProcessingException::class)
|
||||
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T {
|
||||
val node = jp.codec.readTree<JsonNode>(jp)
|
||||
val id = node.get("id").asText()
|
||||
val entity = collection.get<UUID, UuidEntity>(UUID.fromString(id))
|
||||
|
||||
return (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as T
|
||||
inline fun <reified E : Any?> QueryResult.deserialize(): E? {
|
||||
val value = this.rows.firstOrNull()?.getString(0)
|
||||
return if (value == null) {
|
||||
null
|
||||
} else {
|
||||
Serializer().deserialize<E>(value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class EntityIdDeserializer<T: IdEntity> @JvmOverloads constructor(vc: Class<*>? = null): StdDeserializer<T>(vc) {
|
||||
var collection: EntitiesCollections = EntitiesCollections()
|
||||
|
||||
constructor(collection: EntitiesCollections): this() {
|
||||
this.collection = collection
|
||||
}
|
||||
|
||||
@Throws(IOException::class, JsonProcessingException::class)
|
||||
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): T {
|
||||
val node = jp.codec.readTree<JsonNode>(jp)
|
||||
val id = node.get("id").asInt()
|
||||
val entity = collection.get<Int?, IdEntity>(id)
|
||||
|
||||
val obj = (entity ?: ctxt.readValue(jp, UuidEntity::class.javaObjectType)) as EntityI<Int?>
|
||||
collection.set(obj)
|
||||
|
||||
return obj as T
|
||||
}
|
||||
}
|
||||
inline fun <reified T : Any> T.toTypeReference(): TypeReference<T> = object : TypeReference<T>() {}
|
||||
|
||||
@@ -5,6 +5,6 @@ import org.slf4j.LoggerFactory
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
internal class LoggerDelegate<in R: Any>: ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>) = LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
||||
}
|
||||
internal class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>): Logger = LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
||||
}
|
||||
|
||||
7
src/main/kotlin/fr/postgresjson/utils/caseChange.kt
Normal file
7
src/main/kotlin/fr/postgresjson/utils/caseChange.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
package fr.postgresjson.utils
|
||||
|
||||
fun String.toCamelCase(): String {
|
||||
return "_[a-zA-Z]".toRegex().replace(this) {
|
||||
it.value.replace("_", "").uppercase()
|
||||
}
|
||||
}
|
||||
51
src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt
Normal file
51
src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package fr.postgresjson.utils
|
||||
|
||||
import fr.postgresjson.definition.Resource
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.net.URI
|
||||
import java.net.URL
|
||||
import java.nio.file.FileSystemNotFoundException
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.FileVisitOption
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import kotlin.streams.asSequence
|
||||
|
||||
fun URL.searchSqlFiles() = this.toURI().searchSqlFiles()
|
||||
|
||||
fun URI.searchSqlFiles(): Sequence<Resource> = sequence {
|
||||
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
||||
val uri: URI = this@searchSqlFiles
|
||||
logger.debug("""SQL files found in "${uri.toString().substringAfter('!')}" :""")
|
||||
if (uri.scheme == "jar") {
|
||||
try {
|
||||
FileSystems.getFileSystem(uri)
|
||||
} catch (e: FileSystemNotFoundException) {
|
||||
FileSystems.newFileSystem(uri, emptyMap<String, Any>())
|
||||
}
|
||||
|
||||
uri
|
||||
.walk(5)
|
||||
.asSequence()
|
||||
.filter { it.fileName.toString().endsWith(".sql") }
|
||||
.map { it.toUri().toURL() }
|
||||
.forEach {
|
||||
logger.debug(it.toString())
|
||||
yield(Resource.build(it))
|
||||
}
|
||||
} else {
|
||||
uri
|
||||
.walk(5)
|
||||
.asSequence()
|
||||
.map { it.toFile() }
|
||||
.filter { it.isFile && it.extension == "sql" }
|
||||
.forEach {
|
||||
logger.debug(it.toString())
|
||||
yield(Resource.build(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Path.walk(maxDepth: Int = 2147483647, vararg options: FileVisitOption) = Files.walk(this, maxDepth, *options).sorted()
|
||||
private fun URI.walk(maxDepth: Int = 2147483647, vararg options: FileVisitOption) = Files.walk(Path.of(this), maxDepth, *options).sorted()
|
||||
@@ -1,167 +1,351 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import org.junit.Assert.*
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.connexion.DataNotFoundException
|
||||
import fr.postgresjson.connexion.SqlSerializable
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import fr.postgresjson.serializer.toTypeReference
|
||||
import io.kotest.core.spec.style.StringSpec
|
||||
import io.kotest.matchers.nulls.shouldBeNull
|
||||
import io.kotest.matchers.nulls.shouldNotBeNull
|
||||
import org.amshove.kluent.`should be equal to`
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.util.UUID
|
||||
import kotlin.reflect.full.hasAnnotation
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class ConnectionTest(): TestAbstract() {
|
||||
private class ObjTest(var name: String): IdEntity()
|
||||
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
||||
private class ObjTest3(var first: String, var seconde: String, var third: Int): IdEntity()
|
||||
class ConnectionTest : StringSpec({
|
||||
val connection = TestConnection()
|
||||
|
||||
@Test
|
||||
fun getObject() {
|
||||
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj!!.id == 1)
|
||||
@SqlSerializable
|
||||
class ObjTest(val name: String, val id: UUID = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
||||
|
||||
@SqlSerializable
|
||||
class ObjTest2(val id: UUID, val title: String, var test: ObjTest?)
|
||||
|
||||
@SqlSerializable
|
||||
class ObjTest3(val id: UUID, val first: String, var second: String, var third: Int)
|
||||
|
||||
@SqlSerializable
|
||||
class ParameterObject(var third: String)
|
||||
|
||||
@SqlSerializable
|
||||
class ObjTestWithParameterObject(val id: UUID, var first: ParameterObject, var second: ParameterObject)
|
||||
class ObjTest4
|
||||
|
||||
"serializable" {
|
||||
assertTrue(ObjTest("plop")::class.hasAnnotation<SqlSerializable>())
|
||||
assertFalse(ObjTest4()::class.hasAnnotation<SqlSerializable>())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getExistingObject() {
|
||||
val objs: List<ObjTest2> = connection.select("""
|
||||
select
|
||||
json_agg(j)
|
||||
FROM (
|
||||
SELECT
|
||||
t.id, t.title,
|
||||
t2 as test
|
||||
from test2 t
|
||||
JOIN test t2 ON t.test_id = t2.id
|
||||
) j;
|
||||
""".trimIndent()
|
||||
)
|
||||
assertNotNull(objs)
|
||||
assertTrue(objs is List<ObjTest2>)
|
||||
assertEquals(objs!!.size, 2)
|
||||
assertEquals(objs[0].id, 1)
|
||||
assertEquals(objs[0].test!!.id, 1)
|
||||
}
|
||||
"getObject" {
|
||||
val obj: ObjTest? = connection.rollbackAfter {
|
||||
sendQuery(
|
||||
"""
|
||||
create table test(
|
||||
id UUID primary key,
|
||||
name text
|
||||
);
|
||||
INSERT INTO test (id, name) VALUES ('1e5f5d41-6d14-4007-897b-0ed2616bec96', 'one');
|
||||
INSERT INTO test (id, name) VALUES ('26fa76cf-7688-4a1d-b611-e3060b38bf58', 'two');
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
@Test
|
||||
fun callRequestWithArgs() {
|
||||
val result: ObjTest? = connection.selectOne("select json_build_object('id', 1, 'name', ?::text)", listOf("myName"))
|
||||
assertNotNull(result)
|
||||
assertEquals("myName", result!!.name)
|
||||
}
|
||||
execute("select to_json(a) from test a limit 1")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun callRequestWithArgsEntity() {
|
||||
val o = ObjTest("myName")
|
||||
o.id = 88
|
||||
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id int, name text);", listOf(o))
|
||||
assertNotNull(obj)
|
||||
assertTrue(obj is ObjTest)
|
||||
assertEquals(obj!!.id, 88)
|
||||
assertEquals(obj.name, "myName")
|
||||
assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), obj.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun callExec() {
|
||||
"getExistingObject" {
|
||||
val objs: List<ObjTest2>? = connection.rollbackAfter {
|
||||
sendQuery(
|
||||
"""
|
||||
create table test(
|
||||
id uuid primary key,
|
||||
name text
|
||||
);
|
||||
create table test2(
|
||||
id uuid primary key,
|
||||
title text,
|
||||
test_id uuid
|
||||
);
|
||||
INSERT INTO test VALUES ('1e5f5d41-6d14-4007-897b-0ed2616bec96', 'one');
|
||||
INSERT INTO test2 VALUES ('a0214677-7332-4eec-8e9b-af0658ea72a6', 'two', '1e5f5d41-6d14-4007-897b-0ed2616bec96');
|
||||
INSERT INTO test2 VALUES ('8545577e-2785-421f-bb7e-1ec3faa1d79a', 'three', null);
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
execute<List<ObjTest2>>(
|
||||
"""
|
||||
select json_agg(j)
|
||||
from (
|
||||
select
|
||||
t.id,
|
||||
t.title,
|
||||
t2 as test
|
||||
from test2 t
|
||||
join test t2 ON t.test_id = t2.id
|
||||
) j;
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
objs.shouldNotBeNull()
|
||||
objs.size `should be equal to` 1
|
||||
objs.first().id `should be equal to` UUID.fromString("a0214677-7332-4eec-8e9b-af0658ea72a6")
|
||||
objs.first().title `should be equal to` "two"
|
||||
objs.first().test!!.id `should be equal to` UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")
|
||||
objs.first().test!!.name `should be equal to` "one"
|
||||
}
|
||||
|
||||
"test call request with args" {
|
||||
val result: ObjTest? = connection.execute(
|
||||
"select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)",
|
||||
listOf("myName")
|
||||
)
|
||||
result.shouldNotBeNull()
|
||||
result.name `should be equal to` "myName"
|
||||
}
|
||||
|
||||
"test call request without args" {
|
||||
val result: ObjTest? = connection.execute(
|
||||
"select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', 'myName')",
|
||||
object : TypeReference<ObjTest>() {}
|
||||
) {
|
||||
assertEquals("myName", this.deserialize<ObjTest>()?.name)
|
||||
}
|
||||
result.shouldNotBeNull()
|
||||
result.name `should be equal to` "myName"
|
||||
}
|
||||
|
||||
"test call request return null" {
|
||||
val result: ObjTest? = connection.execute("select null;", object : TypeReference<ObjTest>() {})
|
||||
result.shouldBeNull()
|
||||
}
|
||||
|
||||
"test call request return nothing" {
|
||||
val e = connection.rollbackAfter {
|
||||
sendQuery(
|
||||
"""
|
||||
create table test(
|
||||
id UUID primary key,
|
||||
name text
|
||||
);
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
assertThrows<DataNotFoundException> {
|
||||
execute("select * from test where false;", object : TypeReference<ObjTest>() {})
|
||||
}
|
||||
}
|
||||
|
||||
e.shouldNotBeNull()
|
||||
e.message `should be equal to` "No data return for the query"
|
||||
e.queryExecuted `should be equal to` "select * from test where false;"
|
||||
}
|
||||
|
||||
"callRequestWithArgsEntity" {
|
||||
val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
||||
val obj: ObjTest? = connection.execute(
|
||||
"select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);",
|
||||
listOf(o)
|
||||
)
|
||||
obj.shouldNotBeNull()
|
||||
obj.id `should be equal to` UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00")
|
||||
obj.name `should be equal to` "myName"
|
||||
}
|
||||
|
||||
"test update Entity" {
|
||||
val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||
val objUpdated: ObjTest? = connection.execute(
|
||||
"select ?::jsonb || jsonb_build_object('name', 'after');",
|
||||
obj.toTypeReference(), listOf(obj)
|
||||
)
|
||||
objUpdated.shouldNotBeNull()
|
||||
objUpdated.id `should be equal to` UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")
|
||||
objUpdated.name `should be equal to` "after"
|
||||
}
|
||||
|
||||
"test update Entity with vararg" {
|
||||
val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||
val objUpdated: ObjTest? = connection.execute(
|
||||
"select :obj::jsonb || jsonb_build_object('name', 'after');",
|
||||
obj.toTypeReference(),
|
||||
"obj" to obj
|
||||
)
|
||||
assertNotNull(objUpdated)
|
||||
assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id)
|
||||
assertEquals("after", objUpdated.name)
|
||||
}
|
||||
|
||||
"callExec" {
|
||||
val o = ObjTest("myName")
|
||||
val result = connection.exec("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o))
|
||||
Assertions.assertNotNull(result.getString(1))
|
||||
val result = connection.exec(
|
||||
"select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')",
|
||||
listOf(o)
|
||||
)
|
||||
assertEquals(1, result.rowsAffected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select one with named parameters`() {
|
||||
val result: ObjTest3? = connection.selectOne(
|
||||
"SELECT json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int)",
|
||||
"select one with named parameters" {
|
||||
val result: ObjTest3? = connection.execute(
|
||||
"""
|
||||
SELECT json_build_object(
|
||||
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
|
||||
'first', :first::text,
|
||||
'second', :second::text,
|
||||
'third', :third::int
|
||||
)
|
||||
""".trimIndent(),
|
||||
mapOf(
|
||||
"first" to "ff",
|
||||
"seconde" to "sec",
|
||||
"second" to "sec",
|
||||
"third" to 123
|
||||
)
|
||||
)
|
||||
assertEquals(result!!.first, "ff")
|
||||
assertEquals(result.seconde, "sec")
|
||||
assertEquals(result.third, 123)
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result.first)
|
||||
assertEquals("sec", result.second)
|
||||
assertEquals(123, result.third)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select with named parameters`() {
|
||||
val params: Map<String, Any?> = mapOf(
|
||||
"first" to "ff",
|
||||
"third" to 123,
|
||||
"seconde" to "sec"
|
||||
)
|
||||
val result: List<ObjTest3> = connection.select(
|
||||
"select one with named parameters object" {
|
||||
val result: ObjTestWithParameterObject? = connection.execute(
|
||||
"""
|
||||
SELECT json_build_array(
|
||||
json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int),
|
||||
json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int)
|
||||
SELECT json_build_object(
|
||||
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
|
||||
'first', :first::json,
|
||||
'second', :second::json
|
||||
)
|
||||
""".trimIndent(),
|
||||
params
|
||||
)
|
||||
assertEquals(result[0].first, "ff")
|
||||
assertEquals(result[0].seconde, "sec")
|
||||
assertEquals(result[0].third, 123)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selectOne with named parameters`() {
|
||||
val params: Map<String, Any?> = mapOf(
|
||||
"first" to "ff",
|
||||
"third" to 123,
|
||||
"seconde" to "sec"
|
||||
)
|
||||
val result: ObjTest3? = connection.selectOne(
|
||||
"""
|
||||
SELECT json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int)
|
||||
""".trimIndent(),
|
||||
params
|
||||
mapOf(
|
||||
"first" to ParameterObject("one"),
|
||||
"second" to ParameterObject("two")
|
||||
)
|
||||
)
|
||||
assertNotNull(result)
|
||||
assertEquals(result!!.first, "ff")
|
||||
assertEquals(result.seconde, "sec")
|
||||
assertEquals(result.third, 123)
|
||||
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", result.id.toString())
|
||||
assertEquals("one", result.first.third)
|
||||
assertEquals("two", result.second.third)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select paginated`() {
|
||||
val result: Paginated<ObjTest> = connection.select(
|
||||
"select with named parameters" {
|
||||
val result: List<ObjTest3>? = connection.execute(
|
||||
"""
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', :name::text),
|
||||
json_build_object('id', 4, 'name', :name::text || '-2')
|
||||
), 10 as total
|
||||
LIMIT :limit OFFSET :offset
|
||||
json_build_object(
|
||||
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
|
||||
'first', :first::text,
|
||||
'second', :second::text,
|
||||
'third', :third::int
|
||||
),
|
||||
json_build_object(
|
||||
'id', 'ce9ae3c9-dc0e-4561-a168-811b996d913e',
|
||||
'first', :first::text,
|
||||
'second', :second::text,
|
||||
'third', :third::int
|
||||
)
|
||||
)
|
||||
""".trimIndent(),
|
||||
1,
|
||||
2,
|
||||
mapOf("name" to "ff")
|
||||
|
||||
mapOf(
|
||||
"first" to "ff",
|
||||
"third" to 123,
|
||||
"second" to "sec"
|
||||
)
|
||||
)
|
||||
assertNotNull(result)
|
||||
assertEquals(result.result[0].name, "ff")
|
||||
assertEquals(result.result[1].name, "ff-2")
|
||||
assertEquals(result.total, 10)
|
||||
assertEquals(result.offset, 0)
|
||||
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", result[0].id.toString())
|
||||
assertEquals("ff", result[0].first)
|
||||
assertEquals("sec", result[0].second)
|
||||
assertEquals(123, result[0].third)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selectOne with extra parameters`() {
|
||||
"select with named parameters as vararg of Pair" {
|
||||
val result: List<ObjTest3>? = connection.execute(
|
||||
"""
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576', 'first', :first::text, 'second', :second::text, 'third', :third::int),
|
||||
json_build_object('id', '0c9d55d2-f69a-4750-a278-fac821774276', 'first', :first::text, 'second', :second::text, 'third', :third::int)
|
||||
)
|
||||
""".trimIndent(),
|
||||
"first" to "ff",
|
||||
"third" to 123,
|
||||
"second" to "sec"
|
||||
)
|
||||
assertNotNull(result)
|
||||
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", result[0].id.toString())
|
||||
assertEquals("ff", result[0].first)
|
||||
assertEquals("sec", result[0].second)
|
||||
assertEquals(123, result[0].third)
|
||||
}
|
||||
|
||||
"execute with extra parameters" {
|
||||
val params: Map<String, Any?> = mapOf(
|
||||
"first" to "ff",
|
||||
"third" to 123,
|
||||
"seconde" to "sec"
|
||||
"second" to "sec"
|
||||
)
|
||||
val result: ObjTest3? = connection.selectOne(
|
||||
val result: ObjTest3? = connection.execute(
|
||||
"""
|
||||
SELECT json_build_object('first', :first::text, 'seconde', :seconde::text, 'third', :third::int), 'plop'::text as other
|
||||
SELECT json_build_object(
|
||||
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
|
||||
'first', :first::text,
|
||||
'second', :second::text,
|
||||
'third', :third::int
|
||||
), 'plop'::text as other
|
||||
""".trimIndent(),
|
||||
params
|
||||
) {
|
||||
assertEquals("ff", it!!.first)
|
||||
assertEquals("plop", getString("other"))
|
||||
assertNotNull(it)
|
||||
assertEquals("bf0e5605-3a8f-4db9-8b98-c8e0691dd576", it.id.toString())
|
||||
assertEquals("ff", it.first)
|
||||
assertEquals("plop", rows[0].getString("other"))
|
||||
}
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result!!.first)
|
||||
assertEquals("sec", result.seconde)
|
||||
assertEquals("ff", result.first)
|
||||
assertEquals("sec", result.second)
|
||||
assertEquals(123, result.third)
|
||||
}
|
||||
}
|
||||
|
||||
"test exec without parameters" {
|
||||
connection.exec("select 42, 'hello';").run {
|
||||
assertEquals(42, rows[0].getInt(0))
|
||||
assertEquals("hello", rows[0].getString(1))
|
||||
}
|
||||
}
|
||||
|
||||
"test exec with one object as parameter" {
|
||||
val obj = ObjTest("myName", UUID.fromString("c606e216-53b3-43c8-a900-e727cb4a017c"))
|
||||
connection.exec("select ?::jsonb->>'name'", obj).run {
|
||||
assertEquals("myName", rows[0].getString(0))
|
||||
}
|
||||
}
|
||||
|
||||
"select one in transaction" {
|
||||
connection.inTransaction {
|
||||
execute<ObjTestWithParameterObject>(
|
||||
"""
|
||||
SELECT json_build_object(
|
||||
'id', 'bf0e5605-3a8f-4db9-8b98-c8e0691dd576',
|
||||
'first', :first::json,
|
||||
'second', :second::json
|
||||
)
|
||||
""".trimIndent(),
|
||||
mapOf(
|
||||
"first" to ParameterObject("one"),
|
||||
"second" to ParameterObject("two")
|
||||
)
|
||||
).let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("one", result.first.third)
|
||||
assertEquals("two", result.second.third)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.*
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class EntityTest() {
|
||||
private class User(override var id: Int?): EntityI<Int?>
|
||||
private class ObjTest(var name: String): EntityExtended<Int?, User>()
|
||||
|
||||
@Test
|
||||
fun getObject() {
|
||||
val obj: ObjTest? = ObjTest("plop")
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj is EntityExtended<Int?, User>)
|
||||
assertTrue(obj is EntityI<Int?>)
|
||||
assertTrue(obj is Entity<Int?>)
|
||||
assertTrue(obj is Published<User>)
|
||||
assertTrue(obj is CreatedBy<User>)
|
||||
assertTrue(obj is UpdatedBy<User>)
|
||||
assertTrue(obj is EntityCreatedAt)
|
||||
assertTrue(obj is EntityUpdatedAt)
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,26 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.migration.Migration
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import fr.postgresjson.migration.MigrationExecutor
|
||||
import org.amshove.kluent.invoking
|
||||
import org.amshove.kluent.`should be equal to`
|
||||
import org.amshove.kluent.`should contain`
|
||||
import org.amshove.kluent.invoking
|
||||
import org.amshove.kluent.shouldThrow
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MigrationTest(): TestAbstract() {
|
||||
class MigrationTest : TestAbstract() {
|
||||
@Test
|
||||
fun `run up query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val m = Migrations(resources, connection)
|
||||
val resources = this::class.java.getResource("/sql/migrations")!!.toURI()
|
||||
val m = MigrationExecutor(connection, resources)
|
||||
m.up().apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
size `should be equal to` 1
|
||||
@@ -28,16 +31,16 @@ class MigrationTest(): TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `migration up Query should throw error if no down`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
|
||||
val resources = this::class.java.getResource("/sql/migration_without_down")!!.toURI()
|
||||
invoking {
|
||||
Migrations(resources, connection)
|
||||
} shouldThrow Migrations.DownMigrationNotDefined::class
|
||||
MigrationExecutor(resources, connection)
|
||||
} shouldThrow MigrationExecutor.DownMigrationNotDefined::class
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `run forced down query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val m = Migrations(resources, connection)
|
||||
val resources = this::class.java.getResource("/sql/migrations")!!.toURI()
|
||||
val m = MigrationExecutor(resources, connection)
|
||||
repeat(3) {
|
||||
m.down(true).apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
@@ -48,19 +51,19 @@ class MigrationTest(): TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run dry migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, connection).apply {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
MigrationExecutor(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
Migrations(resources, connection).apply {
|
||||
MigrationExecutor(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `run dry migrations launch twice`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, connection).apply {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
MigrationExecutor(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
@@ -68,8 +71,8 @@ class MigrationTest(): TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, connection).apply {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
MigrationExecutor(resources, connection).apply {
|
||||
run().apply {
|
||||
size `should be equal to` 1
|
||||
}
|
||||
@@ -78,14 +81,14 @@ class MigrationTest(): TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run migrations force down`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
val resourcesFunctions = File(this::class.java.getResource("/sql/function").toURI())
|
||||
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
val resourcesFunctions = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
MigrationExecutor(listOf(resources, resourcesFunctions), connection).apply {
|
||||
up().apply {
|
||||
size `should be equal to` 6
|
||||
}
|
||||
}
|
||||
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||
MigrationExecutor(listOf(resources, resourcesFunctions), connection).apply {
|
||||
forceAllDown().apply {
|
||||
size `should be equal to` 6
|
||||
}
|
||||
@@ -94,17 +97,37 @@ class MigrationTest(): TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run functions migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
Migrations(resources, connection).apply {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
MigrationExecutor(resources, connection).apply {
|
||||
run().size `should be equal to` 5
|
||||
}
|
||||
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
.execute(listOf("test", "plip"))
|
||||
|
||||
Assertions.assertEquals(objTest!!.id, 3)
|
||||
Assertions.assertEquals(objTest.name, "test")
|
||||
assertNotNull(objTest)
|
||||
assertEquals(objTest.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||
assertEquals(objTest.name, "test")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `run functions migrations and drop if exist`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test1")!!.toURI()
|
||||
MigrationExecutor(resources, connection).apply {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_duplicate")
|
||||
.execute(listOf("test"))
|
||||
|
||||
Assertions.assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||
Assertions.assertEquals(objTest.name, "test")
|
||||
|
||||
val resources2 = this::class.java.getResource("/sql/function/Test2")!!.toURI()
|
||||
MigrationExecutor(resources2, connection).apply {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,170 +1,447 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.connexion.Connection.QueryError
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import org.junit.Assert
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||
import fr.postgresjson.connexion.Requester.NoFunctionDefined
|
||||
import fr.postgresjson.connexion.Requester.NoQueryDefined
|
||||
import fr.postgresjson.connexion.SqlSerializable
|
||||
import fr.postgresjson.connexion.execute
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
class RequesterTest: TestAbstract() {
|
||||
class ObjTest(var name:String): IdEntity(1)
|
||||
class RequesterTest : TestAbstract() {
|
||||
@SqlSerializable
|
||||
class ObjTest(val name: String, val id: UUID = UUID.fromString("5623d902-3067-42f3-bfd9-095dbb12c29f"))
|
||||
|
||||
@Test
|
||||
fun `requester constructor empty`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
val name: String = Requester(connection)
|
||||
.apply { addFunctions(resources) }
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `requester constructor function directory`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val name: String = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `requester constructor query directory`() {
|
||||
val resources = this::class.java.getResource("/sql/query/Test")?.toURI()
|
||||
val name: String = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("DeleteTest")
|
||||
.name
|
||||
|
||||
assertEquals("DeleteTest", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `function toString`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val name: String = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.toString()
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add function as string`() {
|
||||
val sql = """
|
||||
CREATE OR REPLACE FUNCTION test_function (name text default 'plop', IN hi text default 'hello', out result json)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
END;
|
||||
$$
|
||||
""".trimIndent()
|
||||
val name: String = Requester(connection)
|
||||
.apply { addFunction(sql) }
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add query from string`() {
|
||||
val result: Int? = Requester(connection)
|
||||
.apply { addQuery("simpleTest", "select 42;") }
|
||||
.getQuery("simpleTest")
|
||||
.exec()
|
||||
.rows[0].getInt(0)
|
||||
|
||||
assertNotNull(result)
|
||||
assertEquals(42, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get query from file`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val resources = this::class.java.getResource("/sql/query")!!.toURI()
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOne")
|
||||
.selectOne()
|
||||
.apply { addQuery(resources) }
|
||||
.getQuery("selectOne")
|
||||
.execute()
|
||||
|
||||
assertEquals(objTest!!.id, 2)
|
||||
assertEquals(objTest.name, "test")
|
||||
assertNotNull(objTest)
|
||||
assertEquals(objTest.id, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
||||
assertEquals("test", objTest.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get query from file with wrong name throw exception`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
assertThrows(NoQueryDefined::class.java) {
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("wrongName")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get queries from file`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val name: String = Requester(connection, queriesDirectory = resources)
|
||||
.getQueries()[0].name
|
||||
|
||||
assertEquals(name, "DeleteTest")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get function from file with wrong name throw exception`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
assertThrows(NoFunctionDefined::class.java) {
|
||||
Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("wrongName")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get function from file`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val objTest: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
.execute(listOf("test", "plip"))
|
||||
|
||||
assertEquals(objTest!!.id, 3)
|
||||
assertEquals(objTest.name, "test")
|
||||
assertNotNull(objTest)
|
||||
assertEquals(objTest.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||
assertEquals("test", objTest.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call exec on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOne")
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOne")
|
||||
.exec()
|
||||
|
||||
assertNotNull(result.getString(1))
|
||||
assertEquals(1, result.rowsAffected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call exec on query with a list of arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters")
|
||||
.exec(listOf("myName"))
|
||||
|
||||
assertEquals("myName", result.deserialize<ObjTest>()?.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call exec on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val result = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.exec(listOf("test", "plip"))
|
||||
|
||||
assertNotNull(result.getString(1))
|
||||
assertEquals(1, result.rowsAffected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call sendQuery on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/exec")
|
||||
.sendQuery()
|
||||
fun `call exec on query with name`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("DeleteTest")
|
||||
.exec()
|
||||
|
||||
assertEquals(0, result)
|
||||
assertEquals(0, result.rowsAffected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call sendQuery on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("function_void")
|
||||
.sendQuery(listOf("test"))
|
||||
|
||||
assertEquals(0, result)
|
||||
fun `call sendQuery with same name of arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleWithSameArgs")
|
||||
.sendQuery("name" to "myName").run {
|
||||
assertEquals("myName", rows[0].getString("firstName"))
|
||||
assertEquals("myName", rows[0].getString("secondName"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
fun `call sendQuery with same name of arguments as list`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleWithSameArgs")
|
||||
.sendQuery(listOf("myName", "myName2")).run {
|
||||
assertEquals("myName", rows[0].getString("firstName"))
|
||||
assertEquals("myName2", rows[0].getString("secondName"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call sendQuery with arguments on not same orders`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleDifferentArgs")
|
||||
.sendQuery("first" to "firstName", "second" to "secondName").run {
|
||||
assertEquals("firstName", rows[0].getString("firstName"))
|
||||
assertEquals("secondName", rows[0].getString("secondName"))
|
||||
}
|
||||
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleDifferentArgs")
|
||||
.sendQuery("second" to "secondName", "first" to "firstName").run {
|
||||
assertEquals("firstName", rows[0].getString("firstName"))
|
||||
assertEquals("secondName", rows[0].getString("secondName"))
|
||||
}
|
||||
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleDifferentArgs")
|
||||
.sendQuery("second" to "secondName", "first" to "firstName").run {
|
||||
assertEquals("firstName", rows[0].getString(0))
|
||||
assertEquals("secondName", rows[0].getString(1))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call sendQuery with wrong number of arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
|
||||
assertThrows(QueryError::class.java) {
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleDifferentArgs")
|
||||
.sendQuery("first" to "firstName")
|
||||
}.let {
|
||||
assertEquals(
|
||||
"""
|
||||
Parameter "second" missing
|
||||
|
||||
> :first = firstName
|
||||
> SELECT :first::text as "firstName", :second::text as "secondName";
|
||||
""".trimIndent(),
|
||||
it.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call sendQuery with wrong number of arguments as list`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
|
||||
assertThrows(QueryError::class.java) {
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleDifferentArgs")
|
||||
.sendQuery(listOf("firstName"))
|
||||
}.let {
|
||||
assertEquals(
|
||||
"""
|
||||
Parameter 1 missing
|
||||
|
||||
> firstName
|
||||
> SELECT ?::text as "firstName", ?::text as "secondName";
|
||||
""".trimIndent(),
|
||||
it.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call exec on function with pair as arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val result = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_void")
|
||||
.exec("name" to "test")
|
||||
|
||||
assertEquals(1, result.rowsAffected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call execute on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
.execute(mapOf("name" to "myName"))
|
||||
|
||||
assertNotNull(obj)
|
||||
assertEquals("myName", obj.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function with object`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
fun `call execute on function with object and named argument`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_object")
|
||||
.selectOne("resource" to obj2)!!
|
||||
.execute("resource" to obj2)
|
||||
|
||||
assertNotNull(obj)
|
||||
assertEquals("changedName", obj.name)
|
||||
assertEquals("changedName", obj2.name)
|
||||
assertEquals("original", obj2.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
fun `call execute on function with object`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_object")
|
||||
.execute(listOf(obj2))
|
||||
|
||||
assertNotNull(obj)
|
||||
assertEquals("changedName", obj.name)
|
||||
assertEquals("original", obj2.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call execute on function with object and no arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.execute()
|
||||
|
||||
assertNotNull(obj)
|
||||
assertEquals("plop", obj.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call execute on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val obj: ObjTest? = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters")
|
||||
.execute(mapOf("name" to "myName"))
|
||||
|
||||
assertNotNull(obj)
|
||||
assertEquals("myName", obj.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select (multiple) on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val obj: List<ObjTest>? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
fun `call select (multiple) on function with named argument`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: List<ObjTest>? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_multiple")
|
||||
.select(mapOf("name" to "myName"))
|
||||
.execute(mapOf("name" to "myName"))
|
||||
|
||||
assertEquals("myName", obj!![0].name)
|
||||
assertNotNull(obj)
|
||||
assertNotNull(obj[0])
|
||||
assertEquals("myName", obj[0].name)
|
||||
assertEquals("myName", obj[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select paginated on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectPaginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
Assert.assertNotNull(result)
|
||||
Assert.assertEquals(result.result[0].name, "ff")
|
||||
Assert.assertEquals(result.result[1].name, "ff-2")
|
||||
Assert.assertEquals(result.total, 10)
|
||||
Assert.assertEquals(result.offset, 0)
|
||||
fun `call select (multiple) on function with ordered arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: List<ObjTest>? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_multiple")
|
||||
.execute(listOf("myName"))
|
||||
|
||||
assertNotNull(obj)
|
||||
assertEquals("myName", obj[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select paginated on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function_paginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
Assert.assertNotNull(result)
|
||||
Assert.assertEquals(result.result[0].name, "ff")
|
||||
Assert.assertEquals(result.result[1].name, "ff-2")
|
||||
Assert.assertEquals(result.total, 10)
|
||||
Assert.assertEquals(result.offset, 0)
|
||||
fun `call select multiple (named arguments)`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultiple").apply {
|
||||
execute<List<ObjTest>>(mapOf("name" to "ff")).let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result[0].name)
|
||||
assertEquals("ff-2", result[1].name)
|
||||
}
|
||||
}.apply {
|
||||
execute(object : TypeReference<List<ObjTest>>() {}, mapOf("name" to "ff")).let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result[0].name)
|
||||
assertEquals("ff-2", result[1].name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query with extra parameter`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName")) {
|
||||
assertEquals("myName", it!!.name)
|
||||
Assert.assertEquals("plop", getString("other"))
|
||||
}!!
|
||||
|
||||
assertEquals("myName", obj.name)
|
||||
fun `call select multiple (named arguments as pair)`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultiple").apply {
|
||||
execute<List<ObjTest>>("name" to "ff").let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result[0].name)
|
||||
assertEquals("ff-2", result[1].name)
|
||||
}
|
||||
}.apply {
|
||||
execute(object : TypeReference<List<ObjTest>>() {}, "name" to "ff").let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result[0].name)
|
||||
assertEquals("ff-2", result[1].name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select multiple (ordered argument)`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleOrderedArgs").apply {
|
||||
execute<List<ObjTest>>(listOf("ff", "aa")).let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result[0].name)
|
||||
assertEquals("aa-2", result[1].name)
|
||||
}
|
||||
}.apply {
|
||||
execute(object : TypeReference<List<ObjTest>>() {}, listOf("ff", "aa")).let { result ->
|
||||
assertNotNull(result)
|
||||
assertEquals("ff", result[0].name)
|
||||
assertEquals("aa-2", result[1].name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call execute on query with extra parameter`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters").apply {
|
||||
execute<ObjTest>(mapOf("name" to "myName")) {
|
||||
assertNotNull(it)
|
||||
assertEquals("myName", it.name)
|
||||
assertEquals("plop", rows[0].getString("other"))
|
||||
}.run {
|
||||
assertEquals("selectOneWithParameters", name)
|
||||
}
|
||||
}.apply {
|
||||
execute(typeReference = object : TypeReference<ObjTest>() {}, values = mapOf("name" to "myName")) {
|
||||
assertNotNull(it)
|
||||
assertEquals("myName", it.name)
|
||||
assertEquals("plop", rows[0].getString("other"))
|
||||
}.run {
|
||||
assertEquals("selectOneWithParameters", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,28 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import fr.postgresjson.serializer.serialize
|
||||
import org.joda.time.DateTime
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.util.UUID
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
internal class SerializerTest {
|
||||
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
||||
private class ObjTestDate(var val1: DateTime) : IdEntity(2)
|
||||
private class ObjTest(var val1: String, var val2: Int, val id: UUID = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||
private class ObjTestDate(var val1: DateTime, val id: UUID = UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
||||
|
||||
private val serializer = Serializer()
|
||||
|
||||
private val objSerialized: String = """{"val1":"plop","val2":123,"id":2}"""
|
||||
private val objSerializedWithExtra: String = """{"val1":"plop","val2":123,"id":2,"toto":"tata"}"""
|
||||
private val objSerializedUpdate = """{"val1":"update","val2":123}"""
|
||||
private val objSerialized: String = """{"val1":"plop","val2":123,"id":"829b1a29-5db8-47f9-9562-961c561ac528"}"""
|
||||
private val objSerializedWithExtra: String = """{"val1":"plop","val2":123,"id":"829b1a29-5db8-47f9-9562-961c561ac528","toto":"tata"}"""
|
||||
private lateinit var obj: ObjTest
|
||||
|
||||
@BeforeEach
|
||||
fun before() {
|
||||
obj = ObjTest("plop", 123)
|
||||
obj.id = 2
|
||||
obj = ObjTest("plop", 123, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -36,15 +32,17 @@ internal class SerializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun serialize2() {
|
||||
val json = obj.serialize()
|
||||
assertTrue(json.contains(""""val1":"plop","val2":123"""))
|
||||
fun serializeList() {
|
||||
val list = listOf(ObjTest("one", 1), ObjTest("two", 2))
|
||||
val json = serializer.serialize(list)
|
||||
assertTrue(json.contains(""""val1":"one","val2":1"""))
|
||||
assertTrue(json.contains(""""val1":"two","val2":2"""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun serializeDate() {
|
||||
val objDate = ObjTestDate(DateTime.parse("2019-07-30T14:08:51.420108+04:00"))
|
||||
val json = objDate.serialize()
|
||||
val json = serializer.serialize(objDate)
|
||||
assertTrue(json.contains(""""val1":"2019-07-30T10:08:51.420Z""""), json)
|
||||
}
|
||||
|
||||
@@ -61,20 +59,4 @@ internal class SerializerTest {
|
||||
assertEquals(obj.val1, objDeserialized!!.val1)
|
||||
assertEquals(obj.val2, objDeserialized.val2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deserializeUpdate() {
|
||||
val objDeserialized: ObjTest = serializer.deserialize(objSerializedUpdate, obj)
|
||||
assertTrue(obj === objDeserialized)
|
||||
assertEquals("update", objDeserialized.val1)
|
||||
assertEquals(123, objDeserialized.val2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deserializeUpdate2() {
|
||||
val objDeserialized = obj.deserialize(objSerializedUpdate)
|
||||
assertTrue(obj === objDeserialized)
|
||||
assertEquals("update", objDeserialized.val1)
|
||||
assertEquals(123, objDeserialized.val2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
26
src/test/kotlin/fr/postgresjson/SqlFixtureListener.kt
Normal file
26
src/test/kotlin/fr/postgresjson/SqlFixtureListener.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import io.kotest.core.listeners.AfterSpecListener
|
||||
import io.kotest.core.listeners.BeforeSpecListener
|
||||
import io.kotest.core.spec.Spec
|
||||
import java.io.File
|
||||
|
||||
open class SqlFixtureListener : BeforeSpecListener, AfterSpecListener {
|
||||
private val connection = Connection(database = "json_test", username = "test", password = "test", port = 35555)
|
||||
|
||||
override suspend fun beforeSpec(spec: Spec) {
|
||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql")!!.toURI())
|
||||
connection
|
||||
.connect()
|
||||
.sendQuery(initSQL.readText())
|
||||
.join()
|
||||
}
|
||||
|
||||
override suspend fun afterSpec(spec: Spec) {
|
||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql")!!.toURI())
|
||||
connection
|
||||
.apply { connect().sendQuery(downSQL.readText()).join() }
|
||||
.disconnect()
|
||||
}
|
||||
}
|
||||
@@ -8,24 +8,23 @@ import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
||||
import java.io.File
|
||||
|
||||
@TestInstance(PER_CLASS)
|
||||
abstract class TestAbstract {
|
||||
protected val connection = Connection(database = "test", username = "test", password = "test")
|
||||
open class TestAbstract {
|
||||
protected val connection = Connection(database = "json_test", username = "test", password = "test", port = 35555)
|
||||
|
||||
@BeforeEach
|
||||
fun beforeAll() {
|
||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
|
||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql")!!.toURI())
|
||||
connection
|
||||
.connect()
|
||||
.createStatement()
|
||||
.executeUpdate(initSQL.readText())
|
||||
.sendQuery(initSQL.readText())
|
||||
.join()
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun afterAll() {
|
||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
|
||||
connection.connect().apply {
|
||||
createStatement()
|
||||
.executeUpdate(downSQL.readText())
|
||||
}.close()
|
||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql")!!.toURI())
|
||||
connection
|
||||
.apply { connect().sendQuery(downSQL.readText()).join() }
|
||||
.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
src/test/kotlin/fr/postgresjson/TestConnection.kt
Normal file
16
src/test/kotlin/fr/postgresjson/TestConnection.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
fun TestConnection(): Connection =
|
||||
Connection(database = "json_test", username = "test", password = "test", port = 35555)
|
||||
|
||||
fun <A> Connection.rollbackAfter(block: Connection.() -> A?) = connect().run {
|
||||
sendQuery("BEGIN")
|
||||
try {
|
||||
block().apply { sendQuery("ROLLBACK") }
|
||||
} catch (e: Throwable) {
|
||||
sendQuery("ROLLBACK")
|
||||
throw e
|
||||
}
|
||||
}
|
||||
648
src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt
Normal file
648
src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt
Normal file
@@ -0,0 +1,648 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import fr.postgresjson.definition.Parameter.Direction.IN
|
||||
import fr.postgresjson.definition.Parameter.Direction.INOUT
|
||||
import fr.postgresjson.definition.Parameter.Direction.OUT
|
||||
import fr.postgresjson.definition.Returns.Primitive
|
||||
import fr.postgresjson.definition.parse.parseFunction
|
||||
import io.kotest.core.spec.style.FreeSpec
|
||||
import io.kotest.matchers.collections.shouldHaveSize
|
||||
import io.kotest.matchers.shouldBe
|
||||
import org.amshove.kluent.shouldBeInstanceOf
|
||||
|
||||
class FunctionTest : FreeSpec({
|
||||
"Function name" - {
|
||||
"all in lower" {
|
||||
parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun() returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).apply {
|
||||
name shouldBe "myfun"
|
||||
}
|
||||
}
|
||||
|
||||
"first letter caps without quoted" {
|
||||
parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function Myfun() returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).apply {
|
||||
name shouldBe "myfun"
|
||||
}
|
||||
}
|
||||
|
||||
"with numbers" {
|
||||
parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun001() returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).apply {
|
||||
name shouldBe "myfun001"
|
||||
}
|
||||
}
|
||||
|
||||
"escaped name with space" {
|
||||
parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function "My fun"() returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).apply {
|
||||
name shouldBe "My fun"
|
||||
}
|
||||
}
|
||||
|
||||
"quoted name with double quote in name" {
|
||||
parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function "My""fun" () returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).apply {
|
||||
name shouldBe "My\"fun"
|
||||
}
|
||||
}
|
||||
|
||||
"name with new line before and after" {
|
||||
parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function
|
||||
myfun
|
||||
()
|
||||
returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).apply {
|
||||
name shouldBe "myfun"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters" - {
|
||||
"One parameter text" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one text) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have one parameter" {
|
||||
param shouldHaveSize 1
|
||||
}
|
||||
|
||||
"should have first parameter name" {
|
||||
param[0].name shouldBe "one"
|
||||
}
|
||||
|
||||
"should have first parameter type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
}
|
||||
}
|
||||
|
||||
"Two parameters" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one text, two int) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 2 parameters" {
|
||||
param shouldHaveSize 2
|
||||
}
|
||||
|
||||
"should have names" {
|
||||
param[0].name shouldBe "one"
|
||||
param[1].name shouldBe "two"
|
||||
}
|
||||
|
||||
"should have first parameter type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
param[1].type.name shouldBe "int"
|
||||
}
|
||||
}
|
||||
|
||||
"Escaped parameters name" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun("one""or two" text, "#@€" int) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 2 parameters" {
|
||||
param shouldHaveSize 2
|
||||
}
|
||||
|
||||
"should have names" {
|
||||
param[0].name shouldBe "one\"or two"
|
||||
param[1].name shouldBe "#@€"
|
||||
}
|
||||
}
|
||||
"Parameters with Caps" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun("One" text, Two text) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have first parameter name" {
|
||||
param[0].name shouldBe "One"
|
||||
param[1].name shouldBe "two"
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with type `character varying(255)`" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one character varying(255)) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 1 parameters" {
|
||||
param shouldHaveSize 1
|
||||
}
|
||||
|
||||
"should have name" {
|
||||
param[0].name shouldBe "one"
|
||||
}
|
||||
|
||||
"should have type name" {
|
||||
param[0].type.name shouldBe "character varying"
|
||||
}
|
||||
|
||||
"should have type precision" {
|
||||
param[0].type.precision shouldBe 255
|
||||
param[0].type.scale shouldBe null
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with type `numeric(16, 8)`" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one numeric(16, 8)) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 1 parameters" {
|
||||
param shouldHaveSize 1
|
||||
}
|
||||
|
||||
"should have name" {
|
||||
param[0].name shouldBe "one"
|
||||
}
|
||||
|
||||
"should have type name" {
|
||||
param[0].type.name shouldBe "numeric"
|
||||
}
|
||||
|
||||
"should have type precision" {
|
||||
param[0].type.precision shouldBe 16
|
||||
}
|
||||
|
||||
"should have type scale" {
|
||||
param[0].type.scale shouldBe 8
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with default text" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one text default 'example') returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 1 parameters" {
|
||||
param shouldHaveSize 1
|
||||
}
|
||||
|
||||
"should have name" {
|
||||
param[0].name shouldBe "one"
|
||||
}
|
||||
|
||||
"should have type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
}
|
||||
|
||||
"should have default text" {
|
||||
param[0].default shouldBe "'example'"
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with default int" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one int DEFAULT 123456 ) returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 1 parameters" {
|
||||
param shouldHaveSize 1
|
||||
}
|
||||
|
||||
"should have name" {
|
||||
param[0].name shouldBe "one"
|
||||
}
|
||||
|
||||
"should have type name" {
|
||||
param[0].type.name shouldBe "int"
|
||||
}
|
||||
|
||||
"should have default text" {
|
||||
param[0].default shouldBe "123456"
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with multiple default and equal" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one int DEFAULT 123456 , two text default 'hello', three text = '654') returns text language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 3 parameters" {
|
||||
param shouldHaveSize 3
|
||||
}
|
||||
|
||||
"should have name" {
|
||||
param[0].name shouldBe "one"
|
||||
param[1].name shouldBe "two"
|
||||
param[2].name shouldBe "three"
|
||||
}
|
||||
|
||||
"should have type name" {
|
||||
param[0].type.name shouldBe "int"
|
||||
param[1].type.name shouldBe "text"
|
||||
param[2].type.name shouldBe "text"
|
||||
}
|
||||
|
||||
"should have default text" {
|
||||
param[0].default shouldBe "123456"
|
||||
param[1].default shouldBe "'hello'"
|
||||
param[2].default shouldBe "'654'"
|
||||
}
|
||||
}
|
||||
|
||||
"parameters with IN OUT INOUT" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(in one text, inout two text, out three text, four text) language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 4 parameters" {
|
||||
param shouldHaveSize 4
|
||||
}
|
||||
|
||||
"should have parameter name" {
|
||||
param[0].name shouldBe "one"
|
||||
param[1].name shouldBe "two"
|
||||
param[2].name shouldBe "three"
|
||||
param[3].name shouldBe "four"
|
||||
}
|
||||
|
||||
"should have parameter type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
param[1].type.name shouldBe "text"
|
||||
param[2].type.name shouldBe "text"
|
||||
}
|
||||
|
||||
"should have parameter direction" {
|
||||
param[0].direction shouldBe IN
|
||||
param[1].direction shouldBe INOUT
|
||||
param[2].direction shouldBe OUT
|
||||
param[3].direction shouldBe IN
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with type array of numeric" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one numeric(10, 2)[]) language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 1 parameters" {
|
||||
param shouldHaveSize 1
|
||||
}
|
||||
|
||||
"should have parameter name" {
|
||||
param[0].name shouldBe "one"
|
||||
}
|
||||
|
||||
"should have parameter type is array" {
|
||||
param[0].type.isArray shouldBe true
|
||||
}
|
||||
|
||||
"should have parameter type name" {
|
||||
param[0].type.name shouldBe "numeric"
|
||||
}
|
||||
|
||||
"should have parameter type precision" {
|
||||
param[0].type.precision shouldBe 10
|
||||
}
|
||||
|
||||
"should have parameter type scale" {
|
||||
param[0].type.scale shouldBe 2
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with type array of text" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one text[], two int[], three text) language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have 2 parameters" {
|
||||
param shouldHaveSize 3
|
||||
}
|
||||
|
||||
"should have parameter name" {
|
||||
param[0].name shouldBe "one"
|
||||
param[1].name shouldBe "two"
|
||||
param[2].name shouldBe "three"
|
||||
}
|
||||
|
||||
"should have parameter type is array" {
|
||||
param[0].type.isArray shouldBe true
|
||||
param[1].type.isArray shouldBe true
|
||||
param[2].type.isArray shouldBe false
|
||||
}
|
||||
|
||||
"should have parameter type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
param[1].type.name shouldBe "int"
|
||||
param[2].type.name shouldBe "text"
|
||||
}
|
||||
|
||||
"should have parameter direction" {
|
||||
param[0].direction shouldBe IN
|
||||
param[1].direction shouldBe IN
|
||||
param[2].direction shouldBe IN
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with type array multidimensional of text" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one text[][]) language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have parameter type is array" {
|
||||
param[0].type.isArray shouldBe true
|
||||
}
|
||||
|
||||
"should have parameter type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
}
|
||||
}
|
||||
|
||||
"Parameters with type fixed size array" - {
|
||||
val param = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun(one text[45], two text[1][]) language plpgsql as
|
||||
$$ begin end;$$;
|
||||
""".trimIndent()
|
||||
).parameters
|
||||
|
||||
"should have parameter type is array" {
|
||||
param[0].type.isArray shouldBe true
|
||||
param[1].type.isArray shouldBe true
|
||||
}
|
||||
|
||||
"should have parameter type name" {
|
||||
param[0].type.name shouldBe "text"
|
||||
param[1].type.name shouldBe "text"
|
||||
}
|
||||
|
||||
"should return the type with array" {
|
||||
param[0].type.toString() shouldBe "text[45]"
|
||||
param[1].type.toString() shouldBe "text[1][]"
|
||||
}
|
||||
|
||||
"should return the type name" {
|
||||
param[0].toString() shouldBe "in one text[45]"
|
||||
param[1].toString() shouldBe "in two text[1][]"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"Function Returns" - {
|
||||
"should return the type text" {
|
||||
val returns = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun() returns text language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).returns
|
||||
|
||||
returns shouldBeInstanceOf Primitive::class
|
||||
returns.definition shouldBe "text"
|
||||
returns.isSetOf shouldBe false
|
||||
}
|
||||
|
||||
"should return the type character varying" {
|
||||
val returns = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun() returns character varying language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).returns
|
||||
|
||||
returns shouldBeInstanceOf Primitive::class
|
||||
returns.definition shouldBe "character varying"
|
||||
returns.isSetOf shouldBe false
|
||||
}
|
||||
|
||||
"should return the type character varying(255)" {
|
||||
val returns = parseFunction(
|
||||
// language=PostgreSQL
|
||||
"""
|
||||
create or replace function myfun() returns character varying(255) language plpgsql as
|
||||
$$ begin; end$$;
|
||||
""".trimIndent()
|
||||
).returns
|
||||
|
||||
returns shouldBeInstanceOf Primitive::class
|
||||
returns.definition shouldBe "character varying(255)"
|
||||
returns.isSetOf shouldBe false
|
||||
}
|
||||
}
|
||||
|
||||
// "function returns" - {
|
||||
// "should return the type text if function return text" {
|
||||
// Function(
|
||||
// // language=PostgreSQL
|
||||
// """
|
||||
// create or replace function test001() returns text language plpgsql as
|
||||
// $$ begin; end$$;
|
||||
// """.trimIndent()
|
||||
// ).returns shouldBe "text"
|
||||
// }
|
||||
//
|
||||
// "return null if function return void" {
|
||||
// Function(
|
||||
// // language=PostgreSQL
|
||||
// """
|
||||
// create or replace function test001() returns void language plpgsql as
|
||||
// $$ begin; end$$;
|
||||
// """.trimIndent()
|
||||
// ).returns shouldBe null
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// "Parameters" - {
|
||||
// "One parameter text" - {
|
||||
// val param = Function(
|
||||
// // language=PostgreSQL
|
||||
// """
|
||||
// create or replace function myfun(
|
||||
// one text
|
||||
// ) returns text language plpgsql as
|
||||
// $$ begin end;$$;
|
||||
// """.trimIndent()
|
||||
// ).parameters
|
||||
//
|
||||
// "Function must have one parameter" {
|
||||
// param shouldHaveSize 1
|
||||
// }
|
||||
//
|
||||
// "The parameter must be in lower case" {
|
||||
// param.getOrNull(0)?.name shouldBe "one"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// "parameters" - {
|
||||
// val param = Function(
|
||||
// // language=PostgreSQL
|
||||
// """
|
||||
// create or replace function myfun(
|
||||
// one text,
|
||||
// "Two" INTEGER default 5,
|
||||
// "Three ""and"" half" character varying = 'Yes',
|
||||
// Three_and_more character varying(255) default 'Hello',
|
||||
// dot point default '(1, 2)'::point,
|
||||
// num NUMERIC(10, 3) default 123.654,
|
||||
// arr01 text[] default '{hello, world, "and others", and\ more, "with \", ], [ , ) and as $$ in text"}'::text[],
|
||||
// arr02 "point"[] default array['(1, 2)'::point, '(7, 12)'::point]::point[],
|
||||
// arr03 text[] default array[
|
||||
// 'text01',
|
||||
// 'text02"([,#-',
|
||||
// null
|
||||
// ],
|
||||
// last "text" default 'Hi'
|
||||
// ) returns text language plpgsql as
|
||||
// $$ begin end;$$;
|
||||
// """.trimIndent()
|
||||
// ).parameters
|
||||
//
|
||||
// "count must be correct" {
|
||||
// param shouldHaveSize 10
|
||||
// }
|
||||
//
|
||||
// "name" - {
|
||||
// "in lower case" {
|
||||
// param.getOrNull(0)?.name shouldBe "one"
|
||||
// }
|
||||
// "in camel case with double quote" {
|
||||
// param.getOrNull(1)?.name shouldBe "Two"
|
||||
// }
|
||||
// "with spaces and double quote" {
|
||||
// param.getOrNull(2)?.name shouldBe "Three \"and\" half"
|
||||
// }
|
||||
// "in snake_case" {
|
||||
// param.getOrNull(3)?.name shouldBe "three_and_more"
|
||||
// }
|
||||
// "with numbers" {
|
||||
// param.getOrNull(5)?.name shouldBe "arr01"
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// "type" - {
|
||||
// "text in lower case" {
|
||||
// param.getOrNull(0)?.type shouldBe "text"
|
||||
// }
|
||||
// "integer in UPPER case" {
|
||||
// param.getOrNull(1)?.type shouldBe "integer"
|
||||
// }
|
||||
// "character varying in two word" {
|
||||
// param.getOrNull(2)?.type shouldBe "character varying"
|
||||
// }
|
||||
// "character varying with max size" - {
|
||||
// "dont return the scale type in name" {
|
||||
// param.getOrNull(3)?.type shouldBe "character varying"
|
||||
// }
|
||||
// "return the correct size" {
|
||||
// param.getOrNull(3)?.type?.precision shouldBe 255
|
||||
// }
|
||||
// }
|
||||
// "numeric with precision and scale" - {
|
||||
// "dont return the precision and scale type in name" {
|
||||
// param.getOrNull(5)?.type shouldBe "numeric"
|
||||
// }
|
||||
// "return the correct precision" {
|
||||
// param.getOrNull(5)?.type?.precision shouldBe 10
|
||||
// }
|
||||
// "return the correct scale" {
|
||||
// param.getOrNull(5)?.type?.scale shouldBe 3
|
||||
// }
|
||||
// }
|
||||
// "array of text" {
|
||||
// param.getOrNull(7)?.type shouldBe "text[]"
|
||||
// }
|
||||
// }
|
||||
// "default" - {
|
||||
// "with array of composite type Point" - {
|
||||
// """must return "arr02" at name""" {
|
||||
// param.getOrNull(7)?.name shouldBe "arr02"
|
||||
// }
|
||||
// """must return "point[]" at type""" {
|
||||
// param.getOrNull(7)?.type shouldBe "point[]"
|
||||
// }
|
||||
// """must return "array[(1, 2)::point, (7, 12)::point]::point[]" at default""" {
|
||||
// param.getOrNull(7)?.default shouldBe "array[(1, 2)::point, (7, 12)::point]::point[]"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
})
|
||||
@@ -0,0 +1,97 @@
|
||||
package fr.postgresjson.functionGenerator
|
||||
|
||||
import fr.postgresjson.definition.parse.parseFunction
|
||||
import io.kotest.core.Tag
|
||||
import io.kotest.core.annotation.Ignored
|
||||
import io.kotest.core.spec.style.StringSpec
|
||||
import org.amshove.kluent.`should be equal to`
|
||||
|
||||
@Ignored
|
||||
class FunctionGeneratorTest : StringSpec({
|
||||
tags(Tag("Generator"))
|
||||
val functionDirectory = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
val generator = FunctionGenerator(functionDirectory)
|
||||
|
||||
"generate function with input object and output object" {
|
||||
val functionSql = """
|
||||
|create or replace function test_function_object (inout resource json)
|
||||
|language plpgsql
|
||||
|as
|
||||
|$$
|
||||
|begin
|
||||
| resource = json_build_object('id', '1e5f5d41-6d14-4007-897b-0ed2616bec96', 'name', 'changedName');
|
||||
|end;
|
||||
|$$
|
||||
""".trimMargin()
|
||||
|
||||
val expectedGenerated = """
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import com.fasterxml.jackson.core.type.TypeReference
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|
|
||||
|inline fun <reified E: Any, S: Any?> Requester.testFunctionObject(resource: S): E? {
|
||||
| return getFunction("test_function_object")
|
||||
| .execute<E>(object : TypeReference<E>() {}, mapOf("resource" to resource))
|
||||
|}
|
||||
""".trimMargin()
|
||||
|
||||
generator.generate(parseFunction(functionSql)) `should be equal to` expectedGenerated
|
||||
}
|
||||
|
||||
"generate function with return void" {
|
||||
val functionSql = """
|
||||
|create or replace function test_function_void (name text default 'plop') returns void
|
||||
|language plpgsql
|
||||
|as
|
||||
|$$
|
||||
|begin
|
||||
| perform 1;
|
||||
|end;
|
||||
|$$;
|
||||
""".trimMargin()
|
||||
|
||||
val expectedGenerated = """
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|
|
||||
|fun Requester.testFunctionVoid(name: String = "plop"): Unit {
|
||||
| getFunction("test_function_void")
|
||||
| .exec(mapOf("name" to name))
|
||||
|}
|
||||
""".trimMargin()
|
||||
|
||||
generator.generate(parseFunction(functionSql)) `should be equal to` expectedGenerated
|
||||
}
|
||||
|
||||
"generate function with multiple args and defaults" {
|
||||
val functionSql = """
|
||||
|create or replace function test_function_multiple (name text default 'plop', in hi text default 'hello', out result json)
|
||||
|language plpgsql
|
||||
|as
|
||||
|$$
|
||||
|begin
|
||||
| result = json_build_array(
|
||||
| json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name),
|
||||
| json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', hi)
|
||||
| );
|
||||
|end;
|
||||
|$$
|
||||
""".trimMargin()
|
||||
|
||||
val expectedGenerated = """
|
||||
|package fr.postgresjson.functionGenerator.generated
|
||||
|
|
||||
|import com.fasterxml.jackson.core.type.TypeReference
|
||||
|import fr.postgresjson.connexion.Requester
|
||||
|
|
||||
|inline fun <reified E: Any> Requester.testFunctionMultiple(name: String = "plop", hi: String = "hello"): E? {
|
||||
| return getFunction("test_function_multiple")
|
||||
| .execute<E>(object : TypeReference<E>() {}, mapOf("name" to name, "hi" to hi))
|
||||
|}
|
||||
""".trimMargin()
|
||||
|
||||
generator.generate(parseFunction(functionSql)) `should be equal to` expectedGenerated
|
||||
}
|
||||
})
|
||||
@@ -8,7 +8,7 @@ create schema if not exists public;
|
||||
|
||||
create table if not exists test
|
||||
(
|
||||
id serial not null
|
||||
id uuid not null
|
||||
constraint test_pk
|
||||
primary key,
|
||||
name text
|
||||
@@ -16,24 +16,24 @@ create table if not exists test
|
||||
|
||||
create table if not exists test2
|
||||
(
|
||||
id serial not null,
|
||||
id uuid not null,
|
||||
title text,
|
||||
test_id integer
|
||||
test_id uuid
|
||||
constraint test2_test_id_fk
|
||||
references test
|
||||
);
|
||||
|
||||
INSERT INTO test (id, name) VALUES (1, 'plop') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES (1, 'plop', 1) ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES (2, 'plip', 1) ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES (3, 'ttt', null) ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test (id, name) VALUES ('1e5f5d41-6d14-4007-897b-0ed2616bec96', 'plop') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES ('1e5f5d41-6d14-4007-897b-0ed2616bec96', 'plop', '1e5f5d41-6d14-4007-897b-0ed2616bec96') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES ('829b1a29-5db8-47f9-9562-961c561ac528', 'plip', '1e5f5d41-6d14-4007-897b-0ed2616bec96') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES ('457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'ttt', null) ON CONFLICT DO NOTHING;
|
||||
|
||||
CREATE OR REPLACE FUNCTION test_function (name text default 'plop', IN hi text default 'hello', out result json)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_object('id', 3, 'name', name);
|
||||
result = json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
END;
|
||||
$$;
|
||||
|
||||
@@ -44,8 +44,8 @@ AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_array(
|
||||
json_build_object('id', 3, 'name', name),
|
||||
json_build_object('id', 4, 'name', hi)
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', hi)
|
||||
);
|
||||
END;
|
||||
$$;
|
||||
@@ -56,8 +56,8 @@ AS
|
||||
$$
|
||||
BEGIN
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', name::text),
|
||||
json_build_object('id', 4, 'name', name::text || '-2')
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name::text),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', name::text || '-2')
|
||||
),
|
||||
10
|
||||
INTO result, total
|
||||
@@ -70,11 +70,11 @@ CREATE OR REPLACE FUNCTION test_function_object (inout resource json)
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
resource = json_build_object('id', 1, 'name', 'changedName');
|
||||
resource = json_build_object('id', '1e5f5d41-6d14-4007-897b-0ed2616bec96', 'name', 'changedName');
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void
|
||||
CREATE OR REPLACE FUNCTION test_function_void (name text default 'plop') returns void
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
CREATE OR REPLACE FUNCTION test_function (name text default 'plop', IN hi text default 'hello', out result json)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_object('id', 3, 'name', name);
|
||||
END;
|
||||
$$
|
||||
@@ -1,8 +0,0 @@
|
||||
CREATE OR REPLACE FUNCTION test_function_object (inout resource json)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
resource = json_build_object('id', 1, 'name', 'changedName');
|
||||
END;
|
||||
$$
|
||||
@@ -1,8 +0,0 @@
|
||||
CREATE OR REPLACE FUNCTION function_void (name text default 'plop') returns void
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
PERFORM 1;
|
||||
END;
|
||||
$$;
|
||||
8
src/test/resources/sql/function/Test/test_function.sql
Normal file
8
src/test/resources/sql/function/Test/test_function.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
create or replace function test_function(name text default 'plop', in hi text default 'hello', out result json)
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
begin
|
||||
result = json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
end;
|
||||
$$
|
||||
@@ -0,0 +1,34 @@
|
||||
create or replace function test_function_multiparam(
|
||||
name varchar(45) default 'plop',
|
||||
numeric(4, 5),
|
||||
num float(5),
|
||||
num2 timestamp without time zone default '2002-01-01T00:00:00'::timestamp,
|
||||
num3 int,
|
||||
num4 integer,
|
||||
num5 smallint,
|
||||
num6 bigint,
|
||||
num7 decimal,
|
||||
num8 decimal(4, 6),
|
||||
num9 real,
|
||||
num10 double precision,
|
||||
num11 smallserial,
|
||||
num12 serial,
|
||||
num13 bigserial,
|
||||
num14 serial,
|
||||
num15 money,
|
||||
num16 character varying(789),
|
||||
num16b character varying(789) default 'abc',
|
||||
num16c character varying default 'abc',
|
||||
num17 character(56),
|
||||
num18 char(2),
|
||||
num19 any,
|
||||
num20 anyelement,
|
||||
num21 anyarray
|
||||
)
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
begin
|
||||
perform 1;
|
||||
end;
|
||||
$$;
|
||||
@@ -1,11 +1,11 @@
|
||||
CREATE OR REPLACE FUNCTION test_function_multiple (name text default 'plop', IN hi text default 'hello', out result json)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
create or replace function test_function_multiple(name text default 'plop', in hi text default 'hello', out result json)
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
BEGIN
|
||||
begin
|
||||
result = json_build_array(
|
||||
json_build_object('id', 3, 'name', name),
|
||||
json_build_object('id', 4, 'name', hi)
|
||||
);
|
||||
END;
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', hi)
|
||||
);
|
||||
end;
|
||||
$$
|
||||
@@ -0,0 +1,8 @@
|
||||
create or replace function test_function_object(inout resource json)
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
begin
|
||||
resource = json_build_object('id', '1e5f5d41-6d14-4007-897b-0ed2616bec96', 'name', 'changedName');
|
||||
end;
|
||||
$$
|
||||
@@ -1,14 +0,0 @@
|
||||
CREATE OR REPLACE FUNCTION test_function_paginated (name text default 'plop', IN "limit" int default 10, IN "offset" int default 0, out result json, out total int)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', name::text),
|
||||
json_build_object('id', 4, 'name', name::text || '-2')
|
||||
),
|
||||
10
|
||||
INTO result, total
|
||||
LIMIT "limit" OFFSET "offset";
|
||||
END;
|
||||
$$
|
||||
@@ -0,0 +1,8 @@
|
||||
create or replace function test_function_void(name text default 'plop') returns void
|
||||
language plpgsql
|
||||
as
|
||||
$$
|
||||
begin
|
||||
perform 1;
|
||||
end;
|
||||
$$;
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE OR REPLACE FUNCTION test_function_duplicate (name text default 'plop') returns json
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
return json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
END;
|
||||
$$
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE OR REPLACE FUNCTION test_function_duplicate (name text default 'plop', out result text)
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
result = name;
|
||||
END;
|
||||
$$
|
||||
@@ -1 +1,2 @@
|
||||
-- name: DeleteTest
|
||||
delete FROM test where 2038538 = 2;
|
||||
4
src/test/resources/sql/query/Test/selectMultiple.sql
Normal file
4
src/test/resources/sql/query/Test/selectMultiple.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', :name::text),
|
||||
json_build_object('id', '6085c12e-e94d-4ae1-b7ad-23acc7a82a98', 'name', :name::text || '-2')
|
||||
)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user