Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Kramer committed Mar 18, 2013
2 parents 8c0a3e5 + 0fe1f61 commit 8ba85cb
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -4,4 +4,4 @@
tags
blib
Makefile

Makefile.old
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -14,3 +14,4 @@ lib/SQL/Bibliosoph/Sims.pm
lib/SQL/Bibliosoph/CatalogFile.pm
lib/SQL/Bibliosoph/Query.pm
lib/SQL/Bibliosoph/Dummy.pm
lib/SQL/Bibliosoph/Exceptions.pm
174 changes: 136 additions & 38 deletions Makefile

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Makefile.PL
@@ -1,6 +1,6 @@
use ExtUtils::MakeMaker;

use 5.10.0;
use 5.10.1;

WriteMakefile(
NAME => 'SQL::Bibliosoph',
Expand All @@ -14,14 +14,14 @@ WriteMakefile(
'Tie::Array::Random'=> 1.0,
'Tie::Hash::Random' => 1.0,
'Moose' => 0.82,
'DBD::Mock' => undef,
'Time::HiRes' => undef,
'Carp' => undef,
'Package::Constants'=> undef,
'Devel::Cover' => undef,
'DBD::Mock' => 1.43,
'Time::HiRes' => 1.97,
'Carp' => 1.25,
'Package::Constants'=> 0.02,
'Digest::MD5' => 2.39,
'Cache::Memcached::Fast' => 0.17,
'Storable' => undef,
'Storable' => 2.30,
'Exception::Class' => 1.30,
},
MIN_PERL_VERSION => '5.10.0',
);
Expand Down
213 changes: 140 additions & 73 deletions Makefile.old

Large diffs are not rendered by default.

54 changes: 36 additions & 18 deletions lib/SQL/Bibliosoph.pm
@@ -1,7 +1,6 @@
package SQL::Bibliosoph; {
use Moose;

use Carp;
use Data::Dumper;
use Digest::MD5 qw/ md5_hex /;
use Cache::Memcached::Fast;
Expand All @@ -10,7 +9,7 @@ package SQL::Bibliosoph; {
use SQL::Bibliosoph::Query;
use SQL::Bibliosoph::CatalogFile;

our $VERSION = "2.46";
our $VERSION = "2.52";


has 'dbh' => ( is => 'ro', isa => 'DBI::db', required=> 1);
Expand All @@ -26,21 +25,28 @@ package SQL::Bibliosoph; {

has 'queries' => ( is => 'rw', default=> sub { return {}; } );
has 'memc' => ( is => 'rw');
has 'log_prefix'=> ( is => 'rw');
has throw_errors=> ( is => 'rw', default=> 1);

## OLD (just for backwards compat)
has 'path' => ( is => 'rw', isa => 'Str', default=> '');

sub d {
my $self = shift;
print STDERR join (' ', map { $_ // 'NULL' } @_ ) if $self->debug();
my $name = shift;
print STDERR
$self->log_prefix()
. $name
. join (':', map { $_ // 'NULL' } @_ ) if $self->debug();
}

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

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

$self->log_prefix('') if ! $self->log_prefix();

$self->d( "Constructing Bibliosoph\n" ) ;

$self->path( $self->path() . '/' ) if $self->path() ;
Expand Down Expand Up @@ -227,7 +233,7 @@ package SQL::Bibliosoph; {
# Many
*$name = sub {
my ($that) = shift;
$self->d('many ',$name,@_);
$self->d('Q ',$name,@_);
return $self->queries()->{$name}->select_many([@_]);
};

Expand All @@ -236,7 +242,7 @@ package SQL::Bibliosoph; {
# Many
*$name_row = sub {
my ($that) = shift;
$self->d('manyh ',$name,@_);
$self->d('Q h_',$name,@_);
return $self->queries()->{$name}->select_many([@_],{});
};

Expand All @@ -245,7 +251,7 @@ package SQL::Bibliosoph; {

*$name_row = sub {
my ($that) = shift;
$self->d('row ',$name,@_);
$self->d('Q row_',$name,@_);
return $self->queries()->{$name}->select_row([@_]);
};

Expand All @@ -254,7 +260,7 @@ package SQL::Bibliosoph; {

*$name_row = sub {
my ($that) = shift;
$self->d('rowh ',$name,@_);
$self->d('Q rowh_',$name,@_);
return $self->queries()->{$name}->select_row_hash([@_]);
};

Expand All @@ -267,9 +273,11 @@ package SQL::Bibliosoph; {
my $ttl;
my $cfg = shift @_;

$self->d('manyCh',$name,@_);
$self->d('Q ch_',$name,@_);

croak "we calling a ch_* function, first argument must be a hash_ref and must have a 'ttl' keyword" if ref ($cfg) ne 'HASH' || ! ( $ttl = $cfg->{ttl} );
SQL::Bibliosoph::Exception::CallError->throw(
desc => "when calling a ch_* function, first argument must be a hash_ref and must have a 'ttl' keyword"
) if ref ($cfg) ne 'HASH' || ! ( $ttl = $cfg->{ttl} );

if (! $self->memc() ) {
$self->d("\n\tMemcached is NOT used, no server is defined");
Expand Down Expand Up @@ -318,7 +326,7 @@ package SQL::Bibliosoph; {
$name_row = $name . '_sth';
*$name_row = sub {
my ($that) = shift;
$self->d('sth ', $name, @_);
$self->d('Q ', $name, @_);
return $self->queries()->{$name}->select_do([@_]);
};

Expand All @@ -334,7 +342,7 @@ package SQL::Bibliosoph; {
# Many
*$name = sub {
my ($that) = shift;
$self->d('many ',$name,@_);
$self->d('Q ',$name,@_);


return wantarray
Expand All @@ -348,7 +356,7 @@ package SQL::Bibliosoph; {
*$nameh = sub {
my ($that) = shift;

$self->d('manyh ',$name,@_);
$self->d('Q h_',$name,@_);

return wantarray
? $self->queries()->{$name}->select_many2([@_],{})
Expand All @@ -365,9 +373,11 @@ package SQL::Bibliosoph; {
my $ttl;
my $cfg = shift @_;

$self->d('manyCh',$name,@_);
$self->d('Q ch_',$name,@_);

croak "we calling a ch_* function, first argument must be a hash_ref and must have a 'ttl' keyword" if ref ($cfg) ne 'HASH' || ! ( $ttl = $cfg->{ttl} );
SQL::Bibliosoph::Exception::CallError->throw(
desc => "when calling a ch_* function, first argument must be a hash_ref and must have a 'ttl' keyword"
) if ref ($cfg) ne 'HASH' || ! ( $ttl = $cfg->{ttl} );

if (! $self->memc() ) {
$self->d("\n\tMemcached is NOT used, no server is defined");
Expand Down Expand Up @@ -437,7 +447,7 @@ package SQL::Bibliosoph; {
# do
*$name = sub {
my ($that) = shift;
$self->d('inse ',$name,@_);
$self->d('Q ',$name,@_);

my $ret = $self->queries()
->{$name}
Expand All @@ -464,7 +474,7 @@ package SQL::Bibliosoph; {
# scalar : SQL_ROWS (modified rows)
*$name = sub {
my ($that) = shift;
$self->d('oth ',$name,@_);
$self->d('Q ',$name,@_);

return $self->queries()
->{$name}
Expand Down Expand Up @@ -667,8 +677,8 @@ SQL::Bibliosoph supports bind parameters in statements definition and bind
parements reordering (See SQL::Bibliosoph::CatalogFile for details).
All functions throw 'carp' on error. The error message is 'SQL ERROR' and the
mysql error reported by the driver.
All functions throw 'SQL::Bibliosoph::Exception::QuerySyntaxError' on error. The
error message is 'SQL ERROR' and the mysql error reported by the driver.
=head1 Constructor parameters
Expand Down Expand Up @@ -722,6 +732,14 @@ printed to STDERR after each query execution, if the time is bigger that
To enable debug (prints each query, and arguments, very useful during
development).
=head3 throw_errors
Enable by default. Will throw SQL::Bibliosoph::Exceptions on errors. If disabled,
will print to STDERR. By default, duplicate key errors are not throwed are exception
set this variable to '2' if you want that.
=head3 duplicate_key
=head1 Bibliosoph
n. person having deep knowledge of books. bibliognostic.
Expand Down
10 changes: 7 additions & 3 deletions lib/SQL/Bibliosoph/CatalogFile.pm
@@ -1,9 +1,9 @@
package SQL::Bibliosoph::CatalogFile; {
use Moose;
use utf8;
use Carp;
use Data::Dumper;
use Package::Constants;
use SQL::Bibliosoph::Exceptions;

our $VERSION = "2.00";

Expand All @@ -25,7 +25,9 @@ package SQL::Bibliosoph::CatalogFile; {
$self->read_only(1);
}

croak "File does not exists $file " if ! -e $file;
SQL::Bibliosoph::Exception::CatalogFileError->throw(
desc => "File does not exists $file "
) if ! -e $file;
}

#------------------------------------------------------------------
Expand Down Expand Up @@ -116,7 +118,9 @@ package SQL::Bibliosoph::CatalogFile; {

my $FH;
open ($FH,$file)
or croak "Could not read \"".$file."\" : $!";
or SQL::Bibliosoph::Exception::CatalogFileError->throw(
desc => "Could not read \"".$file."\" : $!"
);

my @all = <$FH>;
close ($FH);
Expand Down
27 changes: 27 additions & 0 deletions lib/SQL/Bibliosoph/Exceptions.pm
@@ -0,0 +1,27 @@
#===============================================================================
#
# FILE: Exceptions.pm
# CREATED: 07/13/2008 07:00:36 AM ART
#===============================================================================

use strict;
use warnings;

package SQL::Bibliosoph::Exceptions;

use Exception::Class (
'SQL::Bibliosoph::Exception::QuerySyntaxError' => {
description => 'Syntax Error',
fields => [ qw(desc) ],
},
'SQL::Bibliosoph::Exception::CallError' => {
description => 'Function Call Error',
fields => [ qw(desc) ],
},
'SQL::Bibliosoph::Exception::CatalogFileError' => {
description => 'Catalog File Error',
fields => [ qw(desc) ],
},
);

1;
23 changes: 15 additions & 8 deletions lib/SQL/Bibliosoph/Query.pm
@@ -1,12 +1,12 @@
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";

Expand All @@ -22,7 +22,7 @@ package SQL::Bibliosoph::Query; {
has bind_links => ( is => 'rw', default => sub { return []; } );
has bind_params=> ( is => 'rw');

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



Expand All @@ -45,7 +45,9 @@ package SQL::Bibliosoph::Query; {
#say 'Preparing "' . $self->name() ;

$self->sth( $self->dbh()->prepare_cached($st) )
or croak "error preparing : $st";
or SQL::Bibliosoph::Exception::QuerySyntaxError->throw(
desc => "error preparing : $st"
);

# Set numeric bind variables
foreach (@$numeric_fields) {
Expand Down Expand Up @@ -140,9 +142,9 @@ package SQL::Bibliosoph::Query; {
}
$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;

SQL::Bibliosoph::Exception::QuerySyntaxError->throw(
desc => "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;
Expand Down Expand Up @@ -200,9 +202,14 @@ package SQL::Bibliosoph::Query; {
. '\"'
;

if ($self->throw_errors() ) {
if (
$self->throw_errors() == 2
|| ($self->throw_errors() == 1 && $e !~ /\sDuplicate entry\s/ )
) {
# $sth->err and $DBI::err will be true if error was from DBI
carp $e unless $self->quiet() ; # print the error
SQL::Bibliosoph::Exception::QuerySyntaxError->throw (
desc => $e,
) unless $self->quiet() ; # print the error
}
else {
print STDERR $e;
Expand Down
9 changes: 8 additions & 1 deletion t/biblio.t
Expand Up @@ -17,6 +17,10 @@ SELECT * FROM users WHERE id = #1? AND name = 2?
--[ USERS3 ]
SELECT * FROM users WHERE id = #1? AND name = 2?
--[ BROKEN ]
SELECT * BROKEN users WHERE id = #1? AND name = 2?
END
Expand All @@ -27,7 +31,8 @@ my $dbh = DBI->connect('DBI:Mock:', '', '')

require_ok('SQL::Bibliosoph');

my $bb = new SQL::Bibliosoph( {dbh => $dbh, catalog_str => $str } );
my $bb = new SQL::Bibliosoph( {dbh => $dbh, catalog_str => $str,
} );

isa_ok($bb,'SQL::Bibliosoph');

Expand All @@ -48,4 +53,6 @@ is(ref($q),'HASH','Simple query 3');
$q = $bb->USERS3_sth();
is(ref($q), 'DBI::st', 'Simple query 3 STH');



done_testing();

0 comments on commit 8ba85cb

Please sign in to comment.