Git Product home page Git Product logo

dynamicodatatosql / dynamicodatatosql Goto Github PK

View Code? Open in Web Editor NEW
60.0 11.0 18.0 98 KB

Dotnet NuGet package to convert OData query to SQL query when the data model is dynamic and hence entity framework or any other ORM with IQuerable support cannot be used. In a multi-tenant enterprise or Saas applications, the data model is usually not fixed (dynamic).

License: MIT License

C# 100.00%
odata sql odata-query odata-syntax dynamic sql-query

dynamicodatatosql's Introduction

DynamicODataToSQL

Dotnet NuGet package to convert OData query to SQL query when the data model is dynamic and hence entity framework or any other ORM with IQuerable support cannot be used. In a multi-tenant enterprise or Saas applications, the data model is usually not fixed (dynamic).

License GitHub Actions Status GitHub release (latest SemVer) Nuget

Table of Contents

Example Scenario

Let's consider you are building a Saas application for project and issue tracking, similar to Jira. Development teams and organizations track issues differently, to personalize and measure their agile process.
Your Saas application provides this by allowing tenants to add properties(columns) to existing object types(tables) or create completely new object types, it further allows querying new object types and filtering using new properties through an OData service.

Contoso Inc, one of your tenant

  • Adds a boolean property Internal to Issue object. It is used to track internal vs customer reported issues.
  • Adds another object type called Customer to track which customer reported the issue or was affected by it. Customer object contains standard properties like Name, Email etc.

It is not trivial to expose a multi-tenant OData service in such a scenario using Entity Framework since DB schema/EF's DBContext can be different for each tenant and can be modified on the fly.

GET https://api.trackerOne.com/contoso/odata/Issues?$filter=Internal eq true
GET https://api.trackerOne.com/contoso/odata/Customers?$filter=contains(Email,'outlook.com')

This project aims to solve this issue by providing a simple API to convert an OData query to an SQL query when the data model is dynamic.

Getting Started

  • Install Nuget Package
    Install-Package DynamicODataToSQL
var converter = new ODataToSqlConverter(new EdmModelBuilder(), new SqlServerCompiler() { UseLegacyPagination = false });
var tableName = "Customers"; 
var odataQueryParams = new Dictionary<string, string>
                {
                    {"select", "Name, Email" },
                    {"filter", "contains(Email,'outlook.com')" },
                    {"orderby", "Name" },
                    {"top", "20" },
                    {"skip", "5" },
                };
 var result = converter.ConvertToSQL(
                tableName,
                odataQueryParams,
                false);

string sql = result.Item1;
// SELECT [Name], [Email] FROM [Customers] WHERE [Email] like @p0 ORDER BY [Name] ASC OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY

IDictionary<string, object> sqlParams = result.Item2; 
// {"@p0", "%outlook.com%"},{"@p1", 5}, {"@p2", 20}

See Unit tests for more examples

Example OData Service

See DynamicODataSampleService for and example OData service.

  1. Download AdventureWorks2019 Sample Database

  2. Restore AdventureWorks2019 database.

  3. Setup database user and permissions

    CREATE LOGIN odata_service WITH PASSWORD = 'Password123';   
    use AdventureWorks2019
    CREATE USER odata_service FOR LOGIN odata_service;
    GRANT SELECT ON DATABASE::AdventureWorks2019 TO odata_service;
    GO
  4. Run dotnet run --project .\Samples\DynamicODataSampleService\DynamicODataSampleService.csproj

  5. Use Powershell to query the service, Top 10 Persons by ModifiedDate

    Invoke-RestMethod 'https://localhost:5001/tables/Person.Person?orderby=ModifiedDate desc&skip=0&top=10&select=FirstName,LastName,ModifiedDate' | ConvertTo-Json

    Products with StockLevel less than 100

    Invoke-RestMethod 'https://localhost:5001/tables/Production.Product?filter=SafetyStockLevel lt 100' | ConvertTo-Json

Features

  • Supports basic OData syntax for select, filter, skip, top, orderby and now apply
  • Currently does NOT support expand and lambda operators.
  • You can now retrieve SqlKata Query instead of SQL result. This is useful if you want to modify the query further using SqlKata. To do so simply use ConvertToSQLKata method instead of ConvertToSQL.

filter support

apply support (aggregations)

Aggregations using Odata apply query option is supported. Spec: http://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/odata-data-aggregation-ext-v4.0.html

  • Transformations: filter, groupby and aggregate are supported. expand, concat, search, top, bottom are NOT supported.
  • sum, min, max, avg, countdistinct and count are supported.

Example

\orders?$apply=groupby((Country),aggregate(Amount with sum as Total,Amount with average as AvgAmt))

is converted to

SELECT [Country], Sum(Amount) AS Total, Avg(Amount) AS AvgAmt FROM [Orders] GROUP BY [Country]

For more advanced aggreagate scenarios supported, see unit tests.

Handling Dates on filter

By default filter values are checked if they can be converted to dates. Sometimes this is not expected. You can disable date parsing by setting up tryToParseDate to false on ConvertToSqlFromRawSql function.

Example

var converter = new ODataToSqlConverter(new EdmModelBuilder(), new SqlServerCompiler() { UseLegacyPagination = false });
var tableName = "Customers"; 
converter.ConvertToSqlFromRawSql(customSqlQuery, oDataParams, false, false);

var odataQueryParams = new Dictionary<string, string>
                {
                    {"select", "Name, Type" },
                    {"filter", "Name eq '2022-11-30'" },
                };

// Default conversion with Date Parsing
 var result = converter.ConvertToSQL(
                tableName,
                odataQueryParams,
                false);
                
