yiisoft / yiisoft/db

Refactor `Command` and `Query` for DB 3.0

Open
#1,046 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

severity:BC breaking
Dominant language
PHP
Stars
216
Forks
51
Avg merge
3d 10h
Merged PRs (30d)
2

Description

classDiagram
    Connection --> InsertCommand
    Connection --> SelectCommand
    Connection --> UpdateCommand
    Connection --> DeleteCommand
    
    InsertCommand --> InsertQuery
    SelectCommand --> SelectQuery
    UpdateCommand --> UpdateQuery
    DeleteCommand --> DeleteQuery

    InsertQuery -- InsertQueryBuilder
    SelectQuery -- SelectQueryBuilder
    UpdateQuery -- UpdateQueryBuilder
    DeleteQuery -- DeleteQueryBuilder

    class Connection {
        insert()
        select()
        update()
        delete()
    }

    class InsertCommand {
        execute()
        onConflict() / upsert()
        returning()
        batch()
    }

    class SelectCommand {
        all()
        one()
        column()
        scalar()
    }

    class UpdateCommand {
        execute()
    }

    class DeleteCommand {
        execute()
    }
  1. The current Query needs to be split into SelectCommand and SelectQuery;
  2. Everything related to Query should be moved from Command to SelectCommand;
  3. SelectCommand contains SelectQuery and acts as a proxy;
  4. Parts related to Query should be moved from QueryBuilder to SelectQueryBuilder and should work with SelectQuery;
  5. SelectQuery must not depend on ConnectionInterface;
  6. The remaining InsertCommand, UpdateCommand, DeleteCommand commands are done similarly, but with their own executing methods, for example InsertCommand::retuning() similar to the current Command::insertReturning();
  7. InsertQuery, SelectQuery, UpdateQuery, DeleteQuery should follow to #714
  8. The corresponding methods for creating the commands should be added to ConnectionInterface;
  9. Examples of how it will work after the changes:
$db->insert($table, $values)->execute();
$db->insert($table, $values)->onConflict($newValues)->returning(['id']);
$db->insert($table, $values)->batch();
$db->select($columns)->from($table)->where($condition)->all(); // as now
$db->update($table, $values, $condition)->execute();
$db->update($table, $values)->from($from)->where($condition)->execute();
$db->delete($table, $condition)->execute();

This will allow to add more options to commands and build more complex queries.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing Command, Query, and QueryBuilder implementations together with ConnectionInterface. Compare their current responsibilities with the proposed command/query split and review issue #714 before deciding the migration scope. Done means the four command families and their builders expose the stated examples without SelectQuery depending on ConnectionInterface.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.