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 1jdYHW-0006wl-VS for psycopg@arkaria.postgresql.org; Tue, 26 May 2020 12:02:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jdYHV-0007La-Uj for psycopg@arkaria.postgresql.org; Tue, 26 May 2020 12:02:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdYHC-0005GX-1s for psycopg@lists.postgresql.org; Tue, 26 May 2020 12:01:46 +0000 Received: from securemail-y17.synaq.com ([196.35.198.77]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdYH4-0002nH-Np for psycopg@postgresql.org; Tue, 26 May 2020 12:01:44 +0000 Received: from [197.90.32.26] (helo=[192.168.0.8]) by securemail-pl-omx12.synaq.com with esmtpa (Exim 4.92.3) (envelope-from ) id 1jdYGW-000aQH-Lx; Tue, 26 May 2020 14:01:05 +0200 Subject: Re: Minor issue To: Rory Campbell-Lange Cc: psycopg@postgresql.org References: <92a25cd6-5b50-3074-0da6-7394db9e0f0a@chagford.com> <20200526114541.GA21741@campbell-lange.net> From: Frank Millman Message-ID: <372a461a-6b6d-b499-db42-dfd045b76720@chagford.com> Date: Tue, 26 May 2020 14:00:56 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 MIME-Version: 1.0 In-Reply-To: <20200526114541.GA21741@campbell-lange.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information X-SYNAQ-Pinpoint-ID: 1jdYGW-000aQH-Lx X-SYNAQ-Pinpoint: Found to be clean X-SYNAQ-Pinpoint-SpamCheck: not spam, SpamAssassin (not cached, score=-1.1, required 9, autolearn=disabled, ALL_TRUSTED -1.00, DCC_REPUT_13_19 -0.10) X-Pinpoint-From: frank@chagford.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-05-26 1:45 PM, Rory Campbell-Lange wrote: > On 26/05/20, Frank Millman (frank@chagford.com) wrote: >> I have a function that returns a complex SQL query and a tuple of >> parameters. The query is stored inside the function as a triple-quoted >> string, and the parameters are derived depending on the input arguments. >> >> Sometimes while testing I will comment out some of the SQL using '--'. If >> those lines happen to contain a parameter placeholder ('%s') I expected to >> remove the parameter from the tuple as well. > > Could you provide an example? > Sure. Here is my function - """ def get_sql(company, conn, bal_date): sql = (""" SELECT {1} AS "[DATE]" , c.gl_code , SUM(COALESCE(b.tran_tot, 0)) AS "[REAL2]" FROM ( -- SELECT ( -- SELECT c.row_id FROM {0}.gl_totals c -- WHERE c.tran_date <= {1} -- AND c.gl_code_id = d.row_id -- AND c.location_row_id = e.row_id -- AND c.function_row_id = f.row_id -- AND c.tran_type_id = g.row_id -- AND c.deleted_id = 0 -- ORDER BY c.tran_date DESC LIMIT 1 -- ) AS cl_row_id -- FROM {0}.gl_codes d, {0}.adm_locations e, {0}.adm_functions f, {0}.gl_tran_types g -- WHERE e.location_type != 'group' -- AND f.function_type != 'group' SELECT a.row_id FROM {0}.gl_codes b CROSS JOIN {0}.adm_locations c CROSS JOIN {0}.adm_functions d CROSS JOIN {0}.gl_tran_types e LEFT OUTER JOIN ( SELECT gl_code_id,location_row_id,function_row_id,tran_type_id,row_id,tran_date, ROW_NUMBER() OVER (PARTITION BY gl_code_id,location_row_id,function_row_id ORDER BY tran_date DESC) row_num FROM {0}.gl_totals WHERE deleted_id = 0 AND tran_date <= {1} ) a ON a.gl_code_id = b.row_id AND a.location_row_id = c.row_id AND a.function_row_id = d.row_id AND a.tran_type_id = e.row_id AND a.row_num = 1 ) AS a LEFT JOIN {0}.gl_totals b on b.row_id = a.row_id JOIN {0}.gl_codes c ON c.row_id = b.gl_code_id GROUP BY b.gl_code_id, c.gl_code HAVING b.gl_code_id IS NOT NULL ORDER BY b.gl_code_id """.format(company, conn.constants.param_style) ) params = (bal_date, bal_date) fmt = '{:%d-%m-%Y} : {:<12}{:>12}' return sql, params, fmt """ You will see 3 occurrences of '{1}'. This is replaced at runtime by the appropriate placeholder, namely '?' for pyodbc and sqlite3, '%s' for psycopg2. One of them is in a line that starts with '--'. The tuple of parameters has 2 items. It works with pyodbc and sqlite3. Using psyocpg2, it only works if I add a third item. Frank