mirror of
https://github.com/pikami/cosmium.git
synced 2026-01-10 04:55:23 +00:00
Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6b816b55a | ||
|
|
12215fba76 | ||
|
|
a1793c17ab | ||
|
|
96d3a0a7ae | ||
|
|
8b8b087aab | ||
|
|
c2c9dc03b3 | ||
|
|
d86bac7d79 | ||
|
|
69b76c1c3e | ||
|
|
8e3db3e44d | ||
|
|
f5b8453995 | ||
|
|
928ca29fe4 | ||
|
|
39cd9e2357 | ||
|
|
bcf4b513b6 | ||
|
|
363f822e5a | ||
|
|
be7a615931 | ||
|
|
83f086a2dc | ||
|
|
777034181f | ||
|
|
84c33e3c8e | ||
|
|
5e677431a3 | ||
|
|
a4659d90a9 | ||
|
|
503e6bb8ad | ||
|
|
e5ddc143f0 | ||
|
|
66ea859f34 | ||
|
|
3584f9b5ce | ||
|
|
c7d01b4593 | ||
|
|
2834f3f641 | ||
|
|
a6b5d32ff7 | ||
|
|
0e98e3481a | ||
|
|
827046f634 | ||
|
|
475d586dc5 | ||
|
|
9abef691d6 | ||
|
|
62dcbc1f2b | ||
|
|
2f42651fb7 | ||
|
|
20af73ee9c | ||
|
|
3bdff9b643 | ||
|
|
b808e97c72 | ||
|
|
e623a563f4 | ||
|
|
2cd61aa620 | ||
|
|
0cec7816c1 | ||
|
|
ec5f82cd54 | ||
|
|
26dcd68ace | ||
|
|
86c0275410 | ||
|
|
398584368f | ||
|
|
5b66828bd0 | ||
|
|
6ed74688ca | ||
|
|
b72bba86c8 | ||
|
|
18edb925bf | ||
|
|
6ccb7c4bdd | ||
|
|
b9e38575bc | ||
|
|
3aeae98404 | ||
|
|
5ff923ce2c | ||
|
|
f3f3966dd5 | ||
|
|
19f62f8173 |
14
.github/ISSUE_TEMPLATE.md
vendored
Normal file
14
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
#### Summary
|
||||
Bug report in one concise sentence
|
||||
|
||||
#### Steps to reproduce
|
||||
How can we reproduce the issue (what version are you using?)
|
||||
|
||||
#### Expected behavior
|
||||
Describe your issue in detail
|
||||
|
||||
#### Observed behavior (that appears unintentional)
|
||||
What did you see happen? Please include relevant error messages.
|
||||
|
||||
#### Possible fixes
|
||||
If you can, link to the line of code that might be responsible for the problem
|
||||
18
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
18
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<!-- Thank you for contributing a pull request! Here are a few tips to help you:
|
||||
|
||||
1. If applicable, please check if unit tests are added for new features
|
||||
2. Read the contribution guide lines https://github.com/pikami/cosmium/docs/CONTRIBUTING.md
|
||||
|
||||
-->
|
||||
|
||||
#### Summary
|
||||
<!--
|
||||
A description of what this pull request does, as well as QA test steps (if applicable).
|
||||
-->
|
||||
|
||||
#### Ticket Link
|
||||
<!--
|
||||
If applicable, please include a link to the GitHub issue:
|
||||
|
||||
Fixes https://github.com/pikami/cosmium/issues/XXX
|
||||
-->
|
||||
30
.github/workflows/compile-shared-libraries.yml
vendored
Normal file
30
.github/workflows/compile-shared-libraries.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
name: Cross-Compile Shared Libraries
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Cross-Compile with xgo
|
||||
uses: crazy-max/ghaction-xgo@v3.1.0
|
||||
with:
|
||||
xgo_version: latest
|
||||
go_version: 1.22.0
|
||||
dest: dist
|
||||
pkg: sharedlibrary
|
||||
prefix: cosmium
|
||||
targets: linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
|
||||
v: true
|
||||
buildmode: c-shared
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: shared-libraries
|
||||
path: dist/*
|
||||
9
.github/workflows/release.yml
vendored
9
.github/workflows/release.yml
vendored
@@ -7,6 +7,7 @@ on:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
|
||||
jobs:
|
||||
goreleaser:
|
||||
@@ -19,7 +20,13 @@ jobs:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: 1.21.6
|
||||
go-version: 1.22.0
|
||||
- name: Docker Login
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Run GoReleaser
|
||||
uses: goreleaser/goreleaser-action@v5
|
||||
with:
|
||||
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,6 @@
|
||||
dist/
|
||||
ignored/
|
||||
explorer_www/
|
||||
main
|
||||
save.json
|
||||
.vscode/
|
||||
|
||||
100
.goreleaser.yaml
100
.goreleaser.yaml
@@ -1,5 +1,6 @@
|
||||
builds:
|
||||
- binary: cosmium
|
||||
main: ./cmd/server
|
||||
goos:
|
||||
- darwin
|
||||
- linux
|
||||
@@ -9,11 +10,6 @@ builds:
|
||||
- arm64
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
ignore:
|
||||
- goos: linux
|
||||
goarch: arm64
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
|
||||
release:
|
||||
prerelease: auto
|
||||
@@ -26,10 +22,102 @@ brews:
|
||||
homepage: 'https://github.com/pikami/cosmium'
|
||||
repository:
|
||||
owner: pikami
|
||||
name: homebrew-pikami
|
||||
name: homebrew-brew
|
||||
commit_author:
|
||||
name: pikami
|
||||
email: git@pikami.org
|
||||
|
||||
dockers:
|
||||
- id: docker-linux-amd64
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-amd64"
|
||||
dockerfile: Dockerfile
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
- "--platform=linux/amd64"
|
||||
- "--pull"
|
||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
||||
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
|
||||
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
||||
- id: docker-linux-arm64
|
||||
goos: linux
|
||||
goarch: arm64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64v8"
|
||||
dockerfile: Dockerfile
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
- "--platform=linux/arm64"
|
||||
- "--pull"
|
||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
||||
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
|
||||
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
||||
- id: docker-explorer-linux-amd64
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
|
||||
dockerfile: Explorer.Dockerfile
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
- "--platform=linux/amd64"
|
||||
- "--pull"
|
||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
||||
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
|
||||
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
||||
- id: docker-explorer-linux-arm64
|
||||
goos: linux
|
||||
goarch: arm64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64v8"
|
||||
dockerfile: Explorer.Dockerfile
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
- "--platform=linux/arm64"
|
||||
- "--pull"
|
||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
||||
- "--label=org.opencontainers.image.description=Lightweight Cosmos DB emulator"
|
||||
- "--label=org.opencontainers.image.url=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.source=https://github.com/pikami/cosmium"
|
||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
||||
|
||||
docker_manifests:
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:latest'
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64v8"
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}'
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:explorer'
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64v8"
|
||||
|
||||
checksum:
|
||||
name_template: 'checksums.txt'
|
||||
|
||||
6
Dockerfile
Normal file
6
Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM alpine:latest
|
||||
|
||||
WORKDIR /app
|
||||
COPY cosmium /app/cosmium
|
||||
|
||||
ENTRYPOINT ["/app/cosmium"]
|
||||
9
Explorer.Dockerfile
Normal file
9
Explorer.Dockerfile
Normal file
@@ -0,0 +1,9 @@
|
||||
FROM ghcr.io/cosmiumdev/cosmos-explorer-base:latest AS explorer-base
|
||||
FROM alpine:latest
|
||||
|
||||
COPY --from=explorer-base /cosmos-explorer /cosmos-explorer
|
||||
|
||||
WORKDIR /app
|
||||
COPY cosmium /app/cosmium
|
||||
|
||||
ENTRYPOINT ["/app/cosmium", "-ExplorerDir", "/cosmos-explorer"]
|
||||
47
Makefile
47
Makefile
@@ -4,28 +4,65 @@ GOTEST=$(GOCMD) test
|
||||
GOCLEAN=$(GOCMD) clean
|
||||
|
||||
BINARY_NAME=cosmium
|
||||
SERVER_LOCATION=./cmd/server
|
||||
|
||||
SHARED_LIB_LOCATION=./sharedlibrary
|
||||
SHARED_LIB_OPT=-buildmode=c-shared
|
||||
XGO_TARGETS=linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
|
||||
GOVERSION=1.22.0
|
||||
|
||||
DIST_DIR=dist
|
||||
|
||||
SHARED_LIB_TEST_CC=gcc
|
||||
SHARED_LIB_TEST_CFLAGS=-Wall -ldl
|
||||
SHARED_LIB_TEST_TARGET=$(DIST_DIR)/sharedlibrary_test
|
||||
SHARED_LIB_TEST_DIR=./sharedlibrary/tests
|
||||
SHARED_LIB_TEST_SOURCES=$(wildcard $(SHARED_LIB_TEST_DIR)/*.c)
|
||||
|
||||
all: test build-all
|
||||
|
||||
build-all: build-darwin-arm64 build-darwin-amd64 build-linux-amd64 build-windows-amd64
|
||||
build-all: build-darwin-arm64 build-darwin-amd64 build-linux-amd64 build-linux-arm64 build-windows-amd64 build-windows-arm64
|
||||
|
||||
build-darwin-arm64:
|
||||
@echo "Building macOS ARM binary..."
|
||||
@GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64 .
|
||||
@GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64 $(SERVER_LOCATION)
|
||||
|
||||
build-darwin-amd64:
|
||||
@echo "Building macOS x64 binary..."
|
||||
@GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64 .
|
||||
@GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64 $(SERVER_LOCATION)
|
||||
|
||||
build-linux-amd64:
|
||||
@echo "Building Linux x64 binary..."
|
||||
@GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 .
|
||||
@GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 $(SERVER_LOCATION)
|
||||
|
||||
build-linux-arm64:
|
||||
@echo "Building Linux ARM binary..."
|
||||
@GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-linux-arm64 $(SERVER_LOCATION)
|
||||
|
||||
build-windows-amd64:
|
||||
@echo "Building Windows x64 binary..."
|
||||
@GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe .
|
||||
@GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe $(SERVER_LOCATION)
|
||||
|
||||
build-windows-arm64:
|
||||
@echo "Building Windows ARM binary..."
|
||||
@GOOS=windows GOARCH=arm64 $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME)-windows-arm64.exe $(SERVER_LOCATION)
|
||||
|
||||
build-sharedlib-linux-amd64:
|
||||
@echo "Building shared library for Linux x64..."
|
||||
@GOOS=linux GOARCH=amd64 $(GOBUILD) $(SHARED_LIB_OPT) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64.so $(SHARED_LIB_LOCATION)
|
||||
|
||||
build-sharedlib-tests: build-sharedlib-linux-amd64
|
||||
@echo "Building shared library tests..."
|
||||
@$(SHARED_LIB_TEST_CC) $(SHARED_LIB_TEST_CFLAGS) -o $(SHARED_LIB_TEST_TARGET) $(SHARED_LIB_TEST_SOURCES)
|
||||
|
||||
run-sharedlib-tests: build-sharedlib-tests
|
||||
@echo "Running shared library tests..."
|
||||
@$(SHARED_LIB_TEST_TARGET) $(DIST_DIR)/$(BINARY_NAME)-linux-amd64.so
|
||||
|
||||
xgo-compile-sharedlib:
|
||||
@echo "Building shared libraries using xgo..."
|
||||
@mkdir -p $(DIST_DIR)
|
||||
@xgo -targets=$(XGO_TARGETS) -go $(GOVERSION) -buildmode=c-shared -dest=$(DIST_DIR) -out=$(BINARY_NAME) -pkg=$(SHARED_LIB_LOCATION) .
|
||||
|
||||
generate-parser-nosql:
|
||||
pigeon -o ./parsers/nosql/nosql.go ./parsers/nosql/nosql.peg
|
||||
|
||||
82
README.md
82
README.md
@@ -1,11 +1,23 @@
|
||||
# Cosmium
|
||||
|
||||
Cosmium is a lightweight Cosmos DB emulator designed to facilitate local development and testing. While it aims to provide developers with a solution for running a local database during development, it's important to note that it's not 100% compatible with Cosmos DB. However, it serves as a convenient tool for E2E or integration tests during the CI/CD pipeline.
|
||||
Cosmium is a lightweight Cosmos DB emulator designed to facilitate local development and testing. While it aims to provide developers with a solution for running a local database during development, it's important to note that it's not 100% compatible with Cosmos DB. However, it serves as a convenient tool for E2E or integration tests during the CI/CD pipeline. Read more about compatibility [here](./docs/COMPATIBILITY.md).
|
||||
|
||||
One of Cosmium's notable features is its ability to save and load state to a single JSON file. This feature makes it easy to load different test cases or share state with other developers, enhancing collaboration and efficiency in development workflows.
|
||||
|
||||
# Getting Started
|
||||
### Downloading Cosmium
|
||||
|
||||
### Installation via Homebrew
|
||||
|
||||
You can install Cosmium using Homebrew by adding the `pikami/brew` tap and then installing the package.
|
||||
|
||||
```sh
|
||||
brew tap pikami/brew
|
||||
brew install cosmium
|
||||
```
|
||||
|
||||
This will download and install Cosmium on your system, making it easy to manage and update using Homebrew.
|
||||
|
||||
### Downloading Cosmium Binaries
|
||||
|
||||
You can download the latest version of Cosmium from the [GitHub Releases page](https://github.com/pikami/cosmium/releases). Choose the appropriate release for your operating system and architecture.
|
||||
|
||||
@@ -13,48 +25,80 @@ You can download the latest version of Cosmium from the [GitHub Releases page](h
|
||||
|
||||
Cosmium is available for the following platforms:
|
||||
|
||||
* **Linux**: cosmium-linux-amd64
|
||||
* **macOS**: cosmium-darwin-amd64
|
||||
* **macOS on Apple Silicon**: cosmium-darwin-arm64
|
||||
* **Windows**: cosmium-windows-amd64.exe
|
||||
- **Linux**: cosmium-linux-amd64
|
||||
- **Linux on ARM**: cosmium-linux-arm64
|
||||
- **macOS**: cosmium-darwin-amd64
|
||||
- **macOS on Apple Silicon**: cosmium-darwin-arm64
|
||||
- **Windows**: cosmium-windows-amd64.exe
|
||||
- **Windows on ARM**: cosmium-windows-arm64.exe
|
||||
|
||||
### Running Cosmium
|
||||
|
||||
Once downloaded, you can launch Cosmium using the following command:
|
||||
|
||||
```sh
|
||||
./cosmium-linux-amd64 \
|
||||
-Cert "cert.crt" \
|
||||
-CertKey "cert.key" \
|
||||
-Persist "./save.json" \
|
||||
-InitialData "./save.json"
|
||||
cosmium -Persist "./save.json"
|
||||
```
|
||||
|
||||
Connection String Example:
|
||||
|
||||
```
|
||||
AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;
|
||||
```
|
||||
|
||||
### Running Cosmos DB Explorer
|
||||
### Running Cosmos DB Explorer
|
||||
|
||||
If you want to run Cosmos DB Explorer alongside Cosmium, you'll need to build it yourself and point the `-ExplorerDir` argument to the dist directory. Please refer to the [Cosmos DB Explorer repository](https://github.com/Azure/cosmos-explorer) for instructions on building the application.
|
||||
|
||||
There's also a prebuilt docker image that includes the explorer: `ghcr.io/pikami/cosmium:explorer`
|
||||
|
||||
Once running, the explorer can be reached by navigating following URL: `https://127.0.0.1:8081/_explorer/` (might be different depending on your configuration).
|
||||
|
||||
### Running with docker (optional)
|
||||
|
||||
There are two docker tags available:
|
||||
|
||||
- ghcr.io/pikami/cosmium:latest - Cosmium core service
|
||||
- ghcr.io/pikami/cosmium:explorer - Cosmium with database explorer available on `https://127.0.0.1:8081/_explorer/`
|
||||
|
||||
If you wan to run the application using docker, configure it using environment variables see example:
|
||||
|
||||
```sh
|
||||
docker run --rm \
|
||||
-e COSMIUM_PERSIST=/save.json \
|
||||
-v ./save.json:/save.json \
|
||||
-p 8081:8081 \
|
||||
ghcr.io/pikami/cosmium # or `ghcr.io/pikami/cosmium:explorer`
|
||||
```
|
||||
|
||||
### SSL Certificate
|
||||
|
||||
By default, Cosmium runs on HTTP. However, if you provide an SSL certificate, it will use HTTPS. Most applications will require HTTPS, so you can specify paths to the SSL certificate and key (PEM format) using the `-Cert` and `-CertKey` arguments, respectively.
|
||||
By default, Cosmium uses a pre-generated SSL certificate. You can provide your own certificates by specifying paths to the SSL certificate and key (PEM format) using the `-Cert` and `-CertKey` arguments, respectively.
|
||||
|
||||
To disable SSL and run Cosmium on HTTP instead, you can use the `-DisableTls` flag. However most applications will require HTTPS.
|
||||
|
||||
### Other Available Arguments
|
||||
|
||||
* **-AccountKey**: Account key for authentication (default "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==")
|
||||
* **-DisableAuth**: Disable authentication
|
||||
* **-Host**: Hostname (default "localhost")
|
||||
* **-InitialData**: Path to JSON containing initial state
|
||||
* **-Persist**: Saves data to the given path on application exit
|
||||
* **-Port**: Listen port (default 8081)
|
||||
- **-AccountKey**: Account key for authentication (default "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==")
|
||||
- **-DisableAuth**: Disable authentication
|
||||
- **-Host**: Hostname (default "localhost")
|
||||
- **-InitialData**: Path to JSON containing initial state
|
||||
- **-Persist**: Saves data to the given path on application exit (When `-InitialData` argument is not supplied, it will try to load data from path supplied in `-Persist`)
|
||||
- **-Port**: Listen port (default 8081)
|
||||
- **-LogLevel**: Sets the logging level (one of: debug, info, error, silent) (default info)
|
||||
|
||||
These arguments allow you to configure various aspects of Cosmium's behavior according to your requirements.
|
||||
|
||||
All mentioned arguments can also be set using environment variables:
|
||||
|
||||
- **COSMIUM_ACCOUNTKEY** for `-AccountKey`
|
||||
- **COSMIUM_DISABLEAUTH** for `-DisableAuth`
|
||||
- **COSMIUM_HOST** for `-Host`
|
||||
- **COSMIUM_INITIALDATA** for `-InitialData`
|
||||
- **COSMIUM_PERSIST** for `-Persist`
|
||||
- **COSMIUM_PORT** for `-Port`
|
||||
- **COSMIUM_LOGLEVEL** for `-LogLevel`
|
||||
|
||||
# License
|
||||
|
||||
This project is [MIT licensed](./LICENSE).
|
||||
|
||||
35
api/api_server.go
Normal file
35
api/api_server.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
type ApiServer struct {
|
||||
stopServer chan interface{}
|
||||
isActive bool
|
||||
router *gin.Engine
|
||||
config config.ServerConfig
|
||||
}
|
||||
|
||||
func NewApiServer(dataRepository *repositories.DataRepository, config config.ServerConfig) *ApiServer {
|
||||
stopChan := make(chan interface{})
|
||||
|
||||
apiServer := &ApiServer{
|
||||
stopServer: stopChan,
|
||||
config: config,
|
||||
}
|
||||
|
||||
apiServer.CreateRouter(dataRepository)
|
||||
|
||||
return apiServer
|
||||
}
|
||||
|
||||
func (s *ApiServer) GetRouter() *gin.Engine {
|
||||
return s.router
|
||||
}
|
||||
|
||||
func (s *ApiServer) Stop() {
|
||||
s.stopServer <- true
|
||||
}
|
||||
@@ -3,15 +3,19 @@ package config
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
||||
DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
|
||||
EnvPrefix = "COSMIUM_"
|
||||
ExplorerBaseUrlLocation = "/_explorer"
|
||||
)
|
||||
|
||||
var Config = ServerConfig{}
|
||||
|
||||
func ParseFlags() {
|
||||
func ParseFlags() ServerConfig {
|
||||
host := flag.String("Host", "localhost", "Hostname")
|
||||
port := flag.Int("Port", 8081, "Listen port")
|
||||
explorerPath := flag.String("ExplorerDir", "", "Path to cosmos-explorer files")
|
||||
@@ -20,21 +24,74 @@ func ParseFlags() {
|
||||
initialDataPath := flag.String("InitialData", "", "Path to JSON containing initial state")
|
||||
accountKey := flag.String("AccountKey", DefaultAccountKey, "Account key for authentication")
|
||||
disableAuthentication := flag.Bool("DisableAuth", false, "Disable authentication")
|
||||
disableTls := flag.Bool("DisableTls", false, "Disable TLS, serve over HTTP")
|
||||
persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit")
|
||||
logLevel := NewEnumValue("info", []string{"debug", "info", "error", "silent"})
|
||||
flag.Var(logLevel, "LogLevel", fmt.Sprintf("Sets the logging level %s", logLevel.AllowedValuesList()))
|
||||
|
||||
flag.Parse()
|
||||
setFlagsFromEnvironment()
|
||||
|
||||
Config.Host = *host
|
||||
Config.Port = *port
|
||||
Config.ExplorerPath = *explorerPath
|
||||
Config.TLS_CertificatePath = *tlsCertificatePath
|
||||
Config.TLS_CertificateKey = *tlsCertificateKey
|
||||
Config.InitialDataFilePath = *initialDataPath
|
||||
Config.PersistDataFilePath = *persistDataPath
|
||||
Config.DisableAuth = *disableAuthentication
|
||||
config := ServerConfig{}
|
||||
config.Host = *host
|
||||
config.Port = *port
|
||||
config.ExplorerPath = *explorerPath
|
||||
config.TLS_CertificatePath = *tlsCertificatePath
|
||||
config.TLS_CertificateKey = *tlsCertificateKey
|
||||
config.InitialDataFilePath = *initialDataPath
|
||||
config.PersistDataFilePath = *persistDataPath
|
||||
config.DisableAuth = *disableAuthentication
|
||||
config.DisableTls = *disableTls
|
||||
config.AccountKey = *accountKey
|
||||
config.LogLevel = logLevel.value
|
||||
|
||||
Config.DatabaseAccount = Config.Host
|
||||
Config.DatabaseDomain = Config.Host
|
||||
Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port)
|
||||
Config.AccountKey = *accountKey
|
||||
config.PopulateCalculatedFields()
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (c *ServerConfig) PopulateCalculatedFields() {
|
||||
c.DatabaseAccount = c.Host
|
||||
c.DatabaseDomain = c.Host
|
||||
c.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", c.Host, c.Port)
|
||||
c.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
|
||||
|
||||
switch c.LogLevel {
|
||||
case "debug":
|
||||
logger.LogLevel = logger.LogLevelDebug
|
||||
case "info":
|
||||
logger.LogLevel = logger.LogLevelInfo
|
||||
case "error":
|
||||
logger.LogLevel = logger.LogLevelError
|
||||
case "silent":
|
||||
logger.LogLevel = logger.LogLevelSilent
|
||||
default:
|
||||
logger.LogLevel = logger.LogLevelInfo
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ServerConfig) ApplyDefaultsToEmptyFields() {
|
||||
if c.Host == "" {
|
||||
c.Host = "localhost"
|
||||
}
|
||||
if c.Port == 0 {
|
||||
c.Port = 8081
|
||||
}
|
||||
if c.AccountKey == "" {
|
||||
c.AccountKey = DefaultAccountKey
|
||||
}
|
||||
}
|
||||
|
||||
func setFlagsFromEnvironment() (err error) {
|
||||
flag.VisitAll(func(f *flag.Flag) {
|
||||
name := EnvPrefix + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
|
||||
if value, ok := os.LookupEnv(name); ok {
|
||||
err2 := flag.Set(f.Name, value)
|
||||
if err2 != nil {
|
||||
err = fmt.Errorf("failed setting flag from environment: %w", err2)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
36
api/config/enumFlag.go
Normal file
36
api/config/enumFlag.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type EnumValue struct {
|
||||
allowedValues []string
|
||||
value string
|
||||
}
|
||||
|
||||
func (e *EnumValue) String() string {
|
||||
return e.value
|
||||
}
|
||||
|
||||
func (e *EnumValue) Set(v string) error {
|
||||
for _, allowed := range e.allowedValues {
|
||||
if v == allowed {
|
||||
e.value = v
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("invalid value %q, must be one of: %s", v, strings.Join(e.allowedValues, ", "))
|
||||
}
|
||||
|
||||
func NewEnumValue(defaultValue string, allowedValues []string) *EnumValue {
|
||||
return &EnumValue{
|
||||
allowedValues: allowedValues,
|
||||
value: defaultValue,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EnumValue) AllowedValuesList() string {
|
||||
return fmt.Sprintf("(one of: %s)", strings.Join(e.allowedValues, ", "))
|
||||
}
|
||||
@@ -1,17 +1,20 @@
|
||||
package config
|
||||
|
||||
type ServerConfig struct {
|
||||
DatabaseAccount string
|
||||
DatabaseDomain string
|
||||
DatabaseEndpoint string
|
||||
AccountKey string
|
||||
DatabaseAccount string `json:"databaseAccount"`
|
||||
DatabaseDomain string `json:"databaseDomain"`
|
||||
DatabaseEndpoint string `json:"databaseEndpoint"`
|
||||
AccountKey string `json:"accountKey"`
|
||||
|
||||
ExplorerPath string
|
||||
Port int
|
||||
Host string
|
||||
TLS_CertificatePath string
|
||||
TLS_CertificateKey string
|
||||
InitialDataFilePath string
|
||||
PersistDataFilePath string
|
||||
DisableAuth bool
|
||||
ExplorerPath string `json:"explorerPath"`
|
||||
Port int `json:"port"`
|
||||
Host string `json:"host"`
|
||||
TLS_CertificatePath string `json:"tlsCertificatePath"`
|
||||
TLS_CertificateKey string `json:"tlsCertificateKey"`
|
||||
InitialDataFilePath string `json:"initialDataFilePath"`
|
||||
PersistDataFilePath string `json:"persistDataFilePath"`
|
||||
DisableAuth bool `json:"disableAuth"`
|
||||
DisableTls bool `json:"disableTls"`
|
||||
LogLevel string `json:"logLevel"`
|
||||
ExplorerBaseUrlLocation string `json:"explorerBaseUrlLocation"`
|
||||
}
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllCollections(c *gin.Context) {
|
||||
func (h *Handlers) GetAllCollections(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
|
||||
collections, status := repositories.GetAllCollections(databaseId)
|
||||
collections, status := h.repository.GetAllCollections(databaseId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
database, _ := repositories.GetDatabase(databaseId)
|
||||
database, _ := h.repository.GetDatabase(databaseId)
|
||||
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(collections)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": database.ResourceID,
|
||||
"DocumentCollections": collections,
|
||||
@@ -26,11 +27,11 @@ func GetAllCollections(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetCollection(c *gin.Context) {
|
||||
func (h *Handlers) GetCollection(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
id := c.Param("collId")
|
||||
|
||||
collection, status := repositories.GetCollection(databaseId, id)
|
||||
collection, status := h.repository.GetCollection(databaseId, id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, collection)
|
||||
return
|
||||
@@ -44,11 +45,11 @@ func GetCollection(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteCollection(c *gin.Context) {
|
||||
func (h *Handlers) DeleteCollection(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
id := c.Param("collId")
|
||||
|
||||
status := repositories.DeleteCollection(databaseId, id)
|
||||
status := h.repository.DeleteCollection(databaseId, id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -62,7 +63,7 @@ func DeleteCollection(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func CreateCollection(c *gin.Context) {
|
||||
func (h *Handlers) CreateCollection(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
var newCollection repositorymodels.Collection
|
||||
|
||||
@@ -76,7 +77,7 @@ func CreateCollection(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
createdCollection, status := repositories.CreateCollection(databaseId, newCollection)
|
||||
createdCollection, status := h.repository.CreateCollection(databaseId, newCollection)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
|
||||
@@ -4,9 +4,14 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func CosmiumExport(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, repositories.GetState())
|
||||
func (h *Handlers) CosmiumExport(c *gin.Context) {
|
||||
repositoryState, err := h.repository.GetState()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(http.StatusOK, "application/json", []byte(repositoryState))
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllDatabases(c *gin.Context) {
|
||||
databases, status := repositories.GetAllDatabases()
|
||||
func (h *Handlers) GetAllDatabases(c *gin.Context) {
|
||||
databases, status := h.repository.GetAllDatabases()
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(databases)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": "",
|
||||
"Databases": databases,
|
||||
@@ -22,10 +23,10 @@ func GetAllDatabases(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetDatabase(c *gin.Context) {
|
||||
func (h *Handlers) GetDatabase(c *gin.Context) {
|
||||
id := c.Param("databaseId")
|
||||
|
||||
database, status := repositories.GetDatabase(id)
|
||||
database, status := h.repository.GetDatabase(id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, database)
|
||||
return
|
||||
@@ -39,10 +40,10 @@ func GetDatabase(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteDatabase(c *gin.Context) {
|
||||
func (h *Handlers) DeleteDatabase(c *gin.Context) {
|
||||
id := c.Param("databaseId")
|
||||
|
||||
status := repositories.DeleteDatabase(id)
|
||||
status := h.repository.DeleteDatabase(id)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -56,7 +57,7 @@ func DeleteDatabase(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func CreateDatabase(c *gin.Context) {
|
||||
func (h *Handlers) CreateDatabase(c *gin.Context) {
|
||||
var newDatabase repositorymodels.Database
|
||||
|
||||
if err := c.BindJSON(&newDatabase); err != nil {
|
||||
@@ -69,7 +70,7 @@ func CreateDatabase(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
createdDatabase, status := repositories.CreateDatabase(newDatabase)
|
||||
createdDatabase, status := h.repository.CreateDatabase(newDatabase)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
jsonpatch "github.com/cosmiumdev/json-patch/v5"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllDocuments(c *gin.Context) {
|
||||
func (h *Handlers) GetAllDocuments(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
documents, status := repositories.GetAllDocuments(databaseId, collectionId)
|
||||
documents, status := h.repository.GetAllDocuments(databaseId, collectionId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
||||
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(documents)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": collection.ID,
|
||||
"Documents": documents,
|
||||
@@ -28,12 +33,12 @@ func GetAllDocuments(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func GetDocument(c *gin.Context) {
|
||||
func (h *Handlers) GetDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
document, status := repositories.GetDocument(databaseId, collectionId, documentId)
|
||||
document, status := h.repository.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, document)
|
||||
return
|
||||
@@ -47,12 +52,12 @@ func GetDocument(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DeleteDocument(c *gin.Context) {
|
||||
func (h *Handlers) DeleteDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||
status := h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
@@ -67,7 +72,7 @@ func DeleteDocument(c *gin.Context) {
|
||||
}
|
||||
|
||||
// TODO: Maybe move "replace" logic to repository
|
||||
func ReplaceDocument(c *gin.Context) {
|
||||
func (h *Handlers) ReplaceDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
@@ -78,13 +83,13 @@ func ReplaceDocument(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||
status := h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, requestBody)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
@@ -98,7 +103,83 @@ func ReplaceDocument(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DocumentsPost(c *gin.Context) {
|
||||
func (h *Handlers) PatchDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
document, status := h.repository.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
var requestBody map[string]interface{}
|
||||
if err := c.BindJSON(&requestBody); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
operations := requestBody["operations"]
|
||||
operationsBytes, err := json.Marshal(operations)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "Could not decode operations"})
|
||||
return
|
||||
}
|
||||
|
||||
patch, err := jsonpatch.DecodePatch(operationsBytes)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
currentDocumentBytes, err := json.Marshal(document)
|
||||
if err != nil {
|
||||
logger.ErrorLn("Failed to marshal existing document:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to marshal existing document"})
|
||||
return
|
||||
}
|
||||
|
||||
modifiedDocumentBytes, err := patch.Apply(currentDocumentBytes)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var modifiedDocument map[string]interface{}
|
||||
err = json.Unmarshal(modifiedDocumentBytes, &modifiedDocument)
|
||||
if err != nil {
|
||||
logger.ErrorLn("Failed to unmarshal modified document:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to unmarshal modified document"})
|
||||
return
|
||||
}
|
||||
|
||||
if modifiedDocument["id"] != document["id"] {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"message": "The ID field cannot be modified"})
|
||||
return
|
||||
}
|
||||
|
||||
status = h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, modifiedDocument)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, createdDocument)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) DocumentsPost(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
@@ -120,14 +201,15 @@ func DocumentsPost(c *gin.Context) {
|
||||
queryParameters = parametersToMap(paramsArray)
|
||||
}
|
||||
|
||||
docs, status := repositories.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
||||
docs, status := h.repository.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
||||
if status != repositorymodels.StatusOk {
|
||||
// TODO: Currently we return everything if the query fails
|
||||
GetAllDocuments(c)
|
||||
h.GetAllDocuments(c)
|
||||
return
|
||||
}
|
||||
|
||||
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(docs)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": collection.ResourceID,
|
||||
"Documents": docs,
|
||||
@@ -141,7 +223,12 @@ func DocumentsPost(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
|
||||
if isUpsert {
|
||||
h.repository.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
|
||||
}
|
||||
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, requestBody)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
|
||||
@@ -4,15 +4,14 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func RegisterExplorerHandlers(router *gin.Engine) {
|
||||
explorer := router.Group("/_explorer")
|
||||
func (h *Handlers) RegisterExplorerHandlers(router *gin.Engine) {
|
||||
explorer := router.Group(h.config.ExplorerBaseUrlLocation)
|
||||
{
|
||||
explorer.Use(func(ctx *gin.Context) {
|
||||
if ctx.Param("filepath") == "/config.json" {
|
||||
endpoint := fmt.Sprintf("https://%s:%d", config.Config.Host, config.Config.Port)
|
||||
endpoint := fmt.Sprintf("https://%s:%d", h.config.Host, h.config.Port)
|
||||
ctx.JSON(200, gin.H{
|
||||
"BACKEND_ENDPOINT": endpoint,
|
||||
"MONGO_BACKEND_ENDPOINT": endpoint,
|
||||
@@ -25,8 +24,8 @@ func RegisterExplorerHandlers(router *gin.Engine) {
|
||||
}
|
||||
})
|
||||
|
||||
if config.Config.ExplorerPath != "" {
|
||||
explorer.Static("/", config.Config.ExplorerPath)
|
||||
if h.config.ExplorerPath != "" {
|
||||
explorer.Static("/", h.config.ExplorerPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
api/handlers/handlers.go
Normal file
18
api/handlers/handlers.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
repository *repositories.DataRepository
|
||||
config config.ServerConfig
|
||||
}
|
||||
|
||||
func NewHandlers(dataRepository *repositories.DataRepository, config config.ServerConfig) *Handlers {
|
||||
return &Handlers{
|
||||
repository: dataRepository,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
@@ -1,59 +1,37 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/authentication"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
func Authentication() gin.HandlerFunc {
|
||||
func Authentication(config config.ServerConfig) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
requestUrl := c.Request.URL.String()
|
||||
if config.Config.DisableAuth ||
|
||||
strings.HasPrefix(requestUrl, "/_explorer") ||
|
||||
if config.DisableAuth ||
|
||||
strings.HasPrefix(requestUrl, config.ExplorerBaseUrlLocation) ||
|
||||
strings.HasPrefix(requestUrl, "/cosmium") {
|
||||
return
|
||||
}
|
||||
|
||||
var resourceType string
|
||||
parts := strings.Split(requestUrl, "/")
|
||||
switch len(parts) {
|
||||
case 2, 3:
|
||||
resourceType = parts[1]
|
||||
case 4, 5:
|
||||
resourceType = parts[3]
|
||||
case 6, 7:
|
||||
resourceType = parts[5]
|
||||
}
|
||||
|
||||
databaseId, _ := c.Params.Get("databaseId")
|
||||
collId, _ := c.Params.Get("collId")
|
||||
docId, _ := c.Params.Get("docId")
|
||||
var resourceId string
|
||||
if databaseId != "" {
|
||||
resourceId += "dbs/" + databaseId
|
||||
}
|
||||
if collId != "" {
|
||||
resourceId += "/colls/" + collId
|
||||
}
|
||||
if docId != "" {
|
||||
resourceId += "/docs/" + docId
|
||||
}
|
||||
resourceType := urlToResourceType(requestUrl)
|
||||
resourceId := requestToResourceId(c)
|
||||
|
||||
authHeader := c.Request.Header.Get("authorization")
|
||||
date := c.Request.Header.Get("x-ms-date")
|
||||
expectedSignature := authentication.GenerateSignature(
|
||||
c.Request.Method, resourceType, resourceId, date, config.Config.AccountKey)
|
||||
c.Request.Method, resourceType, resourceId, date, config.AccountKey)
|
||||
|
||||
decoded, _ := url.QueryUnescape(authHeader)
|
||||
params, _ := url.ParseQuery(decoded)
|
||||
clientSignature := strings.Replace(params.Get("sig"), " ", "+", -1)
|
||||
if clientSignature != expectedSignature {
|
||||
fmt.Printf("Got wrong signature from client.\n- Expected: %s\n- Got: %s\n", expectedSignature, clientSignature)
|
||||
logger.Errorf("Got wrong signature from client.\n- Expected: %s\n- Got: %s\n", expectedSignature, clientSignature)
|
||||
c.IndentedJSON(401, gin.H{
|
||||
"code": "Unauthorized",
|
||||
"message": "Wrong signature.",
|
||||
@@ -62,3 +40,44 @@ func Authentication() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func urlToResourceType(requestUrl string) string {
|
||||
var resourceType string
|
||||
parts := strings.Split(requestUrl, "/")
|
||||
switch len(parts) {
|
||||
case 2, 3:
|
||||
resourceType = parts[1]
|
||||
case 4, 5:
|
||||
resourceType = parts[3]
|
||||
case 6, 7:
|
||||
resourceType = parts[5]
|
||||
}
|
||||
|
||||
return resourceType
|
||||
}
|
||||
|
||||
func requestToResourceId(c *gin.Context) string {
|
||||
databaseId, _ := c.Params.Get("databaseId")
|
||||
collId, _ := c.Params.Get("collId")
|
||||
docId, _ := c.Params.Get("docId")
|
||||
resourceType := urlToResourceType(c.Request.URL.String())
|
||||
|
||||
var resourceId string
|
||||
if databaseId != "" {
|
||||
resourceId += "dbs/" + databaseId
|
||||
}
|
||||
if collId != "" {
|
||||
resourceId += "/colls/" + collId
|
||||
}
|
||||
if docId != "" {
|
||||
resourceId += "/docs/" + docId
|
||||
}
|
||||
|
||||
isFeed := c.Request.Header.Get("A-Im") == "Incremental Feed"
|
||||
if resourceType == "pkranges" && isFeed {
|
||||
// CosmosSDK replaces '/' with '-' in resource id requests
|
||||
resourceId = strings.Replace(collId, "-", "/", -1)
|
||||
}
|
||||
|
||||
return resourceId
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
func RequestLogger() gin.HandlerFunc {
|
||||
@@ -16,7 +16,7 @@ func RequestLogger() gin.HandlerFunc {
|
||||
|
||||
bodyStr := readBody(rdr1)
|
||||
if bodyStr != "" {
|
||||
fmt.Println(bodyStr)
|
||||
logger.DebugLn(bodyStr)
|
||||
}
|
||||
|
||||
c.Request.Body = rdr2
|
||||
|
||||
21
api/handlers/middleware/strip_trailing_slashes.go
Normal file
21
api/handlers/middleware/strip_trailing_slashes.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func StripTrailingSlashes(r *gin.Engine, config config.ServerConfig) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
if len(path) > 1 && path[len(path)-1] == '/' && !strings.Contains(path, config.ExplorerBaseUrlLocation) {
|
||||
c.Request.URL.Path = path[:len(path)-1]
|
||||
r.HandleContext(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func GetOffers(c *gin.Context) {
|
||||
c.Header("x-ms-item-count", "0")
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": "",
|
||||
"_count": 0,
|
||||
|
||||
@@ -5,11 +5,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetPartitionKeyRanges(c *gin.Context) {
|
||||
func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
@@ -18,7 +17,7 @@ func GetPartitionKeyRanges(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
partitionKeyRanges, status := repositories.GetPartitionKeyRanges(databaseId, collectionId)
|
||||
partitionKeyRanges, status := h.repository.GetPartitionKeyRanges(databaseId, collectionId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("etag", "\"420\"")
|
||||
c.Header("lsn", "420")
|
||||
@@ -26,9 +25,14 @@ func GetPartitionKeyRanges(c *gin.Context) {
|
||||
c.Header("x-ms-global-committed-lsn", "420")
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(partitionKeyRanges)))
|
||||
|
||||
collection, _ := repositories.GetCollection(databaseId, collectionId)
|
||||
collectionRid := collectionId
|
||||
collection, _ := h.repository.GetCollection(databaseId, collectionId)
|
||||
if collection.ResourceID != "" {
|
||||
collectionRid = collection.ResourceID
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": collection.ResourceID,
|
||||
"_rid": collectionRid,
|
||||
"_count": len(partitionKeyRanges),
|
||||
"PartitionKeyRanges": partitionKeyRanges,
|
||||
})
|
||||
|
||||
@@ -1,12 +1,41 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
)
|
||||
|
||||
func GetServerInfo(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, constants.ServerInfoResponse)
|
||||
func (h *Handlers) GetServerInfo(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_self": "",
|
||||
"id": h.config.DatabaseAccount,
|
||||
"_rid": fmt.Sprintf("%s.%s", h.config.DatabaseAccount, h.config.DatabaseDomain),
|
||||
"media": "//media/",
|
||||
"addresses": "//addresses/",
|
||||
"_dbs": "//dbs/",
|
||||
"writableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"readableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
"userReplicationPolicy": map[string]interface{}{
|
||||
"asyncReplication": false,
|
||||
"minReplicaSetSize": 1,
|
||||
"maxReplicasetSize": 4,
|
||||
},
|
||||
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
|
||||
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
|
||||
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
|
||||
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,23 +1,118 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllStoredProcedures(c *gin.Context) {
|
||||
func (h *Handlers) GetAllStoredProcedures(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
sps, status := repositories.GetAllStoredProcedures(databaseId, collectionId)
|
||||
sps, status := h.repository.GetAllStoredProcedures(databaseId, collectionId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(sps)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "StoredProcedures": sps, "_count": len(sps)})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) GetStoredProcedure(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
spId := c.Param("spId")
|
||||
|
||||
sp, status := h.repository.GetStoredProcedure(databaseId, collectionId, spId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, sp)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) DeleteStoredProcedure(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
spId := c.Param("spId")
|
||||
|
||||
status := h.repository.DeleteStoredProcedure(databaseId, collectionId, spId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) ReplaceStoredProcedure(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
spId := c.Param("spId")
|
||||
|
||||
var sp repositorymodels.StoredProcedure
|
||||
if err := c.BindJSON(&sp); err != nil {
|
||||
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
status := h.repository.DeleteStoredProcedure(databaseId, collectionId, spId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdSP, status := h.repository.CreateStoredProcedure(databaseId, collectionId, sp)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, createdSP)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) CreateStoredProcedure(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
var sp repositorymodels.StoredProcedure
|
||||
if err := c.BindJSON(&sp); err != nil {
|
||||
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
createdSP, status := h.repository.CreateStoredProcedure(databaseId, collectionId, sp)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, createdSP)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
@@ -1,23 +1,118 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllTriggers(c *gin.Context) {
|
||||
func (h *Handlers) GetAllTriggers(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
triggers, status := repositories.GetAllTriggers(databaseId, collectionId)
|
||||
triggers, status := h.repository.GetAllTriggers(databaseId, collectionId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(triggers)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Triggers": triggers, "_count": len(triggers)})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) GetTrigger(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
triggerId := c.Param("triggerId")
|
||||
|
||||
trigger, status := h.repository.GetTrigger(databaseId, collectionId, triggerId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, trigger)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) DeleteTrigger(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
triggerId := c.Param("triggerId")
|
||||
|
||||
status := h.repository.DeleteTrigger(databaseId, collectionId, triggerId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) ReplaceTrigger(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
triggerId := c.Param("triggerId")
|
||||
|
||||
var trigger repositorymodels.Trigger
|
||||
if err := c.BindJSON(&trigger); err != nil {
|
||||
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
status := h.repository.DeleteTrigger(databaseId, collectionId, triggerId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdTrigger, status := h.repository.CreateTrigger(databaseId, collectionId, trigger)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, createdTrigger)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) CreateTrigger(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
var trigger repositorymodels.Trigger
|
||||
if err := c.BindJSON(&trigger); err != nil {
|
||||
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
createdTrigger, status := h.repository.CreateTrigger(databaseId, collectionId, trigger)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, createdTrigger)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
@@ -1,23 +1,118 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func GetAllUserDefinedFunctions(c *gin.Context) {
|
||||
func (h *Handlers) GetAllUserDefinedFunctions(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
udfs, status := repositories.GetAllUserDefinedFunctions(databaseId, collectionId)
|
||||
udfs, status := h.repository.GetAllUserDefinedFunctions(databaseId, collectionId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(udfs)))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "UserDefinedFunctions": udfs, "_count": len(udfs)})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) GetUserDefinedFunction(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
udfId := c.Param("udfId")
|
||||
|
||||
udf, status := h.repository.GetUserDefinedFunction(databaseId, collectionId, udfId)
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, udf)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) DeleteUserDefinedFunction(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
udfId := c.Param("udfId")
|
||||
|
||||
status := h.repository.DeleteUserDefinedFunction(databaseId, collectionId, udfId)
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) ReplaceUserDefinedFunction(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
udfId := c.Param("udfId")
|
||||
|
||||
var udf repositorymodels.UserDefinedFunction
|
||||
if err := c.BindJSON(&udf); err != nil {
|
||||
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
status := h.repository.DeleteUserDefinedFunction(databaseId, collectionId, udfId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdUdf, status := h.repository.CreateUserDefinedFunction(databaseId, collectionId, udf)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, createdUdf)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func (h *Handlers) CreateUserDefinedFunction(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
var udf repositorymodels.UserDefinedFunction
|
||||
if err := c.BindJSON(&udf); err != nil {
|
||||
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
createdUdf, status := h.repository.CreateUserDefinedFunction(databaseId, collectionId, udf)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, createdUdf)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
136
api/router.go
136
api/router.go
@@ -1,45 +1,131 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/handlers"
|
||||
"github.com/pikami/cosmium/api/handlers/middleware"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
||||
)
|
||||
|
||||
func CreateRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
||||
routeHandlers := handlers.NewHandlers(repository, s.config)
|
||||
|
||||
router.Use(middleware.RequestLogger())
|
||||
router.Use(middleware.Authentication())
|
||||
gin.DefaultWriter = logger.InfoWriter()
|
||||
gin.DefaultErrorWriter = logger.ErrorWriter()
|
||||
|
||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)
|
||||
if s.config.LogLevel != "debug" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
router.POST("/dbs/:databaseId/colls/:collId/docs", handlers.DocumentsPost)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs", handlers.GetAllDocuments)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.GetDocument)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.ReplaceDocument)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument)
|
||||
router := gin.Default(func(e *gin.Engine) {
|
||||
e.RedirectTrailingSlash = false
|
||||
})
|
||||
|
||||
router.POST("/dbs/:databaseId/colls", handlers.CreateCollection)
|
||||
router.GET("/dbs/:databaseId/colls", handlers.GetAllCollections)
|
||||
router.GET("/dbs/:databaseId/colls/:collId", handlers.GetCollection)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId", handlers.DeleteCollection)
|
||||
if s.config.LogLevel == "debug" {
|
||||
router.Use(middleware.RequestLogger())
|
||||
}
|
||||
|
||||
router.POST("/dbs", handlers.CreateDatabase)
|
||||
router.GET("/dbs", handlers.GetAllDatabases)
|
||||
router.GET("/dbs/:databaseId", handlers.GetDatabase)
|
||||
router.DELETE("/dbs/:databaseId", handlers.DeleteDatabase)
|
||||
router.Use(middleware.StripTrailingSlashes(router, s.config))
|
||||
router.Use(middleware.Authentication(s.config))
|
||||
|
||||
router.GET("/dbs/:databaseId/colls/:collId/udfs", handlers.GetAllUserDefinedFunctions)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/sprocs", handlers.GetAllStoredProcedures)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/triggers", handlers.GetAllTriggers)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", routeHandlers.GetPartitionKeyRanges)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls/:collId/docs", routeHandlers.DocumentsPost)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs", routeHandlers.GetAllDocuments)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.GetDocument)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.ReplaceDocument)
|
||||
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.PatchDocument)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", routeHandlers.DeleteDocument)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls", routeHandlers.CreateCollection)
|
||||
router.GET("/dbs/:databaseId/colls", routeHandlers.GetAllCollections)
|
||||
router.GET("/dbs/:databaseId/colls/:collId", routeHandlers.GetCollection)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId", routeHandlers.DeleteCollection)
|
||||
|
||||
router.POST("/dbs", routeHandlers.CreateDatabase)
|
||||
router.GET("/dbs", routeHandlers.GetAllDatabases)
|
||||
router.GET("/dbs/:databaseId", routeHandlers.GetDatabase)
|
||||
router.DELETE("/dbs/:databaseId", routeHandlers.DeleteDatabase)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls/:collId/triggers", routeHandlers.CreateTrigger)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/triggers", routeHandlers.GetAllTriggers)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/triggers/:triggerId", routeHandlers.GetTrigger)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/triggers/:triggerId", routeHandlers.ReplaceTrigger)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/triggers/:triggerId", routeHandlers.DeleteTrigger)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls/:collId/sprocs", routeHandlers.CreateStoredProcedure)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/sprocs", routeHandlers.GetAllStoredProcedures)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/sprocs/:sprocId", routeHandlers.GetStoredProcedure)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/sprocs/:sprocId", routeHandlers.ReplaceStoredProcedure)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/sprocs/:sprocId", routeHandlers.DeleteStoredProcedure)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls/:collId/udfs", routeHandlers.CreateUserDefinedFunction)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/udfs", routeHandlers.GetAllUserDefinedFunctions)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/udfs/:udfId", routeHandlers.GetUserDefinedFunction)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/udfs/:udfId", routeHandlers.ReplaceUserDefinedFunction)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/udfs/:udfId", routeHandlers.DeleteUserDefinedFunction)
|
||||
|
||||
router.GET("/offers", handlers.GetOffers)
|
||||
router.GET("/", handlers.GetServerInfo)
|
||||
router.GET("/", routeHandlers.GetServerInfo)
|
||||
|
||||
router.GET("/cosmium/export", handlers.CosmiumExport)
|
||||
router.GET("/cosmium/export", routeHandlers.CosmiumExport)
|
||||
|
||||
handlers.RegisterExplorerHandlers(router)
|
||||
routeHandlers.RegisterExplorerHandlers(router)
|
||||
|
||||
return router
|
||||
s.router = router
|
||||
}
|
||||
|
||||
func (s *ApiServer) Start() {
|
||||
listenAddress := fmt.Sprintf(":%d", s.config.Port)
|
||||
s.isActive = true
|
||||
|
||||
server := &http.Server{
|
||||
Addr: listenAddress,
|
||||
Handler: s.router.Handler(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-s.stopServer
|
||||
logger.InfoLn("Shutting down server...")
|
||||
err := server.Shutdown(context.TODO())
|
||||
if err != nil {
|
||||
logger.ErrorLn("Failed to shutdown server:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if s.config.DisableTls {
|
||||
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
|
||||
err := server.ListenAndServe()
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
logger.ErrorLn("Failed to start HTTP server:", err)
|
||||
}
|
||||
s.isActive = false
|
||||
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
|
||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||
err := server.ListenAndServeTLS(
|
||||
s.config.TLS_CertificatePath,
|
||||
s.config.TLS_CertificateKey)
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
logger.ErrorLn("Failed to start HTTPS server:", err)
|
||||
}
|
||||
s.isActive = false
|
||||
} else {
|
||||
tlsConfig := tlsprovider.GetDefaultTlsConfig()
|
||||
server.TLSConfig = tlsConfig
|
||||
|
||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||
err := server.ListenAndServeTLS("", "")
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
logger.ErrorLn("Failed to start HTTPS server:", err)
|
||||
}
|
||||
s.isActive = false
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -11,16 +11,15 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Authentication(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
@@ -35,26 +34,8 @@ func Test_Authentication(t *testing.T) {
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
})
|
||||
|
||||
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
|
||||
config.Config.DisableAuth = true
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
createResponse, err := client.CreateDatabase(
|
||||
context.TODO(),
|
||||
azcosmos.DatabaseProperties{ID: testDatabaseName},
|
||||
&azcosmos.CreateDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
config.Config.DisableAuth = false
|
||||
})
|
||||
|
||||
t.Run("Should get 401 when wrong account key is used", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||
&azcosmos.ClientOptions{},
|
||||
@@ -85,3 +66,29 @@ func Test_Authentication(t *testing.T) {
|
||||
assert.Contains(t, string(responseBody), "BACKEND_ENDPOINT")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Authentication_Disabled(t *testing.T) {
|
||||
ts := runTestServerCustomConfig(config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
DisableAuth: true,
|
||||
})
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
createResponse, err := client.CreateDatabase(
|
||||
context.TODO(),
|
||||
azcosmos.DatabaseProperties{ID: testDatabaseName},
|
||||
&azcosmos.CreateDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,22 +10,21 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Collections(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
databaseClient, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -40,7 +39,7 @@ func Test_Collections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return conflict when collection exists", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
@@ -60,7 +59,7 @@ func Test_Collections(t *testing.T) {
|
||||
|
||||
t.Run("Collection Read", func(t *testing.T) {
|
||||
t.Run("Should read collection", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
@@ -74,7 +73,7 @@ func Test_Collections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
@@ -93,7 +92,7 @@ func Test_Collections(t *testing.T) {
|
||||
|
||||
t.Run("Collection Delete", func(t *testing.T) {
|
||||
t.Run("Should delete collection", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
@@ -106,7 +105,7 @@ func Test_Collections(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -5,13 +5,37 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func runTestServer() *httptest.Server {
|
||||
config.Config.AccountKey = config.DefaultAccountKey
|
||||
config.Config.ExplorerPath = "/tmp/nothing"
|
||||
type TestServer struct {
|
||||
Server *httptest.Server
|
||||
Repository *repositories.DataRepository
|
||||
URL string
|
||||
}
|
||||
|
||||
return httptest.NewServer(api.CreateRouter())
|
||||
func runTestServerCustomConfig(config config.ServerConfig) *TestServer {
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{})
|
||||
|
||||
api := api.NewApiServer(repository, config)
|
||||
|
||||
server := httptest.NewServer(api.GetRouter())
|
||||
|
||||
return &TestServer{
|
||||
Server: server,
|
||||
Repository: repository,
|
||||
URL: server.URL,
|
||||
}
|
||||
}
|
||||
|
||||
func runTestServer() *TestServer {
|
||||
config := config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
}
|
||||
|
||||
return runTestServerCustomConfig(config)
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -10,24 +10,23 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Databases(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer ts.Server.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Database Create", func(t *testing.T) {
|
||||
t.Run("Should create database", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
|
||||
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
|
||||
ID: testDatabaseName,
|
||||
@@ -38,7 +37,7 @@ func Test_Databases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return conflict when database exists", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
@@ -58,7 +57,7 @@ func Test_Databases(t *testing.T) {
|
||||
|
||||
t.Run("Database Read", func(t *testing.T) {
|
||||
t.Run("Should read database", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
@@ -72,7 +71,7 @@ func Test_Databases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
@@ -91,7 +90,7 @@ func Test_Databases(t *testing.T) {
|
||||
|
||||
t.Run("Database Delete", func(t *testing.T) {
|
||||
t.Run("Should delete database", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
@@ -104,7 +103,7 @@ func Test_Databases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
ts.Repository.DeleteDatabase(testDatabaseName)
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -3,13 +3,17 @@ package tests_test
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -49,9 +53,11 @@ func testCosmosQuery(t *testing.T,
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Documents(t *testing.T) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClient) {
|
||||
ts := runTestServer()
|
||||
|
||||
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
PartitionKey: struct {
|
||||
Paths []string "json:\"paths\""
|
||||
@@ -61,14 +67,11 @@ func Test_Documents(t *testing.T) {
|
||||
Paths: []string{"/pk"},
|
||||
},
|
||||
})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false})
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true})
|
||||
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
ts.Repository.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
|
||||
ts.Repository.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
@@ -76,6 +79,13 @@ func Test_Documents(t *testing.T) {
|
||||
collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
return ts, collectionClient
|
||||
}
|
||||
|
||||
func Test_Documents(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should query document", func(t *testing.T) {
|
||||
testCosmosQuery(t, collectionClient,
|
||||
"SELECT c.id, c[\"pk\"] FROM c ORDER BY c.id",
|
||||
@@ -136,4 +146,236 @@ func Test_Documents(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should query document with query parameters as accessor", func(t *testing.T) {
|
||||
testCosmosQuery(t, collectionClient,
|
||||
`select c.id
|
||||
FROM c
|
||||
WHERE c[@param]="67890"
|
||||
ORDER BY c.id`,
|
||||
[]azcosmos.QueryParameter{
|
||||
{Name: "@param", Value: "id"},
|
||||
},
|
||||
[]interface{}{
|
||||
map[string]interface{}{"id": "67890"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should query array accessor", func(t *testing.T) {
|
||||
testCosmosQuery(t, collectionClient,
|
||||
`SELECT c.id,
|
||||
c["arr"][0] AS arr0,
|
||||
c["arr"][1] AS arr1,
|
||||
c["arr"][2] AS arr2,
|
||||
c["arr"][3] AS arr3
|
||||
FROM c ORDER BY c.id`,
|
||||
nil,
|
||||
[]interface{}{
|
||||
map[string]interface{}{"id": "12345", "arr0": 1.0, "arr1": 2.0, "arr2": 3.0, "arr3": nil},
|
||||
map[string]interface{}{"id": "67890", "arr0": 6.0, "arr1": 7.0, "arr2": 8.0, "arr3": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should handle parallel writes", func(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
rutineCount := 100
|
||||
results := make(chan error, rutineCount)
|
||||
|
||||
createCall := func(i int) {
|
||||
defer wg.Done()
|
||||
item := map[string]interface{}{
|
||||
"id": fmt.Sprintf("id-%d", i),
|
||||
"pk": fmt.Sprintf("pk-%d", i),
|
||||
"val": i,
|
||||
}
|
||||
bytes, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
results <- err
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = collectionClient.CreateItem(
|
||||
ctx,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
results <- err
|
||||
|
||||
collectionClient.ReadItem(ctx, azcosmos.PartitionKey{}, fmt.Sprintf("id-%d", i), nil)
|
||||
collectionClient.DeleteItem(ctx, azcosmos.PartitionKey{}, fmt.Sprintf("id-%d", i), nil)
|
||||
}
|
||||
|
||||
for i := 0; i < rutineCount; i++ {
|
||||
wg.Add(1)
|
||||
go createCall(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
for err := range results {
|
||||
if err != nil {
|
||||
t.Errorf("Error creating item: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Documents_Patch(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should PATCH document", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
expectedData := map[string]interface{}{"id": "67890", "pk": "666", "newField": "newValue", "incr": 15., "setted": "isSet"}
|
||||
|
||||
patch := azcosmos.PatchOperations{}
|
||||
patch.AppendAdd("/newField", "newValue")
|
||||
patch.AppendIncrement("/incr", 15)
|
||||
patch.AppendRemove("/isCool")
|
||||
patch.AppendReplace("/pk", "666")
|
||||
patch.AppendSet("/setted", "isSet")
|
||||
|
||||
itemResponse, err := collectionClient.PatchItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
"67890",
|
||||
patch,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
var itemResponseBody map[string]interface{}
|
||||
json.Unmarshal(itemResponse.Value, &itemResponseBody)
|
||||
|
||||
assert.Equal(t, expectedData["id"], itemResponseBody["id"])
|
||||
assert.Equal(t, expectedData["pk"], itemResponseBody["pk"])
|
||||
assert.Empty(t, itemResponseBody["isCool"])
|
||||
assert.Equal(t, expectedData["newField"], itemResponseBody["newField"])
|
||||
assert.Equal(t, expectedData["incr"], itemResponseBody["incr"])
|
||||
assert.Equal(t, expectedData["setted"], itemResponseBody["setted"])
|
||||
})
|
||||
|
||||
t.Run("Should not allow to PATCH document ID", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
patch := azcosmos.PatchOperations{}
|
||||
patch.AppendReplace("/id", "newValue")
|
||||
|
||||
_, err := collectionClient.PatchItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
"67890",
|
||||
patch,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, http.StatusUnprocessableEntity, respErr.StatusCode)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CreateItem", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{
|
||||
"Id": "6789011",
|
||||
"pk": "456",
|
||||
"newField": "newValue2",
|
||||
}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err2 := collectionClient.CreateItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
t.Run("CreateItem that already exists", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err := collectionClient.CreateItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, http.StatusConflict, respErr.StatusCode)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UpsertItem new", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{"id": "123456", "pk": "1234", "isCool": false, "arr": []int{1, 2, 3}}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err2 := collectionClient.UpsertItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
t.Run("UpsertItem that already exists", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
item := map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3, 4}}
|
||||
bytes, err := json.Marshal(item)
|
||||
assert.Nil(t, err)
|
||||
|
||||
r, err2 := collectionClient.UpsertItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
bytes,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
41
api/tests/documents_trailingslash_test.go
Normal file
41
api/tests/documents_trailingslash_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/authentication"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Request document with trailing slash like python cosmosdb client does.
|
||||
func Test_Documents_Read_Trailing_Slash(t *testing.T) {
|
||||
ts, _ := documents_InitializeDb(t)
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Read doc with client that appends slash to path", func(t *testing.T) {
|
||||
resourceIdTemplate := "dbs/%s/colls/%s/docs/%s"
|
||||
path := fmt.Sprintf(resourceIdTemplate, testDatabaseName, testCollectionName, "12345")
|
||||
testUrl := ts.URL + "/" + path + "/"
|
||||
date := time.Now().Format(time.RFC1123)
|
||||
signature := authentication.GenerateSignature("GET", "docs", path, date, config.DefaultAccountKey)
|
||||
httpClient := &http.Client{}
|
||||
req, _ := http.NewRequest("GET", testUrl, nil)
|
||||
req.Header.Add("x-ms-date", date)
|
||||
req.Header.Add("authorization", "sig="+url.QueryEscape(signature))
|
||||
res, err := httpClient.Do(req)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
if res != nil {
|
||||
defer res.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode, "Expected HTTP status 200 OK")
|
||||
} else {
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
40
cmd/server/server.go
Normal file
40
cmd/server/server.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configuration := config.ParseFlags()
|
||||
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
||||
PersistDataFilePath: configuration.PersistDataFilePath,
|
||||
})
|
||||
|
||||
server := api.NewApiServer(repository, configuration)
|
||||
server.Start()
|
||||
|
||||
waitForExit(server, repository, configuration)
|
||||
}
|
||||
|
||||
func waitForExit(server *api.ApiServer, repository *repositories.DataRepository, config config.ServerConfig) {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// Block until a exit signal is received
|
||||
<-sigs
|
||||
|
||||
// Stop the server
|
||||
server.Stop()
|
||||
|
||||
if config.PersistDataFilePath != "" {
|
||||
repository.SaveStateFS(config.PersistDataFilePath)
|
||||
}
|
||||
}
|
||||
125
docs/CODE_OF_CONDUCT.md
Normal file
125
docs/CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,125 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, caste, color, religion, or sexual
|
||||
identity and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
|
||||
* Demonstrating empathy and kindness toward other people
|
||||
* Being respectful of differing opinions, viewpoints, and experiences
|
||||
* Giving and gracefully accepting constructive feedback
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
* Focusing on what is best not just for us as individuals, but for the overall
|
||||
community
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
|
||||
* The use of sexualized language or imagery, and sexual attention or advances of
|
||||
any kind
|
||||
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or email address,
|
||||
without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official email address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
cosmium@pikami.org.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series of
|
||||
actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or permanent
|
||||
ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within the
|
||||
community.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.1, available at
|
||||
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
||||
|
||||
Community Impact Guidelines were inspired by
|
||||
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
[https://www.contributor-covenant.org/faq][FAQ].
|
||||
217
docs/COMPATIBILITY.md
Normal file
217
docs/COMPATIBILITY.md
Normal file
@@ -0,0 +1,217 @@
|
||||
# Cosmium Compatibility with Cosmos DB
|
||||
|
||||
## Introduction
|
||||
|
||||
Cosmium is designed to emulate the functionality of Cosmos DB, providing developers with a local development environment that closely mimics the behavior of Cosmos DB. While Cosmium aims to be compatible with Cosmos DB, there are certain differences and limitations to be aware of. This document provides an overview of Cosmium's compatibility with Cosmos DB and highlights areas where deviations may occur.
|
||||
|
||||
## Supported Features
|
||||
|
||||
Cosmium strives to support the core features of Cosmos DB, including:
|
||||
|
||||
- REST API
|
||||
- SQL-like query language
|
||||
- Document-based data model
|
||||
|
||||
## Compatibility Matrix
|
||||
|
||||
### Features
|
||||
|
||||
| Feature | Implemented |
|
||||
| ----------------------------- | ----------- |
|
||||
| Subqueries | Yes |
|
||||
| Joins | Yes |
|
||||
| Computed properties | No |
|
||||
| Coalesce operators | No |
|
||||
| Bitwise operators | No |
|
||||
| GeoJSON location data | No |
|
||||
| Parameterized queries | Yes |
|
||||
| Stored procedures | No |
|
||||
| Triggers | No |
|
||||
| User-defined functions (UDFs) | No |
|
||||
|
||||
### Clauses
|
||||
|
||||
| Clause | Implemented |
|
||||
| ------------ | ----------- |
|
||||
| SELECT | Yes |
|
||||
| FROM | Yes |
|
||||
| WHERE | Yes |
|
||||
| ORDER BY | Yes |
|
||||
| GROUP BY | Yes |
|
||||
| OFFSET LIMIT | Yes |
|
||||
|
||||
### Keywords
|
||||
|
||||
| Keyword | Implemented |
|
||||
| -------- | ----------- |
|
||||
| BETWEEN | No |
|
||||
| DISTINCT | Yes |
|
||||
| LIKE | No |
|
||||
| IN | Yes |
|
||||
| TOP | Yes |
|
||||
|
||||
### Aggregate Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| -------- | ----------- |
|
||||
| AVG | Yes |
|
||||
| COUNT | Yes |
|
||||
| MAX | Yes |
|
||||
| MIN | Yes |
|
||||
| SUM | Yes |
|
||||
|
||||
### Array Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| ------------------ | ----------- |
|
||||
| ARRAY_CONCAT | Yes |
|
||||
| ARRAY_CONTAINS | Yes |
|
||||
| ARRAY_CONTAINS_ANY | Yes |
|
||||
| ARRAY_CONTAINS_ALL | Yes |
|
||||
| ARRAY_LENGTH | Yes |
|
||||
| ARRAY_SLICE | Yes |
|
||||
| CHOOSE | No |
|
||||
| ObjectToArray | No |
|
||||
| SetIntersect | Yes |
|
||||
| SetUnion | Yes |
|
||||
|
||||
### Conditional Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| -------- | ----------- |
|
||||
| IIF | No |
|
||||
|
||||
### Date and time Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| ------------------------- | ----------- |
|
||||
| DateTimeAdd | No |
|
||||
| DateTimeBin | No |
|
||||
| DateTimeDiff | No |
|
||||
| DateTimeFromParts | No |
|
||||
| DateTimePart | No |
|
||||
| DateTimeToTicks | No |
|
||||
| DateTimeToTimestamp | No |
|
||||
| GetCurrentDateTime | No |
|
||||
| GetCurrentDateTimeStatic | No |
|
||||
| GetCurrentTicks | No |
|
||||
| GetCurrentTicksStatic | No |
|
||||
| GetCurrentTimestamp | No |
|
||||
| GetCurrentTimestampStatic | No |
|
||||
| TicksToDateTime | No |
|
||||
| TimestampToDateTime | No |
|
||||
|
||||
### Item Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| ---------- | ----------- |
|
||||
| DocumentId | No |
|
||||
|
||||
### Mathematical Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| ---------------- | ----------- |
|
||||
| ABS | Yes |
|
||||
| ACOS | Yes |
|
||||
| ASIN | Yes |
|
||||
| ATAN | Yes |
|
||||
| ATN2 | Yes |
|
||||
| CEILING | Yes |
|
||||
| COS | Yes |
|
||||
| COT | Yes |
|
||||
| DEGREES | Yes |
|
||||
| EXP | Yes |
|
||||
| FLOOR | Yes |
|
||||
| IntAdd | Yes |
|
||||
| IntBitAnd | Yes |
|
||||
| IntBitLeftShift | Yes |
|
||||
| IntBitNot | Yes |
|
||||
| IntBitOr | Yes |
|
||||
| IntBitRightShift | Yes |
|
||||
| IntBitXor | Yes |
|
||||
| IntDiv | Yes |
|
||||
| IntMod | Yes |
|
||||
| IntMul | Yes |
|
||||
| IntSub | Yes |
|
||||
| LOG | Yes |
|
||||
| LOG10 | Yes |
|
||||
| NumberBin | Yes |
|
||||
| PI | Yes |
|
||||
| POWER | Yes |
|
||||
| RADIANS | Yes |
|
||||
| RAND | Yes |
|
||||
| ROUND | Yes |
|
||||
| SIGN | Yes |
|
||||
| SIN | Yes |
|
||||
| SQRT | Yes |
|
||||
| SQUARE | Yes |
|
||||
| TAN | Yes |
|
||||
| TRUNC | Yes |
|
||||
|
||||
### Spatial Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| ------------------ | ----------- |
|
||||
| ST_AREA | No |
|
||||
| ST_DISTANCE | No |
|
||||
| ST_WITHIN | No |
|
||||
| ST_INTERSECTS | No |
|
||||
| ST_ISVALID | No |
|
||||
| ST_ISVALIDDETAILED | No |
|
||||
|
||||
### String Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| --------------- | ----------- |
|
||||
| CONCAT | Yes |
|
||||
| CONTAINS | Yes |
|
||||
| ENDSWITH | Yes |
|
||||
| INDEX_OF | Yes |
|
||||
| LEFT | Yes |
|
||||
| LENGTH | Yes |
|
||||
| LOWER | Yes |
|
||||
| LTRIM | Yes |
|
||||
| REGEXMATCH | No |
|
||||
| REPLACE | Yes |
|
||||
| REPLICATE | Yes |
|
||||
| REVERSE | Yes |
|
||||
| RIGHT | Yes |
|
||||
| RTRIM | Yes |
|
||||
| STARTSWITH | Yes |
|
||||
| STRINGEQUALS | Yes |
|
||||
| StringToArray | No |
|
||||
| StringToBoolean | No |
|
||||
| StringToNull | No |
|
||||
| StringToNumber | No |
|
||||
| StringToObject | No |
|
||||
| SUBSTRING | Yes |
|
||||
| ToString | Yes |
|
||||
| TRIM | Yes |
|
||||
| UPPER | Yes |
|
||||
|
||||
### Type checking Functions
|
||||
|
||||
| Function | Implemented |
|
||||
| ---------------- | ----------- |
|
||||
| IS_ARRAY | Yes |
|
||||
| IS_BOOL | Yes |
|
||||
| IS_DEFINED | Yes |
|
||||
| IS_FINITE_NUMBER | Yes |
|
||||
| IS_INTEGER | Yes |
|
||||
| IS_NULL | Yes |
|
||||
| IS_NUMBER | Yes |
|
||||
| IS_OBJECT | Yes |
|
||||
| IS_PRIMITIVE | Yes |
|
||||
| IS_STRING | Yes |
|
||||
|
||||
## Known Differences
|
||||
|
||||
While Cosmium aims to replicate the behavior of Cosmos DB as closely as possible, there are certain differences and limitations to be aware of:
|
||||
|
||||
1. **Performance**: Cosmium may exhibit different performance characteristics compared to Cosmos DB, especially under heavy load or large datasets.
|
||||
2. **Consistency Levels**: The consistency model in Cosmium may differ slightly from Cosmos DB.
|
||||
3. **Features**: Some advanced features or functionalities of Cosmos DB may not be fully supported or available in Cosmium.
|
||||
|
||||
## Future Development
|
||||
|
||||
Cosmium is actively developed and maintained, with ongoing efforts to improve compatibility with Cosmos DB and enhance its features and capabilities. Future updates may address known differences and limitations, as well as introduce new functionality to bring Cosmium closer to feature parity with Cosmos DB.
|
||||
37
docs/CONTRIBUTING.md
Normal file
37
docs/CONTRIBUTING.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Contributing to Cosmium
|
||||
|
||||
Thank you for considering contributing to Cosmium! We appreciate your interest in helping to improve our project.
|
||||
|
||||
Please note that by participating in this project, you agree to abide by our [Code of Conduct](/docs/CODE_OF_CONDUCT.md). We expect all contributors to uphold the principles of respect, inclusivity, and professionalism.
|
||||
|
||||
If you have any questions or need assistance with the contribution process, feel free to reach out to us by opening an issue or contacting the maintainers directly.
|
||||
|
||||
We look forward to your contributions! 🚀
|
||||
|
||||
## Finding ways to contribute
|
||||
|
||||
A great way to contribute is to scan the [Compatibility Matrix](/docs/compatibility.md) for unsupported features and improving compatibility with CosmosDB.
|
||||
|
||||
A part from that, the [Issues page](https://github.com/pikami/cosmium/issues) might contain issues registered by other users. Fixing reported issues is a great way to contribute.
|
||||
|
||||
## How to Contribute
|
||||
|
||||
1. **Create an Issue**: Before starting work on a new feature or bug fix, please create an issue or look for existing ones on the [Issues page](https://github.com/pikami/cosmium/issues) to discuss your proposed changes. This allows us to provide feedback and ensure that your contribution aligns with the project goals.
|
||||
|
||||
2. **Fork the Repository**: Once you have identified an issue to work on, fork the repository to your own GitHub account.
|
||||
|
||||
3. **Create a Branch**: Create a new branch for your changes using a descriptive name that reflects the issue you are addressing.
|
||||
|
||||
4. **Commit Changes**: Commit your changes with clear and descriptive commit messages. Reference the issue number in the commit message. **Please write unit tests for your implemented feature!**
|
||||
|
||||
5. **Create a Pull Request**: Once your changes are ready, create a pull request from your forked repository to the main repository. Be sure to include a detailed description of your changes and reference the relevant issue.
|
||||
|
||||
6. **Review and Collaborate**: Participate in the code review process by addressing any feedback or comments from maintainers. Collaboration and constructive feedback help ensure the quality of contributions.
|
||||
|
||||
## Example Commits
|
||||
|
||||
To get an idea of how to implement new query functions, you can review the following example commits:
|
||||
|
||||
* [Implement IN function](https://github.com/pikami/cosmium/commit/f37c664c1aef39ee820106eaec1a3708ee7a93c8)
|
||||
* [Implement ToString function](https://github.com/pikami/cosmium/commit/16f41a547956f54481605f0ce035eee978a5e74b)
|
||||
* [Implement ARRAY_CONCAT, ARRAY_LENGTH, ARRAY_SLICE, SetIntersect, SetUnion functions](https://github.com/pikami/cosmium/commit/1c5e5ce85d70ed91e4b9be9e8f76d59e6eafc1b5)
|
||||
51
go.mod
51
go.mod
@@ -1,43 +1,46 @@
|
||||
module github.com/pikami/cosmium
|
||||
|
||||
go 1.21.6
|
||||
go 1.22.0
|
||||
|
||||
require (
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/stretchr/testify v1.8.4
|
||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
|
||||
github.com/cosmiumdev/json-patch/v5 v5.9.3
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
|
||||
github.com/bytedance/sonic v1.9.1 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
|
||||
github.com/bytedance/sonic v1.12.7 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.3 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.0.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.14.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/go-playground/validator/v10 v10.24.0 // indirect
|
||||
github.com/goccy/go-json v0.10.4 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.18.0 // indirect
|
||||
golang.org/x/net v0.20.0 // indirect
|
||||
golang.org/x/sys v0.16.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/protobuf v1.30.0 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
golang.org/x/arch v0.13.0 // indirect
|
||||
golang.org/x/crypto v0.32.0 // indirect
|
||||
golang.org/x/net v0.34.0 // indirect
|
||||
golang.org/x/sys v0.29.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.36.4 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
141
go.sum
141
go.sum
@@ -1,109 +1,118 @@
|
||||
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
|
||||
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2 h1:c4k2FIYIh4xtwqrQwV0Ct1v5+ehlNXj5NI/MWVsiTkQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.2/go.mod h1:5FDJtLEO/GxwNgUxbwrY3LP0pEoThTQJtk2oysdXHxM=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.0.0 h1:Yoicul8bnVdQrhDMTHxdEckRGX01XvwXDHUT9zYZ3k0=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.0.0/go.mod h1:+6sju8gk8FRmSajX3Oz4G5Gm7P+mbqE9FVaXXFYTkCM=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0 h1:1nGuui+4POelzDwI7RG56yfQJHCnKvwfMoU7VsEp+Zg=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0/go.mod h1:99EvauvlcJ1U06amZiksfYz/3aFGyIhWGHVyiZXtBAI=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 h1:tfLQ34V6F7tVSwoTf/4lH5sE0o6eCJuNDTmH09nDpbc=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0/go.mod h1:9kIvujWAA58nmPmWB1m23fyWic1kYZMxD9CxaWn4Qpg=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6 h1:oBqQLSI1pZwGOdXJAoJJSzmff9tlfD4KroVfjQQmd0g=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6/go.mod h1:Beh5cHIXJ0oWEDWk9lNFtuklCojLLQ5hl+LqSNTTs0I=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 h1:LqbJ/WzJUwBf8UiaSzgX7aMclParm9/5Vgp+TY51uBQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2/go.mod h1:yInRyqWXAuaPrgI7p70+lDDgh3mlBohis29jGMISnmc=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v0.4.0 h1:WVsrXCnHlDDX8ls+tootqRE87/hL9S/g4ewig9RsD/c=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v0.4.0/go.mod h1:Vt9sXTKwMyGcOxSmLDMnGPgqsUg7m8pe215qMLrDXw4=
|
||||
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
|
||||
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
|
||||
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mxXfQidrMEnLlPk9UMeRtyBTnEFtxkV0kU=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
|
||||
github.com/bytedance/sonic v1.12.7 h1:CQU8pxOy9HToxhndH0Kx/S1qU/CuS9GnKYrGioDcU1Q=
|
||||
github.com/bytedance/sonic v1.12.7/go.mod h1:tnbal4mxOMju17EGfknm2XyYcpyCnIROYOEYuemj13I=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0=
|
||||
github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/cosmiumdev/json-patch/v5 v5.9.3 h1:l+Og3+5edqV2NHDo58sz72eS733lbXVYP61seYK43Do=
|
||||
github.com/cosmiumdev/json-patch/v5 v5.9.3/go.mod h1:WzSTCdia0WrlZtjnL19P4RiwWtfdyArm/E7stgEeP5g=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
||||
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
|
||||
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
|
||||
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
|
||||
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
||||
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
|
||||
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
|
||||
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI=
|
||||
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
|
||||
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
|
||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
|
||||
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
|
||||
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/arch v0.13.0 h1:KCkqVVV1kGg0X87TFysjCJ8MxtZEIU4Ja/yXGeoECdA=
|
||||
golang.org/x/arch v0.13.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
|
||||
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
|
||||
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
|
||||
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
|
||||
@@ -10,6 +10,11 @@ import (
|
||||
|
||||
// https://learn.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources
|
||||
func GenerateSignature(verb string, resourceType string, resourceId string, date string, masterKey string) string {
|
||||
isNameBased := resourceId != "" && ((len(resourceId) > 4 && resourceId[3] == '/') || strings.HasPrefix(strings.ToLower(resourceId), "interopusers"))
|
||||
if !isNameBased {
|
||||
resourceId = strings.ToLower(resourceId)
|
||||
}
|
||||
|
||||
payload := fmt.Sprintf(
|
||||
"%s\n%s\n%s\n%s\n%s\n",
|
||||
strings.ToLower(verb),
|
||||
|
||||
@@ -27,4 +27,14 @@ func Test_GenerateSignature(t *testing.T) {
|
||||
signature := authentication.GenerateSignature("DELETE", "dbs", "dbs/Test Database", testDate, config.DefaultAccountKey)
|
||||
assert.Equal(t, "LcuXXg0TcXxZG0kUCj9tZIWRy2yCzim3oiqGiHpRqGs=", signature)
|
||||
})
|
||||
|
||||
t.Run("Should generate PKRANGES signature", func(t *testing.T) {
|
||||
signature := authentication.GenerateSignature("GET", "pkranges", "m4d+xG08uVM=", testDate, config.DefaultAccountKey)
|
||||
assert.Equal(t, "6S5ceZsl2EXWB3Jo5bJcK7zv8NxXnsxWPWD9TH3nNMo=", signature)
|
||||
})
|
||||
|
||||
t.Run("Should generate PATCH signature", func(t *testing.T) {
|
||||
signature := authentication.GenerateSignature("PATCH", "docs", "dbs/test-db/colls/test-coll/docs/67890", testDate, config.DefaultAccountKey)
|
||||
assert.Equal(t, "VR1ddfxKBXnoaT+b3WkhyYVc9JmGNpTnaRmyDM44398=", signature)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,43 +1,9 @@
|
||||
package constants
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
var ServerInfoResponse = gin.H{
|
||||
"_self": "",
|
||||
"id": config.Config.DatabaseAccount,
|
||||
"_rid": fmt.Sprintf("%s.%s", config.Config.DatabaseAccount, config.Config.DatabaseDomain),
|
||||
"media": "//media/",
|
||||
"addresses": "//addresses/",
|
||||
"_dbs": "//dbs/",
|
||||
"writableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"readableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
"userReplicationPolicy": map[string]interface{}{
|
||||
"asyncReplication": false,
|
||||
"minReplicaSetSize": 1,
|
||||
"maxReplicasetSize": 4,
|
||||
},
|
||||
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
|
||||
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
|
||||
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
|
||||
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
|
||||
}
|
||||
|
||||
var QueryPlanResponse = gin.H{
|
||||
"partitionedQueryExecutionInfoVersion": 2,
|
||||
"queryInfo": map[string]interface{}{
|
||||
|
||||
124
internal/logger/logger.go
Normal file
124
internal/logger/logger.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LogLevelType int
|
||||
|
||||
var (
|
||||
LogLevelDebug LogLevelType = 0
|
||||
LogLevelInfo LogLevelType = 1
|
||||
LogLevelError LogLevelType = 2
|
||||
LogLevelSilent LogLevelType = 10
|
||||
)
|
||||
|
||||
type LogWriter struct {
|
||||
WriterLevel LogLevelType
|
||||
}
|
||||
|
||||
var LogLevel = LogLevelInfo
|
||||
|
||||
var DebugLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||
var InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||
var ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
|
||||
|
||||
func DebugLn(v ...any) {
|
||||
if LogLevel <= LogLevelDebug {
|
||||
prefix := getCallerPrefix()
|
||||
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Debug(v ...any) {
|
||||
if LogLevel <= LogLevelDebug {
|
||||
prefix := getCallerPrefix()
|
||||
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Debugf(format string, v ...any) {
|
||||
if LogLevel <= LogLevelDebug {
|
||||
prefix := getCallerPrefix()
|
||||
DebugLogger.Printf(prefix+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func InfoLn(v ...any) {
|
||||
if LogLevel <= LogLevelInfo {
|
||||
InfoLogger.Println(v...)
|
||||
}
|
||||
}
|
||||
|
||||
func Info(v ...any) {
|
||||
if LogLevel <= LogLevelInfo {
|
||||
InfoLogger.Print(v...)
|
||||
}
|
||||
}
|
||||
|
||||
func Infof(format string, v ...any) {
|
||||
if LogLevel <= LogLevelInfo {
|
||||
InfoLogger.Printf(format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorLn(v ...any) {
|
||||
if LogLevel <= LogLevelError {
|
||||
prefix := getCallerPrefix()
|
||||
ErrorLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Error(v ...any) {
|
||||
if LogLevel <= LogLevelError {
|
||||
prefix := getCallerPrefix()
|
||||
ErrorLogger.Print(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Errorf(format string, v ...any) {
|
||||
if LogLevel <= LogLevelError {
|
||||
prefix := getCallerPrefix()
|
||||
ErrorLogger.Printf(prefix+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func (lw *LogWriter) Write(p []byte) (n int, err error) {
|
||||
switch lw.WriterLevel {
|
||||
case LogLevelDebug:
|
||||
Debug(string(p))
|
||||
case LogLevelInfo:
|
||||
Info(string(p))
|
||||
case LogLevelError:
|
||||
Error(string(p))
|
||||
}
|
||||
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func ErrorWriter() *LogWriter {
|
||||
return &LogWriter{WriterLevel: LogLevelError}
|
||||
}
|
||||
|
||||
func InfoWriter() *LogWriter {
|
||||
return &LogWriter{WriterLevel: LogLevelInfo}
|
||||
}
|
||||
|
||||
func DebugWriter() *LogWriter {
|
||||
return &LogWriter{WriterLevel: LogLevelDebug}
|
||||
}
|
||||
|
||||
func getCallerPrefix() string {
|
||||
_, file, line, ok := runtime.Caller(2)
|
||||
if ok {
|
||||
parts := strings.Split(file, "/")
|
||||
file = parts[len(parts)-1]
|
||||
return fmt.Sprintf("%s:%d - ", file, line)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
@@ -11,48 +11,60 @@ import (
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
func (r *DataRepository) GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return make([]repositorymodels.Collection, 0), repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
return maps.Values(storeState.Collections[databaseId]), repositorymodels.StatusOk
|
||||
return maps.Values(r.storeState.Collections[databaseId]), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
func (r *DataRepository) GetCollection(databaseId string, collectionId string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
return storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
|
||||
return r.storeState.Collections[databaseId][collectionId], repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
func (r *DataRepository) DeleteCollection(databaseId string, collectionId string) repositorymodels.RepositoryStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
delete(storeState.Collections[databaseId], collectionId)
|
||||
delete(r.storeState.Collections[databaseId], collectionId)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
func (r *DataRepository) CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var database repositorymodels.Database
|
||||
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok = storeState.Collections[databaseId][newCollection.ID]; ok {
|
||||
if _, ok = r.storeState.Collections[databaseId][newCollection.ID]; ok {
|
||||
return repositorymodels.Collection{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
@@ -63,8 +75,11 @@ func CreateCollection(databaseId string, newCollection repositorymodels.Collecti
|
||||
newCollection.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
|
||||
|
||||
storeState.Collections[databaseId][newCollection.ID] = newCollection
|
||||
storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
|
||||
r.storeState.Collections[databaseId][newCollection.ID] = newCollection
|
||||
r.storeState.Documents[databaseId][newCollection.ID] = make(map[string]repositorymodels.Document)
|
||||
r.storeState.Triggers[databaseId][newCollection.ID] = make(map[string]repositorymodels.Trigger)
|
||||
r.storeState.StoredProcedures[databaseId][newCollection.ID] = make(map[string]repositorymodels.StoredProcedure)
|
||||
r.storeState.UserDefinedFunctions[databaseId][newCollection.ID] = make(map[string]repositorymodels.UserDefinedFunction)
|
||||
|
||||
return newCollection, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -10,30 +10,42 @@ import (
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
return maps.Values(storeState.Databases), repositorymodels.StatusOk
|
||||
func (r *DataRepository) GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
return maps.Values(r.storeState.Databases), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
if database, ok := storeState.Databases[id]; ok {
|
||||
func (r *DataRepository) GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if database, ok := r.storeState.Databases[id]; ok {
|
||||
return database, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
return repositorymodels.Database{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
||||
if _, ok := storeState.Databases[id]; !ok {
|
||||
func (r *DataRepository) DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[id]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
delete(storeState.Databases, id)
|
||||
delete(r.storeState.Databases, id)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
if _, ok := storeState.Databases[newDatabase.ID]; ok {
|
||||
func (r *DataRepository) CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[newDatabase.ID]; ok {
|
||||
return repositorymodels.Database{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
@@ -42,9 +54,12 @@ func CreateDatabase(newDatabase repositorymodels.Database) (repositorymodels.Dat
|
||||
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
|
||||
|
||||
storeState.Databases[newDatabase.ID] = newDatabase
|
||||
storeState.Collections[newDatabase.ID] = make(map[string]repositorymodels.Collection)
|
||||
storeState.Documents[newDatabase.ID] = make(map[string]map[string]repositorymodels.Document)
|
||||
r.storeState.Databases[newDatabase.ID] = newDatabase
|
||||
r.storeState.Collections[newDatabase.ID] = make(map[string]repositorymodels.Collection)
|
||||
r.storeState.Documents[newDatabase.ID] = make(map[string]map[string]repositorymodels.Document)
|
||||
r.storeState.Triggers[newDatabase.ID] = make(map[string]map[string]repositorymodels.Trigger)
|
||||
r.storeState.StoredProcedures[newDatabase.ID] = make(map[string]map[string]repositorymodels.StoredProcedure)
|
||||
r.storeState.UserDefinedFunctions[newDatabase.ID] = make(map[string]map[string]repositorymodels.UserDefinedFunction)
|
||||
|
||||
return newDatabase, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -14,70 +14,83 @@ import (
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
func (r *DataRepository) GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return make([]repositorymodels.Document, 0), repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
return maps.Values(storeState.Documents[databaseId][collectionId]), repositorymodels.StatusOk
|
||||
return maps.Values(r.storeState.Documents[databaseId][collectionId]), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
func (r *DataRepository) GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
return storeState.Documents[databaseId][collectionId][documentId], repositorymodels.StatusOk
|
||||
return r.storeState.Documents[databaseId][collectionId][documentId], repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
|
||||
if _, ok := storeState.Databases[databaseId]; !ok {
|
||||
func (r *DataRepository) DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Collections[databaseId][collectionId]; !ok {
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
delete(storeState.Documents[databaseId][collectionId], documentId)
|
||||
delete(r.storeState.Documents[databaseId][collectionId], documentId)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
func (r *DataRepository) CreateDocument(databaseId string, collectionId string, document map[string]interface{}) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var documentId string
|
||||
var database repositorymodels.Database
|
||||
var collection repositorymodels.Collection
|
||||
if documentId, ok = document["id"].(string); !ok || documentId == "" {
|
||||
return repositorymodels.Document{}, repositorymodels.BadRequest
|
||||
documentId = fmt.Sprint(uuid.New())
|
||||
document["id"] = documentId
|
||||
}
|
||||
|
||||
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if collection, ok = storeState.Collections[databaseId][collectionId]; !ok {
|
||||
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := storeState.Documents[databaseId][collectionId][documentId]; ok {
|
||||
if _, ok := r.storeState.Documents[databaseId][collectionId][documentId]; ok {
|
||||
return repositorymodels.Document{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
@@ -86,19 +99,19 @@ func CreateDocument(databaseId string, collectionId string, document map[string]
|
||||
document["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
|
||||
|
||||
storeState.Documents[databaseId][collectionId][documentId] = document
|
||||
r.storeState.Documents[databaseId][collectionId][documentId] = document
|
||||
|
||||
return document, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func ExecuteQueryDocuments(databaseId string, collectionId string, query string, queryParameters map[string]interface{}) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
|
||||
func (r *DataRepository) ExecuteQueryDocuments(databaseId string, collectionId string, query string, queryParameters map[string]interface{}) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
|
||||
parsedQuery, err := nosql.Parse("", []byte(query))
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse query: %s\nerr: %v", query, err)
|
||||
return nil, repositorymodels.BadRequest
|
||||
}
|
||||
|
||||
collectionDocuments, status := GetAllDocuments(databaseId, collectionId)
|
||||
collectionDocuments, status := r.GetAllDocuments(databaseId, collectionId)
|
||||
if status != repositorymodels.StatusOk {
|
||||
return nil, status
|
||||
}
|
||||
@@ -110,7 +123,7 @@ func ExecuteQueryDocuments(databaseId string, collectionId string, query string,
|
||||
|
||||
if typedQuery, ok := parsedQuery.(parsers.SelectStmt); ok {
|
||||
typedQuery.Parameters = queryParameters
|
||||
return memoryexecutor.Execute(typedQuery, covDocs), repositorymodels.StatusOk
|
||||
return memoryexecutor.ExecuteQuery(typedQuery, covDocs), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
return nil, repositorymodels.BadRequest
|
||||
|
||||
@@ -9,20 +9,25 @@ import (
|
||||
)
|
||||
|
||||
// I have no idea what this is tbh
|
||||
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
|
||||
var ok bool
|
||||
var database repositorymodels.Database
|
||||
var collection repositorymodels.Collection
|
||||
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||
return make([]repositorymodels.PartitionKeyRange, 0), repositorymodels.StatusNotFound
|
||||
func (r *DataRepository) GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
databaseRid := databaseId
|
||||
collectionRid := collectionId
|
||||
var timestamp int64 = 0
|
||||
|
||||
if database, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
databaseRid = database.ResourceID
|
||||
}
|
||||
|
||||
if collection, ok = storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return make([]repositorymodels.PartitionKeyRange, 0), repositorymodels.StatusNotFound
|
||||
if collection, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
collectionRid = collection.ResourceID
|
||||
timestamp = collection.TimeStamp
|
||||
}
|
||||
|
||||
pkrResourceId := resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
pkrSelf := fmt.Sprintf("dbs/%s/colls/%s/pkranges/%s/", database.ResourceID, collection.ResourceID, pkrResourceId)
|
||||
pkrResourceId := resourceid.NewCombined(databaseRid, collectionRid, resourceid.New())
|
||||
pkrSelf := fmt.Sprintf("dbs/%s/colls/%s/pkranges/%s/", databaseRid, collectionRid, pkrResourceId)
|
||||
etag := fmt.Sprintf("\"%s\"", uuid.New())
|
||||
|
||||
return []repositorymodels.PartitionKeyRange{
|
||||
@@ -37,7 +42,7 @@ func GetPartitionKeyRanges(databaseId string, collectionId string) ([]repository
|
||||
ThroughputFraction: 1,
|
||||
Status: "online",
|
||||
Parents: []interface{}{},
|
||||
TimeStamp: collection.TimeStamp,
|
||||
TimeStamp: timestamp,
|
||||
Lsn: 17,
|
||||
},
|
||||
}, repositorymodels.StatusOk
|
||||
|
||||
34
internal/repositories/repositories.go
Normal file
34
internal/repositories/repositories.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package repositories
|
||||
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
|
||||
type DataRepository struct {
|
||||
storeState repositorymodels.State
|
||||
|
||||
initialDataFilePath string
|
||||
persistDataFilePath string
|
||||
}
|
||||
|
||||
type RepositoryOptions struct {
|
||||
InitialDataFilePath string
|
||||
PersistDataFilePath string
|
||||
}
|
||||
|
||||
func NewDataRepository(options RepositoryOptions) *DataRepository {
|
||||
repository := &DataRepository{
|
||||
storeState: repositorymodels.State{
|
||||
Databases: make(map[string]repositorymodels.Database),
|
||||
Collections: make(map[string]map[string]repositorymodels.Collection),
|
||||
Documents: make(map[string]map[string]map[string]repositorymodels.Document),
|
||||
Triggers: make(map[string]map[string]map[string]repositorymodels.Trigger),
|
||||
StoredProcedures: make(map[string]map[string]map[string]repositorymodels.StoredProcedure),
|
||||
UserDefinedFunctions: make(map[string]map[string]map[string]repositorymodels.UserDefinedFunction),
|
||||
},
|
||||
initialDataFilePath: options.InitialDataFilePath,
|
||||
persistDataFilePath: options.PersistDataFilePath,
|
||||
}
|
||||
|
||||
repository.InitializeRepository()
|
||||
|
||||
return repository
|
||||
}
|
||||
@@ -2,68 +2,117 @@ package repositories
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
var storedProcedures = []repositorymodels.StoredProcedure{}
|
||||
var triggers = []repositorymodels.Trigger{}
|
||||
var userDefinedFunctions = []repositorymodels.UserDefinedFunction{}
|
||||
var storeState = repositorymodels.State{
|
||||
Databases: make(map[string]repositorymodels.Database),
|
||||
Collections: make(map[string]map[string]repositorymodels.Collection),
|
||||
Documents: make(map[string]map[string]map[string]repositorymodels.Document),
|
||||
func (r *DataRepository) InitializeRepository() {
|
||||
if r.initialDataFilePath != "" {
|
||||
r.LoadStateFS(r.initialDataFilePath)
|
||||
return
|
||||
}
|
||||
|
||||
if r.persistDataFilePath != "" {
|
||||
stat, err := os.Stat(r.persistDataFilePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
logger.ErrorLn("Argument '-Persist' must be a path to file, not a directory.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
r.LoadStateFS(r.persistDataFilePath)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func LoadStateFS(filePath string) {
|
||||
func (r *DataRepository) LoadStateFS(filePath string) {
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading state JSON file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var state repositorymodels.State
|
||||
if err := json.Unmarshal(data, &state); err != nil {
|
||||
err = r.LoadStateJSON(string(data))
|
||||
if err != nil {
|
||||
log.Fatalf("Error unmarshalling state JSON: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Loaded state:")
|
||||
fmt.Printf("Databases: %d\n", getLength(state.Databases))
|
||||
fmt.Printf("Collections: %d\n", getLength(state.Collections))
|
||||
fmt.Printf("Documents: %d\n", getLength(state.Documents))
|
||||
|
||||
storeState = state
|
||||
|
||||
ensureStoreStateNoNullReferences()
|
||||
}
|
||||
|
||||
func SaveStateFS(filePath string) {
|
||||
data, err := json.MarshalIndent(storeState, "", "\t")
|
||||
func (r *DataRepository) LoadStateJSON(jsonData string) error {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
var state repositorymodels.State
|
||||
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.storeState.Collections = state.Collections
|
||||
r.storeState.Databases = state.Databases
|
||||
r.storeState.Documents = state.Documents
|
||||
|
||||
r.ensureStoreStateNoNullReferences()
|
||||
|
||||
logger.InfoLn("Loaded state:")
|
||||
logger.Infof("Databases: %d\n", getLength(r.storeState.Databases))
|
||||
logger.Infof("Collections: %d\n", getLength(r.storeState.Collections))
|
||||
logger.Infof("Documents: %d\n", getLength(r.storeState.Documents))
|
||||
logger.Infof("Triggers: %d\n", getLength(r.storeState.Triggers))
|
||||
logger.Infof("Stored procedures: %d\n", getLength(r.storeState.StoredProcedures))
|
||||
logger.Infof("User defined functions: %d\n", getLength(r.storeState.UserDefinedFunctions))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *DataRepository) SaveStateFS(filePath string) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
data, err := json.MarshalIndent(r.storeState, "", "\t")
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to save state: %v\n", err)
|
||||
logger.Errorf("Failed to save state: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
os.WriteFile(filePath, data, os.ModePerm)
|
||||
|
||||
fmt.Println("Saved state:")
|
||||
fmt.Printf("Databases: %d\n", getLength(storeState.Databases))
|
||||
fmt.Printf("Collections: %d\n", getLength(storeState.Collections))
|
||||
fmt.Printf("Documents: %d\n", getLength(storeState.Documents))
|
||||
logger.InfoLn("Saved state:")
|
||||
logger.Infof("Databases: %d\n", getLength(r.storeState.Databases))
|
||||
logger.Infof("Collections: %d\n", getLength(r.storeState.Collections))
|
||||
logger.Infof("Documents: %d\n", getLength(r.storeState.Documents))
|
||||
logger.Infof("Triggers: %d\n", getLength(r.storeState.Triggers))
|
||||
logger.Infof("Stored procedures: %d\n", getLength(r.storeState.StoredProcedures))
|
||||
logger.Infof("User defined functions: %d\n", getLength(r.storeState.UserDefinedFunctions))
|
||||
}
|
||||
|
||||
func GetState() repositorymodels.State {
|
||||
return storeState
|
||||
func (r *DataRepository) GetState() (string, error) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
data, err := json.MarshalIndent(r.storeState, "", "\t")
|
||||
if err != nil {
|
||||
logger.Errorf("Failed to serialize state: %v\n", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func getLength(v interface{}) int {
|
||||
switch v.(type) {
|
||||
case repositorymodels.Database,
|
||||
repositorymodels.Collection,
|
||||
repositorymodels.Document:
|
||||
repositorymodels.Document,
|
||||
repositorymodels.Trigger,
|
||||
repositorymodels.StoredProcedure,
|
||||
repositorymodels.UserDefinedFunction:
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -84,38 +133,74 @@ func getLength(v interface{}) int {
|
||||
return count
|
||||
}
|
||||
|
||||
func ensureStoreStateNoNullReferences() {
|
||||
if storeState.Databases == nil {
|
||||
storeState.Databases = make(map[string]repositorymodels.Database)
|
||||
func (r *DataRepository) ensureStoreStateNoNullReferences() {
|
||||
if r.storeState.Databases == nil {
|
||||
r.storeState.Databases = make(map[string]repositorymodels.Database)
|
||||
}
|
||||
|
||||
if storeState.Collections == nil {
|
||||
storeState.Collections = make(map[string]map[string]repositorymodels.Collection)
|
||||
if r.storeState.Collections == nil {
|
||||
r.storeState.Collections = make(map[string]map[string]repositorymodels.Collection)
|
||||
}
|
||||
|
||||
if storeState.Documents == nil {
|
||||
storeState.Documents = make(map[string]map[string]map[string]repositorymodels.Document)
|
||||
if r.storeState.Documents == nil {
|
||||
r.storeState.Documents = make(map[string]map[string]map[string]repositorymodels.Document)
|
||||
}
|
||||
|
||||
for database := range storeState.Databases {
|
||||
if storeState.Collections[database] == nil {
|
||||
storeState.Collections[database] = make(map[string]repositorymodels.Collection)
|
||||
if r.storeState.Triggers == nil {
|
||||
r.storeState.Triggers = make(map[string]map[string]map[string]repositorymodels.Trigger)
|
||||
}
|
||||
|
||||
if r.storeState.StoredProcedures == nil {
|
||||
r.storeState.StoredProcedures = make(map[string]map[string]map[string]repositorymodels.StoredProcedure)
|
||||
}
|
||||
|
||||
if r.storeState.UserDefinedFunctions == nil {
|
||||
r.storeState.UserDefinedFunctions = make(map[string]map[string]map[string]repositorymodels.UserDefinedFunction)
|
||||
}
|
||||
|
||||
for database := range r.storeState.Databases {
|
||||
if r.storeState.Collections[database] == nil {
|
||||
r.storeState.Collections[database] = make(map[string]repositorymodels.Collection)
|
||||
}
|
||||
|
||||
if storeState.Documents[database] == nil {
|
||||
storeState.Documents[database] = make(map[string]map[string]repositorymodels.Document)
|
||||
if r.storeState.Documents[database] == nil {
|
||||
r.storeState.Documents[database] = make(map[string]map[string]repositorymodels.Document)
|
||||
}
|
||||
|
||||
for collection := range storeState.Collections[database] {
|
||||
if storeState.Documents[database][collection] == nil {
|
||||
storeState.Documents[database][collection] = make(map[string]repositorymodels.Document)
|
||||
if r.storeState.Triggers[database] == nil {
|
||||
r.storeState.Triggers[database] = make(map[string]map[string]repositorymodels.Trigger)
|
||||
}
|
||||
|
||||
if r.storeState.StoredProcedures[database] == nil {
|
||||
r.storeState.StoredProcedures[database] = make(map[string]map[string]repositorymodels.StoredProcedure)
|
||||
}
|
||||
|
||||
if r.storeState.UserDefinedFunctions[database] == nil {
|
||||
r.storeState.UserDefinedFunctions[database] = make(map[string]map[string]repositorymodels.UserDefinedFunction)
|
||||
}
|
||||
|
||||
for collection := range r.storeState.Collections[database] {
|
||||
if r.storeState.Documents[database][collection] == nil {
|
||||
r.storeState.Documents[database][collection] = make(map[string]repositorymodels.Document)
|
||||
}
|
||||
|
||||
for document := range storeState.Documents[database][collection] {
|
||||
if storeState.Documents[database][collection][document] == nil {
|
||||
delete(storeState.Documents[database][collection], document)
|
||||
for document := range r.storeState.Documents[database][collection] {
|
||||
if r.storeState.Documents[database][collection][document] == nil {
|
||||
delete(r.storeState.Documents[database][collection], document)
|
||||
}
|
||||
}
|
||||
|
||||
if r.storeState.Triggers[database][collection] == nil {
|
||||
r.storeState.Triggers[database][collection] = make(map[string]repositorymodels.Trigger)
|
||||
}
|
||||
|
||||
if r.storeState.StoredProcedures[database][collection] == nil {
|
||||
r.storeState.StoredProcedures[database][collection] = make(map[string]repositorymodels.StoredProcedure)
|
||||
}
|
||||
|
||||
if r.storeState.UserDefinedFunctions[database][collection] == nil {
|
||||
r.storeState.UserDefinedFunctions[database][collection] = make(map[string]repositorymodels.UserDefinedFunction)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,91 @@
|
||||
package repositories
|
||||
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
func GetAllStoredProcedures(databaseId string, collectionId string) ([]repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
||||
return storedProcedures, repositorymodels.StatusOk
|
||||
"github.com/google/uuid"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/pikami/cosmium/internal/resourceid"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func (r *DataRepository) GetAllStoredProcedures(databaseId string, collectionId string) ([]repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
return maps.Values(r.storeState.StoredProcedures[databaseId][collectionId]), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func (r *DataRepository) GetStoredProcedure(databaseId string, collectionId string, spId string) (repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if sp, ok := r.storeState.StoredProcedures[databaseId][collectionId][spId]; ok {
|
||||
return sp, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func (r *DataRepository) DeleteStoredProcedure(databaseId string, collectionId string, spId string) repositorymodels.RepositoryStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.StoredProcedures[databaseId][collectionId][spId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
delete(r.storeState.StoredProcedures[databaseId][collectionId], spId)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func (r *DataRepository) CreateStoredProcedure(databaseId string, collectionId string, sp repositorymodels.StoredProcedure) (repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var database repositorymodels.Database
|
||||
var collection repositorymodels.Collection
|
||||
if sp.ID == "" {
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.BadRequest
|
||||
}
|
||||
|
||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok = r.storeState.StoredProcedures[databaseId][collectionId][sp.ID]; ok {
|
||||
return repositorymodels.StoredProcedure{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
sp.TimeStamp = time.Now().Unix()
|
||||
sp.ResourceID = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
sp.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
sp.Self = fmt.Sprintf("dbs/%s/colls/%s/sprocs/%s/", database.ResourceID, collection.ResourceID, sp.ResourceID)
|
||||
|
||||
r.storeState.StoredProcedures[databaseId][collectionId][sp.ID] = sp
|
||||
|
||||
return sp, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,7 +1,91 @@
|
||||
package repositories
|
||||
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
func GetAllTriggers(databaseId string, collectionId string) ([]repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
||||
return triggers, repositorymodels.StatusOk
|
||||
"github.com/google/uuid"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/pikami/cosmium/internal/resourceid"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func (r *DataRepository) GetAllTriggers(databaseId string, collectionId string) ([]repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
return maps.Values(r.storeState.Triggers[databaseId][collectionId]), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func (r *DataRepository) GetTrigger(databaseId string, collectionId string, triggerId string) (repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Trigger{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.Trigger{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if trigger, ok := r.storeState.Triggers[databaseId][collectionId][triggerId]; ok {
|
||||
return trigger, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
return repositorymodels.Trigger{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func (r *DataRepository) DeleteTrigger(databaseId string, collectionId string, triggerId string) repositorymodels.RepositoryStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Triggers[databaseId][collectionId][triggerId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
delete(r.storeState.Triggers[databaseId][collectionId], triggerId)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func (r *DataRepository) CreateTrigger(databaseId string, collectionId string, trigger repositorymodels.Trigger) (repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var database repositorymodels.Database
|
||||
var collection repositorymodels.Collection
|
||||
if trigger.ID == "" {
|
||||
return repositorymodels.Trigger{}, repositorymodels.BadRequest
|
||||
}
|
||||
|
||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.Trigger{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.Trigger{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok = r.storeState.Triggers[databaseId][collectionId][trigger.ID]; ok {
|
||||
return repositorymodels.Trigger{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
trigger.TimeStamp = time.Now().Unix()
|
||||
trigger.ResourceID = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
trigger.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
trigger.Self = fmt.Sprintf("dbs/%s/colls/%s/triggers/%s/", database.ResourceID, collection.ResourceID, trigger.ResourceID)
|
||||
|
||||
r.storeState.Triggers[databaseId][collectionId][trigger.ID] = trigger
|
||||
|
||||
return trigger, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,7 +1,91 @@
|
||||
package repositories
|
||||
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
func GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
||||
return userDefinedFunctions, repositorymodels.StatusOk
|
||||
"github.com/google/uuid"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/pikami/cosmium/internal/resourceid"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func (r *DataRepository) GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
return maps.Values(r.storeState.UserDefinedFunctions[databaseId][collectionId]), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func (r *DataRepository) GetUserDefinedFunction(databaseId string, collectionId string, udfId string) (repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if udf, ok := r.storeState.UserDefinedFunctions[databaseId][collectionId][udfId]; ok {
|
||||
return udf, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func (r *DataRepository) DeleteUserDefinedFunction(databaseId string, collectionId string, udfId string) repositorymodels.RepositoryStatus {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
if _, ok := r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.UserDefinedFunctions[databaseId][collectionId][udfId]; !ok {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
delete(r.storeState.UserDefinedFunctions[databaseId][collectionId], udfId)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func (r *DataRepository) CreateUserDefinedFunction(databaseId string, collectionId string, udf repositorymodels.UserDefinedFunction) (repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
var ok bool
|
||||
var database repositorymodels.Database
|
||||
var collection repositorymodels.Collection
|
||||
if udf.ID == "" {
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.BadRequest
|
||||
}
|
||||
|
||||
if database, ok = r.storeState.Databases[databaseId]; !ok {
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if collection, ok = r.storeState.Collections[databaseId][collectionId]; !ok {
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
if _, ok := r.storeState.UserDefinedFunctions[databaseId][collectionId][udf.ID]; ok {
|
||||
return repositorymodels.UserDefinedFunction{}, repositorymodels.Conflict
|
||||
}
|
||||
|
||||
udf.TimeStamp = time.Now().Unix()
|
||||
udf.ResourceID = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
udf.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
udf.Self = fmt.Sprintf("dbs/%s/colls/%s/udfs/%s/", database.ResourceID, collection.ResourceID, udf.ResourceID)
|
||||
|
||||
r.storeState.UserDefinedFunctions[databaseId][collectionId][udf.ID] = udf
|
||||
|
||||
return udf, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package repositorymodels
|
||||
|
||||
import "sync"
|
||||
|
||||
type Database struct {
|
||||
ID string `json:"id"`
|
||||
TimeStamp int64 `json:"_ts"`
|
||||
@@ -17,6 +19,22 @@ const (
|
||||
BadRequest = 4
|
||||
)
|
||||
|
||||
type TriggerOperation string
|
||||
|
||||
const (
|
||||
All TriggerOperation = "All"
|
||||
Create TriggerOperation = "Create"
|
||||
Delete TriggerOperation = "Delete"
|
||||
Replace TriggerOperation = "Replace"
|
||||
)
|
||||
|
||||
type TriggerType string
|
||||
|
||||
const (
|
||||
Pre TriggerType = "Pre"
|
||||
Post TriggerType = "Post"
|
||||
)
|
||||
|
||||
type Collection struct {
|
||||
ID string `json:"id"`
|
||||
IndexingPolicy CollectionIndexingPolicy `json:"indexingPolicy"`
|
||||
@@ -58,29 +76,29 @@ type UserDefinedFunction struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
ResourceID string `json:"_rid"`
|
||||
TimeStamp int `json:"_ts"`
|
||||
TimeStamp int64 `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
ETag string `json:"_etag"`
|
||||
}
|
||||
|
||||
type StoredProcedure struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
ResourceID string `json:"_rid"`
|
||||
TimeStamp int `json:"_ts"`
|
||||
TimeStamp int64 `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
ETag string `json:"_etag"`
|
||||
}
|
||||
|
||||
type Trigger struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
TriggerOperation string `json:"triggerOperation"`
|
||||
TriggerType string `json:"triggerType"`
|
||||
ResourceID string `json:"_rid"`
|
||||
TimeStamp int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
TriggerOperation TriggerOperation `json:"triggerOperation"`
|
||||
TriggerType TriggerType `json:"triggerType"`
|
||||
ResourceID string `json:"_rid"`
|
||||
TimeStamp int64 `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
ETag string `json:"_etag"`
|
||||
}
|
||||
|
||||
type Document map[string]interface{}
|
||||
@@ -101,6 +119,8 @@ type PartitionKeyRange struct {
|
||||
}
|
||||
|
||||
type State struct {
|
||||
sync.RWMutex
|
||||
|
||||
// Map databaseId -> Database
|
||||
Databases map[string]Database `json:"databases"`
|
||||
|
||||
@@ -109,4 +129,13 @@ type State struct {
|
||||
|
||||
// Map databaseId -> collectionId -> documentId -> Documents
|
||||
Documents map[string]map[string]map[string]Document `json:"documents"`
|
||||
|
||||
// Map databaseId -> collectionId -> triggerId -> Trigger
|
||||
Triggers map[string]map[string]map[string]Trigger `json:"triggers"`
|
||||
|
||||
// Map databaseId -> collectionId -> spId -> StoredProcedure
|
||||
StoredProcedures map[string]map[string]map[string]StoredProcedure `json:"sprocs"`
|
||||
|
||||
// Map databaseId -> collectionId -> udfId -> UserDefinedFunction
|
||||
UserDefinedFunctions map[string]map[string]map[string]UserDefinedFunction `json:"udfs"`
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package resourceid
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/rand"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -10,6 +11,12 @@ func New() string {
|
||||
id := uuid.New().ID()
|
||||
idBytes := uintToBytes(id)
|
||||
|
||||
// first byte should be bigger than 0x80 for collection ids
|
||||
// clients classify this id as "user" otherwise
|
||||
if (idBytes[0] & 0x80) <= 0 {
|
||||
idBytes[0] = byte(rand.Intn(0x80) + 0x80)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(idBytes)
|
||||
}
|
||||
|
||||
|
||||
61
internal/tls_provider/constants.go
Normal file
61
internal/tls_provider/constants.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package tlsprovider
|
||||
|
||||
const certificate = `
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEaDCCAlCgAwIBAgIUAY7ito1IQfbIi52C0evhqHWgEvQwDQYJKoZIhvcNAQEL
|
||||
BQAwMzELMAkGA1UEBhMCTFQxEjAQBgNVBAgMCUxpdGh1YW5pYTEQMA4GA1UECgwH
|
||||
Q29zbWl1bTAeFw0yNDAyMjcxOTE4NThaFw0zNDAyMjYxOTE4NThaMD8xCzAJBgNV
|
||||
BAYTAkxUMRIwEAYDVQQIDAlMaXRodWFuaWExEDAOBgNVBAoMB0Nvc21pdW0xCjAI
|
||||
BgNVBAMMASowggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCZxGz5clcf
|
||||
fvE6wS9Q2xPsUjeKdwotRCfKRu9kT7o1cZOSRBp7DgdeLvZ7BzqU1tk5wiLLiZwB
|
||||
gI6amQAd6z6EwUcUH0mHtFiWU0y/FROz0QUojbbYp0PMUhWjlPAxAGaiwgF/82z7
|
||||
/lmgMjf5v32XsMfa4U+FaaNYs7gu7aCQBQTAHmOIPnEAeFk9xQ2VzntRUWwzDYOV
|
||||
SimtPZk2O2X18V8KTgTLMQF1KErIyznIwEPB/BLi+ihLkh/8BaaxoIeOPIhRLNFr
|
||||
ecZrc/8+S4dUSUQDfmV3JFYFFheG0XIPEwXIaXiDAphpkCGhMIC2pDL8r14sntvn
|
||||
juHFZxmSP4V5AgMBAAGjaDBmMB8GA1UdIwQYMBaAFEbQ/7hV4FWrptdOk540R2lF
|
||||
SB1BMAkGA1UdEwQCMAAwCwYDVR0PBAQDAgTwMAwGA1UdEQQFMAOCASowHQYDVR0O
|
||||
BBYEFGv5XvoFFzrG54GQ+WMFm6UO36BJMA0GCSqGSIb3DQEBCwUAA4ICAQBZh/vZ
|
||||
PBamebTEpiQz6cgf8+GcTi++ebYUGQ3YJj82pqVBdipOhYQOZJ0fOlT1qRGNglut
|
||||
+m5zn0iuXsNucP/32xdf1aJBnsU/aGrlf5ohJpGNxYfNPsewxeqQI23Yj22ec1gy
|
||||
WL2pFDYNyTZMM7Wgys7m3i9lb6TYOF2lNO3WbNuuuETsDAPa0rD0R8QsQOfYOSNJ
|
||||
YuWE4qZu+ySvTWsMZwlcqs7QL3Sd91UjItIS/AgqbnLvgt4z5ckGCIvickUfAZuQ
|
||||
6x592hTz4OZ+WIYDejtb5MMXRaKEXgfF6o1idrD7YgVutm+2+mYpN1v9aLbCs7QW
|
||||
9RkJoTXFQRNGq6j/cO0ZrCKFkttduziMWRz5X9QWADME1NsL53DfDkaxp9Nh+CCu
|
||||
0S9OF9nVLJVigdXe4O1cQ0Qh633O6k+F/xWYcmMyVt3V2bs7FPfygGUx60tfIbpi
|
||||
cBK3BsuzUrId3ozvYPsmfxYlzmyspyS6G+f7zLFOakm3fuqDJpnFNXmRY2Ljd3Cp
|
||||
punuMT6zSctHAxpgJm1g9R6PcaGr+b/n6zkbxyK9+SFzwN3Lb18WFj5OcslNM/g5
|
||||
ERE5Ws+Vae6MleSmsxSytgH4qn0ormPWuouBLaW0Rv2ZHdkt3myq8kTqtqdw3LRR
|
||||
ogcLQ3cL6I5FKGjm2TOF72DQHvOol8ck0uMz/w==
|
||||
-----END CERTIFICATE-----
|
||||
`
|
||||
|
||||
const certificateKey = `
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCZxGz5clcffvE6
|
||||
wS9Q2xPsUjeKdwotRCfKRu9kT7o1cZOSRBp7DgdeLvZ7BzqU1tk5wiLLiZwBgI6a
|
||||
mQAd6z6EwUcUH0mHtFiWU0y/FROz0QUojbbYp0PMUhWjlPAxAGaiwgF/82z7/lmg
|
||||
Mjf5v32XsMfa4U+FaaNYs7gu7aCQBQTAHmOIPnEAeFk9xQ2VzntRUWwzDYOVSimt
|
||||
PZk2O2X18V8KTgTLMQF1KErIyznIwEPB/BLi+ihLkh/8BaaxoIeOPIhRLNFrecZr
|
||||
c/8+S4dUSUQDfmV3JFYFFheG0XIPEwXIaXiDAphpkCGhMIC2pDL8r14sntvnjuHF
|
||||
ZxmSP4V5AgMBAAECgf89wcgjpZnzoWoiM3Z6QDJnkiUdXQumHQracBnRFXnMy8p9
|
||||
wCd4ecnu9ptd8OArXgVMiaILWZeGXlqtW872m6Lej6DrJkpOt3NG9CvscdaHdthW
|
||||
9dzv8d7IEtuRN4/WWOm7Tke7eD7763ta9i9/niR2q7DazPVw8vYhkyoNe864qVrq
|
||||
Vw6+MMetz3TDHZ68p17yJJ9FJ0z0vHj3KJFrxnJonMe+/LcQX490y4zZw+zeyCkh
|
||||
y/bsgvFGhnUhJ+mOz+qv0KL7HyUR69p9/+mjQH+AQH+j24xgd1IL0Dror9Cy1kxY
|
||||
uKmi8pN1y288GmjkWosGMb0p3Pse1OkOyYFIbxECgYEA2ED3PSPoHWLHfKhg2BFw
|
||||
yMPtern06rjKuwMNlD+mKS66Z+OsQi2EBsqomGnr1HGvYgQik0jwMcx0+Sup9/Zp
|
||||
az8ebH6S4Tdxmnlwn34lhTIAF1KJS19AYvbhOydV+M+hq7Y7QxTqYsJAgEYwsozQ
|
||||
0XeAzRBIiRxdcMFHP40zZIkCgYEAtgdiwo5d5iyvXEqx/5+NdM4b/ImrbaFIAb0v
|
||||
MqiPpOA/+7EKlx72gJKVKh2iv4jvEUfduNEUXt77Yqo66HhfiTBVYxYwThK8E0Mq
|
||||
TSKKdJsdPSThLS3qjeARpzQpWLiBZH90GxbfFL3ogIOa/UcgwRrqPc5a/yq8adSs
|
||||
KGrfvXECgYEAmSMAMbqgn1aY32y5D6jiDjm4jMTsa98qKN5TmlysRNODSxhNnptu
|
||||
uASA+VVgnBNZV/aHqXboKMuZNe22shI7uqd62ueTCYtiljpTB46j8TtkFx/qe4Zb
|
||||
KPmcq3ACkGwwF1G3i5xfEkputKd/yqCvKvYOLqjORNHiVXt5Acby0skCgYBYkZ9s
|
||||
KvllVbi9n1qclnWtr9vONO5EmYT/051zeLDr+HEpditA/L/UL36Ez4awy2AHeIBZ
|
||||
vOG8h6Kpj0q6cleJ2Qqy+8jlNBhvBu8+OOBFfHPtnFQ0N3M5NR1hze+QS7YpwBou
|
||||
VCKXZRAL9/0h38oAK6huCkocfh7PH7vkrpvPAQKBgCFDDtk7aBJsNcOW+aq4IEvf
|
||||
nZ5hhhdelNLeN29RrJ71GwJrCG3NbhopWlCDqZ/Dd6QoEUpebqvlMGvQJBuz/QKb
|
||||
ilcZlmaCS9pqIXAFK9GQ89V/xa8OibOuJUiBgShnfSQqAwQrfX1vYjtKErnjoRFs
|
||||
9+zaWugLCC47Hw6QlMDa
|
||||
-----END PRIVATE KEY-----
|
||||
`
|
||||
19
internal/tls_provider/tls_provider.go
Normal file
19
internal/tls_provider/tls_provider.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package tlsprovider
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
func GetDefaultTlsConfig() *tls.Config {
|
||||
cert, err := tls.X509KeyPair([]byte(certificate), []byte(certificateKey))
|
||||
if err != nil {
|
||||
logger.ErrorLn("Failed to parse certificate and key:", err)
|
||||
return &tls.Config{}
|
||||
}
|
||||
|
||||
return &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
}
|
||||
45
main.go
45
main.go
@@ -1,45 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config.ParseFlags()
|
||||
|
||||
if config.Config.InitialDataFilePath != "" {
|
||||
repositories.LoadStateFS(config.Config.InitialDataFilePath)
|
||||
}
|
||||
|
||||
router := api.CreateRouter()
|
||||
if config.Config.TLS_CertificatePath == "" ||
|
||||
config.Config.TLS_CertificateKey == "" {
|
||||
go router.Run(fmt.Sprintf(":%d", config.Config.Port))
|
||||
} else {
|
||||
go router.RunTLS(
|
||||
fmt.Sprintf(":%d", config.Config.Port),
|
||||
config.Config.TLS_CertificatePath,
|
||||
config.Config.TLS_CertificateKey)
|
||||
}
|
||||
|
||||
waitForExit()
|
||||
}
|
||||
|
||||
func waitForExit() {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// Block until a exit signal is received
|
||||
<-sigs
|
||||
|
||||
if config.Config.PersistDataFilePath != "" {
|
||||
repositories.SaveStateFS(config.Config.PersistDataFilePath)
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,25 @@ package parsers
|
||||
type SelectStmt struct {
|
||||
SelectItems []SelectItem
|
||||
Table Table
|
||||
JoinItems []JoinItem
|
||||
Filters interface{}
|
||||
Exists bool
|
||||
Distinct bool
|
||||
Count int
|
||||
Offset int
|
||||
Parameters map[string]interface{}
|
||||
OrderExpressions []OrderExpression
|
||||
GroupBy []SelectItem
|
||||
}
|
||||
|
||||
type Table struct {
|
||||
Value string
|
||||
Value string
|
||||
SelectItem SelectItem
|
||||
}
|
||||
|
||||
type JoinItem struct {
|
||||
Table Table
|
||||
SelectItem SelectItem
|
||||
}
|
||||
|
||||
type SelectItemType int
|
||||
@@ -21,6 +32,7 @@ const (
|
||||
SelectItemTypeArray
|
||||
SelectItemTypeConstant
|
||||
SelectItemTypeFunctionCall
|
||||
SelectItemTypeSubQuery
|
||||
)
|
||||
|
||||
type SelectItem struct {
|
||||
@@ -111,15 +123,69 @@ const (
|
||||
FunctionCallIsPrimitive FunctionCallType = "IsPrimitive"
|
||||
FunctionCallIsString FunctionCallType = "IsString"
|
||||
|
||||
FunctionCallArrayConcat FunctionCallType = "ArrayConcat"
|
||||
FunctionCallArrayLength FunctionCallType = "ArrayLength"
|
||||
FunctionCallArraySlice FunctionCallType = "ArraySlice"
|
||||
FunctionCallSetIntersect FunctionCallType = "SetIntersect"
|
||||
FunctionCallSetUnion FunctionCallType = "SetUnion"
|
||||
FunctionCallArrayConcat FunctionCallType = "ArrayConcat"
|
||||
FunctionCallArrayContains FunctionCallType = "ArrayContains"
|
||||
FunctionCallArrayContainsAny FunctionCallType = "ArrayContainsAny"
|
||||
FunctionCallArrayContainsAll FunctionCallType = "ArrayContainsAll"
|
||||
FunctionCallArrayLength FunctionCallType = "ArrayLength"
|
||||
FunctionCallArraySlice FunctionCallType = "ArraySlice"
|
||||
FunctionCallSetIntersect FunctionCallType = "SetIntersect"
|
||||
FunctionCallSetUnion FunctionCallType = "SetUnion"
|
||||
|
||||
FunctionCallMathAbs FunctionCallType = "MathAbs"
|
||||
FunctionCallMathAcos FunctionCallType = "MathAcos"
|
||||
FunctionCallMathAsin FunctionCallType = "MathAsin"
|
||||
FunctionCallMathAtan FunctionCallType = "MathAtan"
|
||||
FunctionCallMathAtn2 FunctionCallType = "MathAtn2"
|
||||
FunctionCallMathCeiling FunctionCallType = "MathCeiling"
|
||||
FunctionCallMathCos FunctionCallType = "MathCos"
|
||||
FunctionCallMathCot FunctionCallType = "MathCot"
|
||||
FunctionCallMathDegrees FunctionCallType = "MathDegrees"
|
||||
FunctionCallMathExp FunctionCallType = "MathExp"
|
||||
FunctionCallMathFloor FunctionCallType = "MathFloor"
|
||||
FunctionCallMathIntAdd FunctionCallType = "MathIntAdd"
|
||||
FunctionCallMathIntBitAnd FunctionCallType = "MathIntBitAnd"
|
||||
FunctionCallMathIntBitLeftShift FunctionCallType = "MathIntBitLeftShift"
|
||||
FunctionCallMathIntBitNot FunctionCallType = "MathIntBitNot"
|
||||
FunctionCallMathIntBitOr FunctionCallType = "MathIntBitOr"
|
||||
FunctionCallMathIntBitRightShift FunctionCallType = "MathIntBitRightShift"
|
||||
FunctionCallMathIntBitXor FunctionCallType = "MathIntBitXor"
|
||||
FunctionCallMathIntDiv FunctionCallType = "MathIntDiv"
|
||||
FunctionCallMathIntMod FunctionCallType = "MathIntMod"
|
||||
FunctionCallMathIntMul FunctionCallType = "MathIntMul"
|
||||
FunctionCallMathIntSub FunctionCallType = "MathIntSub"
|
||||
FunctionCallMathLog FunctionCallType = "MathLog"
|
||||
FunctionCallMathLog10 FunctionCallType = "MathLog10"
|
||||
FunctionCallMathNumberBin FunctionCallType = "MathNumberBin"
|
||||
FunctionCallMathPi FunctionCallType = "MathPi"
|
||||
FunctionCallMathPower FunctionCallType = "MathPower"
|
||||
FunctionCallMathRadians FunctionCallType = "MathRadians"
|
||||
FunctionCallMathRand FunctionCallType = "MathRand"
|
||||
FunctionCallMathRound FunctionCallType = "MathRound"
|
||||
FunctionCallMathSign FunctionCallType = "MathSign"
|
||||
FunctionCallMathSin FunctionCallType = "MathSin"
|
||||
FunctionCallMathSqrt FunctionCallType = "MathSqrt"
|
||||
FunctionCallMathSquare FunctionCallType = "MathSquare"
|
||||
FunctionCallMathTan FunctionCallType = "MathTan"
|
||||
FunctionCallMathTrunc FunctionCallType = "MathTrunc"
|
||||
|
||||
FunctionCallAggregateAvg FunctionCallType = "AggregateAvg"
|
||||
FunctionCallAggregateCount FunctionCallType = "AggregateCount"
|
||||
FunctionCallAggregateMax FunctionCallType = "AggregateMax"
|
||||
FunctionCallAggregateMin FunctionCallType = "AggregateMin"
|
||||
FunctionCallAggregateSum FunctionCallType = "AggregateSum"
|
||||
|
||||
FunctionCallIn FunctionCallType = "In"
|
||||
)
|
||||
|
||||
var AggregateFunctions = []FunctionCallType{
|
||||
FunctionCallAggregateAvg,
|
||||
FunctionCallAggregateCount,
|
||||
FunctionCallAggregateMax,
|
||||
FunctionCallAggregateMin,
|
||||
FunctionCallAggregateSum,
|
||||
}
|
||||
|
||||
type FunctionCall struct {
|
||||
Arguments []interface{}
|
||||
Type FunctionCallType
|
||||
|
||||
130
parsers/nosql/aggregate_functions_test.go
Normal file
130
parsers/nosql/aggregate_functions_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
|
||||
t.Run("Should parse function AVG()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT AVG(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COUNT()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT COUNT(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateCount,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function MAX()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT MAX(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMax,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function MIN()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT MIN(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMin,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SUM()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT SUM(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateSum,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
@@ -36,6 +37,119 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ARRAY_CONTAINS()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT ARRAY_CONTAINS(c.a1, "value") FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
testutils.SelectItem_Constant_String("value"),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ARRAY_CONTAINS() with partial match", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT ARRAY_CONTAINS(["a", "b"], "value", true) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeArray,
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Constant_String("a"),
|
||||
testutils.SelectItem_Constant_String("b"),
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("value"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ARRAY_CONTAINS_ANY()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT ARRAY_CONTAINS_ANY(["a", "b"], "value", true) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAny,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeArray,
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Constant_String("a"),
|
||||
testutils.SelectItem_Constant_String("b"),
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("value"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ARRAY_CONTAINS_ALL()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT ARRAY_CONTAINS_ALL(["a", "b"], "value", true) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAll,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeArray,
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Constant_String("a"),
|
||||
testutils.SelectItem_Constant_String("b"),
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("value"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ARRAY_LENGTH()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
@@ -75,20 +189,8 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
Path: []string{"c", "array"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 0,
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(0),
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
57
parsers/nosql/join_test.go
Normal file
57
parsers/nosql/join_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_Join(t *testing.T) {
|
||||
|
||||
t.Run("Should parse simple JOIN", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, c["pk"] FROM c JOIN cc IN c["tags"]`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse JOIN VALUE", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT VALUE cc FROM c JOIN cc IN c["tags"]`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
650
parsers/nosql/math_functions_test.go
Normal file
650
parsers/nosql/math_functions_test.go
Normal file
@@ -0,0 +1,650 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Execute_MathFunctions(t *testing.T) {
|
||||
t.Run("Should parse function ABS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ABS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAbs,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ACOS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ACOS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAcos,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ASIN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ASIN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAsin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ATAN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ATAN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathAtan,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function CEILING(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT CEILING(c.value) FROM c`,
|
||||
parsers.FunctionCallMathCeiling,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT COS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathCos,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COT(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT COT(c.value) FROM c`,
|
||||
parsers.FunctionCallMathCot,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function DEGREES(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT DEGREES(c.value) FROM c`,
|
||||
parsers.FunctionCallMathDegrees,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function EXP(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT EXP(c.value) FROM c`,
|
||||
parsers.FunctionCallMathExp,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function FLOOR(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT FLOOR(c.value) FROM c`,
|
||||
parsers.FunctionCallMathFloor,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitNot(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitNot(c.value) FROM c`,
|
||||
parsers.FunctionCallMathIntBitNot,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function LOG10(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT LOG10(c.value) FROM c`,
|
||||
parsers.FunctionCallMathLog10,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function RADIANS(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT RADIANS(c.value) FROM c`,
|
||||
parsers.FunctionCallMathRadians,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ROUND(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ROUND(c.value) FROM c`,
|
||||
parsers.FunctionCallMathRound,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SIGN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SIGN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSign,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SIN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SIN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SQRT(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SQRT(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSqrt,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SQUARE(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT SQUARE(c.value) FROM c`,
|
||||
parsers.FunctionCallMathSquare,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function TAN(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT TAN(c.value) FROM c`,
|
||||
parsers.FunctionCallMathTan,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function TRUNC(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT TRUNC(c.value) FROM c`,
|
||||
parsers.FunctionCallMathTrunc,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function ATN2(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT ATN2(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathAtn2,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntAdd(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntAdd(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntAdd,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitAnd(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitAnd(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitAnd,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitLeftShift(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitLeftShift(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitLeftShift,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitOr(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitOr(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitOr,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitRightShift(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitRightShift(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitRightShift,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntBitXor(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntBitXor(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntBitXor,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntDiv(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntDiv(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntDiv,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntMod(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntMod(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntMod,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntMul(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntMul(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntMul,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function IntSub(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT IntSub(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathIntSub,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function POWER(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT POWER(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathPower,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function LOG(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT LOG(c.value) FROM c`,
|
||||
parsers.FunctionCallMathLog,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function LOG(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT LOG(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathLog,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function NumberBin(ex)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT NumberBin(c.value) FROM c`,
|
||||
parsers.FunctionCallMathNumberBin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function NumberBin(ex1, ex2)", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT NumberBin(c.value, c.secondValue) FROM c`,
|
||||
parsers.FunctionCallMathNumberBin,
|
||||
[]interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "secondValue"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function PI()", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT PI() FROM c`,
|
||||
parsers.FunctionCallMathPi,
|
||||
[]interface{}{},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function RAND()", func(t *testing.T) {
|
||||
testMathFunctionParse(
|
||||
t,
|
||||
`SELECT RAND() FROM c`,
|
||||
parsers.FunctionCallMathRand,
|
||||
[]interface{}{},
|
||||
"c",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func testMathFunctionParse(
|
||||
t *testing.T,
|
||||
query string,
|
||||
expectedFunctionType parsers.FunctionCallType,
|
||||
expectedArguments []interface{},
|
||||
expectedTable string,
|
||||
) {
|
||||
testQueryParse(
|
||||
t,
|
||||
query,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: expectedFunctionType,
|
||||
Arguments: expectedArguments,
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: expectedTable},
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
"github.com/pikami/cosmium/parsers/nosql"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
// For Parser Debugging
|
||||
@@ -63,6 +64,24 @@ func Test_Parse(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT with GROUP BY", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, c["pk"] FROM c GROUP BY c.id, c.pk`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse IN function", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
@@ -84,24 +103,33 @@ func Test_Parse(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "456",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse IN selector", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM c IN c.tags`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,28 +3,76 @@ package nosql
|
||||
|
||||
import "github.com/pikami/cosmium/parsers"
|
||||
|
||||
func makeSelectStmt(columns, table, whereClause interface{}, count interface{}, orderList interface{}) (parsers.SelectStmt, error) {
|
||||
func makeSelectStmt(
|
||||
columns, fromClause, joinItems,
|
||||
whereClause interface{}, distinctClause interface{},
|
||||
count interface{}, groupByClause interface{}, orderList interface{},
|
||||
offsetClause interface{},
|
||||
) (parsers.SelectStmt, error) {
|
||||
selectStmt := parsers.SelectStmt{
|
||||
SelectItems: columns.([]parsers.SelectItem),
|
||||
Table: table.(parsers.Table),
|
||||
}
|
||||
|
||||
switch v := whereClause.(type) {
|
||||
case parsers.ComparisonExpression, parsers.LogicalExpression, parsers.Constant, parsers.SelectItem:
|
||||
selectStmt.Filters = v
|
||||
if fromTable, ok := fromClause.(parsers.Table); ok {
|
||||
selectStmt.Table = fromTable
|
||||
}
|
||||
|
||||
if joinItemsArray, ok := joinItems.([]interface{}); ok && len(joinItemsArray) > 0 {
|
||||
selectStmt.JoinItems = make([]parsers.JoinItem, len(joinItemsArray))
|
||||
for i, joinItem := range joinItemsArray {
|
||||
selectStmt.JoinItems[i] = joinItem.(parsers.JoinItem)
|
||||
}
|
||||
}
|
||||
|
||||
switch v := whereClause.(type) {
|
||||
case parsers.ComparisonExpression, parsers.LogicalExpression, parsers.Constant, parsers.SelectItem:
|
||||
selectStmt.Filters = v
|
||||
}
|
||||
|
||||
if distinctClause != nil {
|
||||
selectStmt.Distinct = true
|
||||
}
|
||||
|
||||
if n, ok := count.(int); ok {
|
||||
selectStmt.Count = n
|
||||
}
|
||||
|
||||
if orderExpressions, ok := orderList.([]parsers.OrderExpression); ok {
|
||||
selectStmt.OrderExpressions = orderExpressions
|
||||
}
|
||||
if offsetArr, ok := offsetClause.([]interface{}); ok && len(offsetArr) == 2 {
|
||||
if n, ok := offsetArr[0].(int); ok {
|
||||
selectStmt.Offset = n
|
||||
}
|
||||
|
||||
if n, ok := offsetArr[1].(int); ok {
|
||||
selectStmt.Count = n
|
||||
}
|
||||
}
|
||||
|
||||
if orderExpressions, ok := orderList.([]parsers.OrderExpression); ok {
|
||||
selectStmt.OrderExpressions = orderExpressions
|
||||
}
|
||||
|
||||
if groupByClause != nil {
|
||||
selectStmt.GroupBy = groupByClause.([]parsers.SelectItem)
|
||||
}
|
||||
|
||||
return selectStmt, nil
|
||||
}
|
||||
|
||||
func makeJoin(table interface{}, column interface{}) (parsers.JoinItem, error) {
|
||||
joinItem := parsers.JoinItem{}
|
||||
|
||||
if selectItem, isSelectItem := column.(parsers.SelectItem); isSelectItem {
|
||||
joinItem.SelectItem = selectItem
|
||||
joinItem.Table.Value = selectItem.Alias
|
||||
}
|
||||
|
||||
if tableTyped, isTable := table.(parsers.Table); isTable {
|
||||
joinItem.Table = tableTyped
|
||||
}
|
||||
|
||||
return joinItem, nil
|
||||
}
|
||||
|
||||
func makeSelectItem(name interface{}, path interface{}, selectItemType parsers.SelectItemType) (parsers.SelectItem, error) {
|
||||
ps := path.([]interface{})
|
||||
|
||||
@@ -136,17 +184,75 @@ Input <- selectStmt:SelectStmt {
|
||||
return selectStmt, nil
|
||||
}
|
||||
|
||||
SelectStmt <- Select ws topClause:TopClause? ws columns:Selection ws
|
||||
From ws table:TableName ws
|
||||
SelectStmt <- Select ws
|
||||
distinctClause:DistinctClause? ws
|
||||
topClause:TopClause? ws
|
||||
columns:Selection ws
|
||||
fromClause:FromClause? ws
|
||||
joinClauses:(ws join:JoinClause { return join, nil })* ws
|
||||
whereClause:(ws Where ws condition:Condition { return condition, nil })?
|
||||
orderByClause:OrderByClause? {
|
||||
return makeSelectStmt(columns, table, whereClause, topClause, orderByClause)
|
||||
groupByClause:(ws GroupBy ws columns:ColumnList { return columns, nil })?
|
||||
orderByClause:(ws order:OrderByClause { return order, nil })?
|
||||
offsetClause:(ws offset:OffsetClause { return offset, nil })? {
|
||||
return makeSelectStmt(columns, fromClause, joinClauses, whereClause,
|
||||
distinctClause, topClause, groupByClause, orderByClause, offsetClause)
|
||||
}
|
||||
|
||||
DistinctClause <- "DISTINCT"i
|
||||
|
||||
TopClause <- Top ws count:Integer {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
FromClause <- From ws table:TableName selectItem:(ws "IN"i ws column:SelectItem { return column, nil })? {
|
||||
tableTyped := table.(parsers.Table)
|
||||
|
||||
if selectItem != nil {
|
||||
tableTyped.SelectItem = selectItem.(parsers.SelectItem)
|
||||
}
|
||||
|
||||
return tableTyped, nil
|
||||
} / From ws subQuery:SubQuerySelectItem {
|
||||
subQueryTyped := subQuery.(parsers.SelectItem)
|
||||
table := parsers.Table{
|
||||
Value: subQueryTyped.Alias,
|
||||
SelectItem: subQueryTyped,
|
||||
}
|
||||
return table, nil
|
||||
}
|
||||
|
||||
SubQuery <- exists:(exists:Exists ws { return exists, nil })? "(" ws selectStmt:SelectStmt ws ")" {
|
||||
if selectStatement, isGoodValue := selectStmt.(parsers.SelectStmt); isGoodValue {
|
||||
selectStatement.Exists = exists != nil
|
||||
return selectStatement, nil
|
||||
}
|
||||
|
||||
return selectStmt, nil
|
||||
}
|
||||
|
||||
SubQuerySelectItem <- subQuery:SubQuery asClause:(ws alias:AsClause { return alias, nil })? {
|
||||
selectItem := parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: subQuery,
|
||||
}
|
||||
|
||||
if tableName, isString := asClause.(string); isString {
|
||||
selectItem.Alias = tableName
|
||||
}
|
||||
|
||||
return selectItem, nil
|
||||
}
|
||||
|
||||
JoinClause <- Join ws table:TableName ws "IN"i ws column:SelectItem {
|
||||
return makeJoin(table, column)
|
||||
} / Join ws subQuery:SubQuerySelectItem {
|
||||
return makeJoin(nil, subQuery)
|
||||
}
|
||||
|
||||
OffsetClause <- "OFFSET"i ws offset:IntegerLiteral ws "LIMIT"i ws limit:IntegerLiteral {
|
||||
return []interface{}{offset.(parsers.Constant).Value, limit.(parsers.Constant).Value}, nil
|
||||
}
|
||||
|
||||
Selection <- SelectValueSpec / ColumnList / SelectAsterisk
|
||||
|
||||
SelectAsterisk <- "*" {
|
||||
@@ -187,7 +293,7 @@ SelectProperty <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* {
|
||||
return makeSelectItem(name, path, parsers.SelectItemTypeField)
|
||||
}
|
||||
|
||||
SelectItem <- selectItem:(Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
var itemResult parsers.SelectItem
|
||||
switch typedValue := selectItem.(type) {
|
||||
case parsers.SelectItem:
|
||||
@@ -217,9 +323,9 @@ DotFieldAccess <- "." id:Identifier {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
|
||||
return id, nil
|
||||
}
|
||||
ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil }
|
||||
/ "[" id:Integer "]" { return strconv.Itoa(id.(int)), nil }
|
||||
/ "[" id:ParameterConstant "]" { return id.(parsers.Constant).Value.(string), nil }
|
||||
|
||||
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
|
||||
return string(c.text), nil
|
||||
@@ -267,11 +373,17 @@ As <- "AS"i
|
||||
|
||||
From <- "FROM"i
|
||||
|
||||
Join <- "JOIN"i
|
||||
|
||||
Exists <- "EXISTS"i
|
||||
|
||||
Where <- "WHERE"i
|
||||
|
||||
And <- "AND"i
|
||||
|
||||
Or <- "OR"i
|
||||
Or <- "OR"i wss
|
||||
|
||||
GroupBy <- "GROUP"i ws "BY"i
|
||||
|
||||
OrderBy <- "ORDER"i ws "BY"i
|
||||
|
||||
@@ -307,6 +419,8 @@ FunctionCall <- StringFunctions
|
||||
/ TypeCheckingFunctions
|
||||
/ ArrayFunctions
|
||||
/ InFunction
|
||||
/ AggregateFunctions
|
||||
/ MathFunctions
|
||||
|
||||
StringFunctions <- StringEqualsExpression
|
||||
/ ToStringExpression
|
||||
@@ -336,12 +450,58 @@ TypeCheckingFunctions <- IsDefined
|
||||
/ IsPrimitive
|
||||
/ IsString
|
||||
|
||||
AggregateFunctions <- AvgAggregateExpression
|
||||
/ CountAggregateExpression
|
||||
/ MaxAggregateExpression
|
||||
/ MinAggregateExpression
|
||||
/ SumAggregateExpression
|
||||
|
||||
ArrayFunctions <- ArrayConcatExpression
|
||||
/ ArrayContainsExpression
|
||||
/ ArrayContainsAnyExpression
|
||||
/ ArrayContainsAllExpression
|
||||
/ ArrayLengthExpression
|
||||
/ ArraySliceExpression
|
||||
/ SetIntersectExpression
|
||||
/ SetUnionExpression
|
||||
|
||||
MathFunctions <- MathAbsExpression
|
||||
/ MathAcosExpression
|
||||
/ MathAsinExpression
|
||||
/ MathAtanExpression
|
||||
/ MathCeilingExpression
|
||||
/ MathCosExpression
|
||||
/ MathCotExpression
|
||||
/ MathDegreesExpression
|
||||
/ MathExpExpression
|
||||
/ MathFloorExpression
|
||||
/ MathIntBitNotExpression
|
||||
/ MathLog10Expression
|
||||
/ MathRadiansExpression
|
||||
/ MathRoundExpression
|
||||
/ MathSignExpression
|
||||
/ MathSinExpression
|
||||
/ MathSqrtExpression
|
||||
/ MathSquareExpression
|
||||
/ MathTanExpression
|
||||
/ MathTruncExpression
|
||||
/ MathAtn2Expression
|
||||
/ MathIntAddExpression
|
||||
/ MathIntBitAndExpression
|
||||
/ MathIntBitLeftShiftExpression
|
||||
/ MathIntBitOrExpression
|
||||
/ MathIntBitRightShiftExpression
|
||||
/ MathIntBitXorExpression
|
||||
/ MathIntDivExpression
|
||||
/ MathIntModExpression
|
||||
/ MathIntMulExpression
|
||||
/ MathIntSubExpression
|
||||
/ MathPowerExpression
|
||||
/ MathLogExpression
|
||||
/ MathNumberBinExpression
|
||||
/ MathPiExpression
|
||||
/ MathRandExpression
|
||||
|
||||
UpperExpression <- "UPPER"i ws "(" ex:SelectItem ")" {
|
||||
return createFunctionCall(parsers.FunctionCallUpper, []interface{}{ex})
|
||||
}
|
||||
@@ -469,6 +629,18 @@ ArrayConcatExpression <- "ARRAY_CONCAT"i ws "(" ws arrays:SelectItem others:(ws
|
||||
return createFunctionCall(parsers.FunctionCallArrayConcat, append([]interface{}{arrays}, others.([]interface{})...))
|
||||
}
|
||||
|
||||
ArrayContainsExpression <- "ARRAY_CONTAINS"i ws "(" ws array:SelectItem ws "," ws item:SelectItem partialMatch:(ws "," ws ex:SelectItem { return ex, nil })? ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallArrayContains, []interface{}{array, item, partialMatch})
|
||||
}
|
||||
|
||||
ArrayContainsAnyExpression <- "ARRAY_CONTAINS_ANY"i ws "(" ws array:SelectItem items:(ws "," ws ex:SelectItem { return ex, nil })+ ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallArrayContainsAny, append([]interface{}{array}, items.([]interface{})...))
|
||||
}
|
||||
|
||||
ArrayContainsAllExpression <- "ARRAY_CONTAINS_ALL"i ws "(" ws array:SelectItem items:(ws "," ws ex:SelectItem { return ex, nil })+ ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallArrayContainsAll, append([]interface{}{array}, items.([]interface{})...))
|
||||
}
|
||||
|
||||
ArrayLengthExpression <- "ARRAY_LENGTH"i ws "(" ws array:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallArrayLength, []interface{}{array})
|
||||
}
|
||||
@@ -485,10 +657,73 @@ SetUnionExpression <- "SetUnion"i ws "(" ws set1:SelectItem ws "," ws set2:Selec
|
||||
return createFunctionCall(parsers.FunctionCallSetUnion, []interface{}{set1, set2})
|
||||
}
|
||||
|
||||
MathAbsExpression <- "ABS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAbs, []interface{}{ex}) }
|
||||
MathAcosExpression <- "ACOS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAcos, []interface{}{ex}) }
|
||||
MathAsinExpression <- "ASIN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAsin, []interface{}{ex}) }
|
||||
MathAtanExpression <- "ATAN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAtan, []interface{}{ex}) }
|
||||
MathCeilingExpression <- "CEILING"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathCeiling, []interface{}{ex}) }
|
||||
MathCosExpression <- "COS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathCos, []interface{}{ex}) }
|
||||
MathCotExpression <- "COT"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathCot, []interface{}{ex}) }
|
||||
MathDegreesExpression <- "DEGREES"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathDegrees, []interface{}{ex}) }
|
||||
MathExpExpression <- "EXP"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathExp, []interface{}{ex}) }
|
||||
MathFloorExpression <- "FLOOR"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathFloor, []interface{}{ex}) }
|
||||
MathIntBitNotExpression <- "IntBitNot"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitNot, []interface{}{ex}) }
|
||||
MathLog10Expression <- "LOG10"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathLog10, []interface{}{ex}) }
|
||||
MathRadiansExpression <- "RADIANS"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathRadians, []interface{}{ex}) }
|
||||
MathRoundExpression <- "ROUND"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathRound, []interface{}{ex}) }
|
||||
MathSignExpression <- "SIGN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSign, []interface{}{ex}) }
|
||||
MathSinExpression <- "SIN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSin, []interface{}{ex}) }
|
||||
MathSqrtExpression <- "SQRT"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSqrt, []interface{}{ex}) }
|
||||
MathSquareExpression <- "SQUARE"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathSquare, []interface{}{ex}) }
|
||||
MathTanExpression <- "TAN"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathTan, []interface{}{ex}) }
|
||||
MathTruncExpression <- "TRUNC"i ws "(" ws ex:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathTrunc, []interface{}{ex}) }
|
||||
|
||||
MathAtn2Expression <- "ATN2"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathAtn2, []interface{}{set1, set2}) }
|
||||
MathIntAddExpression <- "IntAdd"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntAdd, []interface{}{set1, set2}) }
|
||||
MathIntBitAndExpression <- "IntBitAnd"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitAnd, []interface{}{set1, set2}) }
|
||||
MathIntBitLeftShiftExpression <- "IntBitLeftShift"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitLeftShift, []interface{}{set1, set2}) }
|
||||
MathIntBitOrExpression <- "IntBitOr"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitOr, []interface{}{set1, set2}) }
|
||||
MathIntBitRightShiftExpression <- "IntBitRightShift"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitRightShift, []interface{}{set1, set2}) }
|
||||
MathIntBitXorExpression <- "IntBitXor"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntBitXor, []interface{}{set1, set2}) }
|
||||
MathIntDivExpression <- "IntDiv"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntDiv, []interface{}{set1, set2}) }
|
||||
MathIntModExpression <- "IntMod"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntMod, []interface{}{set1, set2}) }
|
||||
MathIntMulExpression <- "IntMul"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntMul, []interface{}{set1, set2}) }
|
||||
MathIntSubExpression <- "IntSub"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathIntSub, []interface{}{set1, set2}) }
|
||||
MathPowerExpression <- "POWER"i ws "(" ws set1:SelectItem ws "," ws set2:SelectItem ws ")" { return createFunctionCall(parsers.FunctionCallMathPower, []interface{}{set1, set2}) }
|
||||
|
||||
MathLogExpression <- "LOG"i ws "(" ws ex1:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallMathLog, append([]interface{}{ex1}, others.([]interface{})...))
|
||||
}
|
||||
MathNumberBinExpression <- "NumberBin"i ws "(" ws ex1:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallMathNumberBin, append([]interface{}{ex1}, others.([]interface{})...))
|
||||
}
|
||||
MathPiExpression <- "PI"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathPi, []interface{}{}) }
|
||||
MathRandExpression <- "RAND"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathRand, []interface{}{}) }
|
||||
|
||||
InFunction <- ex1:SelectProperty ws "IN"i ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||
}
|
||||
|
||||
AvgAggregateExpression <- "AVG"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateAvg, []interface{}{ex})
|
||||
}
|
||||
|
||||
CountAggregateExpression <- "COUNT"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateCount, []interface{}{ex})
|
||||
}
|
||||
|
||||
MaxAggregateExpression <- "MAX"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateMax, []interface{}{ex})
|
||||
}
|
||||
|
||||
MinAggregateExpression <- "MIN"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateMin, []interface{}{ex})
|
||||
}
|
||||
|
||||
SumAggregateExpression <- "SUM"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateSum, []interface{}{ex})
|
||||
}
|
||||
|
||||
Integer <- [0-9]+ {
|
||||
return strconv.Atoi(string(c.text))
|
||||
}
|
||||
@@ -512,4 +747,6 @@ non_escape_character <- !(escape_character) char:.
|
||||
|
||||
ws <- [ \t\n\r]*
|
||||
|
||||
wss <- [ \t\n\r]+
|
||||
|
||||
EOF <- !.
|
||||
|
||||
@@ -22,6 +22,34 @@ func Test_Parse_Select(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT with query parameters as accessor", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, c[@param] FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "@param"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT DISTINCT", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT DISTINCT c.id FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Distinct: true,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT TOP", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
@@ -36,6 +64,21 @@ func Test_Parse_Select(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT OFFSET", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM c OFFSET 3 LIMIT 5`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Count: 5,
|
||||
Offset: 3,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT VALUE", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_StringFunctions(t *testing.T) {
|
||||
@@ -23,20 +24,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -61,13 +50,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
@@ -93,13 +76,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
@@ -128,20 +105,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -166,20 +131,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -204,20 +157,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -242,20 +183,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "2",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("2"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -352,13 +281,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 5,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(5),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -431,20 +354,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "old",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "new",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("old"),
|
||||
testutils.SelectItem_Constant_String("new"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -469,13 +380,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 3,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -524,13 +429,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 3,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -579,20 +478,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 5,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
testutils.SelectItem_Constant_Int(5),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
125
parsers/nosql/subquery_test.go
Normal file
125
parsers/nosql/subquery_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_SubQuery(t *testing.T) {
|
||||
|
||||
t.Run("Should parse FROM subquery", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM (SELECT VALUE cc["info"] FROM cc) AS c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "c",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
Table: parsers.Table{Value: "cc"},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse JOIN subquery", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id, cc.name FROM c JOIN (SELECT tag.name FROM tag IN c.tags) AS cc`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "cc",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse JOIN EXISTS subquery", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id
|
||||
FROM c
|
||||
JOIN (
|
||||
SELECT VALUE EXISTS(SELECT tag.name FROM tag IN c.tags)
|
||||
) AS hasTags
|
||||
WHERE hasTags`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{Value: "hasTags"},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "hasTags",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
IsTopLevel: true,
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
Exists: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Filters: parsers.SelectItem{
|
||||
Path: []string{"hasTags"},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_Were(t *testing.T) {
|
||||
@@ -22,10 +23,7 @@ func Test_Parse_Were(t *testing.T) {
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -51,18 +49,12 @@ func Test_Parse_Were(t *testing.T) {
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "12345"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_String("12345"),
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "pk"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 123},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Int(123),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -87,10 +79,7 @@ func Test_Parse_Were(t *testing.T) {
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeOr,
|
||||
@@ -98,18 +87,12 @@ func Test_Parse_Were(t *testing.T) {
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "123"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_String("123"),
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "456"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -135,43 +118,28 @@ func Test_Parse_Were(t *testing.T) {
|
||||
Filters: parsers.LogicalExpression{
|
||||
Expressions: []interface{}{
|
||||
parsers.ComparisonExpression{
|
||||
Left: parsers.SelectItem{Path: []string{"c", "boolean"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
||||
},
|
||||
Left: parsers.SelectItem{Path: []string{"c", "boolean"}},
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
Operation: "=",
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Left: parsers.SelectItem{Path: []string{"c", "integer"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 1},
|
||||
},
|
||||
Left: parsers.SelectItem{Path: []string{"c", "integer"}},
|
||||
Right: testutils.SelectItem_Constant_Int(1),
|
||||
Operation: "=",
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Left: parsers.SelectItem{Path: []string{"c", "float"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeFloat, Value: 6.9},
|
||||
},
|
||||
Left: parsers.SelectItem{Path: []string{"c", "float"}},
|
||||
Right: testutils.SelectItem_Constant_Float(6.9),
|
||||
Operation: "=",
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Left: parsers.SelectItem{Path: []string{"c", "string"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "hello"},
|
||||
},
|
||||
Left: parsers.SelectItem{Path: []string{"c", "string"}},
|
||||
Right: testutils.SelectItem_Constant_String("hello"),
|
||||
Operation: "=",
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Left: parsers.SelectItem{Path: []string{"c", "param"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeParameterConstant, Value: "@param_id1"},
|
||||
},
|
||||
Left: parsers.SelectItem{Path: []string{"c", "param"}},
|
||||
Right: testutils.SelectItem_Constant_Parameter("@param_id1"),
|
||||
Operation: "=",
|
||||
},
|
||||
},
|
||||
|
||||
121
query_executors/memory_executor/aggregate_functions.go
Normal file
121
query_executors/memory_executor/aggregate_functions.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (r rowContext) aggregate_Avg(arguments []interface{}) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
sum := 0.0
|
||||
count := 0
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
sum += numericValue
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
sum += float64(numericValue)
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return sum / float64(count)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Count(arguments []interface{}) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
count := 0
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if value != nil {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Max(arguments []interface{}) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
max := 0.0
|
||||
count := 0
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
if numericValue > max {
|
||||
max = numericValue
|
||||
}
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
if float64(numericValue) > max {
|
||||
max = float64(numericValue)
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return max
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Min(arguments []interface{}) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
min := math.MaxFloat64
|
||||
count := 0
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
if numericValue < min {
|
||||
min = numericValue
|
||||
}
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
if float64(numericValue) < min {
|
||||
min = float64(numericValue)
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return min
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) aggregate_Sum(arguments []interface{}) interface{} {
|
||||
selectExpression := arguments[0].(parsers.SelectItem)
|
||||
sum := 0.0
|
||||
count := 0
|
||||
|
||||
for _, item := range r.grouppedRows {
|
||||
value := item.resolveSelectItem(selectExpression)
|
||||
if numericValue, ok := value.(float64); ok {
|
||||
sum += numericValue
|
||||
count++
|
||||
} else if numericValue, ok := value.(int); ok {
|
||||
sum += float64(numericValue)
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return sum
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
210
query_executors/memory_executor/aggregate_functions_test.go
Normal file
210
query_executors/memory_executor/aggregate_functions_test.go
Normal file
@@ -0,0 +1,210 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "number": 123, "key": "a"},
|
||||
map[string]interface{}{"id": "456", "number": 456, "key": "a"},
|
||||
map[string]interface{}{"id": "789", "number": 789, "key": "b"},
|
||||
map[string]interface{}{"id": "no-number", "key": "b"},
|
||||
}
|
||||
|
||||
t.Run("Should execute function AVG()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "avg",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "avg": 289.5},
|
||||
map[string]interface{}{"key": "b", "avg": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function AVG() without GROUP BY clause", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Alias: "avg",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"avg": 456.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COUNT()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "cnt",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateCount,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "cnt": 2},
|
||||
map[string]interface{}{"key": "b", "cnt": 1},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function MAX()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "max",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMax,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "max": 456.0},
|
||||
map[string]interface{}{"key": "b", "max": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function MIN()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "min",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMin,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "min": 123.0},
|
||||
map[string]interface{}{"key": "b", "min": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function SUM()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
{
|
||||
Alias: "sum",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateSum,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "number"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"key": "a", "sum": 579.0},
|
||||
map[string]interface{}{"key": "b", "sum": 789.0},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -1,23 +1,103 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) array_Concat(arguments []interface{}, row RowType) []interface{} {
|
||||
func (r rowContext) array_Concat(arguments []interface{}) []interface{} {
|
||||
var result []interface{}
|
||||
for _, arg := range arguments {
|
||||
array := c.parseArray(arg, row)
|
||||
array := r.parseArray(arg)
|
||||
result = append(result, array...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) array_Length(arguments []interface{}, row RowType) int {
|
||||
array := c.parseArray(arguments[0], row)
|
||||
func (r rowContext) array_Contains(arguments []interface{}) bool {
|
||||
array := r.parseArray(arguments[0])
|
||||
if array == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
exprToSearch := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
partialSearch := false
|
||||
if len(arguments) > 2 {
|
||||
boolExpr := r.resolveSelectItem(arguments[2].(parsers.SelectItem))
|
||||
if boolValue, ok := boolExpr.(bool); ok {
|
||||
partialSearch = boolValue
|
||||
} else {
|
||||
logger.ErrorLn("array_Contains - got parameters of wrong type")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range array {
|
||||
if partialSearch {
|
||||
if r.partialMatch(item, exprToSearch) {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if reflect.DeepEqual(item, exprToSearch) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r rowContext) array_Contains_Any(arguments []interface{}) bool {
|
||||
array := r.parseArray(arguments[0])
|
||||
if array == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
valueSelectItems := arguments[1:]
|
||||
|
||||
for _, valueSelectItem := range valueSelectItems {
|
||||
value := r.resolveSelectItem(valueSelectItem.(parsers.SelectItem))
|
||||
for _, item := range array {
|
||||
if reflect.DeepEqual(item, value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r rowContext) array_Contains_All(arguments []interface{}) bool {
|
||||
array := r.parseArray(arguments[0])
|
||||
if array == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
valueSelectItems := arguments[1:]
|
||||
|
||||
for _, valueSelectItem := range valueSelectItems {
|
||||
value := r.resolveSelectItem(valueSelectItem.(parsers.SelectItem))
|
||||
|
||||
found := false
|
||||
for _, item := range array {
|
||||
if reflect.DeepEqual(item, value) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (r rowContext) array_Length(arguments []interface{}) int {
|
||||
array := r.parseArray(arguments[0])
|
||||
if array == nil {
|
||||
return 0
|
||||
}
|
||||
@@ -25,24 +105,24 @@ func (c memoryExecutorContext) array_Length(arguments []interface{}, row RowType
|
||||
return len(array)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) array_Slice(arguments []interface{}, row RowType) []interface{} {
|
||||
func (r rowContext) array_Slice(arguments []interface{}) []interface{} {
|
||||
var ok bool
|
||||
var start int
|
||||
var length int
|
||||
array := c.parseArray(arguments[0], row)
|
||||
startEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
array := r.parseArray(arguments[0])
|
||||
startEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if arguments[2] != nil {
|
||||
lengthEx := c.getFieldValue(arguments[2].(parsers.SelectItem), row)
|
||||
lengthEx := r.resolveSelectItem(arguments[2].(parsers.SelectItem))
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
fmt.Println("array_Slice - got length parameters of wrong type")
|
||||
logger.ErrorLn("array_Slice - got length parameters of wrong type")
|
||||
return []interface{}{}
|
||||
}
|
||||
}
|
||||
|
||||
if start, ok = startEx.(int); !ok {
|
||||
fmt.Println("array_Slice - got start parameters of wrong type")
|
||||
logger.ErrorLn("array_Slice - got start parameters of wrong type")
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
@@ -65,9 +145,9 @@ func (c memoryExecutorContext) array_Slice(arguments []interface{}, row RowType)
|
||||
return array[start:end]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) set_Intersect(arguments []interface{}, row RowType) []interface{} {
|
||||
set1 := c.parseArray(arguments[0], row)
|
||||
set2 := c.parseArray(arguments[1], row)
|
||||
func (r rowContext) set_Intersect(arguments []interface{}) []interface{} {
|
||||
set1 := r.parseArray(arguments[0])
|
||||
set2 := r.parseArray(arguments[1])
|
||||
|
||||
intersection := make(map[interface{}]struct{})
|
||||
if set1 == nil || set2 == nil {
|
||||
@@ -88,9 +168,9 @@ func (c memoryExecutorContext) set_Intersect(arguments []interface{}, row RowTyp
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) set_Union(arguments []interface{}, row RowType) []interface{} {
|
||||
set1 := c.parseArray(arguments[0], row)
|
||||
set2 := c.parseArray(arguments[1], row)
|
||||
func (r rowContext) set_Union(arguments []interface{}) []interface{} {
|
||||
set1 := r.parseArray(arguments[0])
|
||||
set2 := r.parseArray(arguments[1])
|
||||
|
||||
var result []interface{}
|
||||
union := make(map[interface{}]struct{})
|
||||
@@ -111,13 +191,13 @@ func (c memoryExecutorContext) set_Union(arguments []interface{}, row RowType) [
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) parseArray(argument interface{}, row RowType) []interface{} {
|
||||
func (r rowContext) parseArray(argument interface{}) []interface{} {
|
||||
exItem := argument.(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
arrValue := reflect.ValueOf(ex)
|
||||
if arrValue.Kind() != reflect.Slice {
|
||||
fmt.Println("parseArray got parameters of wrong type")
|
||||
logger.ErrorLn("parseArray got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -129,3 +209,21 @@ func (c memoryExecutorContext) parseArray(argument interface{}, row RowType) []i
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (r rowContext) partialMatch(item interface{}, exprToSearch interface{}) bool {
|
||||
itemValue := reflect.ValueOf(item)
|
||||
exprValue := reflect.ValueOf(exprToSearch)
|
||||
|
||||
if itemValue.Kind() != reflect.Map || exprValue.Kind() != reflect.Map {
|
||||
logger.ErrorLn("partialMatch got parameters of wrong type")
|
||||
return false
|
||||
}
|
||||
|
||||
for _, key := range exprValue.MapKeys() {
|
||||
if itemValue.MapIndex(key).Interface() != exprValue.MapIndex(key).Interface() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
@@ -52,6 +53,286 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ARRAY_CONTAINS()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
Parameters: map[string]interface{}{
|
||||
"@categories": []interface{}{"coats", "jackets", "sweatshirts"},
|
||||
"@objectArray": []interface{}{map[string]interface{}{"category": "shirts", "color": "blue"}},
|
||||
"@fullMatchObject": map[string]interface{}{"category": "shirts", "color": "blue"},
|
||||
"@partialMatchObject": map[string]interface{}{"category": "shirts"},
|
||||
"@missingPartialMatchObject": map[string]interface{}{"category": "shorts", "color": "blue"},
|
||||
},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Alias: "ContainsItem",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@categories"),
|
||||
testutils.SelectItem_Constant_String("coats"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "MissingItem",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@categories"),
|
||||
testutils.SelectItem_Constant_String("hoodies"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "ContainsFullMatchObject",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@objectArray"),
|
||||
testutils.SelectItem_Constant_Parameter("@fullMatchObject"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "MissingFullMatchObject",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@objectArray"),
|
||||
testutils.SelectItem_Constant_Parameter("@partialMatchObject"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "ContainsPartialMatchObject",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@objectArray"),
|
||||
testutils.SelectItem_Constant_Parameter("@partialMatchObject"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "MissingPartialMatchObject",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@objectArray"),
|
||||
testutils.SelectItem_Constant_Parameter("@missingPartialMatchObject"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
[]memoryexecutor.RowType{map[string]interface{}{"id": "123"}},
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{
|
||||
"ContainsItem": true,
|
||||
"MissingItem": false,
|
||||
"ContainsFullMatchObject": true,
|
||||
"MissingFullMatchObject": false,
|
||||
"ContainsPartialMatchObject": true,
|
||||
"MissingPartialMatchObject": false,
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ARRAY_CONTAINS_ANY()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
Parameters: map[string]interface{}{
|
||||
"@mixedArray": []interface{}{1, true, "3", []int{1, 2, 3}},
|
||||
"@numbers": []interface{}{1, 2, 3, 4},
|
||||
"@emptyArray": []interface{}{},
|
||||
"@arr123": []interface{}{1, 2, 3},
|
||||
},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Alias: "matchesEntireArray",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAny,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@mixedArray"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
testutils.SelectItem_Constant_String("3"),
|
||||
testutils.SelectItem_Constant_Parameter("@arr123"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "matchesSomeValues",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAny,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@numbers"),
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
testutils.SelectItem_Constant_Int(4),
|
||||
testutils.SelectItem_Constant_Int(5),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "matchSingleValue",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAny,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@numbers"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "noMatches",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAny,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@numbers"),
|
||||
testutils.SelectItem_Constant_Int(5),
|
||||
testutils.SelectItem_Constant_Int(6),
|
||||
testutils.SelectItem_Constant_Int(7),
|
||||
testutils.SelectItem_Constant_Int(8),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "emptyArray",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAny,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@emptyArray"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
[]memoryexecutor.RowType{map[string]interface{}{"id": "123"}},
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{
|
||||
"matchesEntireArray": true,
|
||||
"matchesSomeValues": true,
|
||||
"matchSingleValue": true,
|
||||
"noMatches": false,
|
||||
"emptyArray": false,
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ARRAY_CONTAINS_ALL()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
Parameters: map[string]interface{}{
|
||||
"@mixedArray": []interface{}{1, true, "3", []interface{}{1, 2, 3}},
|
||||
"@numbers": []interface{}{1, 2, 3, 4},
|
||||
"@emptyArray": []interface{}{},
|
||||
"@arr123": []interface{}{1, 2, 3},
|
||||
},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Alias: "matchesEntireArray",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAll,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@mixedArray"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
testutils.SelectItem_Constant_String("3"),
|
||||
testutils.SelectItem_Constant_Parameter("@arr123"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "matchesSomeValues",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAll,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@numbers"),
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
testutils.SelectItem_Constant_Int(4),
|
||||
testutils.SelectItem_Constant_Int(5),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "matchSingleValue",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAll,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@numbers"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "noMatches",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAll,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@numbers"),
|
||||
testutils.SelectItem_Constant_Int(5),
|
||||
testutils.SelectItem_Constant_Int(6),
|
||||
testutils.SelectItem_Constant_Int(7),
|
||||
testutils.SelectItem_Constant_Int(8),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "emptyArray",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContainsAll,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@emptyArray"),
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
[]memoryexecutor.RowType{map[string]interface{}{"id": "123"}},
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{
|
||||
"matchesEntireArray": true,
|
||||
"matchesSomeValues": false,
|
||||
"matchSingleValue": true,
|
||||
"noMatches": false,
|
||||
"emptyArray": false,
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ARRAY_LENGTH()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
@@ -105,20 +386,8 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
Path: []string{"c", "arr2"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(1),
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
86
query_executors/memory_executor/join_test.go
Normal file
86
query_executors/memory_executor/join_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_Joins(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{
|
||||
"id": 1,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "a"},
|
||||
{"name": "b"},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"id": 2,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "b"},
|
||||
{"name": "c"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Should execute JOIN on 'tags'", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "name": "a"},
|
||||
map[string]interface{}{"id": 1, "name": "b"},
|
||||
map[string]interface{}{"id": 2, "name": "b"},
|
||||
map[string]interface{}{"id": 2, "name": "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute JOIN VALUE on 'tags'", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"name": "a"},
|
||||
map[string]interface{}{"name": "b"},
|
||||
map[string]interface{}{"name": "b"},
|
||||
map[string]interface{}{"name": "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
615
query_executors/memory_executor/math_functions.go
Normal file
615
query_executors/memory_executor/math_functions.go
Normal file
@@ -0,0 +1,615 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (r rowContext) math_Abs(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Abs(val)
|
||||
case int:
|
||||
if val < 0 {
|
||||
return -val
|
||||
}
|
||||
return val
|
||||
default:
|
||||
logger.DebugLn("math_Abs - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Acos(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Acos - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val < -1 || val > 1 {
|
||||
logger.DebugLn("math_Acos - value out of domain for acos")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Acos(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Asin(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Asin - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val < -1 || val > 1 {
|
||||
logger.DebugLn("math_Asin - value out of domain for acos")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Asin(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Atan(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Atan - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Atan(val) * 180 / math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Ceiling(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Ceil(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.DebugLn("math_Ceiling - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Cos(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Cos - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Cos(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Cot(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Cot - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val == 0 {
|
||||
logger.DebugLn("math_Cot - cotangent undefined for zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return 1 / math.Tan(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Degrees(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Degrees - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return val * (180 / math.Pi)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Exp(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Exp - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Exp(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Floor(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Floor(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.DebugLn("math_Floor - got parameters of wrong type")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitNot(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case int:
|
||||
return ^val
|
||||
default:
|
||||
logger.DebugLn("math_IntBitNot - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Log10(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Log10 - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
if val <= 0 {
|
||||
logger.DebugLn("math_Log10 - value must be greater than 0")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Log10(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Radians(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Radians - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return val * (math.Pi / 180.0)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Round(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Round(val)
|
||||
case int:
|
||||
return val
|
||||
default:
|
||||
logger.DebugLn("math_Round - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Sign(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
if val > 0 {
|
||||
return 1
|
||||
} else if val < 0 {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
case int:
|
||||
if val > 0 {
|
||||
return 1
|
||||
} else if val < 0 {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
default:
|
||||
logger.DebugLn("math_Sign - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Sin(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Sin - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Sin(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Sqrt(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Sqrt - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Sqrt(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Square(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Square - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Pow(val, 2)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Tan(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
val, valIsNumber := numToFloat64(ex)
|
||||
if !valIsNumber {
|
||||
logger.DebugLn("math_Tan - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Tan(val)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Trunc(arguments []interface{}) interface{} {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return math.Trunc(val)
|
||||
case int:
|
||||
return float64(val)
|
||||
default:
|
||||
logger.DebugLn("math_Trunc - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r rowContext) math_Atn2(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
y, yIsNumber := numToFloat64(ex1)
|
||||
x, xIsNumber := numToFloat64(ex2)
|
||||
|
||||
if !yIsNumber || !xIsNumber {
|
||||
logger.DebugLn("math_Atn2 - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Atan2(y, x)
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntAdd(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
ex1Number, ex1IsNumber := numToInt(ex1)
|
||||
ex2Number, ex2IsNumber := numToInt(ex2)
|
||||
|
||||
if !ex1IsNumber || !ex2IsNumber {
|
||||
logger.DebugLn("math_IntAdd - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ex1Number + ex2Number
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitAnd(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
ex1Int, ex1IsInt := numToInt(ex1)
|
||||
ex2Int, ex2IsInt := numToInt(ex2)
|
||||
|
||||
if !ex1IsInt || !ex2IsInt {
|
||||
logger.DebugLn("math_IntBitAnd - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return ex1Int & ex2Int
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitLeftShift(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := numToInt(ex1)
|
||||
num2, num2IsInt := numToInt(ex2)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.DebugLn("math_IntBitLeftShift - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 << uint(num2)
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitOr(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.DebugLn("math_IntBitOr - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 | num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitRightShift(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := numToInt(ex1)
|
||||
num2, num2IsInt := numToInt(ex2)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.DebugLn("math_IntBitRightShift - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 >> uint(num2)
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntBitXor(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.DebugLn("math_IntBitXor - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 ^ num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntDiv(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt || num2 == 0 {
|
||||
logger.DebugLn("math_IntDiv - got parameters of wrong type or divide by zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 / num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntMul(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.DebugLn("math_IntMul - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 * num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntSub(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt {
|
||||
logger.DebugLn("math_IntSub - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 - num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_IntMod(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
num1, num1IsInt := ex1.(int)
|
||||
num2, num2IsInt := ex2.(int)
|
||||
|
||||
if !num1IsInt || !num2IsInt || num2 == 0 {
|
||||
logger.DebugLn("math_IntMod - got parameters of wrong type or divide by zero")
|
||||
return nil
|
||||
}
|
||||
|
||||
return num1 % num2
|
||||
}
|
||||
|
||||
func (r rowContext) math_Power(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
ex1 := r.resolveSelectItem(exItem1)
|
||||
ex2 := r.resolveSelectItem(exItem2)
|
||||
|
||||
base, baseIsNumber := numToFloat64(ex1)
|
||||
exponent, exponentIsNumber := numToFloat64(ex2)
|
||||
|
||||
if !baseIsNumber || !exponentIsNumber {
|
||||
logger.DebugLn("math_Power - got parameters of wrong type")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Pow(base, exponent)
|
||||
}
|
||||
|
||||
func (r rowContext) math_Log(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem1)
|
||||
|
||||
var base float64 = math.E
|
||||
if len(arguments) > 1 {
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
baseValueObject := r.resolveSelectItem(exItem2)
|
||||
baseValue, baseValueIsNumber := numToFloat64(baseValueObject)
|
||||
|
||||
if !baseValueIsNumber {
|
||||
logger.DebugLn("math_Log - base parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
if baseValue > 0 && baseValue != 1 {
|
||||
base = baseValue
|
||||
} else {
|
||||
logger.DebugLn("math_Log - base must be greater than 0 and not equal to 1")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, numIsNumber := numToFloat64(ex)
|
||||
if !numIsNumber || num <= 0 {
|
||||
logger.DebugLn("math_Log - parameter must be a positive numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Log(num) / math.Log(base)
|
||||
}
|
||||
|
||||
func (r rowContext) math_NumberBin(arguments []interface{}) interface{} {
|
||||
exItem1 := arguments[0].(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem1)
|
||||
|
||||
binSize := 1.0
|
||||
|
||||
if len(arguments) > 1 {
|
||||
exItem2 := arguments[1].(parsers.SelectItem)
|
||||
binSizeValueObject := r.resolveSelectItem(exItem2)
|
||||
binSizeValue, binSizeValueIsNumber := numToFloat64(binSizeValueObject)
|
||||
|
||||
if !binSizeValueIsNumber {
|
||||
logger.DebugLn("math_NumberBin - base parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
if binSizeValue != 0 {
|
||||
binSize = binSizeValue
|
||||
} else {
|
||||
logger.DebugLn("math_NumberBin - base must not be equal to 0")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
num, numIsNumber := numToFloat64(ex)
|
||||
if !numIsNumber {
|
||||
logger.DebugLn("math_NumberBin - parameter must be a numeric value")
|
||||
return nil
|
||||
}
|
||||
|
||||
return math.Floor(num/binSize) * binSize
|
||||
}
|
||||
|
||||
func (r rowContext) math_Pi() interface{} {
|
||||
return math.Pi
|
||||
}
|
||||
|
||||
func (r rowContext) math_Rand() interface{} {
|
||||
return rand.Float64()
|
||||
}
|
||||
|
||||
func numToInt(ex interface{}) (int, bool) {
|
||||
switch val := ex.(type) {
|
||||
case float64:
|
||||
return int(val), true
|
||||
case int:
|
||||
return val, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func numToFloat64(num interface{}) (float64, bool) {
|
||||
switch val := num.(type) {
|
||||
case float64:
|
||||
return val, true
|
||||
case int:
|
||||
return float64(val), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
269
query_executors/memory_executor/math_functions_test.go
Normal file
269
query_executors/memory_executor/math_functions_test.go
Normal file
@@ -0,0 +1,269 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_MathFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "value": 0.0},
|
||||
map[string]interface{}{"id": 2, "value": 1.0},
|
||||
map[string]interface{}{"id": 3, "value": -1.0},
|
||||
map[string]interface{}{"id": 4, "value": 0.5},
|
||||
map[string]interface{}{"id": 5, "value": -0.5},
|
||||
map[string]interface{}{"id": 6, "value": 0.707},
|
||||
map[string]interface{}{"id": 7, "value": -0.707},
|
||||
map[string]interface{}{"id": 8, "value": 0.866},
|
||||
map[string]interface{}{"id": 9, "value": -0.866},
|
||||
}
|
||||
|
||||
mockDataInts := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": 1, "value": -1},
|
||||
map[string]interface{}{"id": 2, "value": 0},
|
||||
map[string]interface{}{"id": 3, "value": 1},
|
||||
map[string]interface{}{"id": 4, "value": 5},
|
||||
}
|
||||
|
||||
t.Run("Should execute function ABS(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAbs,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": 0.0},
|
||||
map[string]interface{}{"value": 1.0, "result": 1.0},
|
||||
map[string]interface{}{"value": -1.0, "result": 1.0},
|
||||
map[string]interface{}{"value": 0.5, "result": 0.5},
|
||||
map[string]interface{}{"value": -0.5, "result": 0.5},
|
||||
map[string]interface{}{"value": 0.707, "result": 0.707},
|
||||
map[string]interface{}{"value": -0.707, "result": 0.707},
|
||||
map[string]interface{}{"value": 0.866, "result": 0.866},
|
||||
map[string]interface{}{"value": -0.866, "result": 0.866},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ACOS(cosine)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAcos,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Acos(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Acos(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Acos(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Acos(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Acos(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Acos(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Acos(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Acos(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Acos(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ASIN(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAsin,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Asin(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Asin(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Asin(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Asin(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Asin(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Asin(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Asin(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Asin(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Asin(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function ATAN(tangent)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathAtan,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Atan(0.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Atan(1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Atan(-1.0) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Atan(0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Atan(-0.5) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Atan(0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Atan(-0.707) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Atan(0.866) * 180 / math.Pi},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Atan(-0.866) * 180 / math.Pi},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COS(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathCos,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Cos(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Cos(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Cos(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Cos(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Cos(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Cos(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Cos(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Cos(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Cos(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function COT(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathCot,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": nil},
|
||||
map[string]interface{}{"value": 1.0, "result": 1 / math.Tan(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": 1 / math.Tan(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": 1 / math.Tan(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": 1 / math.Tan(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": 1 / math.Tan(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": 1 / math.Tan(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": 1 / math.Tan(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": 1 / math.Tan(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function Degrees(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathDegrees,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": 0.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 1.0, "result": 1.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -1.0, "result": -1.0 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.5, "result": 0.5 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.5, "result": -0.5 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.707, "result": 0.707 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.707, "result": -0.707 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": 0.866, "result": 0.866 * (180 / math.Pi)},
|
||||
map[string]interface{}{"value": -0.866, "result": -0.866 * (180 / math.Pi)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function EXP(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathExp,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Exp(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Exp(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Exp(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Exp(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Exp(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Exp(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Exp(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Exp(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Exp(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function FLOOR(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathFloor,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": math.Floor(0.0)},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Floor(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": math.Floor(-1.0)},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Floor(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": math.Floor(-0.5)},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Floor(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": math.Floor(-0.707)},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Floor(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": math.Floor(-0.866)},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function IntBitNot(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathIntBitNot,
|
||||
mockDataInts,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": -1, "result": ^-1},
|
||||
map[string]interface{}{"value": 0, "result": ^0},
|
||||
map[string]interface{}{"value": 1, "result": ^1},
|
||||
map[string]interface{}{"value": 5, "result": ^5},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function LOG10(value)", func(t *testing.T) {
|
||||
testMathFunctionExecute(
|
||||
t,
|
||||
parsers.FunctionCallMathLog10,
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"value": 0.0, "result": nil},
|
||||
map[string]interface{}{"value": 1.0, "result": math.Log10(1.0)},
|
||||
map[string]interface{}{"value": -1.0, "result": nil},
|
||||
map[string]interface{}{"value": 0.5, "result": math.Log10(0.5)},
|
||||
map[string]interface{}{"value": -0.5, "result": nil},
|
||||
map[string]interface{}{"value": 0.707, "result": math.Log10(0.707)},
|
||||
map[string]interface{}{"value": -0.707, "result": nil},
|
||||
map[string]interface{}{"value": 0.866, "result": math.Log10(0.866)},
|
||||
map[string]interface{}{"value": -0.866, "result": nil},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func testMathFunctionExecute(
|
||||
t *testing.T,
|
||||
functionCallType parsers.FunctionCallType,
|
||||
data []memoryexecutor.RowType,
|
||||
expectedData []memoryexecutor.RowType,
|
||||
) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "result",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: functionCallType,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "value"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
data,
|
||||
expectedData,
|
||||
)
|
||||
}
|
||||
@@ -4,294 +4,229 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type RowType interface{}
|
||||
type ExpressionType interface{}
|
||||
|
||||
type memoryExecutorContext struct {
|
||||
parameters map[string]interface{}
|
||||
type rowContext struct {
|
||||
tables map[string]RowType
|
||||
parameters map[string]interface{}
|
||||
grouppedRows []rowContext
|
||||
}
|
||||
|
||||
func Execute(query parsers.SelectStmt, data []RowType) []RowType {
|
||||
ctx := memoryExecutorContext{
|
||||
parameters: query.Parameters,
|
||||
func ExecuteQuery(query parsers.SelectStmt, documents []RowType) []RowType {
|
||||
currentDocuments := make([]rowContext, 0)
|
||||
for _, doc := range documents {
|
||||
currentDocuments = append(currentDocuments, resolveFrom(query, doc)...)
|
||||
}
|
||||
|
||||
result := make([]RowType, 0)
|
||||
// Handle JOINS
|
||||
nextDocuments := make([]rowContext, 0)
|
||||
for _, currentDocument := range currentDocuments {
|
||||
rowContexts := currentDocument.handleJoin(query)
|
||||
nextDocuments = append(nextDocuments, rowContexts...)
|
||||
}
|
||||
currentDocuments = nextDocuments
|
||||
|
||||
// Apply Filter
|
||||
for _, row := range data {
|
||||
if ctx.evaluateFilters(query.Filters, row) {
|
||||
result = append(result, row)
|
||||
// Apply filters
|
||||
nextDocuments = make([]rowContext, 0)
|
||||
for _, currentDocument := range currentDocuments {
|
||||
if currentDocument.applyFilters(query.Filters) {
|
||||
nextDocuments = append(nextDocuments, currentDocument)
|
||||
}
|
||||
}
|
||||
currentDocuments = nextDocuments
|
||||
|
||||
// Apply order
|
||||
if query.OrderExpressions != nil && len(query.OrderExpressions) > 0 {
|
||||
ctx.orderBy(query.OrderExpressions, result)
|
||||
if len(query.OrderExpressions) > 0 {
|
||||
applyOrder(currentDocuments, query.OrderExpressions)
|
||||
}
|
||||
|
||||
// Apply result limit
|
||||
if query.Count > 0 {
|
||||
count := func() int {
|
||||
if len(result) < query.Count {
|
||||
return len(result)
|
||||
}
|
||||
return query.Count
|
||||
}()
|
||||
result = result[:count]
|
||||
// Apply group by
|
||||
if len(query.GroupBy) > 0 {
|
||||
currentDocuments = applyGroupBy(currentDocuments, query.GroupBy)
|
||||
}
|
||||
|
||||
// Apply select
|
||||
selectedData := make([]RowType, 0)
|
||||
for _, row := range result {
|
||||
selectedData = append(selectedData, ctx.selectRow(query.SelectItems, row))
|
||||
projectedDocuments := applyProjection(currentDocuments, query.SelectItems, query.GroupBy)
|
||||
|
||||
// Apply distinct
|
||||
if query.Distinct {
|
||||
projectedDocuments = deduplicate(projectedDocuments)
|
||||
}
|
||||
|
||||
return selectedData
|
||||
// Apply result limit
|
||||
if query.Count > 0 && len(projectedDocuments) > query.Count {
|
||||
projectedDocuments = projectedDocuments[:query.Count]
|
||||
}
|
||||
|
||||
return projectedDocuments
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) selectRow(selectItems []parsers.SelectItem, row RowType) interface{} {
|
||||
// When the first value is top level, select it instead
|
||||
if len(selectItems) > 0 && selectItems[0].IsTopLevel {
|
||||
return c.getFieldValue(selectItems[0], row)
|
||||
}
|
||||
|
||||
// Construct a new row based on the selected columns
|
||||
newRow := make(map[string]interface{})
|
||||
for index, column := range selectItems {
|
||||
destinationName := column.Alias
|
||||
if destinationName == "" {
|
||||
if len(column.Path) > 0 {
|
||||
destinationName = column.Path[len(column.Path)-1]
|
||||
} else {
|
||||
destinationName = fmt.Sprintf("$%d", index+1)
|
||||
}
|
||||
func resolveFrom(query parsers.SelectStmt, doc RowType) []rowContext {
|
||||
initialRow, gotParentContext := doc.(rowContext)
|
||||
if !gotParentContext {
|
||||
var initialTableName string
|
||||
if query.Table.SelectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
initialTableName = query.Table.SelectItem.Value.(parsers.SelectStmt).Table.Value
|
||||
}
|
||||
|
||||
newRow[destinationName] = c.getFieldValue(column, row)
|
||||
if initialTableName == "" {
|
||||
initialTableName = query.Table.Value
|
||||
}
|
||||
|
||||
initialRow = rowContext{
|
||||
parameters: query.Parameters,
|
||||
tables: map[string]RowType{
|
||||
initialTableName: doc,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return newRow
|
||||
if query.Table.SelectItem.Path != nil || query.Table.SelectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
destinationTableName := query.Table.SelectItem.Alias
|
||||
if destinationTableName == "" {
|
||||
destinationTableName = query.Table.Value
|
||||
}
|
||||
|
||||
selectValue := initialRow.parseArray(query.Table.SelectItem)
|
||||
rowContexts := make([]rowContext, len(selectValue))
|
||||
for i, newRowData := range selectValue {
|
||||
rowContexts[i].parameters = initialRow.parameters
|
||||
rowContexts[i].tables = copyMap(initialRow.tables)
|
||||
rowContexts[i].tables[destinationTableName] = newRowData
|
||||
}
|
||||
return rowContexts
|
||||
}
|
||||
|
||||
return []rowContext{initialRow}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) evaluateFilters(expr ExpressionType, row RowType) bool {
|
||||
if expr == nil {
|
||||
func (r rowContext) handleJoin(query parsers.SelectStmt) []rowContext {
|
||||
currentDocuments := []rowContext{r}
|
||||
|
||||
for _, joinItem := range query.JoinItems {
|
||||
nextDocuments := make([]rowContext, 0)
|
||||
for _, currentDocument := range currentDocuments {
|
||||
joinedItems := currentDocument.resolveJoinItemSelect(joinItem.SelectItem)
|
||||
for _, joinedItem := range joinedItems {
|
||||
tablesCopy := copyMap(currentDocument.tables)
|
||||
tablesCopy[joinItem.Table.Value] = joinedItem
|
||||
nextDocuments = append(nextDocuments, rowContext{
|
||||
parameters: currentDocument.parameters,
|
||||
tables: tablesCopy,
|
||||
})
|
||||
}
|
||||
}
|
||||
currentDocuments = nextDocuments
|
||||
}
|
||||
|
||||
return currentDocuments
|
||||
}
|
||||
|
||||
func (r rowContext) resolveJoinItemSelect(selectItem parsers.SelectItem) []RowType {
|
||||
if selectItem.Path != nil || selectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
selectValue := r.parseArray(selectItem)
|
||||
documents := make([]RowType, len(selectValue))
|
||||
for i, newRowData := range selectValue {
|
||||
documents[i] = newRowData
|
||||
}
|
||||
return documents
|
||||
}
|
||||
|
||||
return []RowType{}
|
||||
}
|
||||
|
||||
func (r rowContext) applyFilters(filters interface{}) bool {
|
||||
if filters == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
switch typedValue := expr.(type) {
|
||||
switch typedFilters := filters.(type) {
|
||||
case parsers.ComparisonExpression:
|
||||
leftValue := c.getExpressionParameterValue(typedValue.Left, row)
|
||||
rightValue := c.getExpressionParameterValue(typedValue.Right, row)
|
||||
|
||||
cmp := compareValues(leftValue, rightValue)
|
||||
switch typedValue.Operation {
|
||||
case "=":
|
||||
return cmp == 0
|
||||
case "!=":
|
||||
return cmp != 0
|
||||
case "<":
|
||||
return cmp < 0
|
||||
case ">":
|
||||
return cmp > 0
|
||||
case "<=":
|
||||
return cmp <= 0
|
||||
case ">=":
|
||||
return cmp >= 0
|
||||
}
|
||||
return r.filters_ComparisonExpression(typedFilters)
|
||||
case parsers.LogicalExpression:
|
||||
var result bool
|
||||
for i, expression := range typedValue.Expressions {
|
||||
expressionResult := c.evaluateFilters(expression, row)
|
||||
if i == 0 {
|
||||
result = expressionResult
|
||||
}
|
||||
|
||||
switch typedValue.Operation {
|
||||
case parsers.LogicalExpressionTypeAnd:
|
||||
result = result && expressionResult
|
||||
if !result {
|
||||
return false
|
||||
}
|
||||
case parsers.LogicalExpressionTypeOr:
|
||||
result = result || expressionResult
|
||||
if result {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
return r.filters_LogicalExpression(typedFilters)
|
||||
case parsers.Constant:
|
||||
if value, ok := typedValue.Value.(bool); ok {
|
||||
if value, ok := typedFilters.Value.(bool); ok {
|
||||
return value
|
||||
}
|
||||
return false
|
||||
case parsers.SelectItem:
|
||||
resolvedValue := c.getFieldValue(typedValue, row)
|
||||
resolvedValue := r.resolveSelectItem(typedFilters)
|
||||
if value, ok := resolvedValue.(bool); ok {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowType) interface{} {
|
||||
if field.Type == parsers.SelectItemTypeArray {
|
||||
arrayValue := make([]interface{}, 0)
|
||||
for _, selectItem := range field.SelectItems {
|
||||
arrayValue = append(arrayValue, c.getFieldValue(selectItem, row))
|
||||
}
|
||||
return arrayValue
|
||||
func (r rowContext) filters_ComparisonExpression(expression parsers.ComparisonExpression) bool {
|
||||
leftExpression, leftExpressionOk := expression.Left.(parsers.SelectItem)
|
||||
rightExpression, rightExpressionOk := expression.Right.(parsers.SelectItem)
|
||||
|
||||
if !leftExpressionOk || !rightExpressionOk {
|
||||
logger.ErrorLn("ComparisonExpression has incorrect Left or Right type")
|
||||
return false
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeObject {
|
||||
objectValue := make(map[string]interface{})
|
||||
for _, selectItem := range field.SelectItems {
|
||||
objectValue[selectItem.Alias] = c.getFieldValue(selectItem, row)
|
||||
}
|
||||
return objectValue
|
||||
leftValue := r.resolveSelectItem(leftExpression)
|
||||
rightValue := r.resolveSelectItem(rightExpression)
|
||||
|
||||
cmp := compareValues(leftValue, rightValue)
|
||||
switch expression.Operation {
|
||||
case "=":
|
||||
return cmp == 0
|
||||
case "!=":
|
||||
return cmp != 0
|
||||
case "<":
|
||||
return cmp < 0
|
||||
case ">":
|
||||
return cmp > 0
|
||||
case "<=":
|
||||
return cmp <= 0
|
||||
case ">=":
|
||||
return cmp >= 0
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeConstant {
|
||||
var typedValue parsers.Constant
|
||||
var ok bool
|
||||
if typedValue, ok = field.Value.(parsers.Constant); !ok {
|
||||
// TODO: Handle error
|
||||
fmt.Println("parsers.Constant has incorrect Value type")
|
||||
return false
|
||||
}
|
||||
|
||||
func (r rowContext) filters_LogicalExpression(expression parsers.LogicalExpression) bool {
|
||||
var result bool
|
||||
for i, subExpression := range expression.Expressions {
|
||||
expressionResult := r.applyFilters(subExpression)
|
||||
if i == 0 {
|
||||
result = expressionResult
|
||||
}
|
||||
|
||||
if typedValue.Type == parsers.ConstantTypeParameterConstant &&
|
||||
c.parameters != nil {
|
||||
if key, ok := typedValue.Value.(string); ok {
|
||||
return c.parameters[key]
|
||||
switch expression.Operation {
|
||||
case parsers.LogicalExpressionTypeAnd:
|
||||
result = result && expressionResult
|
||||
if !result {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return typedValue.Value
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeFunctionCall {
|
||||
var typedValue parsers.FunctionCall
|
||||
var ok bool
|
||||
if typedValue, ok = field.Value.(parsers.FunctionCall); !ok {
|
||||
// TODO: Handle error
|
||||
fmt.Println("parsers.Constant has incorrect Value type")
|
||||
}
|
||||
|
||||
switch typedValue.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return c.strings_StringEquals(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallContains:
|
||||
return c.strings_Contains(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallEndsWith:
|
||||
return c.strings_EndsWith(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallStartsWith:
|
||||
return c.strings_StartsWith(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallConcat:
|
||||
return c.strings_Concat(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIndexOf:
|
||||
return c.strings_IndexOf(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallToString:
|
||||
return c.strings_ToString(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallUpper:
|
||||
return c.strings_Upper(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallLower:
|
||||
return c.strings_Lower(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallLeft:
|
||||
return c.strings_Left(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallLength:
|
||||
return c.strings_Length(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallLTrim:
|
||||
return c.strings_LTrim(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallReplace:
|
||||
return c.strings_Replace(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallReplicate:
|
||||
return c.strings_Replicate(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallReverse:
|
||||
return c.strings_Reverse(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallRight:
|
||||
return c.strings_Right(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallRTrim:
|
||||
return c.strings_RTrim(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallSubstring:
|
||||
return c.strings_Substring(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallTrim:
|
||||
return c.strings_Trim(typedValue.Arguments, row)
|
||||
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return c.typeChecking_IsDefined(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsArray:
|
||||
return c.typeChecking_IsArray(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsBool:
|
||||
return c.typeChecking_IsBool(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsFiniteNumber:
|
||||
return c.typeChecking_IsFiniteNumber(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsInteger:
|
||||
return c.typeChecking_IsInteger(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsNull:
|
||||
return c.typeChecking_IsNull(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsNumber:
|
||||
return c.typeChecking_IsNumber(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsObject:
|
||||
return c.typeChecking_IsObject(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsPrimitive:
|
||||
return c.typeChecking_IsPrimitive(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallIsString:
|
||||
return c.typeChecking_IsString(typedValue.Arguments, row)
|
||||
|
||||
case parsers.FunctionCallArrayConcat:
|
||||
return c.array_Concat(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallArrayLength:
|
||||
return c.array_Length(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallArraySlice:
|
||||
return c.array_Slice(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallSetIntersect:
|
||||
return c.set_Intersect(typedValue.Arguments, row)
|
||||
case parsers.FunctionCallSetUnion:
|
||||
return c.set_Union(typedValue.Arguments, row)
|
||||
|
||||
case parsers.FunctionCallIn:
|
||||
return c.misc_In(typedValue.Arguments, row)
|
||||
}
|
||||
}
|
||||
|
||||
value := row
|
||||
if len(field.Path) > 1 {
|
||||
for _, pathSegment := range field.Path[1:] {
|
||||
if nestedValue, ok := value.(map[string]interface{}); ok {
|
||||
value = nestedValue[pathSegment]
|
||||
} else {
|
||||
return nil
|
||||
case parsers.LogicalExpressionTypeOr:
|
||||
result = result || expressionResult
|
||||
if result {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return value
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) getExpressionParameterValue(
|
||||
parameter interface{},
|
||||
row RowType,
|
||||
) interface{} {
|
||||
switch typedParameter := parameter.(type) {
|
||||
case parsers.SelectItem:
|
||||
return c.getFieldValue(typedParameter, row)
|
||||
}
|
||||
|
||||
fmt.Println("getExpressionParameterValue - got incorrect parameter type")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) orderBy(orderBy []parsers.OrderExpression, data []RowType) {
|
||||
func applyOrder(documents []rowContext, orderExpressions []parsers.OrderExpression) {
|
||||
less := func(i, j int) bool {
|
||||
for _, order := range orderBy {
|
||||
val1 := c.getFieldValue(order.SelectItem, data[i])
|
||||
val2 := c.getFieldValue(order.SelectItem, data[j])
|
||||
for _, order := range orderExpressions {
|
||||
val1 := documents[i].resolveSelectItem(order.SelectItem)
|
||||
val2 := documents[j].resolveSelectItem(order.SelectItem)
|
||||
|
||||
cmp := compareValues(val1, val2)
|
||||
if cmp != 0 {
|
||||
@@ -304,7 +239,392 @@ func (c memoryExecutorContext) orderBy(orderBy []parsers.OrderExpression, data [
|
||||
return i < j
|
||||
}
|
||||
|
||||
sort.SliceStable(data, less)
|
||||
sort.SliceStable(documents, less)
|
||||
}
|
||||
|
||||
func applyGroupBy(documents []rowContext, groupBy []parsers.SelectItem) []rowContext {
|
||||
groupedRows := make(map[string][]rowContext)
|
||||
groupedKeys := make([]string, 0)
|
||||
|
||||
for _, row := range documents {
|
||||
key := row.generateGroupByKey(groupBy)
|
||||
if _, ok := groupedRows[key]; !ok {
|
||||
groupedKeys = append(groupedKeys, key)
|
||||
}
|
||||
groupedRows[key] = append(groupedRows[key], row)
|
||||
}
|
||||
|
||||
grouppedRows := make([]rowContext, 0)
|
||||
for _, key := range groupedKeys {
|
||||
grouppedRowContext := rowContext{
|
||||
tables: groupedRows[key][0].tables,
|
||||
parameters: groupedRows[key][0].parameters,
|
||||
grouppedRows: groupedRows[key],
|
||||
}
|
||||
grouppedRows = append(grouppedRows, grouppedRowContext)
|
||||
}
|
||||
|
||||
return grouppedRows
|
||||
}
|
||||
|
||||
func (r rowContext) generateGroupByKey(groupBy []parsers.SelectItem) string {
|
||||
var keyBuilder strings.Builder
|
||||
for _, selectItem := range groupBy {
|
||||
value := r.resolveSelectItem(selectItem)
|
||||
keyBuilder.WriteString(fmt.Sprintf("%v", value))
|
||||
keyBuilder.WriteString(":")
|
||||
}
|
||||
return keyBuilder.String()
|
||||
}
|
||||
|
||||
func applyProjection(documents []rowContext, selectItems []parsers.SelectItem, groupBy []parsers.SelectItem) []RowType {
|
||||
if len(documents) == 0 {
|
||||
return []RowType{}
|
||||
}
|
||||
|
||||
if hasAggregateFunctions(selectItems) && len(groupBy) == 0 {
|
||||
// When can have aggregate functions without GROUP BY clause,
|
||||
// we should aggregate all rows in that case
|
||||
rowContext := rowContext{
|
||||
tables: documents[0].tables,
|
||||
parameters: documents[0].parameters,
|
||||
grouppedRows: documents,
|
||||
}
|
||||
return []RowType{rowContext.applyProjection(selectItems)}
|
||||
}
|
||||
|
||||
projectedDocuments := make([]RowType, len(documents))
|
||||
for index, row := range documents {
|
||||
projectedDocuments[index] = row.applyProjection(selectItems)
|
||||
}
|
||||
|
||||
return projectedDocuments
|
||||
}
|
||||
|
||||
func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
|
||||
// When the first value is top level, select it instead
|
||||
if len(selectItems) > 0 && selectItems[0].IsTopLevel {
|
||||
return r.resolveSelectItem(selectItems[0])
|
||||
}
|
||||
|
||||
// Construct a new row based on the selected columns
|
||||
row := make(map[string]interface{})
|
||||
for index, selectItem := range selectItems {
|
||||
destinationName := selectItem.Alias
|
||||
if destinationName == "" {
|
||||
if len(selectItem.Path) > 0 {
|
||||
destinationName = selectItem.Path[len(selectItem.Path)-1]
|
||||
} else {
|
||||
destinationName = fmt.Sprintf("$%d", index+1)
|
||||
}
|
||||
|
||||
if destinationName[0] == '@' {
|
||||
destinationName = r.parameters[destinationName].(string)
|
||||
}
|
||||
}
|
||||
|
||||
row[destinationName] = r.resolveSelectItem(selectItem)
|
||||
}
|
||||
|
||||
return row
|
||||
}
|
||||
|
||||
func (r rowContext) resolveSelectItem(selectItem parsers.SelectItem) interface{} {
|
||||
if selectItem.Type == parsers.SelectItemTypeArray {
|
||||
return r.selectItem_SelectItemTypeArray(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeObject {
|
||||
return r.selectItem_SelectItemTypeObject(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeConstant {
|
||||
return r.selectItem_SelectItemTypeConstant(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
return r.selectItem_SelectItemTypeSubQuery(selectItem)
|
||||
}
|
||||
|
||||
if selectItem.Type == parsers.SelectItemTypeFunctionCall {
|
||||
if typedFunctionCall, ok := selectItem.Value.(parsers.FunctionCall); ok {
|
||||
return r.selectItem_SelectItemTypeFunctionCall(typedFunctionCall)
|
||||
}
|
||||
|
||||
logger.ErrorLn("parsers.SelectItem has incorrect Value type (expected parsers.FunctionCall)")
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.selectItem_SelectItemTypeField(selectItem)
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeArray(selectItem parsers.SelectItem) interface{} {
|
||||
arrayValue := make([]interface{}, 0)
|
||||
for _, subSelectItem := range selectItem.SelectItems {
|
||||
arrayValue = append(arrayValue, r.resolveSelectItem(subSelectItem))
|
||||
}
|
||||
return arrayValue
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeObject(selectItem parsers.SelectItem) interface{} {
|
||||
objectValue := make(map[string]interface{})
|
||||
for _, subSelectItem := range selectItem.SelectItems {
|
||||
objectValue[subSelectItem.Alias] = r.resolveSelectItem(subSelectItem)
|
||||
}
|
||||
return objectValue
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeConstant(selectItem parsers.SelectItem) interface{} {
|
||||
var typedValue parsers.Constant
|
||||
var ok bool
|
||||
if typedValue, ok = selectItem.Value.(parsers.Constant); !ok {
|
||||
// TODO: Handle error
|
||||
logger.ErrorLn("parsers.Constant has incorrect Value type")
|
||||
}
|
||||
|
||||
if typedValue.Type == parsers.ConstantTypeParameterConstant &&
|
||||
r.parameters != nil {
|
||||
if key, ok := typedValue.Value.(string); ok {
|
||||
return r.parameters[key]
|
||||
}
|
||||
}
|
||||
|
||||
return typedValue.Value
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeSubQuery(selectItem parsers.SelectItem) interface{} {
|
||||
subQuery := selectItem.Value.(parsers.SelectStmt)
|
||||
subQueryResult := ExecuteQuery(
|
||||
subQuery,
|
||||
[]RowType{r},
|
||||
)
|
||||
|
||||
if subQuery.Exists {
|
||||
return len(subQueryResult) > 0
|
||||
}
|
||||
|
||||
return subQueryResult
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeFunctionCall(functionCall parsers.FunctionCall) interface{} {
|
||||
switch functionCall.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return r.strings_StringEquals(functionCall.Arguments)
|
||||
case parsers.FunctionCallContains:
|
||||
return r.strings_Contains(functionCall.Arguments)
|
||||
case parsers.FunctionCallEndsWith:
|
||||
return r.strings_EndsWith(functionCall.Arguments)
|
||||
case parsers.FunctionCallStartsWith:
|
||||
return r.strings_StartsWith(functionCall.Arguments)
|
||||
case parsers.FunctionCallConcat:
|
||||
return r.strings_Concat(functionCall.Arguments)
|
||||
case parsers.FunctionCallIndexOf:
|
||||
return r.strings_IndexOf(functionCall.Arguments)
|
||||
case parsers.FunctionCallToString:
|
||||
return r.strings_ToString(functionCall.Arguments)
|
||||
case parsers.FunctionCallUpper:
|
||||
return r.strings_Upper(functionCall.Arguments)
|
||||
case parsers.FunctionCallLower:
|
||||
return r.strings_Lower(functionCall.Arguments)
|
||||
case parsers.FunctionCallLeft:
|
||||
return r.strings_Left(functionCall.Arguments)
|
||||
case parsers.FunctionCallLength:
|
||||
return r.strings_Length(functionCall.Arguments)
|
||||
case parsers.FunctionCallLTrim:
|
||||
return r.strings_LTrim(functionCall.Arguments)
|
||||
case parsers.FunctionCallReplace:
|
||||
return r.strings_Replace(functionCall.Arguments)
|
||||
case parsers.FunctionCallReplicate:
|
||||
return r.strings_Replicate(functionCall.Arguments)
|
||||
case parsers.FunctionCallReverse:
|
||||
return r.strings_Reverse(functionCall.Arguments)
|
||||
case parsers.FunctionCallRight:
|
||||
return r.strings_Right(functionCall.Arguments)
|
||||
case parsers.FunctionCallRTrim:
|
||||
return r.strings_RTrim(functionCall.Arguments)
|
||||
case parsers.FunctionCallSubstring:
|
||||
return r.strings_Substring(functionCall.Arguments)
|
||||
case parsers.FunctionCallTrim:
|
||||
return r.strings_Trim(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return r.typeChecking_IsDefined(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsArray:
|
||||
return r.typeChecking_IsArray(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsBool:
|
||||
return r.typeChecking_IsBool(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsFiniteNumber:
|
||||
return r.typeChecking_IsFiniteNumber(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsInteger:
|
||||
return r.typeChecking_IsInteger(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsNull:
|
||||
return r.typeChecking_IsNull(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsNumber:
|
||||
return r.typeChecking_IsNumber(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsObject:
|
||||
return r.typeChecking_IsObject(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsPrimitive:
|
||||
return r.typeChecking_IsPrimitive(functionCall.Arguments)
|
||||
case parsers.FunctionCallIsString:
|
||||
return r.typeChecking_IsString(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallArrayConcat:
|
||||
return r.array_Concat(functionCall.Arguments)
|
||||
case parsers.FunctionCallArrayContains:
|
||||
return r.array_Contains(functionCall.Arguments)
|
||||
case parsers.FunctionCallArrayContainsAny:
|
||||
return r.array_Contains_Any(functionCall.Arguments)
|
||||
case parsers.FunctionCallArrayContainsAll:
|
||||
return r.array_Contains_All(functionCall.Arguments)
|
||||
case parsers.FunctionCallArrayLength:
|
||||
return r.array_Length(functionCall.Arguments)
|
||||
case parsers.FunctionCallArraySlice:
|
||||
return r.array_Slice(functionCall.Arguments)
|
||||
case parsers.FunctionCallSetIntersect:
|
||||
return r.set_Intersect(functionCall.Arguments)
|
||||
case parsers.FunctionCallSetUnion:
|
||||
return r.set_Union(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallMathAbs:
|
||||
return r.math_Abs(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAcos:
|
||||
return r.math_Acos(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAsin:
|
||||
return r.math_Asin(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAtan:
|
||||
return r.math_Atan(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathCeiling:
|
||||
return r.math_Ceiling(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathCos:
|
||||
return r.math_Cos(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathCot:
|
||||
return r.math_Cot(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathDegrees:
|
||||
return r.math_Degrees(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathExp:
|
||||
return r.math_Exp(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathFloor:
|
||||
return r.math_Floor(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitNot:
|
||||
return r.math_IntBitNot(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathLog10:
|
||||
return r.math_Log10(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathRadians:
|
||||
return r.math_Radians(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathRound:
|
||||
return r.math_Round(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSign:
|
||||
return r.math_Sign(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSin:
|
||||
return r.math_Sin(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSqrt:
|
||||
return r.math_Sqrt(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathSquare:
|
||||
return r.math_Square(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathTan:
|
||||
return r.math_Tan(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathTrunc:
|
||||
return r.math_Trunc(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathAtn2:
|
||||
return r.math_Atn2(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntAdd:
|
||||
return r.math_IntAdd(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitAnd:
|
||||
return r.math_IntBitAnd(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitLeftShift:
|
||||
return r.math_IntBitLeftShift(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitOr:
|
||||
return r.math_IntBitOr(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitRightShift:
|
||||
return r.math_IntBitRightShift(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntBitXor:
|
||||
return r.math_IntBitXor(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntDiv:
|
||||
return r.math_IntDiv(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntMod:
|
||||
return r.math_IntMod(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntMul:
|
||||
return r.math_IntMul(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathIntSub:
|
||||
return r.math_IntSub(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathPower:
|
||||
return r.math_Power(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathLog:
|
||||
return r.math_Log(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathNumberBin:
|
||||
return r.math_NumberBin(functionCall.Arguments)
|
||||
case parsers.FunctionCallMathPi:
|
||||
return r.math_Pi()
|
||||
case parsers.FunctionCallMathRand:
|
||||
return r.math_Rand()
|
||||
|
||||
case parsers.FunctionCallAggregateAvg:
|
||||
return r.aggregate_Avg(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateCount:
|
||||
return r.aggregate_Count(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateMax:
|
||||
return r.aggregate_Max(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateMin:
|
||||
return r.aggregate_Min(functionCall.Arguments)
|
||||
case parsers.FunctionCallAggregateSum:
|
||||
return r.aggregate_Sum(functionCall.Arguments)
|
||||
|
||||
case parsers.FunctionCallIn:
|
||||
return r.misc_In(functionCall.Arguments)
|
||||
}
|
||||
|
||||
logger.Errorf("Unknown function call type: %v", functionCall.Type)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem) interface{} {
|
||||
value := r.tables[selectItem.Path[0]]
|
||||
|
||||
if len(selectItem.Path) > 1 {
|
||||
for _, pathSegment := range selectItem.Path[1:] {
|
||||
if pathSegment[0] == '@' {
|
||||
pathSegment = r.parameters[pathSegment].(string)
|
||||
}
|
||||
|
||||
switch nestedValue := value.(type) {
|
||||
case map[string]interface{}:
|
||||
value = nestedValue[pathSegment]
|
||||
case map[string]RowType:
|
||||
value = nestedValue[pathSegment]
|
||||
case []int, []string, []interface{}:
|
||||
slice := reflect.ValueOf(nestedValue)
|
||||
if arrayIndex, err := strconv.Atoi(pathSegment); err == nil && slice.Len() > arrayIndex {
|
||||
value = slice.Index(arrayIndex).Interface()
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func hasAggregateFunctions(selectItems []parsers.SelectItem) bool {
|
||||
if selectItems == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, selectItem := range selectItems {
|
||||
if selectItem.Type == parsers.SelectItemTypeFunctionCall {
|
||||
if typedValue, ok := selectItem.Value.(parsers.FunctionCall); ok && slices.Contains[[]parsers.FunctionCallType](parsers.AggregateFunctions, typedValue.Type) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if hasAggregateFunctions(selectItem.SelectItems) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func compareValues(val1, val2 interface{}) int {
|
||||
@@ -349,3 +669,34 @@ func compareValues(val1, val2 interface{}) int {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func deduplicate[T RowType | interface{}](slice []T) []T {
|
||||
var result []T
|
||||
result = make([]T, 0)
|
||||
|
||||
for i := 0; i < len(slice); i++ {
|
||||
unique := true
|
||||
for j := 0; j < len(result); j++ {
|
||||
if compareValues(slice[i], result[j]) == 0 {
|
||||
unique = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if unique {
|
||||
result = append(result, slice[i])
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func copyMap[T RowType | []RowType](originalMap map[string]T) map[string]T {
|
||||
targetMap := make(map[string]T)
|
||||
|
||||
for k, v := range originalMap {
|
||||
targetMap[k] = v
|
||||
}
|
||||
|
||||
return targetMap
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) misc_In(arguments []interface{}, row RowType) bool {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) misc_In(arguments []interface{}) bool {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
|
||||
for i := 1; i < len(arguments); i++ {
|
||||
compareValue := c.getFieldValue(arguments[i].(parsers.SelectItem), row)
|
||||
compareValue := r.resolveSelectItem(arguments[i].(parsers.SelectItem))
|
||||
if compareValues(value, compareValue) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func testQueryExecute(
|
||||
@@ -14,7 +15,7 @@ func testQueryExecute(
|
||||
data []memoryexecutor.RowType,
|
||||
expectedData []memoryexecutor.RowType,
|
||||
) {
|
||||
result := memoryexecutor.Execute(query, data)
|
||||
result := memoryexecutor.ExecuteQuery(query, data)
|
||||
|
||||
if !reflect.DeepEqual(result, expectedData) {
|
||||
t.Errorf("execution result does not match expected data.\nExpected: %+v\nGot: %+v", expectedData, result)
|
||||
@@ -25,8 +26,20 @@ func Test_Execute(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "12345", "pk": 123, "_self": "self1", "_rid": "rid1", "_ts": 123456, "isCool": false},
|
||||
map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
||||
map[string]interface{}{"id": "456", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
||||
map[string]interface{}{"id": "123", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
||||
map[string]interface{}{
|
||||
"id": "456", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-a"},
|
||||
{"name": "tag-b"},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"id": "123", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true,
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-b"},
|
||||
{"name": "tag-c"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Should execute SELECT with ORDER BY", func(t *testing.T) {
|
||||
@@ -59,6 +72,26 @@ func Test_Execute(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT with GROUP BY", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"pk": 123},
|
||||
map[string]interface{}{"pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute IN function", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
@@ -79,20 +112,8 @@ func Test_Execute(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "456",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -104,4 +125,31 @@ func Test_Execute(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute IN selector", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "name"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"name": "tag-a"},
|
||||
map[string]interface{}{"name": "tag-b"},
|
||||
map[string]interface{}{"name": "tag-b"},
|
||||
map[string]interface{}{"name": "tag-c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -35,6 +35,47 @@ func Test_Execute_Select(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT with query parameters as accessor", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "@param"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Parameters: map[string]interface{}{
|
||||
"@param": "pk",
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "12345", "pk": 123},
|
||||
map[string]interface{}{"id": "67890", "pk": 456},
|
||||
map[string]interface{}{"id": "456", "pk": 456},
|
||||
map[string]interface{}{"id": "123", "pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT DISTINCT", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Distinct: true,
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"pk": 123},
|
||||
map[string]interface{}{"pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT TOP", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
@@ -53,6 +94,32 @@ func Test_Execute_Select(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT OFFSET", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Count: 2,
|
||||
Offset: 1,
|
||||
OrderExpressions: []parsers.OrderExpression{
|
||||
{
|
||||
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Direction: parsers.OrderDirectionDesc,
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "67890", "pk": 456},
|
||||
map[string]interface{}{"id": "456", "pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute SELECT VALUE", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
|
||||
@@ -4,13 +4,14 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) strings_StringEquals(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_StringEquals(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
return strings.EqualFold(str1, str2)
|
||||
@@ -19,10 +20,10 @@ func (c memoryExecutorContext) strings_StringEquals(arguments []interface{}, row
|
||||
return str1 == str2
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Contains(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_Contains(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
@@ -32,10 +33,10 @@ func (c memoryExecutorContext) strings_Contains(arguments []interface{}, row Row
|
||||
return strings.Contains(str1, str2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_EndsWith(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_EndsWith(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
@@ -45,10 +46,10 @@ func (c memoryExecutorContext) strings_EndsWith(arguments []interface{}, row Row
|
||||
return strings.HasSuffix(str1, str2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_StartsWith(arguments []interface{}, row RowType) bool {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
ignoreCase := c.getBoolFlag(arguments, row)
|
||||
func (r rowContext) strings_StartsWith(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
@@ -58,12 +59,12 @@ func (c memoryExecutorContext) strings_StartsWith(arguments []interface{}, row R
|
||||
return strings.HasPrefix(str1, str2)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Concat(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Concat(arguments []interface{}) string {
|
||||
result := ""
|
||||
|
||||
for _, arg := range arguments {
|
||||
if selectItem, ok := arg.(parsers.SelectItem); ok {
|
||||
value := c.getFieldValue(selectItem, row)
|
||||
value := r.resolveSelectItem(selectItem)
|
||||
result += convertToString(value)
|
||||
}
|
||||
}
|
||||
@@ -71,13 +72,13 @@ func (c memoryExecutorContext) strings_Concat(arguments []interface{}, row RowTy
|
||||
return result
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_IndexOf(arguments []interface{}, row RowType) int {
|
||||
str1 := c.parseString(arguments[0], row)
|
||||
str2 := c.parseString(arguments[1], row)
|
||||
func (r rowContext) strings_IndexOf(arguments []interface{}) int {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
|
||||
start := 0
|
||||
if len(arguments) > 2 && arguments[2] != nil {
|
||||
if startPos, ok := c.getFieldValue(arguments[2].(parsers.SelectItem), row).(int); ok {
|
||||
if startPos, ok := r.resolveSelectItem(arguments[2].(parsers.SelectItem)).(int); ok {
|
||||
start = startPos
|
||||
}
|
||||
}
|
||||
@@ -96,29 +97,29 @@ func (c memoryExecutorContext) strings_IndexOf(arguments []interface{}, row RowT
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_ToString(arguments []interface{}, row RowType) string {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) strings_ToString(arguments []interface{}) string {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
return convertToString(value)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Upper(arguments []interface{}, row RowType) string {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) strings_Upper(arguments []interface{}) string {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
return strings.ToUpper(convertToString(value))
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Lower(arguments []interface{}, row RowType) string {
|
||||
value := c.getFieldValue(arguments[0].(parsers.SelectItem), row)
|
||||
func (r rowContext) strings_Lower(arguments []interface{}) string {
|
||||
value := r.resolveSelectItem(arguments[0].(parsers.SelectItem))
|
||||
return strings.ToLower(convertToString(value))
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Left(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Left(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var length int
|
||||
str := c.parseString(arguments[0], row)
|
||||
lengthEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
lengthEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
fmt.Println("strings_Left - got parameters of wrong type")
|
||||
logger.ErrorLn("strings_Left - got parameters of wrong type")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -133,31 +134,31 @@ func (c memoryExecutorContext) strings_Left(arguments []interface{}, row RowType
|
||||
return str[:length]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Length(arguments []interface{}, row RowType) int {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_Length(arguments []interface{}) int {
|
||||
str := r.parseString(arguments[0])
|
||||
return len(str)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_LTrim(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_LTrim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
return strings.TrimLeft(str, " ")
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Replace(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
oldStr := c.parseString(arguments[1], row)
|
||||
newStr := c.parseString(arguments[2], row)
|
||||
func (r rowContext) strings_Replace(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
oldStr := r.parseString(arguments[1])
|
||||
newStr := r.parseString(arguments[2])
|
||||
return strings.Replace(str, oldStr, newStr, -1)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Replicate(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Replicate(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var times int
|
||||
str := c.parseString(arguments[0], row)
|
||||
timesEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
timesEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if times, ok = timesEx.(int); !ok {
|
||||
fmt.Println("strings_Replicate - got parameters of wrong type")
|
||||
logger.ErrorLn("strings_Replicate - got parameters of wrong type")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -172,8 +173,8 @@ func (c memoryExecutorContext) strings_Replicate(arguments []interface{}, row Ro
|
||||
return strings.Repeat(str, times)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Reverse(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_Reverse(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
runes := []rune(str)
|
||||
|
||||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
||||
@@ -183,14 +184,14 @@ func (c memoryExecutorContext) strings_Reverse(arguments []interface{}, row RowT
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Right(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Right(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var length int
|
||||
str := c.parseString(arguments[0], row)
|
||||
lengthEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
lengthEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
fmt.Println("strings_Right - got parameters of wrong type")
|
||||
logger.ErrorLn("strings_Right - got parameters of wrong type")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -205,25 +206,25 @@ func (c memoryExecutorContext) strings_Right(arguments []interface{}, row RowTyp
|
||||
return str[len(str)-length:]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_RTrim(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_RTrim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
return strings.TrimRight(str, " ")
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Substring(arguments []interface{}, row RowType) string {
|
||||
func (r rowContext) strings_Substring(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var startPos int
|
||||
var length int
|
||||
str := c.parseString(arguments[0], row)
|
||||
startPosEx := c.getFieldValue(arguments[1].(parsers.SelectItem), row)
|
||||
lengthEx := c.getFieldValue(arguments[2].(parsers.SelectItem), row)
|
||||
str := r.parseString(arguments[0])
|
||||
startPosEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
lengthEx := r.resolveSelectItem(arguments[2].(parsers.SelectItem))
|
||||
|
||||
if startPos, ok = startPosEx.(int); !ok {
|
||||
fmt.Println("strings_Substring - got start parameters of wrong type")
|
||||
logger.ErrorLn("strings_Substring - got start parameters of wrong type")
|
||||
return ""
|
||||
}
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
fmt.Println("strings_Substring - got length parameters of wrong type")
|
||||
logger.ErrorLn("strings_Substring - got length parameters of wrong type")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -239,16 +240,16 @@ func (c memoryExecutorContext) strings_Substring(arguments []interface{}, row Ro
|
||||
return str[startPos:endPos]
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) strings_Trim(arguments []interface{}, row RowType) string {
|
||||
str := c.parseString(arguments[0], row)
|
||||
func (r rowContext) strings_Trim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) getBoolFlag(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) getBoolFlag(arguments []interface{}) bool {
|
||||
ignoreCase := false
|
||||
if len(arguments) > 2 && arguments[2] != nil {
|
||||
ignoreCaseItem := arguments[2].(parsers.SelectItem)
|
||||
if value, ok := c.getFieldValue(ignoreCaseItem, row).(bool); ok {
|
||||
if value, ok := r.resolveSelectItem(ignoreCaseItem).(bool); ok {
|
||||
ignoreCase = value
|
||||
}
|
||||
}
|
||||
@@ -256,14 +257,14 @@ func (c memoryExecutorContext) getBoolFlag(arguments []interface{}, row RowType)
|
||||
return ignoreCase
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) parseString(argument interface{}, row RowType) string {
|
||||
func (r rowContext) parseString(argument interface{}) string {
|
||||
exItem := argument.(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
if str1, ok := ex.(string); ok {
|
||||
return str1
|
||||
}
|
||||
|
||||
fmt.Println("StringEquals got parameters of wrong type")
|
||||
logger.ErrorLn("StringEquals got parameters of wrong type")
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_StringFunctions(t *testing.T) {
|
||||
@@ -33,20 +34,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "aaa",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("aaa"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -81,13 +70,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "aaa",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("aaa"),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
@@ -119,20 +102,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: " ",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: 123,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String(" "),
|
||||
testutils.SelectItem_Constant_Int(123),
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
@@ -171,20 +142,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "2",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("2"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -219,20 +178,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "3",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("3"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -267,20 +214,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "1",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("1"),
|
||||
testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -315,20 +250,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "str"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "o",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 4,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("o"),
|
||||
testutils.SelectItem_Constant_Int(4),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -397,13 +320,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "str"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 3,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -506,20 +423,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "str"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "world",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "universe",
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("world"),
|
||||
testutils.SelectItem_Constant_String("universe"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -554,13 +459,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "str"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 3,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -629,13 +528,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "str"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 3,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(3),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -704,20 +597,8 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
Path: []string{"c", "str"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeInteger,
|
||||
Value: 4,
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_Int(2),
|
||||
testutils.SelectItem_Constant_Int(4),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
155
query_executors/memory_executor/subquery_test.go
Normal file
155
query_executors/memory_executor/subquery_test.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_SubQuery(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "info": map[string]interface{}{"name": "row-1"}},
|
||||
map[string]interface{}{
|
||||
"id": "456",
|
||||
"info": map[string]interface{}{"name": "row-2"},
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-a"},
|
||||
{"name": "tag-b"},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"id": "789",
|
||||
"info": map[string]interface{}{"name": "row-3"},
|
||||
"tags": []map[string]interface{}{
|
||||
{"name": "tag-b"},
|
||||
{"name": "tag-c"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Should execute FROM subquery", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "c",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
Table: parsers.Table{Value: "cc"},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"name": "row-1"},
|
||||
map[string]interface{}{"name": "row-2"},
|
||||
map[string]interface{}{"name": "row-3"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute JOIN subquery", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
Value: "cc",
|
||||
},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "cc",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "456", "name": "tag-a"},
|
||||
map[string]interface{}{"id": "456", "name": "tag-b"},
|
||||
map[string]interface{}{"id": "789", "name": "tag-b"},
|
||||
map[string]interface{}{"id": "789", "name": "tag-c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute JOIN EXISTS subquery", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{Value: "hasTags"},
|
||||
SelectItem: parsers.SelectItem{
|
||||
Alias: "hasTags",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
IsTopLevel: true,
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
},
|
||||
Exists: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Filters: parsers.SelectItem{
|
||||
Path: []string{"hasTags"},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "456"},
|
||||
map[string]interface{}{"id": "789"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -6,32 +6,32 @@ import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsDefined(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsDefined(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
return ex != nil
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsArray(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsArray(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isArray := ex.([]interface{})
|
||||
return isArray
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsBool(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsBool(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isBool := ex.(bool)
|
||||
return isBool
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsFiniteNumber(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsFiniteNumber(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch num := ex.(type) {
|
||||
case int:
|
||||
@@ -43,41 +43,41 @@ func (c memoryExecutorContext) typeChecking_IsFiniteNumber(arguments []interface
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsInteger(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsInteger(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isInt := ex.(int)
|
||||
return isInt
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsNull(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsNull(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
return ex == nil
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsNumber(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsNumber(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isFloat := ex.(float64)
|
||||
_, isInt := ex.(int)
|
||||
return isFloat || isInt
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsObject(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsObject(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isObject := ex.(map[string]interface{})
|
||||
return isObject
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsPrimitive(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsPrimitive(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
switch ex.(type) {
|
||||
case bool, string, float64, int, nil:
|
||||
@@ -87,9 +87,9 @@ func (c memoryExecutorContext) typeChecking_IsPrimitive(arguments []interface{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c memoryExecutorContext) typeChecking_IsString(arguments []interface{}, row RowType) bool {
|
||||
func (r rowContext) typeChecking_IsString(arguments []interface{}) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := c.getFieldValue(exItem, row)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
|
||||
_, isStr := ex.(string)
|
||||
return isStr
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_Where(t *testing.T) {
|
||||
@@ -26,10 +27,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
@@ -52,10 +50,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeParameterConstant, Value: "@param_id"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Parameter("@param_id"),
|
||||
},
|
||||
Parameters: map[string]interface{}{
|
||||
"@param_id": "456",
|
||||
@@ -83,18 +78,12 @@ func Test_Execute_Where(t *testing.T) {
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "67890"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_String("67890"),
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "pk"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeInteger, Value: 456},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Int(456),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -120,10 +109,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: true},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeOr,
|
||||
@@ -131,18 +117,12 @@ func Test_Execute_Where(t *testing.T) {
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "123"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_String("123"),
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{Type: parsers.ConstantTypeString, Value: "456"},
|
||||
},
|
||||
Right: testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
89
sharedlibrary/collections.go
Normal file
89
sharedlibrary/collections.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
//export CreateCollection
|
||||
func CreateCollection(serverName *C.char, databaseId *C.char, collectionJson *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionStr := C.GoString(collectionJson)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
var collection repositorymodels.Collection
|
||||
err := json.Unmarshal([]byte(collectionStr), &collection)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
|
||||
_, code := serverInstance.repository.CreateCollection(databaseIdStr, collection)
|
||||
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
|
||||
//export GetCollection
|
||||
func GetCollection(serverName *C.char, databaseId *C.char, collectionId *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
collection, code := serverInstance.repository.GetCollection(databaseIdStr, collectionIdStr)
|
||||
if code != repositorymodels.StatusOk {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
collectionJson, _ := json.Marshal(collection)
|
||||
return C.CString(string(collectionJson))
|
||||
}
|
||||
|
||||
//export GetAllCollections
|
||||
func GetAllCollections(serverName *C.char, databaseId *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
collections, code := serverInstance.repository.GetAllCollections(databaseIdStr)
|
||||
if code != repositorymodels.StatusOk {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
collectionsJson, _ := json.Marshal(collections)
|
||||
return C.CString(string(collectionsJson))
|
||||
}
|
||||
|
||||
//export DeleteCollection
|
||||
func DeleteCollection(serverName *C.char, databaseId *C.char, collectionId *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
code := serverInstance.repository.DeleteCollection(databaseIdStr, collectionIdStr)
|
||||
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
89
sharedlibrary/databases.go
Normal file
89
sharedlibrary/databases.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
//export CreateDatabase
|
||||
func CreateDatabase(serverName *C.char, databaseJson *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseStr := C.GoString(databaseJson)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
var database repositorymodels.Database
|
||||
err := json.Unmarshal([]byte(databaseStr), &database)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
|
||||
_, code := serverInstance.repository.CreateDatabase(database)
|
||||
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
|
||||
//export GetDatabase
|
||||
func GetDatabase(serverName *C.char, databaseId *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
database, code := serverInstance.repository.GetDatabase(databaseIdStr)
|
||||
if code != repositorymodels.StatusOk {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
databaseJson, _ := json.Marshal(database)
|
||||
return C.CString(string(databaseJson))
|
||||
}
|
||||
|
||||
//export GetAllDatabases
|
||||
func GetAllDatabases(serverName *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
databases, code := serverInstance.repository.GetAllDatabases()
|
||||
if code != repositorymodels.StatusOk {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
databasesJson, err := json.Marshal(databases)
|
||||
if err != nil {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
return C.CString(string(databasesJson))
|
||||
}
|
||||
|
||||
//export DeleteDatabase
|
||||
func DeleteDatabase(serverName *C.char, databaseId *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
code := serverInstance.repository.DeleteDatabase(databaseIdStr)
|
||||
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
122
sharedlibrary/documents.go
Normal file
122
sharedlibrary/documents.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package main
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
//export CreateDocument
|
||||
func CreateDocument(serverName *C.char, databaseId *C.char, collectionId *C.char, documentJson *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
documentStr := C.GoString(documentJson)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
var document repositorymodels.Document
|
||||
err := json.Unmarshal([]byte(documentStr), &document)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
|
||||
_, code := serverInstance.repository.CreateDocument(databaseIdStr, collectionIdStr, document)
|
||||
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
|
||||
//export GetDocument
|
||||
func GetDocument(serverName *C.char, databaseId *C.char, collectionId *C.char, documentId *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
documentIdStr := C.GoString(documentId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
document, code := serverInstance.repository.GetDocument(databaseIdStr, collectionIdStr, documentIdStr)
|
||||
if code != repositorymodels.StatusOk {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
documentJson, _ := json.Marshal(document)
|
||||
return C.CString(string(documentJson))
|
||||
}
|
||||
|
||||
//export GetAllDocuments
|
||||
func GetAllDocuments(serverName *C.char, databaseId *C.char, collectionId *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
documents, code := serverInstance.repository.GetAllDocuments(databaseIdStr, collectionIdStr)
|
||||
if code != repositorymodels.StatusOk {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
documentsJson, _ := json.Marshal(documents)
|
||||
return C.CString(string(documentsJson))
|
||||
}
|
||||
|
||||
//export UpdateDocument
|
||||
func UpdateDocument(serverName *C.char, databaseId *C.char, collectionId *C.char, documentId *C.char, documentJson *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
documentIdStr := C.GoString(documentId)
|
||||
documentStr := C.GoString(documentJson)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
var document repositorymodels.Document
|
||||
err := json.Unmarshal([]byte(documentStr), &document)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
|
||||
code := serverInstance.repository.DeleteDocument(databaseIdStr, collectionIdStr, documentIdStr)
|
||||
if code != repositorymodels.StatusOk {
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
|
||||
_, code = serverInstance.repository.CreateDocument(databaseIdStr, collectionIdStr, document)
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
|
||||
//export DeleteDocument
|
||||
func DeleteDocument(serverName *C.char, databaseId *C.char, collectionId *C.char, documentId *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
databaseIdStr := C.GoString(databaseId)
|
||||
collectionIdStr := C.GoString(collectionId)
|
||||
documentIdStr := C.GoString(documentId)
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = getInstance(serverNameStr); !ok {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
code := serverInstance.repository.DeleteDocument(databaseIdStr, collectionIdStr, documentIdStr)
|
||||
|
||||
return repositoryStatusToResponseCode(code)
|
||||
}
|
||||
86
sharedlibrary/shared.go
Normal file
86
sharedlibrary/shared.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
type ServerInstance struct {
|
||||
server *api.ApiServer
|
||||
repository *repositories.DataRepository
|
||||
}
|
||||
|
||||
var serverInstances map[string]*ServerInstance
|
||||
var mutex sync.RWMutex
|
||||
|
||||
const (
|
||||
ResponseSuccess = 0
|
||||
|
||||
ResponseUnknown = 100
|
||||
ResponseFailedToParseConfiguration = 101
|
||||
ResponseFailedToLoadState = 102
|
||||
ResponseFailedToParseRequest = 103
|
||||
ResponseServerInstanceAlreadyExists = 104
|
||||
ResponseServerInstanceNotFound = 105
|
||||
|
||||
ResponseRepositoryNotFound = 200
|
||||
ResponseRepositoryConflict = 201
|
||||
ResponseRepositoryBadRequest = 202
|
||||
)
|
||||
|
||||
func getInstance(serverName string) (*ServerInstance, bool) {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
if serverInstances == nil {
|
||||
serverInstances = make(map[string]*ServerInstance)
|
||||
}
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
if serverInstance, ok = serverInstances[serverName]; !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return serverInstance, true
|
||||
}
|
||||
|
||||
func addInstance(serverName string, serverInstance *ServerInstance) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if serverInstances == nil {
|
||||
serverInstances = make(map[string]*ServerInstance)
|
||||
}
|
||||
|
||||
serverInstances[serverName] = serverInstance
|
||||
}
|
||||
|
||||
func removeInstance(serverName string) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if serverInstances == nil {
|
||||
return
|
||||
}
|
||||
|
||||
delete(serverInstances, serverName)
|
||||
}
|
||||
|
||||
func repositoryStatusToResponseCode(status repositorymodels.RepositoryStatus) int {
|
||||
switch status {
|
||||
case repositorymodels.StatusOk:
|
||||
return ResponseSuccess
|
||||
case repositorymodels.StatusNotFound:
|
||||
return ResponseRepositoryNotFound
|
||||
case repositorymodels.Conflict:
|
||||
return ResponseRepositoryConflict
|
||||
case repositorymodels.BadRequest:
|
||||
return ResponseRepositoryBadRequest
|
||||
default:
|
||||
return ResponseUnknown
|
||||
}
|
||||
}
|
||||
90
sharedlibrary/sharedlibrary.go
Normal file
90
sharedlibrary/sharedlibrary.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
//export CreateServerInstance
|
||||
func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
||||
configStr := C.GoString(configurationJSON)
|
||||
serverNameStr := C.GoString(serverName)
|
||||
|
||||
if _, ok := getInstance(serverNameStr); ok {
|
||||
return ResponseServerInstanceAlreadyExists
|
||||
}
|
||||
|
||||
var configuration config.ServerConfig
|
||||
err := json.Unmarshal([]byte(configStr), &configuration)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseConfiguration
|
||||
}
|
||||
|
||||
configuration.PopulateCalculatedFields()
|
||||
configuration.ApplyDefaultsToEmptyFields()
|
||||
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
||||
PersistDataFilePath: configuration.PersistDataFilePath,
|
||||
})
|
||||
|
||||
server := api.NewApiServer(repository, configuration)
|
||||
server.Start()
|
||||
|
||||
addInstance(serverNameStr, &ServerInstance{
|
||||
server: server,
|
||||
repository: repository,
|
||||
})
|
||||
|
||||
return ResponseSuccess
|
||||
}
|
||||
|
||||
//export StopServerInstance
|
||||
func StopServerInstance(serverName *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
|
||||
if serverInstance, ok := getInstance(serverNameStr); ok {
|
||||
serverInstance.server.Stop()
|
||||
removeInstance(serverNameStr)
|
||||
return ResponseSuccess
|
||||
}
|
||||
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
//export GetServerInstanceState
|
||||
func GetServerInstanceState(serverName *C.char) *C.char {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
|
||||
if serverInstance, ok := getInstance(serverNameStr); ok {
|
||||
stateJSON, err := serverInstance.repository.GetState()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return C.CString(stateJSON)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//export LoadServerInstanceState
|
||||
func LoadServerInstanceState(serverName *C.char, stateJSON *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
stateJSONStr := C.GoString(stateJSON)
|
||||
|
||||
if serverInstance, ok := getInstance(serverNameStr); ok {
|
||||
err := serverInstance.repository.LoadStateJSON(stateJSONStr)
|
||||
if err != nil {
|
||||
return ResponseFailedToLoadState
|
||||
}
|
||||
return ResponseSuccess
|
||||
}
|
||||
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
func main() {}
|
||||
46
sharedlibrary/tests/main.c
Normal file
46
sharedlibrary/tests/main.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "shared.h"
|
||||
|
||||
int test_CreateServerInstance();
|
||||
int test_StopServerInstance();
|
||||
int test_ServerInstanceStateMethods();
|
||||
int test_Databases();
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <path_to_shared_library>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const char *libPath = argv[1];
|
||||
handle = dlopen(libPath, RTLD_LAZY);
|
||||
if (!handle)
|
||||
{
|
||||
fprintf(stderr, "Failed to load shared library: %s\n", dlerror());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("Running tests for library: %s\n", libPath);
|
||||
int results[] = {
|
||||
test_CreateServerInstance(),
|
||||
test_Databases(),
|
||||
test_ServerInstanceStateMethods(),
|
||||
test_StopServerInstance(),
|
||||
};
|
||||
|
||||
int numTests = sizeof(results) / sizeof(results[0]);
|
||||
int numPassed = 0;
|
||||
for (int i = 0; i < numTests; i++)
|
||||
{
|
||||
if (results[i])
|
||||
{
|
||||
numPassed++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Tests passed: %d/%d\n", numPassed, numTests);
|
||||
|
||||
dlclose(handle);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
36
sharedlibrary/tests/shared.c
Normal file
36
sharedlibrary/tests/shared.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "shared.h"
|
||||
|
||||
void *handle = NULL;
|
||||
|
||||
void *load_function(const char *func_name)
|
||||
{
|
||||
void *func = dlsym(handle, func_name);
|
||||
if (!func)
|
||||
{
|
||||
fprintf(stderr, "Failed to load function %s: %s\n", func_name, dlerror());
|
||||
}
|
||||
return func;
|
||||
}
|
||||
|
||||
char *compact_json(const char *json)
|
||||
{
|
||||
size_t len = strlen(json);
|
||||
char *compact = (char *)malloc(len + 1);
|
||||
if (!compact)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate memory for compacted JSON\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *dest = compact;
|
||||
for (const char *src = json; *src != '\0'; ++src)
|
||||
{
|
||||
if (!isspace((unsigned char)*src)) // Skip spaces, newlines, tabs, etc.
|
||||
{
|
||||
*dest++ = *src;
|
||||
}
|
||||
}
|
||||
*dest = '\0'; // Null-terminate the string
|
||||
|
||||
return compact;
|
||||
}
|
||||
15
sharedlibrary/tests/shared.h
Normal file
15
sharedlibrary/tests/shared.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef SHARED_H
|
||||
#define SHARED_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
extern void *handle;
|
||||
|
||||
void *load_function(const char *func_name);
|
||||
char *compact_json(const char *json);
|
||||
|
||||
#endif
|
||||
29
sharedlibrary/tests/test_create.c
Normal file
29
sharedlibrary/tests/test_create.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "shared.h"
|
||||
|
||||
int test_CreateServerInstance()
|
||||
{
|
||||
typedef int (*CreateServerInstanceFn)(char *, char *);
|
||||
CreateServerInstanceFn CreateServerInstance = (CreateServerInstanceFn)load_function("CreateServerInstance");
|
||||
|
||||
if (!CreateServerInstance)
|
||||
{
|
||||
fprintf(stderr, "Failed to find CreateServerInstance function\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
char *configJSON = "{\"host\":\"localhost\",\"port\":8080}";
|
||||
|
||||
int result = CreateServerInstance(serverName, configJSON);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("CreateServerInstance: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("CreateServerInstance: FAILED (result = %d)\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
47
sharedlibrary/tests/test_databases.c
Normal file
47
sharedlibrary/tests/test_databases.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "shared.h"
|
||||
|
||||
int test_Databases()
|
||||
{
|
||||
typedef int (*CreateDatabaseFn)(char *, char *);
|
||||
CreateDatabaseFn CreateDatabase = (CreateDatabaseFn)load_function("CreateDatabase");
|
||||
if (!CreateDatabase)
|
||||
{
|
||||
fprintf(stderr, "Failed to find CreateDatabase function\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
char *configJSON = "{\"id\":\"test-db\"}";
|
||||
|
||||
int result = CreateDatabase(serverName, configJSON);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("CreateDatabase: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("CreateDatabase: FAILED (result = %d)\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef char *(*GetDatabaseFn)(char *, char *);
|
||||
GetDatabaseFn GetDatabase = (GetDatabaseFn)load_function("GetDatabase");
|
||||
if (!GetDatabase)
|
||||
{
|
||||
fprintf(stderr, "Failed to find GetDatabase function\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *database = GetDatabase(serverName, "test-db");
|
||||
if (database)
|
||||
{
|
||||
printf("GetDatabase: SUCCESS (database = %s)\n", database);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("GetDatabase: FAILED\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
68
sharedlibrary/tests/test_instance_state.c
Normal file
68
sharedlibrary/tests/test_instance_state.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "shared.h"
|
||||
|
||||
int test_ServerInstanceStateMethods()
|
||||
{
|
||||
typedef int (*LoadServerInstanceStateFn)(char *, char *);
|
||||
LoadServerInstanceStateFn LoadServerInstanceState = (LoadServerInstanceStateFn)load_function("LoadServerInstanceState");
|
||||
if (!LoadServerInstanceState)
|
||||
{
|
||||
fprintf(stderr, "Failed to find LoadServerInstanceState function\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
char *stateJSON = "{\"databases\":{\"test-db\":{\"id\":\"test-db\"}}}";
|
||||
int result = LoadServerInstanceState(serverName, stateJSON);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("LoadServerInstanceState: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("LoadServerInstanceState: FAILED (result = %d)\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef char *(*GetServerInstanceStateFn)(char *);
|
||||
GetServerInstanceStateFn GetServerInstanceState = (GetServerInstanceStateFn)load_function("GetServerInstanceState");
|
||||
if (!GetServerInstanceState)
|
||||
{
|
||||
fprintf(stderr, "Failed to find GetServerInstanceState function\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *state = GetServerInstanceState(serverName);
|
||||
if (state)
|
||||
{
|
||||
printf("GetServerInstanceState: SUCCESS (state = %s)\n", state);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("GetServerInstanceState: FAILED\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *expected_state = "{\"databases\":{\"test-db\":{\"id\":\"test-db\",\"_ts\":0,\"_rid\":\"\",\"_etag\":\"\",\"_self\":\"\"}},\"collections\":{\"test-db\":{}},\"documents\":{\"test-db\":{}},\"triggers\":{\"test-db\":{}},\"sprocs\":{\"test-db\":{}},\"udfs\":{\"test-db\":{}}}";
|
||||
char *compact_state = compact_json(state);
|
||||
if (!compact_state)
|
||||
{
|
||||
free(state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(compact_state, expected_state) == 0)
|
||||
{
|
||||
printf("GetServerInstanceState: State matches expected value.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("GetServerInstanceState: State does not match expected value.\n");
|
||||
printf("Expected: %s\n", expected_state);
|
||||
printf("Actual: %s\n", compact_state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(state);
|
||||
free(compact_state);
|
||||
return 1;
|
||||
}
|
||||
27
sharedlibrary/tests/test_stop.c
Normal file
27
sharedlibrary/tests/test_stop.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "shared.h"
|
||||
|
||||
int test_StopServerInstance()
|
||||
{
|
||||
typedef int (*StopServerInstanceFn)(char *);
|
||||
StopServerInstanceFn StopServerInstance = (StopServerInstanceFn)load_function("StopServerInstance");
|
||||
|
||||
if (!StopServerInstance)
|
||||
{
|
||||
fprintf(stderr, "Failed to find StopServerInstance function\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
int result = StopServerInstance(serverName);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("StopServerInstance: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("StopServerInstance: FAILED (result = %d)\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user