Giter Site home page Giter Site logo

Comments (3)

rehsack avatar rehsack commented on September 28, 2024 1

This should be fixed with #13

from sql-statement.

mishin avatar mishin commented on September 28, 2024

solution for this

use Modern::Perl;
my $sql1 = " SELECT CL.SHRT_NM, RT.EFF_DT, RT.EXG_RATE_VAL 

FROM HTF13.CL CL, HTF13.EXG_RATE_HIST RT 

WHERE RT.FRST_CCY_ID = 751 and RT.CCY_ID = CL.GRP_CL_ID and CL.CL_SCM_ID = 61 and RT.EFF_DT >= '01.08.2005' and (CL.END_DT = '31.12.9999' or CL.CL_ID =1004)";
my $sql2 = " SELECT AR.* 

FROM 

HTF13.AR AR LEFT OUTER JOIN HTF13.AR_X_AGN_REESTR_1 

ON 

HTF13.AR_X_AGN_REESTR_1.CST_AR_ID = AR.AR_ID OR 

HTF13.AR_X_AGN_REESTR_1.CST_SUB_AR_ID = AR.AR_ID  

WHERE 

AR.AR_PPS_TP_ID = 1527 AND 

AR.AR_TP_ID <> 1467 AND 

HTF13.AR_X_AGN_REESTR_1.CST_AR_ID IS NULL 
"
  ;

use SQL::Statement;
use Test::Warn;
use Test::More;


sub get_tables {
    my ($sql, $parser) = @_;
    my $stmt = SQL::Statement->new($sql, $parser);
    return map { uc $_->name } $stmt->tables();
}

for my $sql ($sql1, $sql2) {
    &process_sql($sql);
}

sub process_sql {
    my ($sql) = @_;

    my $parser = SQL::Parser->new();
    $parser->{RaiseError} = 1;
    $parser->{PrintError} = 1;
    my $msg_err_or  = q{Can't use OR in an ON clause!};
    my $msg_err_ref = q{referenced but not found in FROM list};


    my @tables = ();
    my $ret    = warnings_like {
        eval { @tables = get_tables($sql, $parser); };
    }
    [qr/$msg_err_or/i], "warnings: $msg_err_or";

    if ($ret) {    #change OR на END
        $sql =~ s/OR/AND/xmg;
        my $ret2 = warnings_like {
            eval { @tables = get_tables($sql, $parser); };
        }
        [qr/$msg_err_ref/i], "warnings: $msg_err_ref";
        if ($ret2) {

            use SQL::Tokenizer qw(tokenize_sql);
            my $remove_white_tokens = 1;
            my @tokens = SQL::Tokenizer->tokenize($sql, $remove_white_tokens);

            for my $i (0 .. $#tokens) {
                if (($tokens[$i] eq 'FROM') or ($tokens[$i] eq 'JOIN')) {
                    push @tables, $tokens[$i + 1];
                }
            }

        }
    }
    say join ',', @tables;
}


done_testing();



from sql-statement.

mishin avatar mishin commented on September 28, 2024

can you add this solution to a module?
it's very tricky for me

from sql-statement.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.