Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Returning columns with SQL::Statement error

by LalakisOeisagwmenos (Novice)
on Jan 19, 2018 at 15:28 UTC ( [id://1207536]=perlquestion: print w/replies, xml ) Need Help??

LalakisOeisagwmenos has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am sorry for bothering you but trying to execute the paradigm of SQL::Statement::Structure on a Strawberry perl v5.21.1 I had the following error.

Can't call method "name" on unblessed reference
The code is the following:
use SQL::Statement; use Data::Dumper; my $sql = "SELECT a,aa FROM b JOIN c WHERE c=? AND e=7 ORDER BY f D +ESC LIMIT 5,2"; my $parser = SQL::Parser->new(); $parser->{RaiseError}=1; $parser->{PrintError}=0; # $parser->parse("LOAD 'MyLib::MySyntax' "); my $stmt = SQL::Statement->new($sql,$parser); printf "Command %s\n",$stmt->command; printf "Num of Placeholders %s\n",scalar $stmt->params; printf "Tables %s\n",join( ',', map {$_->name} $stmt->tab +les() ); printf "Where operator %s\n",join( ',', $stmt->where->op() ); printf "Limit %s\n",$stmt->limit(); printf "Offset %s\n",$stmt->offset(); printf "Columns %s\n",join( ',', map {$_->name} $stmt->col +umn_defs() );
Output

Can't call method "name" on unblessed reference at c:\Users\PasvantidisR\Documents\scripts\perl\test_sql_statement.pl line 17.

Command SELECT
Num of Placeholders 1
Tables b,c
Where operator AND
Limit 2
Offset 5

Can you please help? Thank you Lalakis Oeisagwmenos

Replies are listed 'Best First'.
Re: Returning columns with SQL::Statement error
by poj (Abbot) on Jan 19, 2018 at 16:40 UTC

    I see your bug report has been answered but a work around in this case would be

    printf "Columns %s\n",join ',', map { $_->{'value'} } @{$s +tmt->column_defs()};
    poj
      YES! You are totaly correct. Thank you. I didn't thought I had to expand the return of $stmt->column_defs() to an array reference (a hash slice surrounding it with @{}) even though dumper was returning an array reference. What a stupid I was. And yes it is 'value' hash value.
      THANK YOU!!!
Re: Returning columns with SQL::Statement error
by thanos1983 (Parson) on Jan 19, 2018 at 16:24 UTC

    Hello LalakisOeisagwmenos,

    Welcome to the Monastery. I do not run SQL on my OS but the error seems to be coming from line 17:

    printf "Columns %s\n",join( ',', map {$_->name} $stmt->col +umn_defs() );

    Can you check if this method name is contained in?

    print Dumper $stmt->column_defs();

    Always always use strict and warnings.

    Looking forward to your update, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Thank you for your reply. I have done so before to check my self too. It seems though that this method don't exist.
      But either don't exist value map{$_->value()} or trying to access it as a hash value map{$_->{value}} either quoted or not. It seems that the self is not blessed when the columns object is created.
      $VAR1 = [ { 'type' => 'column', 'fullorg' => 'a', 'value' => 'a' }, { 'fullorg' => 'aa', 'type' => 'column', 'value' => 'aa' } ];
Re: Returning columns with SQL::Statement error
by Anonymous Monk on Jan 19, 2018 at 21:27 UTC
    Also, an old-fashioned foreach loop might be more-obvious than map. Use Data::Dumper so that you can see what $stmt consists of.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1207536]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (None)
    As of 2024-04-18 23:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found