#!/usr/bin/perl -w
use strict;
use SOAP::Lite;

#Please read at end of file for more info

#The query string
my $query;

#retrieve query from stdin
my $line = ;
while ($line) { $query = $query . $line; $line = ; }

#Make the call to the openSession service
my $result = SOAP::Lite
    -> uri('SQL99Validator')
    -> proxy('http://sqlvalidator.mimer.com/v1/services/')
    -> openSession('anonymous', 'dummy', 'PerlClient', 'v0.0001', 'N/A', 'N/A', 'N/A', 'N/A', 2)
    -> result;

#Make the call to the validateSQL service
$result = SOAP::Lite
    -> uri('SQL99Validator')
    -> proxy($result->{target})
    -> validateSQL($result->{sessionId}, $result->{sessionKey}, $query, 'text')
    -> result;

print $result->{data}, "\n";

# end of program, start of info section

# How to run this program:
# 1) echo 'select a from t where a like "c"' | ./sqlval.pl
# 2) ./sqlval.pl
#    select a from t where a like "c"
#    ^D
# 3) ./sqlval.pl < aqueryinafile.sql

# For more information about the Mimer SQL Validator web service please go to sqlvalidator.mimer.com
# or Email olof dot edlund at upright dot se
# The SQL Validator core technology was written by Ake Persson at Upright Database Technolgy (www.upright.se)

# SOAP::Lite
# To run this client you must have SOAP::Lite installed, you can install it with command:
#     perl -MCPAN -e 'install SOAP::Lite' 
# at the prompt. You can find more information about SOAP::Lite at www.soaplite.com

# Parameters for openSession
# 1) user name
# 2) password, ignored for user anonymous
# 3) The name of this client, if you write your own use that clients name
# 4) The version of this client
# 5) The target database if applicable, preferrably fetched programaticallly from DBI
# 6) The target database version, preferrably fetched programatically from DBI
# 7) The Connection technology, should almost always be DBI for Perl
# 8) The Connection technology version, preferrably fetched from DBI
# 9) Whether the client is interactive or not. 1) for interactive 2) for not interactive

# Parameters for validateSQL
# 1) target, the name of the server to be used. This may different from the server that you called openSession on for load balancing purposes.
# 2) sessionId, the unique identifier for this session.
# 3) sessionKey, this must be obtained from the openSession call. Otherwise the validateSQL service will return with an error.
# 4) sql query, the query to be validated
# 5) format, the format you want the result in. At the time of the writing of this client valid formats are text and html