string sql = result.Item1;
// SELECT [Name], [Type] FROM [Products] WHERE [Name] = @p0
IDictionary<string, object> sqlParams = resultWithoutParsedDates.Item2; 
// {"@p0", "11/30/2022 12:00:00 AM"}

// Conversion without Date Parsing
var resultWithoutParsedDates = converter.ConvertToSQL(
                tableName,
                odataQueryParams,
                false,
                false);
string sql = resultWithoutParsedDates.Item1;
// SELECT [Name], [Type] FROM [Products] WHERE [Name] = @p0
IDictionary<string, object> sqlParams = resultWithoutParsedDates.Item2; 
// {"@p0", "2022-11-30"}

Custom SQL

You can now use custom query as source (instead of table) and be able to build query in response. WITH clause is used to wrap custom query.

Example

$apply=aggregate(TotalAmount with sum as TotalAmount, TotalAmount with average as AverageAmount,$count as OrderCount)
var rawSql = "SELECT * FROM [Orders]";
converter.ConvertToSqlFromRawSql(customSqlQuery, oDataParams, count);

is converted to

WITH [RawSql] AS (SELECT * FROM [Orders])
SELECT Sum(TotalAmount) AS TotalAmount, AVG(TotalAmount) AS AverageAmount, COUNT(1) AS OrderCount FROM [RawSql] 

Check unit tests for more usage examples.

Roadmap

  • [] Support for validating column names and column data types.

Contributing

We are always looking for people to contribute! To find out how to help out, have a look at our Contributing Guide.

Contributors

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copyright

Copyright MIT © 2020 Vaibhav Goyal. See LICENSE for details.

dynamicodatatosql's People

Contributors

aersam avatar goyalvaibhav avatar juantarquino-ssb avatar maciejpieprzyk avatar sandra-alba avatar vaibhav-goyal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dynamicodatatosql's Issues

Auto-Conversion from String to DateTime format

Hi,

I have one issue on string value which contained Month Name (will auto-change to DateTime format after gone through ConvertToSql), I believe that this is incorrect by auto-changing string to DateTime format.
Kindly refer to the screenshot below for the details.

Thanks.

Regards,
Wong Wei Weng

String with Month Name Issues

Add possibility to retrieve SqlKata query

Describe the feature

Add the possibility to retrieve a SqlKata query instead of Sql string. This will enable future enhancements of SqlKata query in client code.

Support OData syntax "indexof"

Hi,
I was wondering if the syntax "indexof" will be supported in the future. I In my scenario I need it to filter data that not contains a certain value.

Btw, nice libs! :)

if sent top value as wrong it is returning wrong message

Describe the bug

I am sending body as below

{
"filter": "",
"select": "*",
"orderBy": "",
"skip": 10,
"top": 5.5,
"apply": ""
}

To Reproduce

I am getting below response, we don't have any model field in request

{
"code": 400,
"message": "request validation failed",
"validation": [
{
"path": "top",
"message": "Input string '5.5' is not a valid integer. Path 'top', line 6, position 12.",
"id": null
},
{
"path": "model",
"message": "The model field is required.",
"id": null
}
]
}

Expected behaviour

{
"code": 400,
"message": "request validation failed",
"validation": [
{
"path": "top",
"message": "Input string '5.5' is not a valid integer. Path 'top', line 6, position 12.",
"id": null
}
]
}

Filter - column name in single quotes is not parsed

Describe the bug

Column name in SQL query is empty when filter contains column name in single quotes. I need to use single quotes because column name contains spaces and brackets.

To Reproduce

var oDataQueryParams = new Dictionary<string, string>
{
    { "filter", "'Closed (UTC)' gt '2019-10-02T05:27:45.19 02:00'"}
};

var result = converter.ConvertToSQL("t", oDataQueryParams, false);

Result is:

SELECT * FROM [t] WHERE [] > @p0

Expected behaviour

Result should be:

SELECT * FROM [t] WHERE [Closed (UTC)] > @p0

Translate the tablename/fieldname pattern passed in $expand

Describe the feature

  • I have managed to translate expand into join and modify the query that comes back from compilation
  • But i need to adjust the compilation process
  • so that it translates the tablename/fieldname pattern passed in $expand into tablename.fieldname

Case (in)sensitive support

Support for case (in)sensitive for default supported string operations via parameter to the main method.

Compute Support

Some side-note

Hi there, thanks for that cool library, I use it in our internal Generic Backend to dynamically create OData Endpoints out of a Config.

Describe the feature

I'd like to be able to execute a query as such:

compute(year(DateTimeUtc) as y, month(DateTimeUtc) as m)/groupby((y,m),aggregate(value with average as AvgValue))

That would be so awesome! :)

I just tried implementing it and it seems to be quite straightforward. Can you review it?

Upgrade package versions

Describe the feature

Could we upgrade some outdated package versions in the Test project. I am using your package but its causing vulnerability issues for packages needed to be upgraded, namely:

  1. system.text.regularexpressions.4.3.0 -> system.text.regularexpressions.4.3.1
  2. system.net.http.4.3.0 -> system.net.http.4.3.4

ConvertToSqlFromRawSql not in v 1.0.6

Question

Hi I see the ConvertToSqlFromRawSql method on the converter is in the master branch but not in version tag 1.0.6. Is this be accident, or just not yet released?

I'm looking for a join table function, I guess creating a join statement and load it by using ConvertToSqlFromRawSql will work a "work a round"?

Release of new version with SQL Kata exposed

Question

Hi, we would like to use the exported sql kata function unfortunatly the latest version is from January this year (the feature was added in june). Any plans to release a new version with this functionality added anytime soon ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.