pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: stan <stanb@panix.com>
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: plperl syntax question
Date: Thu, 12 Mar 2020 21:39:01 -0400
Message-ID: <20200313013901.GA6722@panix.com> (raw)
In-Reply-To: <FDA76C9B-54A9-43B6-BBB8-64D17FF0BE44@gmail.com>
References: <20200312223402.GA613@panix.com>
	<1F75920B-15CE-445F-8F2A-B8740DC845D8@gmail.com>
	<20200312224943.GA22750@panix.com>
	<BBC0E37E-694C-4DDB-AE55-939682111A43@gmail.com>
	<20200312231600.GA19739@panix.com>
	<FDA76C9B-54A9-43B6-BBB8-64D17FF0BE44@gmail.com>

On Thu, Mar 12, 2020 at 06:00:01PM -0600, Rob Sargent wrote:
> 
> 
> > On Mar 12, 2020, at 5:16 PM, stan <stanb@panix.com> wrote:
> > 
> > On Thu, Mar 12, 2020 at 04:58:08PM -0600, Rob Sargent wrote:
> >> 
> >> 
> >>> On Mar 12, 2020, at 4:49 PM, stan <stanb@panix.com> wrote:
> >>> 
> >>> On Thu, Mar 12, 2020 at 04:45:58PM -0600, Rob Sargent wrote:
> >>>> 
> >>>> 
> >>>>> On Mar 12, 2020, at 4:34 PM, stan <stanb@panix.com> wrote:
> >>>>> 
> >>>>> I am trying to run a query in plperl that uses a Perl variable from the
> >>>>> same function.
> >>>>> 
> >>>>> Here is the code snippet:
> >>>>> 
> >>>>> my $stmt = qq("SELECT employee_key from employee where id = '$user' ;");
> >>>>> elog( NOTICE, "stmt = $stmt" );
> >>>>> my $rv3 = spi_exec_query('$stmt');
> >>>>> 
> >>>>> Here is the runtime output:
> >>>>> 
> >>>>> NOTICE:  stmt = "SELECT employee_key from employee where id = 'stan' ;"
> >>>>> ERROR:  syntax error at or near "$" at line 22.
> >>>>> 
> >>>>> As you can see, the statement seems correctly formatted, right?
> >>>>> 
> >>>>> What am I doing wrong here?
> >>>>> 
> >>>>> -- 
> >>>>> "They that would give up essential liberty for temporary safety deserve
> >>>>> neither liberty nor safety."
> >>>>> 						-- Benjamin Franklin
> >>>>> 
> >>>> ?try
> >>>> my $rv3 = spi_exec_query($stmt);
> >>> 
> >>> OK, now  I have:
> >>> 
> >>> my $stmt = qq("SELECT employee_key from employee where id = '$user' ;");
> >>> elog( NOTICE, "stmt = $stmt" );
> >>> my $rv3 = spi_exec_query($stmt);
> >>> 
> >>> And the runtime output is now:
> >>> 
> >>> NOTICE:  stmt = "SELECT employee_key from employee where id = 'stan' ;"
> >>> ERROR:  syntax error at or near ""SELECT employee_key from employee where
> >>> id = 'stan' ;"" at line 22.
> >>> 
> >> 
> >> Change you double quotes to single around you select stmt.  $user probably doesn???t need quotes at all
> >> 
> >>> Looks like somehow that is createing a double set of quotes
> >>> -- 
> >>> "They that would give up essential liberty for temporary safety deserve
> >>> neither liberty nor safety."
> >>> 						-- Benjamin Franklin
> >> 
> > Thanks.
> > 
> > my $stmt = qq('SELECT employee_key from employee where id = $user ;');
> > elog( NOTICE, "stmt = $stmt" );
> > my $rv3 = spi_exec_query($stmt);
> > 
> > runtimeL
> > 
> > NOTICE:  stmt = 'SELECT employee_key from employee where id = stan ;'
> > ERROR:  syntax error at or near "'SELECT employee_key from employee where
> > id = stan ;'" at line 22.
> > CONTEXT:  PL/Perl function "a_foo"
> > 
> > This is not normally hard to get working in non plperl.
> > 
> Sorry, I didn???t know qq was adding surrounding double quotes.
> You may need something like 
> qq(SELECT employee_key from employee where id = ') + $user + qq(';);
> or
> qq(SELECT employee_key from employee where id = '$user???;);
> 
> depending on how qq works. 
> 

I will play with it, but i am not optimistic. You see the elog, that prints
the string that comes out of qq, right? qq is for quoting strings to be
passed to DBD/DBI calls.

I cannot figure out why the call in plpwel is so strange. I would love to
see a workign example of a call to this fucntion that uses a variable, and
works.

-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





view thread (8+ messages)  latest in thread

Message-ID: <20200313013901.GA6722@panix.com>
Permalink:  ../20200313013901.GA6722@panix.com/
Also on:    postgresql.org/message-id/20200313013901.GA6722@panix.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: stanb@panix.com, robjsargent@gmail.com
  Subject: Re: plperl syntax question
  In-Reply-To: <20200313013901.GA6722@panix.com>
  References: <20200312223402.GA613@panix.com> <1F75920B-15CE-445F-8F2A-B8740DC845D8@gmail.com> <20200312224943.GA22750@panix.com> <BBC0E37E-694C-4DDB-AE55-939682111A43@gmail.com> <20200312231600.GA19739@panix.com> <FDA76C9B-54A9-43B6-BBB8-64D17FF0BE44@gmail.com> <20200313013901.GA6722@panix.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox