Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
2 parents 8c0a3e5 + 0fe1f61 commit 8ba85cb190c0e513ca3dec172476ddcff83a4a8c Jonas Kramer committed Mar 18, 2013
Showing with 378 additions and 149 deletions.
  1. +1 −1 .gitignore
  2. +1 −0 MANIFEST
  3. +136 −38 Makefile
  4. +7 −7 Makefile.PL
  5. +140 −73 Makefile.old
  6. +36 −18 lib/SQL/Bibliosoph.pm
  7. +7 −3 lib/SQL/Bibliosoph/CatalogFile.pm
  8. +27 −0 lib/SQL/Bibliosoph/Exceptions.pm
  9. +15 −8 lib/SQL/Bibliosoph/Query.pm
  10. +8 −1 t/biblio.t
View
@@ -4,4 +4,4 @@
tags
blib
Makefile
-
+Makefile.old
View
@@ -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
View
174 Makefile

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,6 +1,6 @@
use ExtUtils::MakeMaker;
-use 5.10.0;
+use 5.10.1;
WriteMakefile(
NAME => 'SQL::Bibliosoph',
@@ -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',
);
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,7 +1,6 @@
package SQL::Bibliosoph; {
use Moose;
- use Carp;
use Data::Dumper;
use Digest::MD5 qw/ md5_hex /;
use Cache::Memcached::Fast;
@@ -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);
@@ -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() ;
@@ -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([@_]);
};
@@ -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([@_],{});
};
@@ -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([@_]);
};
@@ -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([@_]);
};
@@ -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");
@@ -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([@_]);
};
@@ -334,7 +342,7 @@ package SQL::Bibliosoph; {
# Many
*$name = sub {
my ($that) = shift;
- $self->d('many ',$name,@_);
+ $self->d('Q ',$name,@_);
return wantarray
@@ -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([@_],{})
@@ -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");
@@ -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}
@@ -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}
@@ -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
@@ -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.
@@ -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";
@@ -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;
}
#------------------------------------------------------------------
@@ -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);
@@ -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;
@@ -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";
@@ -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);
@@ -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) {
@@ -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;
@@ -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;
View
@@ -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
@@ -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');
@@ -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.