sqlpp11 icon indicating copy to clipboard operation
sqlpp11 copied to clipboard

Support for views

Open woytaz opened this issue 3 years ago • 7 comments

I started using the library just recently and I like it a lot, but I quickly noticed there is no support whatsoever for views. Are there any plans for changing that?

SQLite makes things a bit easier since views are read-only:

You cannot DELETE, INSERT, or UPDATE a view. Views are read-only in SQLite.

From what I gathered (some) other engines allow inserting through views but even then a read-only implementation would be very helpful.

woytaz avatar Aug 21 '22 10:08 woytaz

Hi,

That's a good observation. I have never used views myself.

Would you want to create the view programmatically? Or would you want to define it in the DDL?

Thanks, Roland

rbock avatar Aug 22 '22 17:08 rbock

I rarely use them myself, but it just so happened that I found a use for one. (It shows sums of values from some records)

I was just thinking about having it in the DDL, just like the tables right now.

woytaz avatar Aug 22 '22 17:08 woytaz

I played with the idea earlier today, see https://github.com/rbock/sqlpp11/tree/feature-view. I guess the most difficult part will be parsing the DDL and creating the correct type structure from it.

Note that sqlpp11 supports sub-queries. In my probably naive understanding of views that is kinda similar since you can store the sub-query in a variable and use it like a virtual table.

rbock avatar Aug 22 '22 18:08 rbock

Yeah, I managed to use subqueries to do the same thing and was impressed when it all just worked as I hoped it would.

(Then I discovered I also need coalesce, which isn't available in the library yet haha. I'll try working around it with custom queries or verbatim.)

woytaz avatar Aug 22 '22 18:08 woytaz

Assuming that the arguments of coalesce all share the same type (e.g. are all text, or are all integral), adding coalesce should be rather easy to implement. Do you want to give it a try?

rbock avatar Aug 23 '22 04:08 rbock

Interestingly, it appears you can mix types in coalesce: http://sqlfiddle.com/#!5/d6a97/8

I might look into implementing it (at least in the more limited version, assuming the same type for all arguments) over the weekend.

woytaz avatar Aug 23 '22 05:08 woytaz

Looking forward to it. Let me know if you need help.

rbock avatar Aug 24 '22 05:08 rbock