database icon indicating copy to clipboard operation
database copied to clipboard

Feature request: REPLACE INTO statment

Open vGhost2000 opened this issue 5 years ago • 3 comments

Feature request: REPLACE INTO выражения

К имеющимся в орм SELECT, INSERT, UPDATE, DELETE выражениям добавить так же REPLACE выражение, полностью повторяющее INSERT выражение за исключением первого слова в запросе. Использоваться одно должно в следующем случае, если при создании новой сущности будет явно задан первичный ключ (возможно так же если задан уникальный ключ). Например:

$e = new SomeEntity;
$e->id = 123; // где id это PK
$e->someField = 'someValue';
$transaction->persisit($e);
$transaction->run();

В данном случае вместо INSERT INTO, должно быть использовано выражение REPLACE INTO, в остальном отличия от insert нету.

Либо, можно посмотреть как решаются такие кейсы в других орм, в некоторых, к примеру, есть метод ->replace(), который форсированно заставляет орм делать REPLACE INTO запрос вместо INSERT INTO. Это был бы более интуитивно понятный вариант. Вообще я предложил бы реализовать такие методы, скажем persist это аналог ->save() из других орм, а рядом с ним реализовать так же методы ->update(), ->replace() и ->create() которые форсированно заставляют делать UPDATE / REPLACE / INSERT соответственно, в то время как ->persist() действует по выше описанной логике, т.е. сам автоматически выбирает между REPLACE / INSERT / UPDATE.

Feature request: REPLACE INTO expressions

To the expressions in the SELECT, INSERT, UPDATE, DELETE expressions, add the REPLACE expression, which repeats the INSERT expression except for the first word in the query. One should be used in the following case, when creating a new entity, a primary key will be explicitly specified (possibly also if a unique key is specified). For instance:

$e = new SomeEntity;
$e->id = 123; // where id is PK
$e->someField = 'someValue';
$transaction->persisit($e);
$transaction->run();

In this case, instead of INSERT INTO, the REPLACE INTO expression should be used, otherwise there is no difference from insert.

Or, you can see how such cases are solved in other orm, in some, for example, there is a method ->replace(), which forces the orm to make a REPLACE INTO request instead of INSERT INTO. This would be a more intuitive option. In general, I would suggest implementing such methods, say persist is an analogue ->save() from other orm, and next to it, implement the same methods ->update (), ->replace() and ->create() which force to do UPDATE / REPLACE / INSERT respectively, while ->persist() acts according to the logic described above, i.e. it automatically chooses between REPLACE / INSERT / UPDATE.

vGhost2000 avatar Jan 08 '20 11:01 vGhost2000

I do think this query supported by all the drivers. Are you sure this is standardized functionally?

wolfy-j avatar Jan 10 '20 14:01 wolfy-j

Например https://docs.phalcon.io/3.4/ru-ru/api/phalcon_mvc_model

image

но решайте сами делать или нет, я не настаиваю и не тороплю, по приоритетам "можно на потом отложить", просто считаю полезная фича, например в сфинксе без неё тяжко будет.




for example https://docs.phalcon.io/3.4/ru-ru/api/phalcon_mvc_model

image

but decide whether to do it yourself or not, I don’t insist and don’t rush, according to priorities “you can postpone it for later”, I just think it’s a useful feature, for example, in the sphinx it’ll be hard without it.

vGhost2000 avatar Jan 10 '20 15:01 vGhost2000

I'll take a look at it after the 1.3.0 release of cycle.

wolfy-j avatar Mar 07 '20 14:03 wolfy-j