Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120603 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60207 invoked from network); 16 Jun 2023 17:14:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jun 2023 17:14:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ECCAB1804BE for ; Fri, 16 Jun 2023 10:14:54 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 16 Jun 2023 10:14:54 -0700 (PDT) Received: by mail-qt1-f171.google.com with SMTP id d75a77b69052e-3f9e207f5f0so10796591cf.1 for ; Fri, 16 Jun 2023 10:14:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686935694; x=1689527694; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=iqus9Vwt8d+l2pl76rEc/tHBOyvWkFb/Ps9hVzhRspI=; b=edWlNtd2QyGjEEYLyhRDrKMGgNe/BWY/PlN7PdkyJRA/Vgfo9l+G27WmLMQkCB0XNE PCnc0H77Bd0XCk41UKOVoqp4VTYLb1G0I9pz27xCKSMAnBBrACVUBfjsqmCVc/oHsDXK TNLvVLY0A6qBKP5rgZWgPLOc08aXPlFnyfvq5fq2i0m677v5IBLRHKH66DNfGqNmwsSf S8DbT6FJEIUyxHvqZ0DQk/6F2NtePPkF6Lded7zPPym1uYqOXG202v9ldXX8k0adKfjE AFNCqnZ+xrKK9iatmmdBM0PkdWD642C0XCBlkrX0CNvn2X4ZCjqu+hNFC9+6bVW9sW0o pWKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686935694; x=1689527694; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iqus9Vwt8d+l2pl76rEc/tHBOyvWkFb/Ps9hVzhRspI=; b=SzhbhkGpjhMyLPbdYncGFagXiKOp5hUDZbgqqnlP/nTQrbbGsgGtDtNgIrFyaxtDQA FmaR+oxMbf7yj+cgV0ESLc4O59+cCJ1Rad9+rtN7vgLOQo8rSiY6pAUM5jRieWtpa/gL SPyNSiFgvxcYEOG2ZXczz9smnfnQvaCqc1eunuxZLQfNx7ItQVuPbsq24q7tmDUZRLSa dI88Pa6A1lU8z4ucr1WrzenzKsTaPBxmnH6NXTwCHAgAO7fo5NKpCCUKB8/+5PuSkTLB Dzkr4Rkw2BLQ6aRKZ1GrXYhps93IyJe7kH3t2+NXaiQu0/u52Y06pLM/nIm5hFtwgXv6 y2Iw== X-Gm-Message-State: AC+VfDzvIr8XPrUZmTT/DnAqkKeLar4mD2Jc5Yj753rv4FMrzt5OhHn3 D3mLicf0+LKbo/aI1LFjnZvn/JqFdzqcw5+HGswQolglMjCSJg== X-Google-Smtp-Source: ACHHUZ58H5W4cJ5QK1yqyiile05ZfTwuXtzgTfk/8ebMl8cEsVmmQNV4mIkRbEkbacR5lSUNtO+DSZ9Dnf53iEgZ8zo= X-Received: by 2002:a05:622a:1902:b0:3f8:1f9b:29e7 with SMTP id w2-20020a05622a190200b003f81f9b29e7mr8951621qtc.26.1686935693882; Fri, 16 Jun 2023 10:14:53 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 16 Jun 2023 14:14:43 -0300 Message-ID: To: Benjamin Morel Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Possible RFC: PDOStatement::addBatch From: carlosv775@gmail.com (Vinicius Dias) > Hi Vinicius, > > What would be the reason to add this? A reason cannot just be that there = is no such function. You can easily do it with a loop like you have just sh= own. There isn't really any other way > unless we introduce some driver spe= cific SQL builder. So what would we gain from it? Is it speed, security, ef= ficiency? > > String concatenation isn't an alternative though. > > Regards, > Kamil Great question, Kamil. This new method would not add new capabilities to PH= P. It would make things easier for a batch insert. Currently, we would need something like the following. This is a real world production code, not written by me. I just decreased the number of columns and changed table/column names: ``` prepare( "INSERT INTO `table_name`( `column_1`, `column_2` ) VALUES {$values}" ); $counter =3D 0; foreach ($data_list as $data) { $statement->bindValue(++$counter, $data['specific_key'], PDO::PARAM_INT= ); $statement->bindValue(++$counter, $data['another_key'], PDO::PARAM_INT)= ; } $statement->execute(); $statement =3D null; ``` So when I said about string concatenation, that's what I meant. With the new method, we would have something like: ``` prepare("INSERT INTO `table_name`(`column_1`, `column_2`) VALUES (?, ?)"); foreach ($data_list as $data) { $statement->bindValue(1, $data['specific_key'], PDO::PARAM_INT); $statement->bindValue(2, $data['another_key'], PDO::PARAM_INT); $statemnt->addBatch(); } $statement->execute(); $statement =3D null; ``` But the biggest pro isn't this case. It's the case where beginners would have multiple individual INSERT queries being separately sent to the database because they are not familiar with the approach of concatenating the parameters to the query string. I hope I made it a bit more clear now, so to sum it up: this new method would not bring any new functionality to PHP, but it would: 1. Make the code a bit more clear; 2. Help newcomers to write bulk INSERTs. > I have a library that abstracts this for you, if that helps: https://gith= ub.com/brick/db > - Benjamin That `BulkInserter` class would basically be the point of this new addition to the core, Benjamin. But a lot of new developers are not even familiar with composer, so adding an external library is out of their reality. Having an `addBatch` method in PHP's core would definitely help them write a better and more performatic code. I once again appreciate the attention and the replies. Vinicius Dias.