sqlkata / querybuilder

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
https://sqlkata.com
MIT License
3.06k stars 499 forks source link

Any plan to add feature to generate Query from raw SQL #669

Closed spaciandd closed 1 year ago

spaciandd commented 1 year ago

I came up with a situation where I have to pass raw sql e.g. select * from country. The example I quoted is simpler one, I have complex SQL from which I would like construct Query object.

ahmad-moussawi commented 1 year ago

I think what you need here is FromRaw https://stackoverflow.com/questions/62890416/using-sqlkata-from-an-existing-query/62910347#62910347

spaciandd commented 1 year ago

It will produce result like,

select * from (select * from country) innerQuery

Which is not required.

ahmad-moussawi commented 1 year ago

Can you give me a code example of what you expect to see?

spaciandd commented 1 year ago

Just want to see SQL that I pass e.g.

select * from country where population is not null

If I above query I want Query instance to be created for it.

The FromRaw method makes it subquery which is in efficient as I want to add more filters on top of existing SQL.

So, following is inefficient query,

select * from (select * from country where population is not null) q where capital > 100000

this one is good query,

select * from country where population is not null and (capital > 100000)

So in crux I would like to construct Query based on raw SQL so that I can start building more filters on top of existing one.

Hope it will clarify, thanks.

ahmad-moussawi commented 1 year ago

Thanks for the clarification, but this is not feasible because it requires SqlKata to parse the SQL query and build an object from it, which is something out of the scope of a Query Builder.

I am not sure about your statement that nested query is inefficient from my own testing, I see most modern dbs, are able to optimize the query, and they provide a very similar query plan.

SysconNielsLucas commented 1 year ago

I just use the .SelectRaw("my full raw query with WHERE, GROUP, ORDER BY ETC"), is this what you need ?