#!/usr/local/bin/perl -T

use lib './module/';
use strict;
use Tie;

########################################

Decoding();

my $quest= $A->param('Q');
my $type= defined $A->param('TYPE') ? $A->param('TYPE') : '';

######################################## SELECT

my( $row,@answer )=();

if( $quest=~/^\s*SELECT/i ){

	$quest= $quest=~s/LIMIT\s+([\d\,]+)//i ? "$quest LIMIT $1" : "$quest LIMIT 1000";

	my $vv= $dbSQL->prepare( $quest );
	$vv->execute();

	while( $row= $vv->fetchrow_arrayref ){
		my @a=(); next unless $row;

		for( @{$row} ){ push(@a, $dbSQL->quote($_)) }

		if( $type=~/values/i ){ push( @answer, '('.join(',',@a).')' ) }
		else{ $Q->append( -name=>'A', -values=>join(',',@a) ) }
	}
	$vv->finish();

	$Q->param(-name=>'A', -values=>join(',',@answer)) if $type=~/values/i;
}

######################################## INSERT, REPLACE, UPDATE, DELETE, CREATE

if(	$quest=~/^\s*INSERT/i or
	$quest=~/^\s*REPLACE/i or
	$quest=~/^\s*UPDATE/i or
	$quest=~/^\s*DELETE/i or
	$quest=~/^\s*DROP\s+TABLE/i or
	$quest=~/^\s*CREATE\s+TABLE/i
){
	if( $quest=~/^\s*UPDATE/i && $quest!~/WHERE/i ){
		$Q->param(-name=>'ERROR', -values=>'в запросе нет WHERE...<br>');
	}
	elsif( $quest=~/^\s*DELETE/i && ($quest!~/WHERE/i || $quest!~/LIMIT\s+1(\s|$)/i) ){
		$Q->param(-name=>'ERROR', -values=>'в запросе нет WHERE или LIMIT 1<br>');
	}
	else{
		$Q->param(-name=>'A', -values=>$dbSQL->do( $quest ));
	}
}

######################################## ALTER, SHOW

if(	$quest=~/^\s*ALTER\s+TABLE/i ){ $Q->param(-name=>'A', -values=>$dbSQL->do($quest)) }

if(	$quest=~/^\s*SHOW\s+CREATE/i ){
	$Q->param(-name=>'A', -values=>[$dbSQL->selectrow_array($quest)]);
}
elsif( $quest=~/^\s*SHOW/i ){
	$Q->param(-name=>'A', -values=>[@{ $dbSQL->selectcol_arrayref($quest) }]);
}

Coding();

exit(0);