mirror of
https://github.com/pikami/cosmium.git
synced 2026-01-08 20:16:11 +00:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd4fe5abec | ||
|
|
f062e03f0c | ||
|
|
058b3271b7 | ||
|
|
1711c8fb5c | ||
|
|
851b3ca3a8 | ||
|
|
d27c633e1d | ||
|
|
3987df89c0 | ||
|
|
6e3f4169a1 | ||
|
|
14c5400d23 | ||
|
|
1cf5ae92f4 | ||
|
|
5d99b653cc | ||
|
|
787cdb33cf | ||
|
|
5caa829ac1 | ||
|
|
887d456ad4 | ||
|
|
da1566875b | ||
|
|
3fee3bc816 | ||
|
|
8657c48fc8 | ||
|
|
e080888c20 | ||
|
|
b8d79fd945 | ||
|
|
f25cb7fb03 | ||
|
|
125f10d8a2 |
@@ -15,16 +15,17 @@ jobs:
|
||||
uses: crazy-max/ghaction-xgo@v3.1.0
|
||||
with:
|
||||
xgo_version: latest
|
||||
go_version: 1.22.0
|
||||
go_version: 1.24.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
|
||||
buildvcs: true
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: shared-libraries
|
||||
path: dist/*
|
||||
|
||||
18
.github/workflows/release.yml
vendored
18
.github/workflows/release.yml
vendored
@@ -17,16 +17,32 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: 1.22.0
|
||||
go-version: 1.24.0
|
||||
|
||||
- name: Cross-Compile with xgo
|
||||
uses: crazy-max/ghaction-xgo@v3.1.0
|
||||
with:
|
||||
xgo_version: latest
|
||||
go_version: 1.24.0
|
||||
dest: sharedlibrary_dist
|
||||
pkg: sharedlibrary
|
||||
prefix: cosmium
|
||||
targets: linux/amd64,linux/arm64,windows/amd64,windows/arm64,darwin/amd64,darwin/arm64
|
||||
v: true
|
||||
buildmode: c-shared
|
||||
buildvcs: true
|
||||
|
||||
- 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:
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
dist/
|
||||
sharedlibrary_dist/
|
||||
ignored/
|
||||
explorer_www/
|
||||
main
|
||||
|
||||
@@ -26,6 +26,23 @@ brews:
|
||||
commit_author:
|
||||
name: pikami
|
||||
email: git@pikami.org
|
||||
skip_upload: auto
|
||||
|
||||
archives:
|
||||
- id: bundle
|
||||
format: tar.gz
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
- id: shared-libraries
|
||||
meta: true
|
||||
format: "tar.gz"
|
||||
wrap_in_directory: true
|
||||
name_template: "{{ .ProjectName }}_{{ .Version }}_shared-libraries"
|
||||
files:
|
||||
- LICENSE
|
||||
- README.md
|
||||
- sharedlibrary_dist/**
|
||||
|
||||
dockers:
|
||||
- id: docker-linux-amd64
|
||||
@@ -33,7 +50,6 @@ dockers:
|
||||
goarch: amd64
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-amd64"
|
||||
dockerfile: Dockerfile
|
||||
use: buildx
|
||||
build_flag_templates:
|
||||
@@ -51,9 +67,7 @@ dockers:
|
||||
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:
|
||||
@@ -104,16 +118,25 @@ dockers:
|
||||
|
||||
docker_manifests:
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:latest'
|
||||
skip_push: auto
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:latest-arm64v8"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-arm64v8"
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}'
|
||||
skip_push: auto
|
||||
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'
|
||||
skip_push: auto
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64v8"
|
||||
- name_template: 'ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer'
|
||||
skip_push: auto
|
||||
image_templates:
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-amd64"
|
||||
- "ghcr.io/pikami/{{ .ProjectName }}:{{ .Version }}-explorer-arm64"
|
||||
|
||||
2
Makefile
2
Makefile
@@ -9,7 +9,7 @@ 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
|
||||
GOVERSION=1.24.0
|
||||
|
||||
DIST_DIR=dist
|
||||
|
||||
|
||||
24
api/api_models/models.go
Normal file
24
api/api_models/models.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package apimodels
|
||||
|
||||
const (
|
||||
BatchOperationTypeCreate = "Create"
|
||||
BatchOperationTypeDelete = "Delete"
|
||||
BatchOperationTypeReplace = "Replace"
|
||||
BatchOperationTypeUpsert = "Upsert"
|
||||
BatchOperationTypeRead = "Read"
|
||||
BatchOperationTypePatch = "Patch"
|
||||
)
|
||||
|
||||
type BatchOperation struct {
|
||||
OperationType string `json:"operationType"`
|
||||
Id string `json:"id"`
|
||||
ResourceBody map[string]interface{} `json:"resourceBody"`
|
||||
}
|
||||
|
||||
type BatchOperationResult struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
RequestCharge float64 `json:"requestCharge"`
|
||||
ResourceBody map[string]interface{} `json:"resourceBody"`
|
||||
Etag string `json:"etag"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
@@ -7,18 +7,21 @@ import (
|
||||
)
|
||||
|
||||
type ApiServer struct {
|
||||
stopServer chan interface{}
|
||||
isActive bool
|
||||
router *gin.Engine
|
||||
config config.ServerConfig
|
||||
stopServer chan interface{}
|
||||
onServerShutdown chan interface{}
|
||||
isActive bool
|
||||
router *gin.Engine
|
||||
config *config.ServerConfig
|
||||
}
|
||||
|
||||
func NewApiServer(dataRepository *repositories.DataRepository, config config.ServerConfig) *ApiServer {
|
||||
func NewApiServer(dataRepository *repositories.DataRepository, config *config.ServerConfig) *ApiServer {
|
||||
stopChan := make(chan interface{})
|
||||
onServerShutdownChan := make(chan interface{})
|
||||
|
||||
apiServer := &ApiServer{
|
||||
stopServer: stopChan,
|
||||
config: config,
|
||||
stopServer: stopChan,
|
||||
onServerShutdown: onServerShutdownChan,
|
||||
config: config,
|
||||
}
|
||||
|
||||
apiServer.CreateRouter(dataRepository)
|
||||
@@ -32,4 +35,5 @@ func (s *ApiServer) GetRouter() *gin.Engine {
|
||||
|
||||
func (s *ApiServer) Stop() {
|
||||
s.stopServer <- true
|
||||
<-s.onServerShutdown
|
||||
}
|
||||
|
||||
@@ -58,15 +58,15 @@ func (c *ServerConfig) PopulateCalculatedFields() {
|
||||
|
||||
switch c.LogLevel {
|
||||
case "debug":
|
||||
logger.LogLevel = logger.LogLevelDebug
|
||||
logger.SetLogLevel(logger.LogLevelDebug)
|
||||
case "info":
|
||||
logger.LogLevel = logger.LogLevelInfo
|
||||
logger.SetLogLevel(logger.LogLevelInfo)
|
||||
case "error":
|
||||
logger.LogLevel = logger.LogLevelError
|
||||
logger.SetLogLevel(logger.LogLevelError)
|
||||
case "silent":
|
||||
logger.LogLevel = logger.LogLevelSilent
|
||||
logger.SetLogLevel(logger.LogLevelSilent)
|
||||
default:
|
||||
logger.LogLevel = logger.LogLevelInfo
|
||||
logger.SetLogLevel(logger.LogLevelInfo)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
jsonpatch "github.com/cosmiumdev/json-patch/v5"
|
||||
"github.com/gin-gonic/gin"
|
||||
apimodels "github.com/pikami/cosmium/api/api_models"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
@@ -183,6 +184,13 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
// Handle batch requests
|
||||
isBatchRequest, _ := strconv.ParseBool(c.GetHeader("x-ms-cosmos-is-batch-request"))
|
||||
if isBatchRequest {
|
||||
h.handleBatchRequest(c)
|
||||
return
|
||||
}
|
||||
|
||||
var requestBody map[string]interface{}
|
||||
if err := c.BindJSON(&requestBody); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
@@ -191,30 +199,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
|
||||
|
||||
query := requestBody["query"]
|
||||
if query != nil {
|
||||
if c.GetHeader("x-ms-cosmos-is-query-plan-request") != "" {
|
||||
c.IndentedJSON(http.StatusOK, constants.QueryPlanResponse)
|
||||
return
|
||||
}
|
||||
|
||||
var queryParameters map[string]interface{}
|
||||
if paramsArray, ok := requestBody["parameters"].([]interface{}); ok {
|
||||
queryParameters = parametersToMap(paramsArray)
|
||||
}
|
||||
|
||||
docs, status := h.repository.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
|
||||
if status != repositorymodels.StatusOk {
|
||||
// TODO: Currently we return everything if the query fails
|
||||
h.GetAllDocuments(c)
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
"_count": len(docs),
|
||||
})
|
||||
h.handleDocumentQuery(c, requestBody)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -253,3 +238,131 @@ func parametersToMap(pairs []interface{}) map[string]interface{} {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (h *Handlers) handleDocumentQuery(c *gin.Context, requestBody map[string]interface{}) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
if c.GetHeader("x-ms-cosmos-is-query-plan-request") != "" {
|
||||
c.IndentedJSON(http.StatusOK, constants.QueryPlanResponse)
|
||||
return
|
||||
}
|
||||
|
||||
var queryParameters map[string]interface{}
|
||||
if paramsArray, ok := requestBody["parameters"].([]interface{}); ok {
|
||||
queryParameters = parametersToMap(paramsArray)
|
||||
}
|
||||
|
||||
docs, status := h.repository.ExecuteQueryDocuments(databaseId, collectionId, requestBody["query"].(string), queryParameters)
|
||||
if status != repositorymodels.StatusOk {
|
||||
// TODO: Currently we return everything if the query fails
|
||||
h.GetAllDocuments(c)
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
"_count": len(docs),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handlers) handleBatchRequest(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
batchOperations := make([]apimodels.BatchOperation, 0)
|
||||
if err := c.BindJSON(&batchOperations); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
batchOperationResults := make([]apimodels.BatchOperationResult, len(batchOperations))
|
||||
for idx, operation := range batchOperations {
|
||||
switch operation.OperationType {
|
||||
case apimodels.BatchOperationTypeCreate:
|
||||
createdDocument, status := h.repository.CreateDocument(databaseId, collectionId, operation.ResourceBody)
|
||||
responseCode := repositoryStatusToResponseCode(status)
|
||||
if status == repositorymodels.StatusOk {
|
||||
responseCode = http.StatusCreated
|
||||
}
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: responseCode,
|
||||
ResourceBody: createdDocument,
|
||||
}
|
||||
case apimodels.BatchOperationTypeDelete:
|
||||
status := h.repository.DeleteDocument(databaseId, collectionId, operation.Id)
|
||||
responseCode := repositoryStatusToResponseCode(status)
|
||||
if status == repositorymodels.StatusOk {
|
||||
responseCode = http.StatusNoContent
|
||||
}
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: responseCode,
|
||||
}
|
||||
case apimodels.BatchOperationTypeReplace:
|
||||
deleteStatus := h.repository.DeleteDocument(databaseId, collectionId, operation.Id)
|
||||
if deleteStatus == repositorymodels.StatusNotFound {
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: http.StatusNotFound,
|
||||
}
|
||||
continue
|
||||
}
|
||||
createdDocument, createStatus := h.repository.CreateDocument(databaseId, collectionId, operation.ResourceBody)
|
||||
responseCode := repositoryStatusToResponseCode(createStatus)
|
||||
if createStatus == repositorymodels.StatusOk {
|
||||
responseCode = http.StatusCreated
|
||||
}
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: responseCode,
|
||||
ResourceBody: createdDocument,
|
||||
}
|
||||
case apimodels.BatchOperationTypeUpsert:
|
||||
documentId := operation.ResourceBody["id"].(string)
|
||||
h.repository.DeleteDocument(databaseId, collectionId, documentId)
|
||||
createdDocument, createStatus := h.repository.CreateDocument(databaseId, collectionId, operation.ResourceBody)
|
||||
responseCode := repositoryStatusToResponseCode(createStatus)
|
||||
if createStatus == repositorymodels.StatusOk {
|
||||
responseCode = http.StatusCreated
|
||||
}
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: responseCode,
|
||||
ResourceBody: createdDocument,
|
||||
}
|
||||
case apimodels.BatchOperationTypeRead:
|
||||
document, status := h.repository.GetDocument(databaseId, collectionId, operation.Id)
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: repositoryStatusToResponseCode(status),
|
||||
ResourceBody: document,
|
||||
}
|
||||
case apimodels.BatchOperationTypePatch:
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: http.StatusNotImplemented,
|
||||
Message: "Patch operation is not implemented",
|
||||
}
|
||||
default:
|
||||
batchOperationResults[idx] = apimodels.BatchOperationResult{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "Unknown operation type",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, batchOperationResults)
|
||||
}
|
||||
|
||||
func repositoryStatusToResponseCode(status repositorymodels.RepositoryStatus) int {
|
||||
switch status {
|
||||
case repositorymodels.StatusOk:
|
||||
return http.StatusOK
|
||||
case repositorymodels.StatusNotFound:
|
||||
return http.StatusNotFound
|
||||
case repositorymodels.Conflict:
|
||||
return http.StatusConflict
|
||||
case repositorymodels.BadRequest:
|
||||
return http.StatusBadRequest
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ import (
|
||||
|
||||
type Handlers struct {
|
||||
repository *repositories.DataRepository
|
||||
config config.ServerConfig
|
||||
config *config.ServerConfig
|
||||
}
|
||||
|
||||
func NewHandlers(dataRepository *repositories.DataRepository, config config.ServerConfig) *Handlers {
|
||||
func NewHandlers(dataRepository *repositories.DataRepository, config *config.ServerConfig) *Handlers {
|
||||
return &Handlers{
|
||||
repository: dataRepository,
|
||||
config: config,
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
func Authentication(config config.ServerConfig) gin.HandlerFunc {
|
||||
func Authentication(config *config.ServerConfig) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
requestUrl := c.Request.URL.String()
|
||||
if config.DisableAuth ||
|
||||
@@ -75,8 +75,7 @@ func requestToResourceId(c *gin.Context) string {
|
||||
|
||||
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)
|
||||
resourceId = collId
|
||||
}
|
||||
|
||||
return resourceId
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
)
|
||||
|
||||
func StripTrailingSlashes(r *gin.Engine, config config.ServerConfig) gin.HandlerFunc {
|
||||
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) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/pikami/cosmium/internal/resourceid"
|
||||
)
|
||||
|
||||
func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
||||
@@ -31,8 +32,9 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
||||
collectionRid = collection.ResourceID
|
||||
}
|
||||
|
||||
rid := resourceid.NewCombined(collectionRid, resourceid.New(resourceid.ResourceTypePartitionKeyRange))
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": collectionRid,
|
||||
"_rid": rid,
|
||||
"_count": len(partitionKeyRanges),
|
||||
"PartitionKeyRanges": partitionKeyRanges,
|
||||
})
|
||||
|
||||
@@ -27,7 +27,9 @@ func (h *Handlers) GetServerInfo(c *gin.Context) {
|
||||
"databaseAccountEndpoint": h.config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
"enableMultipleWriteLocations": false,
|
||||
"continuousBackupEnabled": false,
|
||||
"enableNRegionSynchronousCommit": false,
|
||||
"userReplicationPolicy": map[string]interface{}{
|
||||
"asyncReplication": false,
|
||||
"minReplicaSetSize": 1,
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/handlers"
|
||||
@@ -13,15 +15,19 @@ import (
|
||||
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
||||
)
|
||||
|
||||
var ginMux sync.Mutex
|
||||
|
||||
func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
||||
routeHandlers := handlers.NewHandlers(repository, s.config)
|
||||
|
||||
ginMux.Lock()
|
||||
gin.DefaultWriter = logger.InfoWriter()
|
||||
gin.DefaultErrorWriter = logger.ErrorWriter()
|
||||
|
||||
if s.config.LogLevel != "debug" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
ginMux.Unlock()
|
||||
|
||||
router := gin.Default(func(e *gin.Engine) {
|
||||
e.RedirectTrailingSlash = false
|
||||
@@ -81,7 +87,7 @@ func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
||||
s.router = router
|
||||
}
|
||||
|
||||
func (s *ApiServer) Start() {
|
||||
func (s *ApiServer) Start() error {
|
||||
listenAddress := fmt.Sprintf(":%d", s.config.Port)
|
||||
s.isActive = true
|
||||
|
||||
@@ -90,6 +96,8 @@ func (s *ApiServer) Start() {
|
||||
Handler: s.router.Handler(),
|
||||
}
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
<-s.stopServer
|
||||
logger.InfoLn("Shutting down server...")
|
||||
@@ -97,35 +105,40 @@ func (s *ApiServer) Start() {
|
||||
if err != nil {
|
||||
logger.ErrorLn("Failed to shutdown server:", err)
|
||||
}
|
||||
s.onServerShutdown <- true
|
||||
}()
|
||||
|
||||
go func() {
|
||||
var err error
|
||||
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
|
||||
err = server.ListenAndServe()
|
||||
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
|
||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||
err := server.ListenAndServeTLS(
|
||||
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
|
||||
err = server.ListenAndServeTLS("", "")
|
||||
}
|
||||
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
logger.ErrorLn("Failed to start server:", err)
|
||||
errChan <- err
|
||||
} else {
|
||||
errChan <- nil
|
||||
}
|
||||
s.isActive = false
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@ package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"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/stretchr/testify/assert"
|
||||
@@ -47,12 +45,7 @@ func Test_Authentication(t *testing.T) {
|
||||
azcosmos.DatabaseProperties{ID: testDatabaseName},
|
||||
&azcosmos.CreateDatabaseOptions{})
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusUnauthorized)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
assert.Contains(t, err.Error(), "401 Unauthorized")
|
||||
})
|
||||
|
||||
t.Run("Should allow unauthorized requests to /_explorer", func(t *testing.T) {
|
||||
@@ -68,7 +61,7 @@ func Test_Authentication(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Authentication_Disabled(t *testing.T) {
|
||||
ts := runTestServerCustomConfig(config.ServerConfig{
|
||||
ts := runTestServerCustomConfig(&config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
@@ -14,13 +15,15 @@ type TestServer struct {
|
||||
URL string
|
||||
}
|
||||
|
||||
func runTestServerCustomConfig(config config.ServerConfig) *TestServer {
|
||||
func runTestServerCustomConfig(config *config.ServerConfig) *TestServer {
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{})
|
||||
|
||||
api := api.NewApiServer(repository, config)
|
||||
|
||||
server := httptest.NewServer(api.GetRouter())
|
||||
|
||||
config.DatabaseEndpoint = server.URL
|
||||
|
||||
return &TestServer{
|
||||
Server: server,
|
||||
Repository: repository,
|
||||
@@ -29,12 +32,15 @@ func runTestServerCustomConfig(config config.ServerConfig) *TestServer {
|
||||
}
|
||||
|
||||
func runTestServer() *TestServer {
|
||||
config := config.ServerConfig{
|
||||
config := &config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
}
|
||||
|
||||
config.LogLevel = "debug"
|
||||
logger.SetLogLevel(logger.LogLevelDebug)
|
||||
|
||||
return runTestServerCustomConfig(config)
|
||||
}
|
||||
|
||||
|
||||
@@ -377,5 +377,140 @@ func Test_Documents_Patch(t *testing.T) {
|
||||
assert.NotNil(t, r)
|
||||
assert.Nil(t, err2)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_Documents_TransactionalBatch(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Server.Close()
|
||||
|
||||
t.Run("Should execute CREATE transactional batch", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
|
||||
|
||||
newItem := map[string]interface{}{
|
||||
"id": "678901",
|
||||
}
|
||||
bytes, err := json.Marshal(newItem)
|
||||
assert.Nil(t, err)
|
||||
|
||||
batch.CreateItem(bytes, nil)
|
||||
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, response.Success)
|
||||
assert.Equal(t, 1, len(response.OperationResults))
|
||||
|
||||
operationResponse := response.OperationResults[0]
|
||||
assert.NotNil(t, operationResponse)
|
||||
assert.NotNil(t, operationResponse.ResourceBody)
|
||||
assert.Equal(t, int32(http.StatusCreated), operationResponse.StatusCode)
|
||||
|
||||
var itemResponseBody map[string]interface{}
|
||||
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
|
||||
assert.Equal(t, newItem["id"], itemResponseBody["id"])
|
||||
|
||||
createdDoc, _ := ts.Repository.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
|
||||
assert.Equal(t, newItem["id"], createdDoc["id"])
|
||||
})
|
||||
|
||||
t.Run("Should execute DELETE transactional batch", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
|
||||
|
||||
batch.DeleteItem("12345", nil)
|
||||
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, response.Success)
|
||||
assert.Equal(t, 1, len(response.OperationResults))
|
||||
|
||||
operationResponse := response.OperationResults[0]
|
||||
assert.NotNil(t, operationResponse)
|
||||
assert.Equal(t, int32(http.StatusNoContent), operationResponse.StatusCode)
|
||||
|
||||
_, status := ts.Repository.GetDocument(testDatabaseName, testCollectionName, "12345")
|
||||
assert.Equal(t, repositorymodels.StatusNotFound, int(status))
|
||||
})
|
||||
|
||||
t.Run("Should execute REPLACE transactional batch", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
|
||||
|
||||
newItem := map[string]interface{}{
|
||||
"id": "67890",
|
||||
"pk": "666",
|
||||
}
|
||||
bytes, err := json.Marshal(newItem)
|
||||
assert.Nil(t, err)
|
||||
|
||||
batch.ReplaceItem("67890", bytes, nil)
|
||||
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, response.Success)
|
||||
assert.Equal(t, 1, len(response.OperationResults))
|
||||
|
||||
operationResponse := response.OperationResults[0]
|
||||
assert.NotNil(t, operationResponse)
|
||||
assert.NotNil(t, operationResponse.ResourceBody)
|
||||
assert.Equal(t, int32(http.StatusCreated), operationResponse.StatusCode)
|
||||
|
||||
var itemResponseBody map[string]interface{}
|
||||
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
|
||||
assert.Equal(t, newItem["id"], itemResponseBody["id"])
|
||||
assert.Equal(t, newItem["pk"], itemResponseBody["pk"])
|
||||
|
||||
updatedDoc, _ := ts.Repository.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
|
||||
assert.Equal(t, newItem["id"], updatedDoc["id"])
|
||||
assert.Equal(t, newItem["pk"], updatedDoc["pk"])
|
||||
})
|
||||
|
||||
t.Run("Should execute UPSERT transactional batch", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
|
||||
|
||||
newItem := map[string]interface{}{
|
||||
"id": "678901",
|
||||
"pk": "666",
|
||||
}
|
||||
bytes, err := json.Marshal(newItem)
|
||||
assert.Nil(t, err)
|
||||
|
||||
batch.UpsertItem(bytes, nil)
|
||||
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, response.Success)
|
||||
assert.Equal(t, 1, len(response.OperationResults))
|
||||
|
||||
operationResponse := response.OperationResults[0]
|
||||
assert.NotNil(t, operationResponse)
|
||||
assert.NotNil(t, operationResponse.ResourceBody)
|
||||
assert.Equal(t, int32(http.StatusCreated), operationResponse.StatusCode)
|
||||
|
||||
var itemResponseBody map[string]interface{}
|
||||
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
|
||||
assert.Equal(t, newItem["id"], itemResponseBody["id"])
|
||||
assert.Equal(t, newItem["pk"], itemResponseBody["pk"])
|
||||
|
||||
updatedDoc, _ := ts.Repository.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
|
||||
assert.Equal(t, newItem["id"], updatedDoc["id"])
|
||||
assert.Equal(t, newItem["pk"], updatedDoc["pk"])
|
||||
})
|
||||
|
||||
t.Run("Should execute READ transactional batch", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
batch := collectionClient.NewTransactionalBatch(azcosmos.NewPartitionKeyString("pk"))
|
||||
|
||||
batch.ReadItem("67890", nil)
|
||||
response, err := collectionClient.ExecuteTransactionalBatch(context, batch, &azcosmos.TransactionalBatchOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, response.Success)
|
||||
assert.Equal(t, 1, len(response.OperationResults))
|
||||
|
||||
operationResponse := response.OperationResults[0]
|
||||
assert.NotNil(t, operationResponse)
|
||||
assert.NotNil(t, operationResponse.ResourceBody)
|
||||
assert.Equal(t, int32(http.StatusOK), operationResponse.StatusCode)
|
||||
|
||||
var itemResponseBody map[string]interface{}
|
||||
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
|
||||
assert.Equal(t, "67890", itemResponseBody["id"])
|
||||
})
|
||||
}
|
||||
|
||||
@@ -18,8 +18,11 @@ func main() {
|
||||
PersistDataFilePath: configuration.PersistDataFilePath,
|
||||
})
|
||||
|
||||
server := api.NewApiServer(repository, configuration)
|
||||
server.Start()
|
||||
server := api.NewApiServer(repository, &configuration)
|
||||
err := server.Start()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
waitForExit(server, repository, configuration)
|
||||
}
|
||||
|
||||
@@ -204,6 +204,19 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
||||
| IS_PRIMITIVE | Yes |
|
||||
| IS_STRING | Yes |
|
||||
|
||||
### Transactional batch operations
|
||||
|
||||
Note: There's actually no transaction here. Think of this as a 'bulk operation' that can partially succeed.
|
||||
|
||||
| Operation | Implemented |
|
||||
| --------- | ----------- |
|
||||
| Create | Yes |
|
||||
| Delete | Yes |
|
||||
| Replace | Yes |
|
||||
| Upsert | Yes |
|
||||
| Read | Yes |
|
||||
| Patch | No |
|
||||
|
||||
## 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:
|
||||
|
||||
28
go.mod
28
go.mod
@@ -1,21 +1,21 @@
|
||||
module github.com/pikami/cosmium
|
||||
|
||||
go 1.22.0
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
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/Azure/azure-sdk-for-go/sdk/azcore v1.17.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.3.0
|
||||
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
|
||||
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // 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 v1.12.9 // 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
|
||||
@@ -23,10 +23,10 @@ require (
|
||||
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.24.0 // indirect
|
||||
github.com/goccy/go-json v0.10.4 // indirect
|
||||
github.com/go-playground/validator/v10 v10.25.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // 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
|
||||
@@ -36,11 +36,11 @@ require (
|
||||
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.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
|
||||
golang.org/x/arch v0.14.0 // indirect
|
||||
golang.org/x/crypto v0.35.0 // indirect
|
||||
golang.org/x/net v0.35.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/text v0.22.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
61
go.sum
61
go.sum
@@ -1,17 +1,17 @@
|
||||
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.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/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 h1:B/dfvscEQtew9dVuoxqxrUKKv8Ih2f55PydknDamU+g=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0/go.mod h1:fiPSssYvltE08HJchL04dOy+RD4hgrjph0cwGGMntdI=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.3.0 h1:RGcdpSElvcXCwxydI0xzOBu1Gvp88OoiTGfbtO/z1m0=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v1.3.0/go.mod h1:YwUyrNUtcZcibA99JcfCP6UUp95VVQKO2MJfBzgJDwA=
|
||||
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/AzureAD/microsoft-authentication-library-for-go v1.3.2 h1:kYRSnvJju5gYVyhkij+RTJ/VR6QIUaCfWeaFm2ycsjQ=
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
|
||||
github.com/bytedance/sonic v1.12.9 h1:Od1BvK55NnewtGaJsTDeAOSnLVO2BTSLOe0+ooKokmQ=
|
||||
github.com/bytedance/sonic v1.12.9/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
|
||||
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=
|
||||
@@ -35,11 +35,10 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
|
||||
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.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/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8=
|
||||
github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
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=
|
||||
@@ -50,8 +49,8 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
|
||||
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.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
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=
|
||||
@@ -94,21 +93,21 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
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/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4=
|
||||
golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
|
||||
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
|
||||
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa h1:t2QcU6V556bFjYgu4L6C+6VrCPyJZ+eyRsABUPs1mz4=
|
||||
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
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=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||
google.golang.org/protobuf v1.36.5/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=
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type LogLevelType int
|
||||
@@ -21,67 +22,68 @@ type LogWriter struct {
|
||||
WriterLevel LogLevelType
|
||||
}
|
||||
|
||||
var LogLevel = LogLevelInfo
|
||||
var logLevelMutex sync.RWMutex
|
||||
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 {
|
||||
if GetLogLevel() <= LogLevelDebug {
|
||||
prefix := getCallerPrefix()
|
||||
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Debug(v ...any) {
|
||||
if LogLevel <= LogLevelDebug {
|
||||
if GetLogLevel() <= LogLevelDebug {
|
||||
prefix := getCallerPrefix()
|
||||
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Debugf(format string, v ...any) {
|
||||
if LogLevel <= LogLevelDebug {
|
||||
if GetLogLevel() <= LogLevelDebug {
|
||||
prefix := getCallerPrefix()
|
||||
DebugLogger.Printf(prefix+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func InfoLn(v ...any) {
|
||||
if LogLevel <= LogLevelInfo {
|
||||
if GetLogLevel() <= LogLevelInfo {
|
||||
InfoLogger.Println(v...)
|
||||
}
|
||||
}
|
||||
|
||||
func Info(v ...any) {
|
||||
if LogLevel <= LogLevelInfo {
|
||||
if GetLogLevel() <= LogLevelInfo {
|
||||
InfoLogger.Print(v...)
|
||||
}
|
||||
}
|
||||
|
||||
func Infof(format string, v ...any) {
|
||||
if LogLevel <= LogLevelInfo {
|
||||
if GetLogLevel() <= LogLevelInfo {
|
||||
InfoLogger.Printf(format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorLn(v ...any) {
|
||||
if LogLevel <= LogLevelError {
|
||||
if GetLogLevel() <= LogLevelError {
|
||||
prefix := getCallerPrefix()
|
||||
ErrorLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Error(v ...any) {
|
||||
if LogLevel <= LogLevelError {
|
||||
if GetLogLevel() <= LogLevelError {
|
||||
prefix := getCallerPrefix()
|
||||
ErrorLogger.Print(append([]interface{}{prefix}, v...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func Errorf(format string, v ...any) {
|
||||
if LogLevel <= LogLevelError {
|
||||
if GetLogLevel() <= LogLevelError {
|
||||
prefix := getCallerPrefix()
|
||||
ErrorLogger.Printf(prefix+format, v...)
|
||||
}
|
||||
@@ -112,11 +114,25 @@ func DebugWriter() *LogWriter {
|
||||
return &LogWriter{WriterLevel: LogLevelDebug}
|
||||
}
|
||||
|
||||
func SetLogLevel(level LogLevelType) {
|
||||
logLevelMutex.Lock()
|
||||
defer logLevelMutex.Unlock()
|
||||
logLevel = level
|
||||
}
|
||||
|
||||
func GetLogLevel() LogLevelType {
|
||||
logLevelMutex.RLock()
|
||||
defer logLevelMutex.RUnlock()
|
||||
return logLevel
|
||||
}
|
||||
|
||||
func getCallerPrefix() string {
|
||||
_, file, line, ok := runtime.Caller(2)
|
||||
if ok {
|
||||
parts := strings.Split(file, "/")
|
||||
file = parts[len(parts)-1]
|
||||
if len(parts) > 0 {
|
||||
file = parts[len(parts)-1]
|
||||
}
|
||||
return fmt.Sprintf("%s:%d - ", file, line)
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,10 @@ func (r *DataRepository) DeleteCollection(databaseId string, collectionId string
|
||||
}
|
||||
|
||||
delete(r.storeState.Collections[databaseId], collectionId)
|
||||
delete(r.storeState.Documents[databaseId], collectionId)
|
||||
delete(r.storeState.Triggers[databaseId], collectionId)
|
||||
delete(r.storeState.StoredProcedures[databaseId], collectionId)
|
||||
delete(r.storeState.UserDefinedFunctions[databaseId], collectionId)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
@@ -71,7 +75,7 @@ func (r *DataRepository) CreateCollection(databaseId string, newCollection repos
|
||||
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
|
||||
|
||||
newCollection.TimeStamp = time.Now().Unix()
|
||||
newCollection.ResourceID = resourceid.NewCombined(database.ResourceID, resourceid.New())
|
||||
newCollection.ResourceID = resourceid.NewCombined(database.ResourceID, resourceid.New(resourceid.ResourceTypeCollection))
|
||||
newCollection.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
|
||||
|
||||
|
||||
@@ -37,6 +37,11 @@ func (r *DataRepository) DeleteDatabase(id string) repositorymodels.RepositorySt
|
||||
}
|
||||
|
||||
delete(r.storeState.Databases, id)
|
||||
delete(r.storeState.Collections, id)
|
||||
delete(r.storeState.Documents, id)
|
||||
delete(r.storeState.Triggers, id)
|
||||
delete(r.storeState.StoredProcedures, id)
|
||||
delete(r.storeState.UserDefinedFunctions, id)
|
||||
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
@@ -50,7 +55,7 @@ func (r *DataRepository) CreateDatabase(newDatabase repositorymodels.Database) (
|
||||
}
|
||||
|
||||
newDatabase.TimeStamp = time.Now().Unix()
|
||||
newDatabase.ResourceID = resourceid.New()
|
||||
newDatabase.ResourceID = resourceid.New(resourceid.ResourceTypeDatabase)
|
||||
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ func (r *DataRepository) CreateDocument(databaseId string, collectionId string,
|
||||
}
|
||||
|
||||
document["_ts"] = time.Now().Unix()
|
||||
document["_rid"] = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
document["_rid"] = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeDocument))
|
||||
document["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ func (r *DataRepository) GetPartitionKeyRanges(databaseId string, collectionId s
|
||||
timestamp = collection.TimeStamp
|
||||
}
|
||||
|
||||
pkrResourceId := resourceid.NewCombined(databaseRid, collectionRid, resourceid.New())
|
||||
pkrResourceId := resourceid.NewCombined(collectionRid, resourceid.New(resourceid.ResourceTypePartitionKeyRange))
|
||||
pkrSelf := fmt.Sprintf("dbs/%s/colls/%s/pkranges/%s/", databaseRid, collectionRid, pkrResourceId)
|
||||
etag := fmt.Sprintf("\"%s\"", uuid.New())
|
||||
|
||||
|
||||
@@ -46,8 +46,8 @@ func (r *DataRepository) LoadStateFS(filePath string) {
|
||||
}
|
||||
|
||||
func (r *DataRepository) LoadStateJSON(jsonData string) error {
|
||||
r.storeState.RLock()
|
||||
defer r.storeState.RUnlock()
|
||||
r.storeState.Lock()
|
||||
defer r.storeState.Unlock()
|
||||
|
||||
var state repositorymodels.State
|
||||
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
|
||||
|
||||
@@ -81,7 +81,7 @@ func (r *DataRepository) CreateStoredProcedure(databaseId string, collectionId s
|
||||
}
|
||||
|
||||
sp.TimeStamp = time.Now().Unix()
|
||||
sp.ResourceID = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
sp.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeStoredProcedure))
|
||||
sp.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
sp.Self = fmt.Sprintf("dbs/%s/colls/%s/sprocs/%s/", database.ResourceID, collection.ResourceID, sp.ResourceID)
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ func (r *DataRepository) CreateTrigger(databaseId string, collectionId string, t
|
||||
}
|
||||
|
||||
trigger.TimeStamp = time.Now().Unix()
|
||||
trigger.ResourceID = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
trigger.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeTrigger))
|
||||
trigger.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
trigger.Self = fmt.Sprintf("dbs/%s/colls/%s/triggers/%s/", database.ResourceID, collection.ResourceID, trigger.ResourceID)
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ func (r *DataRepository) CreateUserDefinedFunction(databaseId string, collection
|
||||
}
|
||||
|
||||
udf.TimeStamp = time.Now().Unix()
|
||||
udf.ResourceID = resourceid.NewCombined(database.ResourceID, collection.ResourceID, resourceid.New())
|
||||
udf.ResourceID = resourceid.NewCombined(collection.ResourceID, resourceid.New(resourceid.ResourceTypeUserDefinedFunction))
|
||||
udf.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||
udf.Self = fmt.Sprintf("dbs/%s/colls/%s/udfs/%s/", database.ResourceID, collection.ResourceID, udf.ResourceID)
|
||||
|
||||
|
||||
@@ -3,32 +3,76 @@ package resourceid
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func New() string {
|
||||
id := uuid.New().ID()
|
||||
idBytes := uintToBytes(id)
|
||||
type ResourceType int
|
||||
|
||||
// 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)
|
||||
const (
|
||||
ResourceTypeDatabase ResourceType = iota
|
||||
ResourceTypeCollection
|
||||
ResourceTypeDocument
|
||||
ResourceTypeStoredProcedure
|
||||
ResourceTypeTrigger
|
||||
ResourceTypeUserDefinedFunction
|
||||
ResourceTypeConflict
|
||||
ResourceTypePartitionKeyRange
|
||||
ResourceTypeSchema
|
||||
)
|
||||
|
||||
func New(resourceType ResourceType) string {
|
||||
var idBytes []byte
|
||||
switch resourceType {
|
||||
case ResourceTypeDatabase:
|
||||
idBytes = randomBytes(4)
|
||||
case ResourceTypeCollection:
|
||||
idBytes = randomBytes(4)
|
||||
// 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)
|
||||
}
|
||||
case ResourceTypeDocument:
|
||||
idBytes = randomBytes(8)
|
||||
idBytes[7] = byte(rand.Intn(0x10)) // Upper 4 bits = 0
|
||||
case ResourceTypeStoredProcedure:
|
||||
idBytes = randomBytes(8)
|
||||
idBytes[7] = byte(rand.Intn(0x10)) | 0x08 // Upper 4 bits = 0x08
|
||||
case ResourceTypeTrigger:
|
||||
idBytes = randomBytes(8)
|
||||
idBytes[7] = byte(rand.Intn(0x10)) | 0x07 // Upper 4 bits = 0x07
|
||||
case ResourceTypeUserDefinedFunction:
|
||||
idBytes = randomBytes(8)
|
||||
idBytes[7] = byte(rand.Intn(0x10)) | 0x06 // Upper 4 bits = 0x06
|
||||
case ResourceTypeConflict:
|
||||
idBytes = randomBytes(8)
|
||||
idBytes[7] = byte(rand.Intn(0x10)) | 0x04 // Upper 4 bits = 0x04
|
||||
case ResourceTypePartitionKeyRange:
|
||||
// we don't do partitions yet, so just use a fixed id
|
||||
idBytes = []byte{0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x69, 0x50}
|
||||
case ResourceTypeSchema:
|
||||
idBytes = randomBytes(8)
|
||||
idBytes[7] = byte(rand.Intn(0x10)) | 0x09 // Upper 4 bits = 0x09
|
||||
default:
|
||||
idBytes = randomBytes(4)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(idBytes)
|
||||
encoded := base64.StdEncoding.EncodeToString(idBytes)
|
||||
return strings.ReplaceAll(encoded, "/", "-")
|
||||
}
|
||||
|
||||
func NewCombined(ids ...string) string {
|
||||
combinedIdBytes := make([]byte, 0)
|
||||
|
||||
for _, id := range ids {
|
||||
idBytes, _ := base64.StdEncoding.DecodeString(id)
|
||||
idBytes, _ := base64.StdEncoding.DecodeString(strings.ReplaceAll(id, "-", "/"))
|
||||
combinedIdBytes = append(combinedIdBytes, idBytes...)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(combinedIdBytes)
|
||||
encoded := base64.StdEncoding.EncodeToString(combinedIdBytes)
|
||||
return strings.ReplaceAll(encoded, "/", "-")
|
||||
}
|
||||
|
||||
func uintToBytes(id uint32) []byte {
|
||||
@@ -39,3 +83,13 @@ func uintToBytes(id uint32) []byte {
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
func randomBytes(count int) []byte {
|
||||
buf := make([]byte, count)
|
||||
for i := 0; i < count; i += 4 {
|
||||
id := uuid.New().ID()
|
||||
idBytes := uintToBytes(id)
|
||||
copy(buf[i:], idBytes)
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ type SelectStmt struct {
|
||||
type Table struct {
|
||||
Value string
|
||||
SelectItem SelectItem
|
||||
IsInSelect bool
|
||||
}
|
||||
|
||||
type JoinItem struct {
|
||||
@@ -41,6 +42,7 @@ type SelectItem struct {
|
||||
SelectItems []SelectItem
|
||||
Type SelectItemType
|
||||
Value interface{}
|
||||
Invert bool
|
||||
IsTopLevel bool
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
@@ -27,7 +28,7 @@ func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -51,7 +52,7 @@ func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -75,7 +76,7 @@ func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -99,7 +100,7 @@ func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -123,7 +124,7 @@ func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
@@ -32,7 +32,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -58,7 +58,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -87,7 +87,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -116,7 +116,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -145,7 +145,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -169,7 +169,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -195,7 +195,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -223,7 +223,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -251,7 +251,7 @@ func Test_Parse_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_Join(t *testing.T) {
|
||||
@@ -17,7 +18,7 @@ func Test_Parse_Join(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
@@ -40,7 +41,7 @@ func Test_Parse_Join(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_MathFunctions(t *testing.T) {
|
||||
@@ -644,7 +645,7 @@ func testMathFunctionParse(
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: expectedTable},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path(expectedTable)},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func Test_Parse(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
OrderExpressions: []parsers.OrderExpression{
|
||||
{
|
||||
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
@@ -73,7 +73,7 @@ func Test_Parse(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
@@ -93,7 +93,7 @@ func Test_Parse(t *testing.T) {
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -112,6 +112,38 @@ func Test_Parse(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse IN function with function call", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`Select c.id FROM c WHERE (ToString(c.id) IN ("123", "456"))`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallIn,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallToString,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse IN selector", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
@@ -124,10 +156,9 @@ func Test_Parse(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
Value: "c",
|
||||
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||
IsInSelect: true,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -204,14 +204,22 @@ TopClause <- Top ws count:Integer {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
FromClause <- From ws table:TableName selectItem:(ws "IN"i ws column:SelectItem { return column, nil })? {
|
||||
FromClause <- From ws table:TableName selectItem:(ws In ws column:SelectItem { return column, nil }) {
|
||||
tableTyped := table.(parsers.Table)
|
||||
|
||||
if selectItem != nil {
|
||||
tableTyped.SelectItem = selectItem.(parsers.SelectItem)
|
||||
tableTyped.IsInSelect = true
|
||||
}
|
||||
|
||||
return tableTyped, nil
|
||||
} / From ws column:SelectItem {
|
||||
tableSelectItem := column.(parsers.SelectItem)
|
||||
table := parsers.Table{
|
||||
Value: tableSelectItem.Alias,
|
||||
SelectItem: tableSelectItem,
|
||||
}
|
||||
return table, nil
|
||||
} / From ws subQuery:SubQuerySelectItem {
|
||||
subQueryTyped := subQuery.(parsers.SelectItem)
|
||||
table := parsers.Table{
|
||||
@@ -243,13 +251,13 @@ SubQuerySelectItem <- subQuery:SubQuery asClause:(ws alias:AsClause { return ali
|
||||
return selectItem, nil
|
||||
}
|
||||
|
||||
JoinClause <- Join ws table:TableName ws "IN"i ws column:SelectItem {
|
||||
JoinClause <- Join ws table:TableName ws In 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 {
|
||||
OffsetClause <- Offset ws offset:IntegerLiteral ws "LIMIT"i ws limit:IntegerLiteral {
|
||||
return []interface{}{offset.(parsers.Constant).Value, limit.(parsers.Constant).Value}, nil
|
||||
}
|
||||
|
||||
@@ -317,7 +325,11 @@ SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectAr
|
||||
return itemResult, nil
|
||||
}
|
||||
|
||||
AsClause <- ws As ws alias:Identifier { return alias, nil }
|
||||
AsClause <- (ws As)? ws !ExcludedKeywords alias:Identifier {
|
||||
return alias, nil
|
||||
}
|
||||
|
||||
ExcludedKeywords <- Select / Top / As / From / In / Join / Exists / Where / And / Or / Not / GroupBy / OrderBy / Offset
|
||||
|
||||
DotFieldAccess <- "." id:Identifier {
|
||||
return id, nil
|
||||
@@ -346,8 +358,14 @@ AndExpression <- ex1:ComparisonExpression ex2:(ws And ws ex:ComparisonExpression
|
||||
ComparisonExpression <- "(" ws ex:OrExpression ws ")" { return ex, nil }
|
||||
/ left:SelectItem ws op:ComparisonOperator ws right:SelectItem {
|
||||
return parsers.ComparisonExpression{Left:left,Right:right,Operation:op.(string)}, nil
|
||||
} / inv:(Not ws)? ex:SelectItem {
|
||||
if inv != nil {
|
||||
ex1 := ex.(parsers.SelectItem)
|
||||
ex1.Invert = true
|
||||
return ex1, nil
|
||||
}
|
||||
return ex, nil
|
||||
} / ex:BooleanLiteral { return ex, nil }
|
||||
/ ex:SelectItem { return ex, nil }
|
||||
|
||||
OrderByClause <- OrderBy ws ex1:OrderExpression others:(ws "," ws ex:OrderExpression { return ex, nil })* {
|
||||
return makeOrderByClause(ex1, others)
|
||||
@@ -373,6 +391,8 @@ As <- "AS"i
|
||||
|
||||
From <- "FROM"i
|
||||
|
||||
In <- "IN"i
|
||||
|
||||
Join <- "JOIN"i
|
||||
|
||||
Exists <- "EXISTS"i
|
||||
@@ -383,11 +403,15 @@ And <- "AND"i
|
||||
|
||||
Or <- "OR"i wss
|
||||
|
||||
Not <- "NOT"i
|
||||
|
||||
GroupBy <- "GROUP"i ws "BY"i
|
||||
|
||||
OrderBy <- "ORDER"i ws "BY"i
|
||||
|
||||
ComparisonOperator <- ("=" / "!=" / "<" / "<=" / ">" / ">=") {
|
||||
Offset <- "OFFSET"i
|
||||
|
||||
ComparisonOperator <- ("<=" / ">=" / "=" / "!=" / "<" / ">") {
|
||||
return string(c.text), nil
|
||||
}
|
||||
|
||||
@@ -700,7 +724,9 @@ MathNumberBinExpression <- "NumberBin"i ws "(" ws ex1:SelectItem others:(ws ","
|
||||
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 ")" {
|
||||
InFunction <- ex1:SelectProperty ws In ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||
} / "(" ws ex1:SelectItem ws In ws "(" ws ex2:SelectItem others:(ws "," ws ex:SelectItem { return ex, nil })* ws ")" ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_Select(t *testing.T) {
|
||||
@@ -17,7 +18,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -31,7 +32,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "@param"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -44,7 +45,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Distinct: true,
|
||||
},
|
||||
)
|
||||
@@ -58,7 +59,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Count: 1,
|
||||
},
|
||||
)
|
||||
@@ -72,7 +73,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Count: 5,
|
||||
Offset: 3,
|
||||
},
|
||||
@@ -87,7 +88,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -100,7 +101,20 @@ func Test_Parse_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT c", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c"}, IsTopLevel: false},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -120,7 +134,27 @@ func Test_Parse_Select(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT with alias", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT
|
||||
c.id AS aliasWithAs,
|
||||
c.pk aliasWithoutAs
|
||||
FROM root c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Alias: "aliasWithAs", Path: []string{"c", "id"}},
|
||||
{Alias: "aliasWithoutAs", Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{Alias: "c", Path: []string{"root"}},
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -140,7 +174,7 @@ func Test_Parse_Select(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
@@ -30,7 +30,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -56,7 +56,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -85,7 +85,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -111,7 +111,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -137,7 +137,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -163,7 +163,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -189,7 +189,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -213,7 +213,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -237,7 +237,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -261,7 +261,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -286,7 +286,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -310,7 +310,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -334,7 +334,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -360,7 +360,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -385,7 +385,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -409,7 +409,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -434,7 +434,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -458,7 +458,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -484,7 +484,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -508,7 +508,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Parse_SubQuery(t *testing.T) {
|
||||
@@ -22,7 +23,7 @@ func Test_Parse_SubQuery(t *testing.T) {
|
||||
Alias: "c",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
Table: parsers.Table{Value: "cc"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("cc")},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||
},
|
||||
@@ -42,9 +43,7 @@ func Test_Parse_SubQuery(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
@@ -55,13 +54,12 @@ func Test_Parse_SubQuery(t *testing.T) {
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
testutils.SelectItem_Path("tag", "name"),
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
Value: "tag",
|
||||
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||
IsInSelect: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -82,10 +80,10 @@ func Test_Parse_SubQuery(t *testing.T) {
|
||||
WHERE hasTags`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: testutils.SelectItem_Path("c"),
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
@@ -100,13 +98,12 @@ func Test_Parse_SubQuery(t *testing.T) {
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
testutils.SelectItem_Path("tag", "name"),
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
Value: "tag",
|
||||
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||
IsInSelect: true,
|
||||
},
|
||||
Exists: true,
|
||||
},
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
testutils "github.com/pikami/cosmium/test_utils"
|
||||
)
|
||||
|
||||
func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
@@ -27,7 +28,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -63,7 +64,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -99,7 +100,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -135,7 +136,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -171,7 +172,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -207,7 +208,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -243,7 +244,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -279,7 +280,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -315,7 +316,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -351,7 +352,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
|
||||
@@ -19,7 +19,7 @@ func Test_Parse_Were(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||
@@ -42,7 +42,7 @@ func Test_Parse_Were(t *testing.T) {
|
||||
{Path: []string{"c", "_rid"}},
|
||||
{Path: []string{"c", "_ts"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeOr,
|
||||
Expressions: []interface{}{
|
||||
@@ -67,12 +67,12 @@ func Test_Parse_Were(t *testing.T) {
|
||||
t,
|
||||
`select c.id
|
||||
FROM c
|
||||
WHERE c.isCool=true AND (c.id = "123" OR c.id = "456")`,
|
||||
WHERE c.isCool=true AND (c.id = "123" OR c.id <= "456")`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeAnd,
|
||||
Expressions: []interface{}{
|
||||
@@ -90,7 +90,7 @@ func Test_Parse_Were(t *testing.T) {
|
||||
Right: testutils.SelectItem_Constant_String("123"),
|
||||
},
|
||||
parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Operation: "<=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
Right: testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
@@ -114,7 +114,7 @@ func Test_Parse_Were(t *testing.T) {
|
||||
AND c.param=@param_id1`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.LogicalExpression{
|
||||
Expressions: []interface{}{
|
||||
parsers.ComparisonExpression{
|
||||
@@ -148,4 +148,21 @@ func Test_Parse_Were(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should correctly parse NOT conditions", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`select c.id
|
||||
FROM c
|
||||
WHERE NOT c.boolean`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Path: []string{"c", "boolean"},
|
||||
Invert: 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_AggregateFunctions(t *testing.T) {
|
||||
@@ -38,7 +39,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -67,7 +68,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -99,7 +100,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -132,7 +133,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -165,7 +166,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -198,7 +199,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "key"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
|
||||
@@ -220,7 +220,7 @@ func (r rowContext) partialMatch(item interface{}, exprToSearch interface{}) boo
|
||||
}
|
||||
|
||||
for _, key := range exprValue.MapKeys() {
|
||||
if itemValue.MapIndex(key).Interface() != exprValue.MapIndex(key).Interface() {
|
||||
if !reflect.DeepEqual(itemValue.MapIndex(key).Interface(), exprValue.MapIndex(key).Interface()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -59,10 +59,11 @@ func Test_Execute_ArrayFunctions(t *testing.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"},
|
||||
"@objectArray": []interface{}{map[string]interface{}{"category": "shirts", "color": "blue", "nestedObject": map[string]interface{}{"size": "M"}}},
|
||||
"@fullMatchObject": map[string]interface{}{"category": "shirts", "color": "blue", "nestedObject": map[string]interface{}{"size": "M"}},
|
||||
"@partialMatchObject": map[string]interface{}{"category": "shirts"},
|
||||
"@missingPartialMatchObject": map[string]interface{}{"category": "shorts", "color": "blue"},
|
||||
"@nestedPartialMatchObject": map[string]interface{}{"nestedObject": map[string]interface{}{"size": "M"}},
|
||||
},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
@@ -133,17 +134,30 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Alias: "ContainsNestedPartialMatchObject",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallArrayContains,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Constant_Parameter("@objectArray"),
|
||||
testutils.SelectItem_Constant_Parameter("@nestedPartialMatchObject"),
|
||||
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,
|
||||
"ContainsItem": true,
|
||||
"MissingItem": false,
|
||||
"ContainsFullMatchObject": true,
|
||||
"MissingFullMatchObject": false,
|
||||
"ContainsPartialMatchObject": true,
|
||||
"MissingPartialMatchObject": false,
|
||||
"ContainsNestedPartialMatchObject": true,
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -356,7 +370,7 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -392,7 +406,7 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -430,7 +444,7 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -468,7 +482,7 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
|
||||
@@ -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_Joins(t *testing.T) {
|
||||
@@ -33,7 +34,7 @@ func Test_Execute_Joins(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
@@ -62,7 +63,7 @@ func Test_Execute_Joins(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
|
||||
@@ -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 Test_Execute_MathFunctions(t *testing.T) {
|
||||
@@ -261,7 +262,7 @@ func testMathFunctionExecute(
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
data,
|
||||
expectedData,
|
||||
|
||||
@@ -60,6 +60,15 @@ func ExecuteQuery(query parsers.SelectStmt, documents []RowType) []RowType {
|
||||
projectedDocuments = deduplicate(projectedDocuments)
|
||||
}
|
||||
|
||||
// Apply offset
|
||||
if query.Offset > 0 {
|
||||
if query.Offset < len(projectedDocuments) {
|
||||
projectedDocuments = projectedDocuments[query.Offset:]
|
||||
} else {
|
||||
projectedDocuments = []RowType{}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply result limit
|
||||
if query.Count > 0 && len(projectedDocuments) > query.Count {
|
||||
projectedDocuments = projectedDocuments[:query.Count]
|
||||
@@ -80,10 +89,15 @@ func resolveFrom(query parsers.SelectStmt, doc RowType) []rowContext {
|
||||
initialTableName = query.Table.Value
|
||||
}
|
||||
|
||||
if initialTableName == "" {
|
||||
initialTableName = resolveDestinationColumnName(query.Table.SelectItem, 0, query.Parameters)
|
||||
}
|
||||
|
||||
initialRow = rowContext{
|
||||
parameters: query.Parameters,
|
||||
tables: map[string]RowType{
|
||||
initialTableName: doc,
|
||||
"$root": doc,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -93,15 +107,33 @@ func resolveFrom(query parsers.SelectStmt, doc RowType) []rowContext {
|
||||
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
|
||||
if destinationTableName == "" {
|
||||
destinationTableName = resolveDestinationColumnName(query.Table.SelectItem, 0, initialRow.parameters)
|
||||
}
|
||||
return rowContexts
|
||||
|
||||
if query.Table.IsInSelect || query.Table.SelectItem.Type == parsers.SelectItemTypeSubQuery {
|
||||
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
|
||||
}
|
||||
|
||||
if len(query.Table.SelectItem.Path) > 0 {
|
||||
sourceTableName := query.Table.SelectItem.Path[0]
|
||||
sourceTableData := initialRow.tables[sourceTableName]
|
||||
if sourceTableData == nil {
|
||||
// When source table is not found, assume it's root document
|
||||
initialRow.tables[sourceTableName] = initialRow.tables["$root"]
|
||||
}
|
||||
}
|
||||
|
||||
newRowData := initialRow.resolveSelectItem(query.Table.SelectItem)
|
||||
initialRow.tables[destinationTableName] = newRowData
|
||||
return []rowContext{initialRow}
|
||||
}
|
||||
|
||||
return []rowContext{initialRow}
|
||||
@@ -160,6 +192,10 @@ func (r rowContext) applyFilters(filters interface{}) bool {
|
||||
case parsers.SelectItem:
|
||||
resolvedValue := r.resolveSelectItem(typedFilters)
|
||||
if value, ok := resolvedValue.(bool); ok {
|
||||
if typedFilters.Invert {
|
||||
return !value
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -310,18 +346,7 @@ func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
destinationName := resolveDestinationColumnName(selectItem, index, r.parameters)
|
||||
|
||||
row[destinationName] = r.resolveSelectItem(selectItem)
|
||||
}
|
||||
@@ -329,6 +354,23 @@ func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
|
||||
return row
|
||||
}
|
||||
|
||||
func resolveDestinationColumnName(selectItem parsers.SelectItem, itemIndex int, queryParameters map[string]interface{}) string {
|
||||
if selectItem.Alias != "" {
|
||||
return selectItem.Alias
|
||||
}
|
||||
|
||||
destinationName := fmt.Sprintf("$%d", itemIndex+1)
|
||||
if len(selectItem.Path) > 0 {
|
||||
destinationName = selectItem.Path[len(selectItem.Path)-1]
|
||||
}
|
||||
|
||||
if destinationName[0] == '@' {
|
||||
destinationName = queryParameters[destinationName].(string)
|
||||
}
|
||||
|
||||
return destinationName
|
||||
}
|
||||
|
||||
func (r rowContext) resolveSelectItem(selectItem parsers.SelectItem) interface{} {
|
||||
if selectItem.Type == parsers.SelectItemTypeArray {
|
||||
return r.selectItem_SelectItemTypeArray(selectItem)
|
||||
@@ -628,6 +670,14 @@ func hasAggregateFunctions(selectItems []parsers.SelectItem) bool {
|
||||
}
|
||||
|
||||
func compareValues(val1, val2 interface{}) int {
|
||||
if val1 == nil && val2 == nil {
|
||||
return 0
|
||||
} else if val1 == nil {
|
||||
return -1
|
||||
} else if val2 == nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if reflect.TypeOf(val1) != reflect.TypeOf(val2) {
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func Test_Execute(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
OrderExpressions: []parsers.OrderExpression{
|
||||
{
|
||||
SelectItem: parsers.SelectItem{Path: []string{"c", "pk"}},
|
||||
@@ -79,7 +79,7 @@ func Test_Execute(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
GroupBy: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
@@ -102,7 +102,7 @@ func Test_Execute(t *testing.T) {
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
@@ -126,6 +126,42 @@ func Test_Execute(t *testing.T) {
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute IN function with function call", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallIn,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallToString,
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
},
|
||||
},
|
||||
testutils.SelectItem_Constant_String("123"),
|
||||
testutils.SelectItem_Constant_String("456"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "456"},
|
||||
map[string]interface{}{"id": "123"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute IN selector", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
@@ -137,10 +173,9 @@ func Test_Execute(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
Value: "c",
|
||||
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||
IsInSelect: true,
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
|
||||
@@ -5,14 +5,15 @@ 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_Select(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": "12345", "pk": 123, "_self": "self1", "_rid": "rid1", "_ts": 123456, "isCool": false, "order": nil},
|
||||
map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true, "order": 1},
|
||||
map[string]interface{}{"id": "456", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true, "order": 2},
|
||||
map[string]interface{}{"id": "123", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true, "order": 3},
|
||||
}
|
||||
|
||||
t.Run("Should execute simple SELECT", func(t *testing.T) {
|
||||
@@ -23,7 +24,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -43,7 +44,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "@param"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Parameters: map[string]interface{}{
|
||||
"@param": "pk",
|
||||
},
|
||||
@@ -65,7 +66,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Distinct: true,
|
||||
},
|
||||
mockData,
|
||||
@@ -84,7 +85,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Count: 1,
|
||||
},
|
||||
mockData,
|
||||
@@ -102,20 +103,20 @@ func Test_Execute_Select(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "pk"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Count: 2,
|
||||
Offset: 1,
|
||||
OrderExpressions: []parsers.OrderExpression{
|
||||
{
|
||||
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
SelectItem: parsers.SelectItem{Path: []string{"c", "order"}},
|
||||
Direction: parsers.OrderDirectionDesc,
|
||||
},
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "67890", "pk": 456},
|
||||
map[string]interface{}{"id": "456", "pk": 456},
|
||||
map[string]interface{}{"id": "67890", "pk": 456},
|
||||
},
|
||||
)
|
||||
})
|
||||
@@ -127,7 +128,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -146,7 +147,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c"}, IsTopLevel: true},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
mockData,
|
||||
@@ -167,7 +168,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -193,7 +194,7 @@ func Test_Execute_Select(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
|
||||
@@ -9,10 +9,14 @@ import (
|
||||
)
|
||||
|
||||
func (r rowContext) strings_StringEquals(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
str1, str1ok := r.parseString(arguments[0])
|
||||
str2, str2ok := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if !str1ok || !str2ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if ignoreCase {
|
||||
return strings.EqualFold(str1, str2)
|
||||
}
|
||||
@@ -21,10 +25,14 @@ func (r rowContext) strings_StringEquals(arguments []interface{}) bool {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_Contains(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
str1, str1ok := r.parseString(arguments[0])
|
||||
str2, str2ok := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if !str1ok || !str2ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
str2 = strings.ToLower(str2)
|
||||
@@ -34,10 +42,14 @@ func (r rowContext) strings_Contains(arguments []interface{}) bool {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_EndsWith(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
str1, str1ok := r.parseString(arguments[0])
|
||||
str2, str2ok := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if !str1ok || !str2ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
str2 = strings.ToLower(str2)
|
||||
@@ -47,10 +59,14 @@ func (r rowContext) strings_EndsWith(arguments []interface{}) bool {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_StartsWith(arguments []interface{}) bool {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
str1, str1ok := r.parseString(arguments[0])
|
||||
str2, str2ok := r.parseString(arguments[1])
|
||||
ignoreCase := r.getBoolFlag(arguments)
|
||||
|
||||
if !str1ok || !str2ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if ignoreCase {
|
||||
str1 = strings.ToLower(str1)
|
||||
str2 = strings.ToLower(str2)
|
||||
@@ -73,8 +89,12 @@ func (r rowContext) strings_Concat(arguments []interface{}) string {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_IndexOf(arguments []interface{}) int {
|
||||
str1 := r.parseString(arguments[0])
|
||||
str2 := r.parseString(arguments[1])
|
||||
str1, str1ok := r.parseString(arguments[0])
|
||||
str2, str2ok := r.parseString(arguments[1])
|
||||
|
||||
if !str1ok || !str2ok {
|
||||
return -1
|
||||
}
|
||||
|
||||
start := 0
|
||||
if len(arguments) > 2 && arguments[2] != nil {
|
||||
@@ -115,9 +135,13 @@ func (r rowContext) strings_Lower(arguments []interface{}) string {
|
||||
func (r rowContext) strings_Left(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var length int
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
lengthEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
logger.ErrorLn("strings_Left - got parameters of wrong type")
|
||||
return ""
|
||||
@@ -135,28 +159,45 @@ func (r rowContext) strings_Left(arguments []interface{}) string {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_Length(arguments []interface{}) int {
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
if !strOk {
|
||||
return 0
|
||||
}
|
||||
|
||||
return len(str)
|
||||
}
|
||||
|
||||
func (r rowContext) strings_LTrim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimLeft(str, " ")
|
||||
}
|
||||
|
||||
func (r rowContext) strings_Replace(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
oldStr := r.parseString(arguments[1])
|
||||
newStr := r.parseString(arguments[2])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
oldStr, oldStrOk := r.parseString(arguments[1])
|
||||
newStr, newStrOk := r.parseString(arguments[2])
|
||||
|
||||
if !strOk || !oldStrOk || !newStrOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.Replace(str, oldStr, newStr, -1)
|
||||
}
|
||||
|
||||
func (r rowContext) strings_Replicate(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var times int
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
timesEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
if times, ok = timesEx.(int); !ok {
|
||||
logger.ErrorLn("strings_Replicate - got parameters of wrong type")
|
||||
return ""
|
||||
@@ -174,9 +215,13 @@ func (r rowContext) strings_Replicate(arguments []interface{}) string {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_Reverse(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
runes := []rune(str)
|
||||
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
||||
runes[i], runes[j] = runes[j], runes[i]
|
||||
}
|
||||
@@ -187,9 +232,13 @@ func (r rowContext) strings_Reverse(arguments []interface{}) string {
|
||||
func (r rowContext) strings_Right(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var length int
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
lengthEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
if length, ok = lengthEx.(int); !ok {
|
||||
logger.ErrorLn("strings_Right - got parameters of wrong type")
|
||||
return ""
|
||||
@@ -207,7 +256,11 @@ func (r rowContext) strings_Right(arguments []interface{}) string {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_RTrim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimRight(str, " ")
|
||||
}
|
||||
|
||||
@@ -215,10 +268,14 @@ func (r rowContext) strings_Substring(arguments []interface{}) string {
|
||||
var ok bool
|
||||
var startPos int
|
||||
var length int
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
startPosEx := r.resolveSelectItem(arguments[1].(parsers.SelectItem))
|
||||
lengthEx := r.resolveSelectItem(arguments[2].(parsers.SelectItem))
|
||||
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
if startPos, ok = startPosEx.(int); !ok {
|
||||
logger.ErrorLn("strings_Substring - got start parameters of wrong type")
|
||||
return ""
|
||||
@@ -241,7 +298,11 @@ func (r rowContext) strings_Substring(arguments []interface{}) string {
|
||||
}
|
||||
|
||||
func (r rowContext) strings_Trim(arguments []interface{}) string {
|
||||
str := r.parseString(arguments[0])
|
||||
str, strOk := r.parseString(arguments[0])
|
||||
if !strOk {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
@@ -257,15 +318,15 @@ func (r rowContext) getBoolFlag(arguments []interface{}) bool {
|
||||
return ignoreCase
|
||||
}
|
||||
|
||||
func (r rowContext) parseString(argument interface{}) string {
|
||||
func (r rowContext) parseString(argument interface{}) (value string, ok bool) {
|
||||
exItem := argument.(parsers.SelectItem)
|
||||
ex := r.resolveSelectItem(exItem)
|
||||
if str1, ok := ex.(string); ok {
|
||||
return str1
|
||||
return str1, true
|
||||
}
|
||||
|
||||
logger.ErrorLn("StringEquals got parameters of wrong type")
|
||||
return ""
|
||||
return "", false
|
||||
}
|
||||
|
||||
func convertToString(value interface{}) string {
|
||||
|
||||
@@ -40,7 +40,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -76,7 +76,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -112,7 +112,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -148,7 +148,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -184,7 +184,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -220,7 +220,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -256,7 +256,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -290,7 +290,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -325,7 +325,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -359,7 +359,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -393,7 +393,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -429,7 +429,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -464,7 +464,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -498,7 +498,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -533,7 +533,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -567,7 +567,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -603,7 +603,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -637,7 +637,7 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
|
||||
@@ -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_SubQuery(t *testing.T) {
|
||||
@@ -41,7 +42,7 @@ func Test_Execute_SubQuery(t *testing.T) {
|
||||
Alias: "c",
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
Table: parsers.Table{Value: "cc"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("cc")},
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||
},
|
||||
@@ -66,9 +67,7 @@ func Test_Execute_SubQuery(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"cc", "name"}},
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
Table: parsers.Table{
|
||||
@@ -79,13 +78,12 @@ func Test_Execute_SubQuery(t *testing.T) {
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
testutils.SelectItem_Path("tag", "name"),
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
Value: "tag",
|
||||
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||
IsInSelect: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -107,10 +105,10 @@ func Test_Execute_SubQuery(t *testing.T) {
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "c",
|
||||
SelectItem: testutils.SelectItem_Path("c"),
|
||||
},
|
||||
JoinItems: []parsers.JoinItem{
|
||||
{
|
||||
@@ -125,13 +123,12 @@ func Test_Execute_SubQuery(t *testing.T) {
|
||||
Type: parsers.SelectItemTypeSubQuery,
|
||||
Value: parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"tag", "name"}},
|
||||
testutils.SelectItem_Path("tag", "name"),
|
||||
},
|
||||
Table: parsers.Table{
|
||||
Value: "tag",
|
||||
SelectItem: parsers.SelectItem{
|
||||
Path: []string{"c", "tags"},
|
||||
},
|
||||
Value: "tag",
|
||||
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||
IsInSelect: true,
|
||||
},
|
||||
Exists: 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 Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
@@ -40,7 +41,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -76,7 +77,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -112,7 +113,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -148,7 +149,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -184,7 +185,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -220,7 +221,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -256,7 +257,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -292,7 +293,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -328,7 +329,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
@@ -364,7 +365,7 @@ func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
|
||||
@@ -23,7 +23,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||
@@ -46,7 +46,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Operation: "=",
|
||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||
@@ -71,7 +71,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
{Path: []string{"c", "id"}},
|
||||
{Path: []string{"c", "_self"}, Alias: "self"},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeAnd,
|
||||
Expressions: []interface{}{
|
||||
@@ -102,7 +102,7 @@ func Test_Execute_Where(t *testing.T) {
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.LogicalExpression{
|
||||
Operation: parsers.LogicalExpressionTypeAnd,
|
||||
Expressions: []interface{}{
|
||||
@@ -136,4 +136,22 @@ func Test_Execute_Where(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute parse NOT conditions", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.SelectItem{
|
||||
Path: []string{"c", "isCool"},
|
||||
Invert: true,
|
||||
},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "12345"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
@@ -20,7 +21,7 @@ func CreateCollection(serverName *C.char, databaseId *C.char, collectionJson *C.
|
||||
}
|
||||
|
||||
var collection repositorymodels.Collection
|
||||
err := json.Unmarshal([]byte(collectionStr), &collection)
|
||||
err := json.NewDecoder(strings.NewReader(collectionStr)).Decode(&collection)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
@@ -47,7 +48,10 @@ func GetCollection(serverName *C.char, databaseId *C.char, collectionId *C.char)
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
collectionJson, _ := json.Marshal(collection)
|
||||
collectionJson, err := json.Marshal(collection)
|
||||
if err != nil {
|
||||
return C.CString("")
|
||||
}
|
||||
return C.CString(string(collectionJson))
|
||||
}
|
||||
|
||||
@@ -67,7 +71,10 @@ func GetAllCollections(serverName *C.char, databaseId *C.char) *C.char {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
collectionsJson, _ := json.Marshal(collections)
|
||||
collectionsJson, err := json.Marshal(collections)
|
||||
if err != nil {
|
||||
return C.CString("")
|
||||
}
|
||||
return C.CString(string(collectionsJson))
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
@@ -19,7 +20,7 @@ func CreateDatabase(serverName *C.char, databaseJson *C.char) int {
|
||||
}
|
||||
|
||||
var database repositorymodels.Database
|
||||
err := json.Unmarshal([]byte(databaseStr), &database)
|
||||
err := json.NewDecoder(strings.NewReader(databaseStr)).Decode(&database)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
@@ -45,7 +46,10 @@ func GetDatabase(serverName *C.char, databaseId *C.char) *C.char {
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
databaseJson, _ := json.Marshal(database)
|
||||
databaseJson, err := json.Marshal(database)
|
||||
if err != nil {
|
||||
return C.CString("")
|
||||
}
|
||||
return C.CString(string(databaseJson))
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
@@ -21,7 +22,7 @@ func CreateDocument(serverName *C.char, databaseId *C.char, collectionId *C.char
|
||||
}
|
||||
|
||||
var document repositorymodels.Document
|
||||
err := json.Unmarshal([]byte(documentStr), &document)
|
||||
err := json.NewDecoder(strings.NewReader(documentStr)).Decode(&document)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseRequest
|
||||
}
|
||||
@@ -49,7 +50,10 @@ func GetDocument(serverName *C.char, databaseId *C.char, collectionId *C.char, d
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
documentJson, _ := json.Marshal(document)
|
||||
documentJson, err := json.Marshal(document)
|
||||
if err != nil {
|
||||
return C.CString("")
|
||||
}
|
||||
return C.CString(string(documentJson))
|
||||
}
|
||||
|
||||
@@ -70,7 +74,10 @@ func GetAllDocuments(serverName *C.char, databaseId *C.char, collectionId *C.cha
|
||||
return C.CString("")
|
||||
}
|
||||
|
||||
documentsJson, _ := json.Marshal(documents)
|
||||
documentsJson, err := json.Marshal(documents)
|
||||
if err != nil {
|
||||
return C.CString("")
|
||||
}
|
||||
return C.CString(string(documentsJson))
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,10 @@ type ServerInstance struct {
|
||||
repository *repositories.DataRepository
|
||||
}
|
||||
|
||||
var serverInstances map[string]*ServerInstance
|
||||
var mutex sync.RWMutex
|
||||
var (
|
||||
serverInstances = make(map[string]*ServerInstance)
|
||||
mutex = sync.Mutex{}
|
||||
)
|
||||
|
||||
const (
|
||||
ResponseSuccess = 0
|
||||
@@ -25,6 +27,7 @@ const (
|
||||
ResponseFailedToParseRequest = 103
|
||||
ResponseServerInstanceAlreadyExists = 104
|
||||
ResponseServerInstanceNotFound = 105
|
||||
ResponseFailedToStartServer = 106
|
||||
|
||||
ResponseRepositoryNotFound = 200
|
||||
ResponseRepositoryConflict = 201
|
||||
@@ -32,12 +35,8 @@ const (
|
||||
)
|
||||
|
||||
func getInstance(serverName string) (*ServerInstance, bool) {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
if serverInstances == nil {
|
||||
serverInstances = make(map[string]*ServerInstance)
|
||||
}
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
var ok bool
|
||||
var serverInstance *ServerInstance
|
||||
@@ -52,10 +51,6 @@ func addInstance(serverName string, serverInstance *ServerInstance) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if serverInstances == nil {
|
||||
serverInstances = make(map[string]*ServerInstance)
|
||||
}
|
||||
|
||||
serverInstances[serverName] = serverInstance
|
||||
}
|
||||
|
||||
@@ -63,10 +58,6 @@ func removeInstance(serverName string) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if serverInstances == nil {
|
||||
return
|
||||
}
|
||||
|
||||
delete(serverInstances, serverName)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
@@ -11,29 +16,32 @@ import (
|
||||
|
||||
//export CreateServerInstance
|
||||
func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
||||
configStr := C.GoString(configurationJSON)
|
||||
serverNameStr := C.GoString(serverName)
|
||||
configStr := C.GoString(configurationJSON)
|
||||
|
||||
if _, ok := getInstance(serverNameStr); ok {
|
||||
return ResponseServerInstanceAlreadyExists
|
||||
}
|
||||
|
||||
var configuration config.ServerConfig
|
||||
err := json.Unmarshal([]byte(configStr), &configuration)
|
||||
err := json.NewDecoder(strings.NewReader(configStr)).Decode(&configuration)
|
||||
if err != nil {
|
||||
return ResponseFailedToParseConfiguration
|
||||
}
|
||||
|
||||
configuration.PopulateCalculatedFields()
|
||||
configuration.ApplyDefaultsToEmptyFields()
|
||||
configuration.PopulateCalculatedFields()
|
||||
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
||||
PersistDataFilePath: configuration.PersistDataFilePath,
|
||||
})
|
||||
|
||||
server := api.NewApiServer(repository, configuration)
|
||||
server.Start()
|
||||
server := api.NewApiServer(repository, &configuration)
|
||||
err = server.Start()
|
||||
if err != nil {
|
||||
return ResponseFailedToStartServer
|
||||
}
|
||||
|
||||
addInstance(serverNameStr, &ServerInstance{
|
||||
server: server,
|
||||
@@ -87,4 +95,9 @@ func LoadServerInstanceState(serverName *C.char, stateJSON *C.char) int {
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
//export FreeMemory
|
||||
func FreeMemory(ptr *C.char) {
|
||||
C.free(unsafe.Pointer(ptr))
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
@@ -51,3 +51,9 @@ func SelectItem_Constant_Parameter(name string) parsers.SelectItem {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func SelectItem_Path(path ...string) parsers.SelectItem {
|
||||
return parsers.SelectItem{
|
||||
Path: path,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user