Skip to content
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

Feature: Add support for inserting multiple rows using a subquery #7538

Closed
wants to merge 2 commits into from

Conversation

kjohnson
Copy link
Member

@kjohnson kjohnson commented Sep 12, 2024

Related stellarwp/db#2

Description

This PR adds support for inserting the results on a query into another table using INSERT INTO.

Note: This does not support inserting multiple rows of known data (ie an array of data), rather this specifically supports using a subquery and a fluent syntax of chained methods. While support for insertMany() (or a similar feature) would also use INSERT INTO under the hood, the application logic is not covered by this implementation. Further, such a feature would more likely belong to the DB class rather than the QueryBuilder.

Affects

Adds a new insertInto() method to the CRUD trait of the QueryBuilder.

Visuals

DB::table('posts')

    // Subquery
    ->select(['ID', 'post_id'])
    ->selectRaw('(SELECT "postTitle") as meta_key')
    ->select(['post_title', 'meta_value'])

    // Insert Into
    ->insertInto('postmeta', ['post_id', 'meta_key', 'meta_value']);
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
    SELECT
        ID AS post_id,
        (SELECT "postTitle") as meta_key,
        post_title AS meta_value
    FROM wp_posts;

Testing Instructions

Pre-review Checklist

  • Acceptance criteria satisfied and marked in related issue
  • Relevant @unreleased tags included in DocBlocks
  • Includes unit tests
  • Self Review of code and UX completed

@JasonTheAdams
Copy link
Contributor

Throwing in my 0.02, I was imagining a syntax such as:

DB::table('table1'
  ->select('a', 'b', 'c')
  ->where('a', '=', 'Kyle')
  ->insertColumns('col1', 'col2', 'col3')
  ->insertInto('talble2')

@kjohnson
Copy link
Member Author

@JasonTheAdams I see what you were going for. When I did this first pass I was transcribing the INSERT INTO syntax to the Query Builder, but I'm reminded that a big benefit of the Query Builder is creating a syntax that better fits the application logic.

Ironic given my recent Slack post about this very benefit!

Goes to show how perspective can be a blind spot. As a writer I connected the path of least resistance.

@kjohnson kjohnson changed the title Feature: Add new Insert Into method to the Query Builder Feature: Add support for inserting multiple rows using a subquery Sep 13, 2024
@kjohnson kjohnson marked this pull request as ready for review September 13, 2024 16:22
@JasonTheAdams
Copy link
Contributor

I'm going to close this as we'll re-open this in the https://github.com/stellarwp/db library after some syntax brainstorming. Let's link back to this, though, for reference!

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.

3 participants