Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JSON_VALUE queries #171

Closed
wants to merge 4 commits into from
Closed

Conversation

emilol
Copy link
Contributor

@emilol emilol commented Dec 14, 2021

Not all properties on a document need be mapped as columns, e.g. for Product:

public class Product
{
    public string Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }

    public ProductType Type { get; set; } = ProductType.Normal;
}

The nevermore mapping could be:

public class ProductMap : DocumentMap<Product>
{
    public ProductMap()
    {
        Column(m => m.Name);
        TypeResolutionColumn(m => m.Type);
    }
}

This means that currently, if we try and query by anything other than Name or Type e.g:

var products = t.Query<Product>()
    .Where(c => c.Price == 100)
    .ToList();

We get the following error:

Error while executing SQL command in transaction 'NonParallelWorker': Invalid column name 'Price'.
The command being executed was:
SELECT Id,Name,Type,JSON
FROM [TestSchema].[Product]
WHERE ([Price] = @price)
ORDER BY [Id]

The Price field was queried as a column, instead of being parsed as part of the json payload like so:

SELECT Id,Name,Type,JSON
FROM [TestSchema].[Product]
-WHERE ([Price] = @price)
+WHERE (JSON_VALUE([JSON], '$.Price') = @price)
ORDER BY [Id]


public override void AddWhere(UnaryWhereParameter whereParams)
{
if (From.ColumnNames.Contains(whereParams.FieldName))
Copy link
Contributor Author

@emilol emilol Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adam-mccoy just a heads up on this one; SelectAllColumnsTableResolver will return the columns as new [] { "*" }.

It's not wired in by default since the most recent release (we now use JsonLastTableColumnNameResolver which will return the column names) but this logic might need to be flipped to maintain backwards compat with the old resolver, which can still be manually wired in to the nevermore config (see example here).

@emilol emilol closed this Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants