Skip to content

Add BaseBuilder::deleteBatch() #6734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 21, 2022
Merged

Conversation

sclubricants
Copy link
Member

@sclubricants sclubricants commented Oct 22, 2022

Needs #6741

The *Batch() saga continues..

This adds deleteBatch() to Builder. This allows using a set of data to selectively delete items from a table.

$data = [
    ['userid' => 1, 'username' => 'Derek Does Not Match', 'unused' => 'You can have fields you dont use'],
    ['userid' => 2, 'username' => 'Ahmadinejad', 'unused' => 'You can have fields you dont use'],
];

$this->db->table('user')
    ->setData($data)
    ->onConstraint(['id' => 'userid', 'name' => 'username'])
    ->deleteBatch();

$this->seeInDatabase('user', ['email' => '[email protected]', 'name' => 'Derek Jones']);

$this->dontSeeInDatabase('user', ['email' => '[email protected]', 'name' => 'Ahmadinejad']);

You can also use where() for additional conditions except with SQLite.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@sclubricants sclubricants added enhancement PRs that improve existing functionalities database Issues or pull requests that affect the database layer 4.3 labels Oct 22, 2022
@sclubricants
Copy link
Member Author

One thing that this PR does is allow to delete a set of data with a composite primary key.

$orderLineItems = [
    ['order' => 48321, 'line' => 3, 'product' => '500G Hardrive',    'qty' => 1, 'price' => 149.99],
    ['order' => 48321, 'line' => 5, 'product' => '32G DDR4 Ram',     'qty' => 2, 'price' => 284.75],
    ['order' => 48321, 'line' => 6, 'product' => 'English Keyboard', 'qty' => 1, 'price' =>  49.87],
];

$this->db->table('order_line_items')
         ->setData($orderLineItems)
         ->onConstraint('order, line')
         ->deleteBatch();

@sclubricants
Copy link
Member Author

If we can merge #6689 then I will also add delete from query.

@sclubricants
Copy link
Member Author

Or we could merge this first and add delete from query to #6689

@kenjis
Copy link
Member

kenjis commented Nov 4, 2022

This is just an English question.
What do you mean by sudo or sudo table ?

/**
* Set table alias for dataset sudo table.
*/
public function setAlias(string $alias): BaseBuilder

@kenjis
Copy link
Member

kenjis commented Nov 4, 2022

You can also use where() for additional conditions except with SQLite.

Please add a note in the user guide.

@sclubricants
Copy link
Member Author

sclubricants commented Nov 4, 2022

What do you mean by sudo or sudo table ?

I spelled it wrong, it should be pseudo.

I mean that when you have:

(
    SELECT 1 id, 'Jason' name UNION ALL
    SELECT 2 id, 'Todd'  name
) AS pseudo_table

You are creating a table in SQL. This snippet of code can be dropped anywhere a table name can go. By itself though it is not a valid query. You can query this pseudo table just like any other table.. you can join this table like any other table. Its a pseudo table because the table as aliased doesn't really exist as a table in the schema but is generated on the fly.

SELECT pseudo_table.name
FROM
(
    SELECT 1 id, 'Jason' name UNION ALL
    SELECT 2 id, 'Todd'  name
) AS pseudo_table
WHERE pseudo_table.id = 1

# Here is with a real table
SELECT pseudo_table.name
FROM pseudo_table
WHERE pseudo_table.id = 1

pseudo-

prefix: pseudo-; prefix: pseud-
1. supposed or purporting to be but not really so; false; not genuine.
2. resembling or imitating.

https://en.wikipedia.org/wiki/Pseudo-

@kenjis
Copy link
Member

kenjis commented Nov 4, 2022

I spelled it wrong, it should be pseudo.

Ah, it's pronounced the same.
I got it!

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to ask you to check one thing, but other than that, I think this is ready.

@sclubricants
Copy link
Member Author

I would like to ask you to check one thing, but other than that, I think this is ready.

This PR is pretty strait forward I think. This mostly falls in the footprint of the other *Batch() methods. I don't think there are any major controversies to discuss here are there? I know for me this would be very helpful as most of my database has composite primary keys. I think the order line item is a good example of where this is useful.

@kenjis what do you think? Any chance of getting this committed before 4.3?

@kenjis
Copy link
Member

kenjis commented Nov 18, 2022

I would like to include this in v4.3.0.
I will see this today or tomorrow.

@MGatner
Copy link
Member

MGatner commented Nov 19, 2022

Great news! Let me know how I can help.

@kenjis
Copy link
Member

kenjis commented Nov 20, 2022

I have made some minor comments, but overall it looks good.

Also, the base is a bit old, so please rebase.

@kenjis kenjis merged commit 2477f08 into codeigniter4:4.3 Nov 21, 2022
@sclubricants sclubricants deleted the DeleteBatch branch November 21, 2022 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Issues or pull requests that affect the database layer enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants