Skip to content

Feat/polyfill condition tables#6

Draft
gwillz wants to merge 4 commits intomasterfrom
feat/polyfill-condition-tables
Draft

Feat/polyfill condition tables#6
gwillz wants to merge 4 commits intomasterfrom
feat/polyfill-condition-tables

Conversation

@gwillz
Copy link
Copy Markdown
Collaborator

@gwillz gwillz commented Feb 15, 2023

This adds a query modifier for any fields that don't already have a table.column form.

It takes the table name, or alias, from the from() modifier and simply tacks it onto any fields that are I guess 'naked'.

This applies to:

  • select() fields
  • where() conditions
  • join() conditions
  • having() conditions
  • orderBy()
  • groupBy()

Example

$query = $pdb->find('users AS user')
  ->fixTables()
  ->select('name', 'other.field')
  ->innerJoin('others AS other', ['other.user_id = user.id'])
  ->where(['OR' => [
     'other.status' => 'deleted', 
     'date_deleted' => null,
  ]])
  ->orderBy('record_order');

produces:

SELECT user.name, other.field
FROM users AS user
INNER JOIN others AS other
  ON other.user_id = user.id
WHERE other.status = 'deleted'
  OR user.date_deleted IS NULL
ORDER BY user.record_order

Regarding conditions

It only performs this modification on the left-hand side of an expression. If the right-hand is missing the table then there's not much we can do. Or is there?

I'm not sure about string conditions either. They're quite an weird edge-case altogether.

Some testing will clarify all of this.

Draft because:

  • fixTables() is a terrible name
  • no tests!
  • orderBy()/groupBy() should check against the select() aliases to prevent accidentally prefixing an alias

@gwillz gwillz requested a review from aitken85 February 15, 2023 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant