Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b7e816c127
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 299 lines (201 sloc) 6.793 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
package SQL::Bibliosoph::Query; {
use Moose;
use Carp;
use DBI;
    use Data::Dumper;
    use Time::HiRes qw(gettimeofday tv_interval);
    use feature qw(say);

    use SQL::Bibliosoph::Dummy;
    use SQL::Bibliosoph::Exceptions;

    our $VERSION = "2.00";

    has benchmark => ( is => 'rw', isa=>'Num', default => 0);
    has debug => ( is => 'rw', isa=>'Bool', default => 0);
    has quiet => ( is => 'rw', isa=>'Bool', default => 0);
    has dbh => ( is => 'rw', isa => 'DBI::db', required=> 1);
    has delayed => ( is => 'rw', isa => 'Bool', default=> 0);

    has name => ( is => 'rw', default=> 'unnamed');
    has st => ( is => 'rw');
    has sth => ( is => 'rw');
    has bind_links => ( is => 'rw', default => sub { return []; } );
    has bind_params=> ( is => 'rw');

    has throw_errors=> ( is => 'rw', default => 1);



sub BUILD {
my ($self) = @_;

        $self->prepare() unless $self->delayed();
}

#------------------------------------------------------------------
    
sub prepare {
        my ($self) = @_;

my $st = $self->st;

# Process bb language
my $numeric_fields = $self->parse(\$st);

        #say 'Preparing "' . $self->name() ;

$self->sth( $self->dbh()->prepare_cached($st) )
or croak "error preparing : $st";

# Set numeric bind variables
foreach (@$numeric_fields) {
$self->sth()->bind_param($_,100,DBI::SQL_INTEGER);
}

        $self->delayed(0);
    }

#------------------------------------------------------------------
sub select_many {
my ($self, $values, $splice) = @_;

        $self->prepare() if $self->delayed();

return $self->pexecute($values)->fetchall_arrayref($splice)
}

#------------------------------------------------------------------
    # with sql_calc_found_rows
sub select_many2 {
my ($self, $values,$splice) = @_;

        $self->prepare() if $self->delayed();

return (
            $self->pexecute($values)->fetchall_arrayref($splice),
            $self->dbh()->selectrow_array('SELECT FOUND_ROWS()'),
        )
}

#------------------------------------------------------------------
# It's good to return [] if not found in order to allow
# to do @{xxxx} in the caller
sub select_row {
my ($self,$values) = @_;

        $self->prepare() if $self->delayed();



return $self->pexecute($values)->fetchrow_arrayref() || [];
}

#------------------------------------------------------------------
    # Returns a hash ref
sub select_row_hash {
my ($self, $values) = @_;

        $self->prepare() if $self->delayed();

return $self->pexecute($values)->fetchrow_hashref() || {};
}

#------------------------------------------------------------------
sub select_do {
my ($self, $values) = @_;

        $self->prepare() if $self->delayed();

return $self->pexecute($values);
}

#------------------------------------------------------------------
# Private
#------------------------------------------------------------------

# Replaces #? bind variables to ?
# and retuns
sub parse {
my ($self,$st) = @_;
my @nums;

my @m = ($$st =~ m/(\#?\d*?\?)/g );
        my $numbered =0;

my $total=0;
foreach (@m) {

# Numeric field?
/\#/ && do {
push @nums, $total+1;
};

# Linked field?
/(\d+)/ && do {

                $self->bind_links()->[$total]= int($1);
                $numbered++;
};
$total++;
}
$self->bind_params($total);

        croak "Bad statament use ALL numbered bind variables, or NONE, but don't mix them in $$st "
            if $numbered && $numbered != $total;


# Replaces nums
$$st =~ s/\#?\d*?\?/?/g;

return \@nums;
}

#------------------------------------------------------------------

sub pexecute {
my ($self,$values) = @_;

        my $start_time = [ gettimeofday ] if $self->benchmark();



# Completes the input array
if (@$values < $self->bind_params()) {
$values->[$self->bind_params()-1] = undef;
}
        #say "EXE ", $self->dump(), 'VAUES', Dumper($values);

# Use links
eval {
# Has Numeric Links? ( i.e. 3? )
            my $l = $self->bind_links();
if ( @$l>0 ) {
#say("start:".Dumper($values), Dumper($l));

my @v;
foreach (@$l) {
push @v, $values->[$_-1];
}
                #say "EXE1 ". Dumper(@v);

$self->sth()->execute (@v);
}

# No links, direct param mapping ( ? ? )
else {
                #say "EXE2 ", Dumper($values);
$self->sth()->execute(
                        @$values[ 0 .. $self->bind_params() - 1 ],
                );
}
};

        if ( $@ ) {

            my $e = __PACKAGE__
                    ." ERROR $@ in statement '"
                    . $self->name()
                    . "': \""
                    . $self->st()
                    . '\"'
                    ;

            if ($self->throw_errors() ) {
               # $sth->err and $DBI::err will be true if error was from DBI
               SQL::Bibliosoph::Exception::QuerySyntaxError->throw (
                   desc => $e,
               ) unless $self->quiet() ; # print the error
            }
            else {
                print STDERR $e;
                return SQL::Bibliosoph::Dummy->new();
            }
        }

        if ( my $min_t = $self->benchmark() ) {

            my $t = tv_interval( $start_time ) ;

            # Only if it takes more that 1ms...
            print STDERR "\t[". $t *1000 . " ms] " if $t > $min_t;
        }
        print STDERR "\n" if $self->debug();

return $self->sth();
}

}

1;

__END__

=head1 NAME

SQL::Bibliosoph::Query - A SQL Prepared statement

=head1 VERSION

2.0

=head1 DESCRIPTION

Implements one prepared statement

=head1 METHODS
=head2 new

Constructor: Parameters are:

=item dbh

a DB handler

=item st

The SQL statement string, using BB syntax (SEE SQL::Bibliosoph::CatalogFile)

=item name

The SQL statement name. (only for debugging information, on statement error).

=head2 destroy

Release the prepared statement.

=head1 AUTHORS

SQL::Bibliosoph by Matias Alejo Garcia (matias at confronte.com) and Lucas Lain (lucas at confronte.com).

=head1 COPYRIGHT

Copyright (c) 2007-2009 Matias Alejo Garcia. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 SUPPORT / WARRANTY

The SQL::Bibliosoph is free Open Source software. IT COMES WITHOUT WARRANTY OF ANY KIND.


=head1 SEE ALSO
SQL::Bibliosoph
SQL::Bibliosoph::CatalogFile

At http://nits.com.ar/bibliosoph you can find:
* Examples
* VIM syntax highlighting definitions for bb files
* CTAGS examples for indexing bb files.


=head1 ACKNOWLEDGEMENTS

To Confronte.com and its associates to support the development of this module.


Something went wrong with that request. Please try again.