mirror of
https://github.com/pikami/cosmium.git
synced 2026-01-26 04:42:58 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cf5ae92f4 | ||
|
|
5d99b653cc | ||
|
|
787cdb33cf | ||
|
|
5caa829ac1 | ||
|
|
887d456ad4 | ||
|
|
da1566875b | ||
|
|
3fee3bc816 | ||
|
|
8657c48fc8 |
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 {
|
type ApiServer struct {
|
||||||
stopServer chan interface{}
|
stopServer chan interface{}
|
||||||
isActive bool
|
onServerShutdown chan interface{}
|
||||||
router *gin.Engine
|
isActive bool
|
||||||
config config.ServerConfig
|
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{})
|
stopChan := make(chan interface{})
|
||||||
|
onServerShutdownChan := make(chan interface{})
|
||||||
|
|
||||||
apiServer := &ApiServer{
|
apiServer := &ApiServer{
|
||||||
stopServer: stopChan,
|
stopServer: stopChan,
|
||||||
config: config,
|
onServerShutdown: onServerShutdownChan,
|
||||||
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
apiServer.CreateRouter(dataRepository)
|
apiServer.CreateRouter(dataRepository)
|
||||||
@@ -32,4 +35,5 @@ func (s *ApiServer) GetRouter() *gin.Engine {
|
|||||||
|
|
||||||
func (s *ApiServer) Stop() {
|
func (s *ApiServer) Stop() {
|
||||||
s.stopServer <- true
|
s.stopServer <- true
|
||||||
|
<-s.onServerShutdown
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
jsonpatch "github.com/cosmiumdev/json-patch/v5"
|
jsonpatch "github.com/cosmiumdev/json-patch/v5"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
apimodels "github.com/pikami/cosmium/api/api_models"
|
||||||
"github.com/pikami/cosmium/internal/constants"
|
"github.com/pikami/cosmium/internal/constants"
|
||||||
"github.com/pikami/cosmium/internal/logger"
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
@@ -183,6 +184,13 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
|
|||||||
databaseId := c.Param("databaseId")
|
databaseId := c.Param("databaseId")
|
||||||
collectionId := c.Param("collId")
|
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{}
|
var requestBody map[string]interface{}
|
||||||
if err := c.BindJSON(&requestBody); err != nil {
|
if err := c.BindJSON(&requestBody); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||||
@@ -191,30 +199,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
|
|||||||
|
|
||||||
query := requestBody["query"]
|
query := requestBody["query"]
|
||||||
if query != nil {
|
if query != nil {
|
||||||
if c.GetHeader("x-ms-cosmos-is-query-plan-request") != "" {
|
h.handleDocumentQuery(c, requestBody)
|
||||||
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),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,3 +238,131 @@ func parametersToMap(pairs []interface{}) map[string]interface{} {
|
|||||||
|
|
||||||
return result
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -75,8 +75,7 @@ func requestToResourceId(c *gin.Context) string {
|
|||||||
|
|
||||||
isFeed := c.Request.Header.Get("A-Im") == "Incremental Feed"
|
isFeed := c.Request.Header.Get("A-Im") == "Incremental Feed"
|
||||||
if resourceType == "pkranges" && isFeed {
|
if resourceType == "pkranges" && isFeed {
|
||||||
// CosmosSDK replaces '/' with '-' in resource id requests
|
resourceId = collId
|
||||||
resourceId = strings.Replace(collId, "-", "/", -1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resourceId
|
return resourceId
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||||
|
"github.com/pikami/cosmium/internal/resourceid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
||||||
@@ -31,8 +32,9 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
|
|||||||
collectionRid = collection.ResourceID
|
collectionRid = collection.ResourceID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rid := resourceid.NewCombined(collectionRid, resourceid.New(resourceid.ResourceTypePartitionKeyRange))
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{
|
c.IndentedJSON(http.StatusOK, gin.H{
|
||||||
"_rid": collectionRid,
|
"_rid": rid,
|
||||||
"_count": len(partitionKeyRanges),
|
"_count": len(partitionKeyRanges),
|
||||||
"PartitionKeyRanges": partitionKeyRanges,
|
"PartitionKeyRanges": partitionKeyRanges,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pikami/cosmium/api/handlers"
|
"github.com/pikami/cosmium/api/handlers"
|
||||||
@@ -86,7 +87,7 @@ func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
|||||||
s.router = router
|
s.router = router
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiServer) Start() {
|
func (s *ApiServer) Start() error {
|
||||||
listenAddress := fmt.Sprintf(":%d", s.config.Port)
|
listenAddress := fmt.Sprintf(":%d", s.config.Port)
|
||||||
s.isActive = true
|
s.isActive = true
|
||||||
|
|
||||||
@@ -95,6 +96,8 @@ func (s *ApiServer) Start() {
|
|||||||
Handler: s.router.Handler(),
|
Handler: s.router.Handler(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errChan := make(chan error, 1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-s.stopServer
|
<-s.stopServer
|
||||||
logger.InfoLn("Shutting down server...")
|
logger.InfoLn("Shutting down server...")
|
||||||
@@ -102,35 +105,40 @@ func (s *ApiServer) Start() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.ErrorLn("Failed to shutdown server:", err)
|
logger.ErrorLn("Failed to shutdown server:", err)
|
||||||
}
|
}
|
||||||
|
s.onServerShutdown <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
var err error
|
||||||
if s.config.DisableTls {
|
if s.config.DisableTls {
|
||||||
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
|
logger.Infof("Listening and serving HTTP on %s\n", server.Addr)
|
||||||
err := server.ListenAndServe()
|
err = server.ListenAndServe()
|
||||||
if err != nil && err != http.ErrServerClosed {
|
|
||||||
logger.ErrorLn("Failed to start HTTP server:", err)
|
|
||||||
}
|
|
||||||
s.isActive = false
|
|
||||||
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
|
} else if s.config.TLS_CertificatePath != "" && s.config.TLS_CertificateKey != "" {
|
||||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||||
err := server.ListenAndServeTLS(
|
err = server.ListenAndServeTLS(
|
||||||
s.config.TLS_CertificatePath,
|
s.config.TLS_CertificatePath,
|
||||||
s.config.TLS_CertificateKey)
|
s.config.TLS_CertificateKey)
|
||||||
if err != nil && err != http.ErrServerClosed {
|
|
||||||
logger.ErrorLn("Failed to start HTTPS server:", err)
|
|
||||||
}
|
|
||||||
s.isActive = false
|
|
||||||
} else {
|
} else {
|
||||||
tlsConfig := tlsprovider.GetDefaultTlsConfig()
|
tlsConfig := tlsprovider.GetDefaultTlsConfig()
|
||||||
server.TLSConfig = tlsConfig
|
server.TLSConfig = tlsConfig
|
||||||
|
|
||||||
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
logger.Infof("Listening and serving HTTPS on %s\n", server.Addr)
|
||||||
err := server.ListenAndServeTLS("", "")
|
err = server.ListenAndServeTLS("", "")
|
||||||
if err != nil && err != http.ErrServerClosed {
|
|
||||||
logger.ErrorLn("Failed to start HTTPS server:", err)
|
|
||||||
}
|
|
||||||
s.isActive = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/api"
|
"github.com/pikami/cosmium/api"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
"github.com/pikami/cosmium/internal/repositories"
|
"github.com/pikami/cosmium/internal/repositories"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,6 +36,9 @@ func runTestServer() *TestServer {
|
|||||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.LogLevel = "debug"
|
||||||
|
logger.SetLogLevel(logger.LogLevelDebug)
|
||||||
|
|
||||||
return runTestServerCustomConfig(config)
|
return runTestServerCustomConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -377,5 +377,140 @@ func Test_Documents_Patch(t *testing.T) {
|
|||||||
assert.NotNil(t, r)
|
assert.NotNil(t, r)
|
||||||
assert.Nil(t, err2)
|
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"])
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
server := api.NewApiServer(repository, configuration)
|
server := api.NewApiServer(repository, configuration)
|
||||||
server.Start()
|
err := server.Start()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
waitForExit(server, repository, configuration)
|
waitForExit(server, repository, configuration)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,6 +204,19 @@ Cosmium strives to support the core features of Cosmos DB, including:
|
|||||||
| IS_PRIMITIVE | Yes |
|
| IS_PRIMITIVE | Yes |
|
||||||
| IS_STRING | 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
|
## 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:
|
While Cosmium aims to replicate the behavior of Cosmos DB as closely as possible, there are certain differences and limitations to be aware of:
|
||||||
|
|||||||
@@ -50,6 +50,10 @@ func (r *DataRepository) DeleteCollection(databaseId string, collectionId string
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete(r.storeState.Collections[databaseId], collectionId)
|
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
|
return repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
@@ -71,7 +75,7 @@ func (r *DataRepository) CreateCollection(databaseId string, newCollection repos
|
|||||||
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
|
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
|
||||||
|
|
||||||
newCollection.TimeStamp = time.Now().Unix()
|
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.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
newCollection.Self = fmt.Sprintf("dbs/%s/colls/%s/", database.ResourceID, newCollection.ResourceID)
|
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.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
|
return repositorymodels.StatusOk
|
||||||
}
|
}
|
||||||
@@ -50,7 +55,7 @@ func (r *DataRepository) CreateDatabase(newDatabase repositorymodels.Database) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
newDatabase.TimeStamp = time.Now().Unix()
|
newDatabase.TimeStamp = time.Now().Unix()
|
||||||
newDatabase.ResourceID = resourceid.New()
|
newDatabase.ResourceID = resourceid.New(resourceid.ResourceTypeDatabase)
|
||||||
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
newDatabase.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
newDatabase.Self = fmt.Sprintf("dbs/%s/", newDatabase.ResourceID)
|
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["_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["_etag"] = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
document["_self"] = fmt.Sprintf("dbs/%s/colls/%s/docs/%s/", database.ResourceID, collection.ResourceID, document["_rid"])
|
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
|
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)
|
pkrSelf := fmt.Sprintf("dbs/%s/colls/%s/pkranges/%s/", databaseRid, collectionRid, pkrResourceId)
|
||||||
etag := fmt.Sprintf("\"%s\"", uuid.New())
|
etag := fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ func (r *DataRepository) CreateStoredProcedure(databaseId string, collectionId s
|
|||||||
}
|
}
|
||||||
|
|
||||||
sp.TimeStamp = time.Now().Unix()
|
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.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
sp.Self = fmt.Sprintf("dbs/%s/colls/%s/sprocs/%s/", database.ResourceID, collection.ResourceID, sp.ResourceID)
|
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.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.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
trigger.Self = fmt.Sprintf("dbs/%s/colls/%s/triggers/%s/", database.ResourceID, collection.ResourceID, trigger.ResourceID)
|
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.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.ETag = fmt.Sprintf("\"%s\"", uuid.New())
|
||||||
udf.Self = fmt.Sprintf("dbs/%s/colls/%s/udfs/%s/", database.ResourceID, collection.ResourceID, udf.ResourceID)
|
udf.Self = fmt.Sprintf("dbs/%s/colls/%s/udfs/%s/", database.ResourceID, collection.ResourceID, udf.ResourceID)
|
||||||
|
|
||||||
|
|||||||
@@ -3,32 +3,76 @@ package resourceid
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New() string {
|
type ResourceType int
|
||||||
id := uuid.New().ID()
|
|
||||||
idBytes := uintToBytes(id)
|
|
||||||
|
|
||||||
// first byte should be bigger than 0x80 for collection ids
|
const (
|
||||||
// clients classify this id as "user" otherwise
|
ResourceTypeDatabase ResourceType = iota
|
||||||
if (idBytes[0] & 0x80) <= 0 {
|
ResourceTypeCollection
|
||||||
idBytes[0] = byte(rand.Intn(0x80) + 0x80)
|
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 {
|
func NewCombined(ids ...string) string {
|
||||||
combinedIdBytes := make([]byte, 0)
|
combinedIdBytes := make([]byte, 0)
|
||||||
|
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
idBytes, _ := base64.StdEncoding.DecodeString(id)
|
idBytes, _ := base64.StdEncoding.DecodeString(strings.ReplaceAll(id, "-", "/"))
|
||||||
combinedIdBytes = append(combinedIdBytes, idBytes...)
|
combinedIdBytes = append(combinedIdBytes, idBytes...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return base64.StdEncoding.EncodeToString(combinedIdBytes)
|
encoded := base64.StdEncoding.EncodeToString(combinedIdBytes)
|
||||||
|
return strings.ReplaceAll(encoded, "/", "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
func uintToBytes(id uint32) []byte {
|
func uintToBytes(id uint32) []byte {
|
||||||
@@ -39,3 +83,13 @@ func uintToBytes(id uint32) []byte {
|
|||||||
|
|
||||||
return buf
|
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 {
|
type Table struct {
|
||||||
Value string
|
Value string
|
||||||
SelectItem SelectItem
|
SelectItem SelectItem
|
||||||
|
IsInSelect bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type JoinItem struct {
|
type JoinItem struct {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Parse_AggregateFunctions(t *testing.T) {
|
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"
|
"testing"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Parse_Join(t *testing.T) {
|
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", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
@@ -40,7 +41,7 @@ func Test_Parse_Join(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"cc"}, IsTopLevel: true},
|
{Path: []string{"cc"}, IsTopLevel: true},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_MathFunctions(t *testing.T) {
|
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", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
OrderExpressions: []parsers.OrderExpression{
|
OrderExpressions: []parsers.OrderExpression{
|
||||||
{
|
{
|
||||||
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
||||||
@@ -73,7 +73,7 @@ func Test_Parse(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
@@ -93,7 +93,7 @@ func Test_Parse(t *testing.T) {
|
|||||||
Type: parsers.SelectItemTypeField,
|
Type: parsers.SelectItemTypeField,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.SelectItem{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
Value: parsers.FunctionCall{
|
||||||
@@ -124,10 +124,9 @@ func Test_Parse(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "c",
|
Value: "c",
|
||||||
SelectItem: parsers.SelectItem{
|
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||||
Path: []string{"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
|
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)
|
tableTyped := table.(parsers.Table)
|
||||||
|
|
||||||
if selectItem != nil {
|
if selectItem != nil {
|
||||||
tableTyped.SelectItem = selectItem.(parsers.SelectItem)
|
tableTyped.SelectItem = selectItem.(parsers.SelectItem)
|
||||||
|
tableTyped.IsInSelect = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return tableTyped, nil
|
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 {
|
} / From ws subQuery:SubQuerySelectItem {
|
||||||
subQueryTyped := subQuery.(parsers.SelectItem)
|
subQueryTyped := subQuery.(parsers.SelectItem)
|
||||||
table := parsers.Table{
|
table := parsers.Table{
|
||||||
@@ -243,13 +251,13 @@ SubQuerySelectItem <- subQuery:SubQuery asClause:(ws alias:AsClause { return ali
|
|||||||
return selectItem, nil
|
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)
|
return makeJoin(table, column)
|
||||||
} / Join ws subQuery:SubQuerySelectItem {
|
} / Join ws subQuery:SubQuerySelectItem {
|
||||||
return makeJoin(nil, subQuery)
|
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
|
return []interface{}{offset.(parsers.Constant).Value, limit.(parsers.Constant).Value}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,7 +325,11 @@ SelectItem <- selectItem:(SubQuerySelectItem / Literal / FunctionCall / SelectAr
|
|||||||
return itemResult, nil
|
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 / GroupBy / OrderBy / Offset
|
||||||
|
|
||||||
DotFieldAccess <- "." id:Identifier {
|
DotFieldAccess <- "." id:Identifier {
|
||||||
return id, nil
|
return id, nil
|
||||||
@@ -373,6 +385,8 @@ As <- "AS"i
|
|||||||
|
|
||||||
From <- "FROM"i
|
From <- "FROM"i
|
||||||
|
|
||||||
|
In <- "IN"i
|
||||||
|
|
||||||
Join <- "JOIN"i
|
Join <- "JOIN"i
|
||||||
|
|
||||||
Exists <- "EXISTS"i
|
Exists <- "EXISTS"i
|
||||||
@@ -387,6 +401,8 @@ GroupBy <- "GROUP"i ws "BY"i
|
|||||||
|
|
||||||
OrderBy <- "ORDER"i ws "BY"i
|
OrderBy <- "ORDER"i ws "BY"i
|
||||||
|
|
||||||
|
Offset <- "OFFSET"i
|
||||||
|
|
||||||
ComparisonOperator <- ("=" / "!=" / "<" / "<=" / ">" / ">=") {
|
ComparisonOperator <- ("=" / "!=" / "<" / "<=" / ">" / ">=") {
|
||||||
return string(c.text), nil
|
return string(c.text), nil
|
||||||
}
|
}
|
||||||
@@ -700,7 +716,7 @@ MathNumberBinExpression <- "NumberBin"i ws "(" ws ex1:SelectItem others:(ws ","
|
|||||||
MathPiExpression <- "PI"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathPi, []interface{}{}) }
|
MathPiExpression <- "PI"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathPi, []interface{}{}) }
|
||||||
MathRandExpression <- "RAND"i ws "(" ws ")" { return createFunctionCall(parsers.FunctionCallMathRand, []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{})...))
|
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Parse_Select(t *testing.T) {
|
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", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{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", "id"}},
|
||||||
{Path: []string{"c", "@param"}},
|
{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{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Distinct: true,
|
Distinct: true,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -58,7 +59,7 @@ func Test_Parse_Select(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Count: 1,
|
Count: 1,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -72,7 +73,7 @@ func Test_Parse_Select(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Count: 5,
|
Count: 5,
|
||||||
Offset: 3,
|
Offset: 3,
|
||||||
},
|
},
|
||||||
@@ -87,7 +88,7 @@ func Test_Parse_Select(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}, IsTopLevel: true},
|
{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{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c"}, IsTopLevel: true},
|
{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"
|
"testing"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Parse_SubQuery(t *testing.T) {
|
func Test_Parse_SubQuery(t *testing.T) {
|
||||||
@@ -22,7 +23,7 @@ func Test_Parse_SubQuery(t *testing.T) {
|
|||||||
Alias: "c",
|
Alias: "c",
|
||||||
Type: parsers.SelectItemTypeSubQuery,
|
Type: parsers.SelectItemTypeSubQuery,
|
||||||
Value: parsers.SelectStmt{
|
Value: parsers.SelectStmt{
|
||||||
Table: parsers.Table{Value: "cc"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("cc")},
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||||
},
|
},
|
||||||
@@ -42,9 +43,7 @@ func Test_Parse_SubQuery(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"cc", "name"}},
|
{Path: []string{"cc", "name"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Value: "c",
|
|
||||||
},
|
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
@@ -55,13 +54,12 @@ func Test_Parse_SubQuery(t *testing.T) {
|
|||||||
Type: parsers.SelectItemTypeSubQuery,
|
Type: parsers.SelectItemTypeSubQuery,
|
||||||
Value: parsers.SelectStmt{
|
Value: parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"tag", "name"}},
|
testutils.SelectItem_Path("tag", "name"),
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "tag",
|
Value: "tag",
|
||||||
SelectItem: parsers.SelectItem{
|
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||||
Path: []string{"c", "tags"},
|
IsInSelect: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -82,10 +80,10 @@ func Test_Parse_SubQuery(t *testing.T) {
|
|||||||
WHERE hasTags`,
|
WHERE hasTags`,
|
||||||
parsers.SelectStmt{
|
parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
testutils.SelectItem_Path("c", "id"),
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "c",
|
SelectItem: testutils.SelectItem_Path("c"),
|
||||||
},
|
},
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
@@ -100,13 +98,12 @@ func Test_Parse_SubQuery(t *testing.T) {
|
|||||||
Type: parsers.SelectItemTypeSubQuery,
|
Type: parsers.SelectItemTypeSubQuery,
|
||||||
Value: parsers.SelectStmt{
|
Value: parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"tag", "name"}},
|
testutils.SelectItem_Path("tag", "name"),
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "tag",
|
Value: "tag",
|
||||||
SelectItem: parsers.SelectItem{
|
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||||
Path: []string{"c", "tags"},
|
IsInSelect: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Exists: true,
|
Exists: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
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{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
Value: parsers.FunctionCall{
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func Test_Parse_Were(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.ComparisonExpression{
|
Filters: parsers.ComparisonExpression{
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
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", "_rid"}},
|
||||||
{Path: []string{"c", "_ts"}},
|
{Path: []string{"c", "_ts"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Operation: parsers.LogicalExpressionTypeOr,
|
Operation: parsers.LogicalExpressionTypeOr,
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
@@ -72,7 +72,7 @@ func Test_Parse_Were(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Operation: parsers.LogicalExpressionTypeAnd,
|
Operation: parsers.LogicalExpressionTypeAnd,
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
@@ -114,7 +114,7 @@ func Test_Parse_Were(t *testing.T) {
|
|||||||
AND c.param=@param_id1`,
|
AND c.param=@param_id1`,
|
||||||
parsers.SelectStmt{
|
parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
|
SelectItems: []parsers.SelectItem{{Path: []string{"c", "id"}, Alias: ""}},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
parsers.ComparisonExpression{
|
parsers.ComparisonExpression{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_AggregateFunctions(t *testing.T) {
|
func Test_Execute_AggregateFunctions(t *testing.T) {
|
||||||
@@ -38,7 +39,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
|||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "key"}},
|
{Path: []string{"c", "key"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -99,7 +100,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
|||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "key"}},
|
{Path: []string{"c", "key"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -132,7 +133,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
|||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "key"}},
|
{Path: []string{"c", "key"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -165,7 +166,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
|||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "key"}},
|
{Path: []string{"c", "key"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -198,7 +199,7 @@ func Test_Execute_AggregateFunctions(t *testing.T) {
|
|||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "key"}},
|
{Path: []string{"c", "key"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ func (r rowContext) partialMatch(item interface{}, exprToSearch interface{}) boo
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range exprValue.MapKeys() {
|
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
|
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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -59,10 +59,11 @@ func Test_Execute_ArrayFunctions(t *testing.T) {
|
|||||||
parsers.SelectStmt{
|
parsers.SelectStmt{
|
||||||
Parameters: map[string]interface{}{
|
Parameters: map[string]interface{}{
|
||||||
"@categories": []interface{}{"coats", "jackets", "sweatshirts"},
|
"@categories": []interface{}{"coats", "jackets", "sweatshirts"},
|
||||||
"@objectArray": []interface{}{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"},
|
"@fullMatchObject": map[string]interface{}{"category": "shirts", "color": "blue", "nestedObject": map[string]interface{}{"size": "M"}},
|
||||||
"@partialMatchObject": map[string]interface{}{"category": "shirts"},
|
"@partialMatchObject": map[string]interface{}{"category": "shirts"},
|
||||||
"@missingPartialMatchObject": map[string]interface{}{"category": "shorts", "color": "blue"},
|
"@missingPartialMatchObject": map[string]interface{}{"category": "shorts", "color": "blue"},
|
||||||
|
"@nestedPartialMatchObject": map[string]interface{}{"nestedObject": map[string]interface{}{"size": "M"}},
|
||||||
},
|
},
|
||||||
SelectItems: []parsers.SelectItem{
|
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{}{"id": "123"}},
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"ContainsItem": true,
|
"ContainsItem": true,
|
||||||
"MissingItem": false,
|
"MissingItem": false,
|
||||||
"ContainsFullMatchObject": true,
|
"ContainsFullMatchObject": true,
|
||||||
"MissingFullMatchObject": false,
|
"MissingFullMatchObject": false,
|
||||||
"ContainsPartialMatchObject": true,
|
"ContainsPartialMatchObject": true,
|
||||||
"MissingPartialMatchObject": false,
|
"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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_Joins(t *testing.T) {
|
func Test_Execute_Joins(t *testing.T) {
|
||||||
@@ -33,7 +34,7 @@ func Test_Execute_Joins(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"cc", "name"}},
|
{Path: []string{"cc", "name"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
@@ -62,7 +63,7 @@ func Test_Execute_Joins(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"cc"}, IsTopLevel: true},
|
{Path: []string{"cc"}, IsTopLevel: true},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_MathFunctions(t *testing.T) {
|
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,
|
data,
|
||||||
expectedData,
|
expectedData,
|
||||||
|
|||||||
@@ -60,6 +60,15 @@ func ExecuteQuery(query parsers.SelectStmt, documents []RowType) []RowType {
|
|||||||
projectedDocuments = deduplicate(projectedDocuments)
|
projectedDocuments = deduplicate(projectedDocuments)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply offset
|
||||||
|
if query.Offset > 0 {
|
||||||
|
if query.Offset < len(projectedDocuments) {
|
||||||
|
projectedDocuments = projectedDocuments[query.Offset:]
|
||||||
|
} else {
|
||||||
|
projectedDocuments = []RowType{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Apply result limit
|
// Apply result limit
|
||||||
if query.Count > 0 && len(projectedDocuments) > query.Count {
|
if query.Count > 0 && len(projectedDocuments) > query.Count {
|
||||||
projectedDocuments = projectedDocuments[:query.Count]
|
projectedDocuments = projectedDocuments[:query.Count]
|
||||||
@@ -80,10 +89,15 @@ func resolveFrom(query parsers.SelectStmt, doc RowType) []rowContext {
|
|||||||
initialTableName = query.Table.Value
|
initialTableName = query.Table.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if initialTableName == "" {
|
||||||
|
initialTableName = resolveDestinationColumnName(query.Table.SelectItem, 0, query.Parameters)
|
||||||
|
}
|
||||||
|
|
||||||
initialRow = rowContext{
|
initialRow = rowContext{
|
||||||
parameters: query.Parameters,
|
parameters: query.Parameters,
|
||||||
tables: map[string]RowType{
|
tables: map[string]RowType{
|
||||||
initialTableName: doc,
|
initialTableName: doc,
|
||||||
|
"$root": doc,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,15 +107,33 @@ func resolveFrom(query parsers.SelectStmt, doc RowType) []rowContext {
|
|||||||
if destinationTableName == "" {
|
if destinationTableName == "" {
|
||||||
destinationTableName = query.Table.Value
|
destinationTableName = query.Table.Value
|
||||||
}
|
}
|
||||||
|
if destinationTableName == "" {
|
||||||
selectValue := initialRow.parseArray(query.Table.SelectItem)
|
destinationTableName = resolveDestinationColumnName(query.Table.SelectItem, 0, initialRow.parameters)
|
||||||
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 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}
|
return []rowContext{initialRow}
|
||||||
@@ -310,18 +342,7 @@ func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
|
|||||||
// Construct a new row based on the selected columns
|
// Construct a new row based on the selected columns
|
||||||
row := make(map[string]interface{})
|
row := make(map[string]interface{})
|
||||||
for index, selectItem := range selectItems {
|
for index, selectItem := range selectItems {
|
||||||
destinationName := selectItem.Alias
|
destinationName := resolveDestinationColumnName(selectItem, index, r.parameters)
|
||||||
if destinationName == "" {
|
|
||||||
if len(selectItem.Path) > 0 {
|
|
||||||
destinationName = selectItem.Path[len(selectItem.Path)-1]
|
|
||||||
} else {
|
|
||||||
destinationName = fmt.Sprintf("$%d", index+1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if destinationName[0] == '@' {
|
|
||||||
destinationName = r.parameters[destinationName].(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
row[destinationName] = r.resolveSelectItem(selectItem)
|
row[destinationName] = r.resolveSelectItem(selectItem)
|
||||||
}
|
}
|
||||||
@@ -329,6 +350,23 @@ func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
|
|||||||
return row
|
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{} {
|
func (r rowContext) resolveSelectItem(selectItem parsers.SelectItem) interface{} {
|
||||||
if selectItem.Type == parsers.SelectItemTypeArray {
|
if selectItem.Type == parsers.SelectItemTypeArray {
|
||||||
return r.selectItem_SelectItemTypeArray(selectItem)
|
return r.selectItem_SelectItemTypeArray(selectItem)
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func Test_Execute(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
OrderExpressions: []parsers.OrderExpression{
|
OrderExpressions: []parsers.OrderExpression{
|
||||||
{
|
{
|
||||||
SelectItem: parsers.SelectItem{Path: []string{"c", "pk"}},
|
SelectItem: parsers.SelectItem{Path: []string{"c", "pk"}},
|
||||||
@@ -79,7 +79,7 @@ func Test_Execute(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
GroupBy: []parsers.SelectItem{
|
GroupBy: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
@@ -102,7 +102,7 @@ func Test_Execute(t *testing.T) {
|
|||||||
Type: parsers.SelectItemTypeField,
|
Type: parsers.SelectItemTypeField,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.SelectItem{
|
Filters: parsers.SelectItem{
|
||||||
Type: parsers.SelectItemTypeFunctionCall,
|
Type: parsers.SelectItemTypeFunctionCall,
|
||||||
Value: parsers.FunctionCall{
|
Value: parsers.FunctionCall{
|
||||||
@@ -137,10 +137,9 @@ func Test_Execute(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "c",
|
Value: "c",
|
||||||
SelectItem: parsers.SelectItem{
|
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||||
Path: []string{"c", "tags"},
|
IsInSelect: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
|
|||||||
@@ -5,14 +5,15 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_Select(t *testing.T) {
|
func Test_Execute_Select(t *testing.T) {
|
||||||
mockData := []memoryexecutor.RowType{
|
mockData := []memoryexecutor.RowType{
|
||||||
map[string]interface{}{"id": "12345", "pk": 123, "_self": "self1", "_rid": "rid1", "_ts": 123456, "isCool": false},
|
map[string]interface{}{"id": "12345", "pk": 123, "_self": "self1", "_rid": "rid1", "_ts": 123456, "isCool": false, "order": 1},
|
||||||
map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true},
|
map[string]interface{}{"id": "67890", "pk": 456, "_self": "self2", "_rid": "rid2", "_ts": 789012, "isCool": true, "order": 2},
|
||||||
map[string]interface{}{"id": "456", "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, "order": 3},
|
||||||
map[string]interface{}{"id": "123", "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, "order": 4},
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Should execute simple SELECT", func(t *testing.T) {
|
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", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -43,7 +44,7 @@ func Test_Execute_Select(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "@param"}},
|
{Path: []string{"c", "@param"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Parameters: map[string]interface{}{
|
Parameters: map[string]interface{}{
|
||||||
"@param": "pk",
|
"@param": "pk",
|
||||||
},
|
},
|
||||||
@@ -65,7 +66,7 @@ func Test_Execute_Select(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Distinct: true,
|
Distinct: true,
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
@@ -84,7 +85,7 @@ func Test_Execute_Select(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Count: 1,
|
Count: 1,
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
@@ -102,20 +103,20 @@ func Test_Execute_Select(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"c", "pk"}},
|
{Path: []string{"c", "pk"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Count: 2,
|
Count: 2,
|
||||||
Offset: 1,
|
Offset: 1,
|
||||||
OrderExpressions: []parsers.OrderExpression{
|
OrderExpressions: []parsers.OrderExpression{
|
||||||
{
|
{
|
||||||
SelectItem: parsers.SelectItem{Path: []string{"c", "id"}},
|
SelectItem: parsers.SelectItem{Path: []string{"c", "order"}},
|
||||||
Direction: parsers.OrderDirectionDesc,
|
Direction: parsers.OrderDirectionDesc,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
map[string]interface{}{"id": "67890", "pk": 456},
|
|
||||||
map[string]interface{}{"id": "456", "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{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}, IsTopLevel: true},
|
{Path: []string{"c", "id"}, IsTopLevel: true},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
@@ -146,7 +147,7 @@ func Test_Execute_Select(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c"}, IsTopLevel: true},
|
{Path: []string{"c"}, IsTopLevel: true},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
},
|
},
|
||||||
mockData,
|
mockData,
|
||||||
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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
|
|||||||
@@ -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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_SubQuery(t *testing.T) {
|
func Test_Execute_SubQuery(t *testing.T) {
|
||||||
@@ -41,7 +42,7 @@ func Test_Execute_SubQuery(t *testing.T) {
|
|||||||
Alias: "c",
|
Alias: "c",
|
||||||
Type: parsers.SelectItemTypeSubQuery,
|
Type: parsers.SelectItemTypeSubQuery,
|
||||||
Value: parsers.SelectStmt{
|
Value: parsers.SelectStmt{
|
||||||
Table: parsers.Table{Value: "cc"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("cc")},
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
{Path: []string{"cc", "info"}, IsTopLevel: true},
|
||||||
},
|
},
|
||||||
@@ -66,9 +67,7 @@ func Test_Execute_SubQuery(t *testing.T) {
|
|||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
{Path: []string{"cc", "name"}},
|
{Path: []string{"cc", "name"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Value: "c",
|
|
||||||
},
|
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
@@ -79,13 +78,12 @@ func Test_Execute_SubQuery(t *testing.T) {
|
|||||||
Type: parsers.SelectItemTypeSubQuery,
|
Type: parsers.SelectItemTypeSubQuery,
|
||||||
Value: parsers.SelectStmt{
|
Value: parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"tag", "name"}},
|
testutils.SelectItem_Path("tag", "name"),
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "tag",
|
Value: "tag",
|
||||||
SelectItem: parsers.SelectItem{
|
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||||
Path: []string{"c", "tags"},
|
IsInSelect: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -107,10 +105,10 @@ func Test_Execute_SubQuery(t *testing.T) {
|
|||||||
t,
|
t,
|
||||||
parsers.SelectStmt{
|
parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
testutils.SelectItem_Path("c", "id"),
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "c",
|
SelectItem: testutils.SelectItem_Path("c"),
|
||||||
},
|
},
|
||||||
JoinItems: []parsers.JoinItem{
|
JoinItems: []parsers.JoinItem{
|
||||||
{
|
{
|
||||||
@@ -125,13 +123,12 @@ func Test_Execute_SubQuery(t *testing.T) {
|
|||||||
Type: parsers.SelectItemTypeSubQuery,
|
Type: parsers.SelectItemTypeSubQuery,
|
||||||
Value: parsers.SelectStmt{
|
Value: parsers.SelectStmt{
|
||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"tag", "name"}},
|
testutils.SelectItem_Path("tag", "name"),
|
||||||
},
|
},
|
||||||
Table: parsers.Table{
|
Table: parsers.Table{
|
||||||
Value: "tag",
|
Value: "tag",
|
||||||
SelectItem: parsers.SelectItem{
|
SelectItem: testutils.SelectItem_Path("c", "tags"),
|
||||||
Path: []string{"c", "tags"},
|
IsInSelect: true,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Exists: true,
|
Exists: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pikami/cosmium/parsers"
|
"github.com/pikami/cosmium/parsers"
|
||||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||||
|
testutils "github.com/pikami/cosmium/test_utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]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,
|
mockData,
|
||||||
[]memoryexecutor.RowType{
|
[]memoryexecutor.RowType{
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func Test_Execute_Where(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.ComparisonExpression{
|
Filters: parsers.ComparisonExpression{
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
Left: parsers.SelectItem{Path: []string{"c", "isCool"}},
|
||||||
@@ -46,7 +46,7 @@ func Test_Execute_Where(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.ComparisonExpression{
|
Filters: parsers.ComparisonExpression{
|
||||||
Operation: "=",
|
Operation: "=",
|
||||||
Left: parsers.SelectItem{Path: []string{"c", "id"}},
|
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", "id"}},
|
||||||
{Path: []string{"c", "_self"}, Alias: "self"},
|
{Path: []string{"c", "_self"}, Alias: "self"},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Operation: parsers.LogicalExpressionTypeAnd,
|
Operation: parsers.LogicalExpressionTypeAnd,
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
@@ -102,7 +102,7 @@ func Test_Execute_Where(t *testing.T) {
|
|||||||
SelectItems: []parsers.SelectItem{
|
SelectItems: []parsers.SelectItem{
|
||||||
{Path: []string{"c", "id"}},
|
{Path: []string{"c", "id"}},
|
||||||
},
|
},
|
||||||
Table: parsers.Table{Value: "c"},
|
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||||
Filters: parsers.LogicalExpression{
|
Filters: parsers.LogicalExpression{
|
||||||
Operation: parsers.LogicalExpressionTypeAnd,
|
Operation: parsers.LogicalExpressionTypeAnd,
|
||||||
Expressions: []interface{}{
|
Expressions: []interface{}{
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
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
|
var collection repositorymodels.Collection
|
||||||
err := json.Unmarshal([]byte(collectionStr), &collection)
|
err := json.NewDecoder(strings.NewReader(collectionStr)).Decode(&collection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ResponseFailedToParseRequest
|
return ResponseFailedToParseRequest
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
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
|
var database repositorymodels.Database
|
||||||
err := json.Unmarshal([]byte(databaseStr), &database)
|
err := json.NewDecoder(strings.NewReader(databaseStr)).Decode(&database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ResponseFailedToParseRequest
|
return ResponseFailedToParseRequest
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
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
|
var document repositorymodels.Document
|
||||||
err := json.Unmarshal([]byte(documentStr), &document)
|
err := json.NewDecoder(strings.NewReader(documentStr)).Decode(&document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ResponseFailedToParseRequest
|
return ResponseFailedToParseRequest
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,10 @@ type ServerInstance struct {
|
|||||||
repository *repositories.DataRepository
|
repository *repositories.DataRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverInstances map[string]*ServerInstance
|
var (
|
||||||
var mutex sync.Mutex
|
serverInstances = make(map[string]*ServerInstance)
|
||||||
|
mutex = sync.Mutex{}
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ResponseSuccess = 0
|
ResponseSuccess = 0
|
||||||
@@ -25,6 +27,7 @@ const (
|
|||||||
ResponseFailedToParseRequest = 103
|
ResponseFailedToParseRequest = 103
|
||||||
ResponseServerInstanceAlreadyExists = 104
|
ResponseServerInstanceAlreadyExists = 104
|
||||||
ResponseServerInstanceNotFound = 105
|
ResponseServerInstanceNotFound = 105
|
||||||
|
ResponseFailedToStartServer = 106
|
||||||
|
|
||||||
ResponseRepositoryNotFound = 200
|
ResponseRepositoryNotFound = 200
|
||||||
ResponseRepositoryConflict = 201
|
ResponseRepositoryConflict = 201
|
||||||
@@ -35,10 +38,6 @@ func getInstance(serverName string) (*ServerInstance, bool) {
|
|||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
if serverInstances == nil {
|
|
||||||
serverInstances = make(map[string]*ServerInstance)
|
|
||||||
}
|
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
var serverInstance *ServerInstance
|
var serverInstance *ServerInstance
|
||||||
if serverInstance, ok = serverInstances[serverName]; !ok {
|
if serverInstance, ok = serverInstances[serverName]; !ok {
|
||||||
@@ -52,10 +51,6 @@ func addInstance(serverName string, serverInstance *ServerInstance) {
|
|||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
if serverInstances == nil {
|
|
||||||
serverInstances = make(map[string]*ServerInstance)
|
|
||||||
}
|
|
||||||
|
|
||||||
serverInstances[serverName] = serverInstance
|
serverInstances[serverName] = serverInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,10 +58,6 @@ func removeInstance(serverName string) {
|
|||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
if serverInstances == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(serverInstances, serverName)
|
delete(serverInstances, serverName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <stdlib.h>
|
||||||
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/api"
|
"github.com/pikami/cosmium/api"
|
||||||
"github.com/pikami/cosmium/api/config"
|
"github.com/pikami/cosmium/api/config"
|
||||||
@@ -11,21 +16,21 @@ import (
|
|||||||
|
|
||||||
//export CreateServerInstance
|
//export CreateServerInstance
|
||||||
func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
||||||
configStr := C.GoString(configurationJSON)
|
|
||||||
serverNameStr := C.GoString(serverName)
|
serverNameStr := C.GoString(serverName)
|
||||||
|
configStr := C.GoString(configurationJSON)
|
||||||
|
|
||||||
if _, ok := getInstance(serverNameStr); ok {
|
if _, ok := getInstance(serverNameStr); ok {
|
||||||
return ResponseServerInstanceAlreadyExists
|
return ResponseServerInstanceAlreadyExists
|
||||||
}
|
}
|
||||||
|
|
||||||
var configuration config.ServerConfig
|
var configuration config.ServerConfig
|
||||||
err := json.Unmarshal([]byte(configStr), &configuration)
|
err := json.NewDecoder(strings.NewReader(configStr)).Decode(&configuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ResponseFailedToParseConfiguration
|
return ResponseFailedToParseConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration.PopulateCalculatedFields()
|
|
||||||
configuration.ApplyDefaultsToEmptyFields()
|
configuration.ApplyDefaultsToEmptyFields()
|
||||||
|
configuration.PopulateCalculatedFields()
|
||||||
|
|
||||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
||||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
InitialDataFilePath: configuration.InitialDataFilePath,
|
||||||
@@ -33,7 +38,10 @@ func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
|||||||
})
|
})
|
||||||
|
|
||||||
server := api.NewApiServer(repository, configuration)
|
server := api.NewApiServer(repository, configuration)
|
||||||
server.Start()
|
err = server.Start()
|
||||||
|
if err != nil {
|
||||||
|
return ResponseFailedToStartServer
|
||||||
|
}
|
||||||
|
|
||||||
addInstance(serverNameStr, &ServerInstance{
|
addInstance(serverNameStr, &ServerInstance{
|
||||||
server: server,
|
server: server,
|
||||||
@@ -87,4 +95,9 @@ func LoadServerInstanceState(serverName *C.char, stateJSON *C.char) int {
|
|||||||
return ResponseServerInstanceNotFound
|
return ResponseServerInstanceNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//export FreeMemory
|
||||||
|
func FreeMemory(ptr *C.char) {
|
||||||
|
C.free(unsafe.Pointer(ptr))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {}
|
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