nextras / dbal

Database Abstraction Layer – concise secure API to construct queries & fetch data
https://nextras.org/dbal
MIT License
79 stars 28 forks source link

Make QueryBuilder extendable #174

Closed mskocik closed 2 years ago

mskocik commented 2 years ago

Due to properties and internal methods marked as private, child class need to copy-paste basically whole class. It would be great if all private properties and method will be changed to protected.

mabar commented 2 years ago

It would be great to specify some examples when QueryBuilder has to be extended. Either creating a system solution or using composition over inheritance are usually the preferable ways.

mskocik commented 2 years ago

My typical use case is for usage in conjunction with Nextras\Orm and Contributte\DataGrid. And for example when I needed QB with UNION support. I had 2 options:

hrach commented 2 years ago

Please be more specific, ideally provide your code. I do not want to make everything protected without any understatement.

hrach commented 2 years ago

cc @mskocik

mskocik commented 2 years ago

Please be more specific, ideally provide your code. I do not want to make everything protected without any understatement.

Here is the link to code diff between QB and extended QB. As you can see I am mainly overriding getSqlForSelect() method and I need to access $args property.

Is the QB class code enough, or shall I provide more context?

hrach commented 2 years ago

I'd rather see what you need to do then how you solved it. I can imagine having totally different class that joins two QueryBuilders and proxies call to them.

mskocik commented 2 years ago

My use case: I have some tables, for which I store changes in separate history table, always named <table>_history. I have grid (using ublaboo/datagrid) which shows data of all these history tables (changes) in one place. This allows me to see all changes chronologically. I can "filter on/off" which history tables (context) I am interested in. Let's say by default I show tables A, B, C, D. But I can set it to show just records from tables B and D. Records are always ordered by datetime across tables.


So basically I need to apply where per table, union them together and order by datetime globally. So in QB among other things need access to $where property and rewrite how SELECT query is generated based on how many unions I have there.