PhpGt / Database

Database query organisation.
https://php.gt/database
MIT License
3 stars 3 forks source link

Dynamic sets #356

Closed g105b closed 1 year ago

g105b commented 1 year ago

This PR closes #355 and #347 - but before it will be merged, it will be tested in a live application.

g105b commented 1 year ago

Well, the database usage works well - inserting 20,000 rows in a single transaction is SO fast compared to doing each insert individually!

g105b commented 1 year ago

I need to introduce a new dynamic set type: "or" sets.

Example:

select
    id,
    artistId,
    title,
    type
from
    Product
where
(
    (title = 'Product A' and artistId = 'Artist 1') or
    (title = 'Product B' and artistId = 'Artist 1') or
    (title = 'Product C' and artistId = 'Artist 2')
)

This example is similar to where ... in but allows for multi-field matching.

g105b commented 1 year ago

Potential bug. I don't know for sure whether it's the responsibility of the dynamic sets to fix, but here's what I've found.

In my application, I'm using array_diff on a list of data and existing data from the database, so I can get an array full of only the new records that need inserting.

Because array_diff preserves keys, the data I end up passing to the __dynamicValueSet has keys that aren't sequential, which seems to mess things up. I can fix it by passing array_values($myData), but should the dynamic value setting normalise this on my behalf?

g105b commented 1 year ago

Something else to chew over...

insert into Product (
  id,
  artistId,
  title
)
values ( :__dynamicValueSet )

The contents of dynamicValueSet is dynamic (well, durr). In my application, I'm building it up within a loop, and the ID is the last thing I set, meaning that the columns are no longer in the same order as how they're written in the SQL.

The SQL's column order needs to match the order of the indices in the associative arrays, otherwise bad things will happen.

I'm not sure how to solve this. Is there another insert syntax to use where each field can be set individually, or is it an OK problem to have>