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 1kdeku-00014l-DP for psycopg@arkaria.postgresql.org; Fri, 13 Nov 2020 19:29:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kdekt-0007S9-2i for psycopg@arkaria.postgresql.org; Fri, 13 Nov 2020 19:29:07 +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 1kdeks-0007Rv-RF for psycopg@lists.postgresql.org; Fri, 13 Nov 2020 19:29:06 +0000 Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kdekq-0007zP-EU for psycopg@postgresql.org; Fri, 13 Nov 2020 19:29:06 +0000 Received: by mail-lj1-x229.google.com with SMTP id l10so12133688lji.4 for ; Fri, 13 Nov 2020 11:29:04 -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=BRBuaE6fPlb+KpdRvd3Pbr0iwQvqxfvWz1DvX4Lb7aw=; b=iEBnIrmwWUIG/OgeUb5OiJ+R5WAdb67JsFurFGe4kDsnj7BqOoYISI+jC/N8QylcAg ucTiu9QK/CfBpcj5nbbjiglTR5AQXEAhKltJ88ECI5JvKhpgzDA8R8SuDMDXHBeuS2oV lbeyBaVTbN8nsUdq2Yb6sXan4ZAh4CdaRyXN2xlUcbtg1YO5WYB1g3Qgkc4jwCmYfghg Nj7GBkC5Y8jqUSJ5LTj7oROgS9BtGtFKDSjRkQv+ls46N04m0T/TySdpWloSD4FvgI1p seg9dqeJ398Zk+iiKoBfS+Tnd3Rj+c7voTvQmIaMcWnkXUA3HCwdYdxSrD/VX1MYzNap YuOA== 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=BRBuaE6fPlb+KpdRvd3Pbr0iwQvqxfvWz1DvX4Lb7aw=; b=aiB2npECxRcdLFKwnIb+Gt4l4mdM1Oj1Mt5BTM/ybgi0tQlxmxhn5hpEfA9Q5O+Ak5 GEOP2IwBpa2gNVuEPvF6WIqWu9jeS4yVLDmLybNzgVKWLfy26pd0MV+YLVs8sW1bMiet AaK71uHjRIIHHLUFbCxJ0Jcdqy5IbdcHF07jyuo/3ZMzrRSIY/bcCFrBtHpSbjK2BQow lrPf9GmI56I0JZGt/HR57QBQOeokhPtBC0tO7NgeiufkWK1khjl8Us3Hxz9CWTFtrIJU OvLvkvd4++dTsZdiEu9s+KAWEB0nHIqJVJ6j/1pqdGhUAMqkI4DjQhQ3VkjvVZWFULto 36mw== X-Gm-Message-State: AOAM533wHt5PjFR5/6qfvPfvF5WgtuSOTOAlKK559ehcsY7r0ap1nYgo as/7rK7WJoQsZ5GssHS8qXjIbrRsQV3bSXA94cc= X-Google-Smtp-Source: ABdhPJyp+krOlRIbm+Vf9gbZ7c4Oyt5fV0Ht3s1A97FGavh0Gh4wLGYLBeLi5VSryJjjFMAGaG4PyDo7gKMPGRC5hx0= X-Received: by 2002:a2e:96c4:: with SMTP id d4mr1742875ljj.37.1605295742595; Fri, 13 Nov 2020 11:29:02 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Fri, 13 Nov 2020 19:28:51 +0000 Message-ID: Subject: Re: First psycopg3 docs To: Karsten Hilbert Cc: psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 13 Nov 2020 at 17:37, Karsten Hilbert wrote: > > Hello Daniele, > > I see that the Cursor.execute() will continue to support > a Mapping for passing in values. > > Perhaps my understanding is lacking, so here goes: > > Will using a mapping still allow for use of the binary > protocol ? I am asking because all the examples I've > seen show the %s (rather than %(varname)s way of > value passing). Yes, mapping is still supported, %(name)s parameters are not going to disappear. >>> cur.execute("select %(p1)s, %(p2)s, %(p1)s", {"p1": "v1", "p2": "v2", "p3": "v3"}).fetchone() There is a query transformation object that will convert a query with Python placeholders to postgres placeholders ($1, $2). In case a mapping is used, it also converts the mapping to a list. Everything happens behind the scene. Roughly: >>> pgq = psycopg3.cursor.PostgresQuery(cur._transformer) >>> pgq.convert("select %(p1)s, %(p2)s, %(p1)s", {"p1": "v1", "p2": "v2", "p3": "v3"}) >>> pgq.query b'select $1, $2, $1' >>> pgq.params [b'v1', b'v2'] On the way out, binary and text parameters can be selected on a per-value basis: >>> pgq.convert("select %(p1)s, %(p2)b, %(p1)s", {"p1": "v1", "p2": Int4(64), "p3": "v3"}) >>> pgq.query b'select $1, $2, $1' >>> pgq.params [b'v1', b'\x00\x00\x00@'] >>> pgq.formats [, ] > In GNUmed I nearly always use %(varname)s with dicts > as that allows for easier collection of values > before the execute() call without needing to account > for the *order* of values. Of course: names placeholders are the handiest way to deal with parameters. While psycopg3's different way of runnng queries will have incompatibilities, the intention is not to force everyone to rewrite the entirety of their codebase :) -- Daniele