Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHlIv-0003PL-48 for psycopg@arkaria.postgresql.org; Thu, 04 Mar 2021 10:34:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHlIu-0002o6-2P for psycopg@arkaria.postgresql.org; Thu, 04 Mar 2021 10:34:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHlIt-0002nw-Sp for psycopg@lists.postgresql.org; Thu, 04 Mar 2021 10:33:59 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lHlIr-0002dc-Me for psycopg@postgresql.org; Thu, 04 Mar 2021 10:33:59 +0000 Received: by mail-lf1-x135.google.com with SMTP id q25so22115943lfc.8 for ; Thu, 04 Mar 2021 02:33:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xfBuGDqKh8FIjG0zN4Ya8b9P8Q8uFrwpXPy6Ps6HO3A=; b=oqaFw/h9FsuyCL8Hm5j5uUdGPGS2Vxb0rpm/Hx1AoibAYC/Q86MkYweNr5Bh4oc+/F OEUOEmf4mp3XN3qfEZT0DDapVCMbjHvHDNZ6K/WNi4HSv61RsfioucpgSOBinCThTtOh H/2+NMbqhd2a2+J+Xsww6WUa+Qp3rqLm1Tlf/8NvmReGG1AHQRzjIh3QWi/Wk3i7gXP2 MJKjtTrP5u5AaJotJl0pCOcDhBg3KER8Ch2i6JGsMwmHf+a8MIZGpekpHreezGTvw0Vh 1/CNYLdBILMwXYyrZ+Ts8XPKe895tlVr7k4Y64WnLtzIvmeD7gdqyjD3ea/jfT8Fo/2I mQoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xfBuGDqKh8FIjG0zN4Ya8b9P8Q8uFrwpXPy6Ps6HO3A=; b=SVx8pNRGkPnixVNAflAPIT7gmoLxRk500qaHFMCHnK/KszYtx/xayVsiClifCZl1j9 YSzDNseE4XuDkwjDpJ4QOEH6Brnvg5vdrmBHEAkYN3nGiDKA6S8EjtSp2tRMRI+Ih9D0 gk6XIMTnaQHMY6IqEYmlGWKhtD1kzQ41j6ToVW+5xlced5jlagdt95nXGNmptC7WzABX BCPTVD7XbiqRUQn5dGEaMkYXBuA/IR0JtY69FiI+iLqR7lF3qoQPoPd/4k1MOsaX+qyM y6979T8mEtF0MLUgLaw9Rhnp6r5hgZKVzpnHgSFHmz+GyBUOsH/CRR4+NqfrGGqryhFH 4WbA== X-Gm-Message-State: AOAM532jmHR1WurSFiWBWGDW+0JrvvLM75o+jLbcJMXjJt9Kxu4sDGN2 xqvniFejzlmDCMkFhZ7Izwvi0olRWXKbE2PBQ04= X-Google-Smtp-Source: ABdhPJyIV0YH/YEQBvh9SfjRapUMj0CGuDNdrHjmKsrFXDar4k76EnkBYpDAmJMv1sDlk7eb9M3GHpX/0q3KEhsGkyc= X-Received: by 2002:ac2:58c2:: with SMTP id u2mr2009091lfo.37.1614854037140; Thu, 04 Mar 2021 02:33:57 -0800 (PST) MIME-Version: 1.0 References: <20210303215137.GI11758@artax.karlin.mff.cuni.cz> <05f8e563-162a-3a86-d939-7fb7f03811e6@aklaver.com> <20210304054327.GL11758@artax.karlin.mff.cuni.cz> In-Reply-To: <20210304054327.GL11758@artax.karlin.mff.cuni.cz> From: Daniele Varrazzo Date: Thu, 4 Mar 2021 11:33:45 +0100 Message-ID: Subject: Re: insert DEFAULT value To: Hans Ginzel Cc: Adrian Klaver , psycopg@postgresql.org Content-Type: multipart/alternative; boundary="0000000000003f78b305bcb381a4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003f78b305bcb381a4 Content-Type: text/plain; charset="UTF-8" On Thu, 4 Mar 2021, 06:43 Hans Ginzel, wrote: > On Wed, Mar 03, 2021 at 03:23:28PM -0800, Adrian Klaver wrote: > >On 3/3/21 1:51 PM, Hans Ginzel wrote: > >You need to do something like: > >cur.execute(sql.SQL("INSERT INTO test_default VALUES > ({})").format(DEFAULT)) > > Formating values into SQL statement is done by the execute(), isn't it? > It's done on the client. It's done by execute(), yes, by calling ".as_string(conn)" on the resulting object, which is something exposed by the API and which you can do too in your program. cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,)) > > Could this be added to the psycopg2 code, please? > Nope, not a chance. I thought it was a good idea when I proposed it, which was not in April 2020, when my message was fished from the interwebz memories, but I think I wrote it around 2006? Anyway I would answer my then self that it is not a good idea. Parameter passing is for parameters, DEFAULT is a SQL construct: it wouldn't work when the adapter evolves into sending query and params separately (which is happening in psycopg3). So it won't land in the adapter, no, and the correct way of doing that job is to use explicit client-side binding through the psycopg2.sql facilities, passing a 'sql.Literal(value)', or a 'sql.SQL("DEFAULT")' to override it, to a '{}' placeholder, or to pass a 'sql.Placeholder()' instead of DEFAULT... many ways to skin a client-side cat. You are free to use my old recipe but it won't be added in psycopg2, which has since grown a better way to do the same task. :) Cheers -- Daniele --0000000000003f78b305bcb381a4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Thu, 4 Mar 2021, 06:43 Hans Ginzel, <hans@matfyz.cz> wrote:
On Wed, Mar 03, 2021 at 03:23:28PM -0800, Adrian Klaver w= rote:
>On 3/3/21 1:51 PM, Hans Ginzel wrote:
>You need to do something like:
>cur.execute(sql.SQL("INSERT INTO test_default VALUES ({})").f= ormat(DEFAULT))

Formating values into SQL statement is done by the execute(), isn't it?=

= It's done on the client. It's done by execute(), yes, by calling &q= uot;.as_string(conn)" on the resulting object, which is something expo= sed by the API and which you can do too in your program.


cursor.execute("INSERT INT= O test_default VALUES (%s)", (DEFAULT,))

Could this be added to the psycopg2 code, please?

Nope, not a chance. I thou= ght it was a good idea when I proposed it, which was not in April 2020, whe= n my message was fished from the interwebz memories, but I think I wrote it= around 2006? Anyway I would answer my then self that it is not a good idea= . Parameter passing is for parameters, DEFAULT is a SQL construct: it would= n't work when the adapter evolves into sending query and params separat= ely (which is happening in psycopg3).

So it won't land in the adapter, no, and the correct way = of doing that job is to use explicit client-side binding through the psycop= g2.sql facilities, passing a 'sql.Literal(value)', or a 'sql.SQ= L("DEFAULT")' to override it, to a '{}' placeholder, = or to pass a 'sql.Placeholder()' instead of DEFAULT... many ways to= skin a client-side cat.

You are free to use my old recipe but it won't be added in psycopg2, w= hich has since grown a better way to do the same task. :)

Cheers=C2=A0

<= /div>
-- Daniele

--0000000000003f78b305bcb381a4--