/usr/share/perl5/Class/DBI/Sweet.pm is in libclass-dbi-sweet-perl 0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 | package Class::DBI::Sweet;
use strict;
use base 'Class::DBI';
use Class::DBI::Iterator; # For the resultset cache
use Data::Page;
use DBI;
use List::Util;
use Carp qw/croak/;
BEGIN { # Use Time::HiRes' time() if possible
eval "use Time::HiRes";
unless ($@) {
import Time::HiRes qw/time/;
}
}
if ( $^O eq 'MSWin32' ) {
eval "require Win32API::GUID;";
}
else {
eval "require Data::UUID;";
}
our $UUID_Is_Available = ( $@ ? 0 : 1 );
our $VERSION = '0.10';
#----------------------------------------------------------------------
# RETRIEVING
#----------------------------------------------------------------------
__PACKAGE__->data_type(
__ROWS => DBI::SQL_INTEGER,
__OFFSET => DBI::SQL_INTEGER
);
__PACKAGE__->set_sql( Join_Retrieve_Count => <<'SQL' );
SELECT COUNT(*)
FROM %s
WHERE %s
SQL
__PACKAGE__->set_sql( Join_Retrieve => <<'SQL' );
SELECT __ESSENTIAL(me)__%s
FROM %s
WHERE %s
SQL
__PACKAGE__->mk_classdata( default_search_attributes => {} );
__PACKAGE__->mk_classdata( profiling_data => {} );
__PACKAGE__->mk_classdata( _live_resultset_cache => {} );
sub retrieve_next {
my $self = shift;
my $class = ref $self
|| croak("retrieve_next cannot be called as a class method");
my ( $criteria, $attributes ) = $class->_search_args(@_);
$attributes = { %{$attributes} }; # Local copy to fiddle with
my $o_by = $attributes->{order_by} || ( $self->columns('Primary') )[0];
my $is_desc = $o_by =~ s/ +DESC//; # If it's previous we'll add it back
my $o_val = (
$o_by =~ m/(.*)\.(.*)/
? $self->$1->$2
: $self->$o_by
);
$criteria->{$o_by} = { ( $is_desc ? '<' : '>' ) => $o_val };
$attributes->{rows} ||= 1;
return wantarray()
? @{ [ $class->_do_search( $criteria, $attributes ) ] }
: $class->_do_search( $criteria, $attributes );
}
sub retrieve_previous {
my $self = shift;
my $class = ref $self
|| croak("retrieve_previous cannot be called as a class method");
my ( $criteria, $attributes ) = $class->_search_args(@_);
$attributes = { %{$attributes} }; # Local copy to fiddle with
my $o_by = $attributes->{order_by} || ( $self->columns('Primary') )[0];
my $is_desc = $o_by =~ s/ +DESC//; # If it's previous we'll add it back
my $o_val = (
$o_by =~ m/(.*)\.(.*)/
? $self->$1->$2
: $self->$o_by
);
$criteria->{$o_by} = { ( $is_desc ? '>' : '<' ) => $o_val };
$attributes->{order_by} = ${o_by} . ( $is_desc ? "" : " DESC" );
$attributes->{rows} ||= 1;
return wantarray()
? @{ [ $class->_do_search( $criteria, $attributes ) ] }
: $class->_do_search( $criteria, $attributes );
}
sub count {
my $proto = shift;
my $class = ref($proto) || $proto;
unless (@_) {
return $class->count_all;
}
my ( $criteria, $attributes ) = $class->_search_args(@_);
# make sure we take copy of $attribues since it can be reused
my $count = { %{$attributes} };
# no need for LIMIT/OFFSET and ORDER BY in COUNT(*)
delete @{$count}{qw( rows offset order_by )};
my ( $sql_parts, $classes, $columns, $values ) =
$proto->_search( $criteria, $count );
my $sql_method = 'sql_' . ( $attributes->{sql_method} || 'Join_Retrieve' );
$sql_method .= '_Count';
my $sth = $class->$sql_method( @{$sql_parts}{qw/ from where /} );
$class->_bind_param( $sth, $columns );
return $sth->select_val(@$values);
}
*pager = \&page;
sub page {
my $proto = shift;
my $class = ref($proto) || $proto;
my ( $criteria, $attributes ) = $proto->_search_args(@_);
$attributes->{rows} ||= 10;
$attributes->{page} ||= 1;
$attributes->{_pager} = ''; # Flag that we need a pager. How ugly!
# No point doing a count(*) if fetching all anyway
unless ( $attributes->{disable_sql_paging} ) {
my $page = Data::Page->new( $class->count( $criteria, $attributes ),
$attributes->{rows}, $attributes->{page}, );
$attributes->{offset} = $page->skipped;
$attributes->{_pager} = $page;
}
my $iterator = $class->search( $criteria, $attributes );
return ( $attributes->{_pager}, $iterator );
}
sub retrieve_all {
my $proto = shift;
my $class = ref($proto) || $proto;
unless ( @_ || keys %{ $class->default_search_attributes } ) {
return $class->SUPER::retrieve_all;
}
return $class->search( {}, ( @_ > 1 ) ? {@_} : ( shift || () ) );
}
sub search {
my $proto = shift;
my $class = ref($proto) || $proto;
my ( $criteria, $attributes ) = $class->_search_args(@_);
$class->_do_search( $criteria, $attributes );
}
sub search_like {
my $proto = shift;
my $class = ref($proto) || $proto;
my ( $criteria, $attributes ) = $class->_search_args(@_);
$attributes->{cmp} = 'like';
$class->_do_search( $criteria, $attributes );
}
sub _do_search {
my ( $class, $criteria, $attributes ) = @_;
foreach my $pre ( @{ $attributes->{prefetch} || [] } ) {
unless ( $class->meta_info( has_a => $pre )
or $class->meta_info( might_have => $pre ) )
{
croak "$pre is not a has_a or might_have rel on $class";
}
}
my ( $sql_parts, $classes, $columns, $values ) =
$class->_search( $criteria, $attributes );
my $cache_key;
if ( $class->cache && $attributes->{use_resultset_cache} ) {
my $sql = join '', @{$sql_parts}{qw/ where from order_by limit /};
$cache_key =
$class->_resultset_cache_key( $sql, $values,
$attributes->{prefetch} );
my $cache_entry;
my ($latest_stale) = sort { $b <=> $a }
grep defined, map { $class->cache->get($_) }
grep defined, map { $_->_staleness_cache_key } values %{$classes};
if ($cache_key) {
if ( $cache_entry = $class->_live_resultset_cache->{$cache_key} ) {
if ( $cache_entry->{created} <= ( $latest_stale || 0 ) ) {
delete $class->_live_resultset_cache->{$cache_key};
undef $cache_entry;
}
else {
# So reset doesn't screw the original copy
# (which might still be in scope and in use)
$cache_entry =
{
%$cache_entry,
iterator => bless(
{ %{ $cache_entry->{iterator} } },
ref $cache_entry->{iterator}
)
};
$cache_entry->{iterator}->reset;
}
}
if ( !( defined $cache_entry )
and $cache_entry = $class->cache->get($cache_key) )
{
if ( $cache_entry->{created} <= ( $latest_stale || 0 ) ) {
$class->cache->remove($cache_key);
undef $cache_entry;
}
else {
$class->_live_resultset_cache->{$cache_key} = $cache_entry;
}
}
}
if ($cache_entry) {
push (
@{ $class->profiling_data->{resultset_cache} },
[ 'HIT', $cache_key ]
)
if $attributes->{profile_cache};
my $iterator =
$class->_slice_iter( $attributes, $cache_entry->{iterator} );
return map $class->construct($_), $iterator->data if wantarray;
return $iterator;
}
push (
@{ $class->profiling_data->{resultset_cache} },
[ 'MISS', $cache_key ]
)
if $attributes->{profile_cache};
}
my $pre_fields = ''; # Used in SELECT
my $pre_names = ''; # for use in GROUP BY
if ( $attributes->{prefetch} ) {
$pre_fields .= ", '"
. join ( ' ', @{ $attributes->{prefetch} } )
. "' AS sweet__joins";
my $jnum = 0;
foreach my $pre ( @{ $attributes->{prefetch} } ) {
$jnum++;
my $f_class = $classes->{$pre};
foreach my $col ( $f_class->columns('Essential') ) {
$pre_names .= ", ${pre}.${col}";
$pre_fields .= ", ${pre}.${col} AS sweet__${jnum}_${col}";
}
}
}
$sql_parts->{prefetch_cols} = $pre_fields;
$sql_parts->{prefetch_names} = $pre_names;
my $sql_method = 'sql_' . ( $attributes->{sql_method} || 'Join_Retrieve' );
my $statement_order = $attributes->{statement_order}
|| [qw/ prefetch_cols from sql /];
my @sql_parts;
for my $part (@$statement_order) {
# For backward compatibility
if ( $part eq 'sql' ) {
push @sql_parts, join ' ',
@{$sql_parts}{qw/ where order_by limit/};
next;
}
if ( exists $sql_parts->{$part} ) {
push @sql_parts, $sql_parts->{$part};
next;
}
die "'statement_order' setting of [$part] is invalid";
}
my $sth = $class->$sql_method(@sql_parts);
$class->_bind_param( $sth, $columns );
my $iterator = $class->sth_to_objects( $sth, $values );
if ( $class->cache && $attributes->{use_resultset_cache} ) {
my $cache_entry = {
created => time(),
iterator => bless( { %{$iterator} }, ref $iterator )
};
$class->cache->set( $cache_key, $cache_entry );
$class->_live_resultset_cache->{$cache_key} = $cache_entry;
}
$iterator = $class->_slice_iter( $attributes, $iterator );
return map $class->construct($_), $iterator->data if wantarray;
return $iterator;
}
sub _slice_iter {
my ( $class, $attributes, $iterator ) = @_;
# Create pager if doesn't already exist
if ( exists $attributes->{_pager} && !$attributes->{_pager} ) {
$attributes->{_pager} =
Data::Page->new( $iterator->count, $attributes->{rows},
$attributes->{page}, );
$attributes->{offset} = $attributes->{_pager}->skipped;
}
# If RDBM is not ROWS/OFFSET supported, slice iterator
if ( $attributes->{rows} && $iterator->count > $attributes->{rows} ) {
my $rows = $attributes->{rows};
my $offset = $attributes->{offset} || 0;
$iterator = $iterator->slice( $offset, $offset + $rows - 1 );
}
return $iterator;
}
sub _search {
my $proto = shift;
my $criteria = shift;
my $attributes = shift;
my $class = ref($proto) || $proto;
# Valid SQL::Abstract params
my %params = map { $_ => $attributes->{$_} } qw(case cmp convert logic);
$params{cdbi_class} = $class;
$params{cdbi_me_alias} = 'me';
# Overide bindtype, we need all columns and values for deflating
my $abstract =
Class::DBI::Sweet::SQL::Abstract->new( %params, bindtype => 'columns' );
my ( $sql, $from, $classes, @bind ) =
$abstract->where( $criteria, '', $attributes->{prefetch} );
my ( @columns, @values, %cache );
foreach my $bind (@bind) {
push ( @columns, $bind->[0] );
push ( @values, @{$bind}[ 1 .. $#$bind ] );
}
unless ( $sql =~ /^\s*WHERE/i )
{ # huh? This is either WHERE.. or empty string.
$sql = "WHERE 1=1 $sql";
}
$sql =~ s/^\s*(WHERE)\s*//i;
my %sql_parts = (
where => $sql,
from => $from,
limit => '',
order_by => '',
);
$sql_parts{order_by} = $abstract->_order_by( $attributes->{order_by} )
if $attributes->{order_by};
if ( $attributes->{rows} && !$attributes->{disable_sql_paging} ) {
my $rows = $attributes->{rows};
my $offset = $attributes->{offset} || 0;
my $driver = lc $class->db_Main->{Driver}->{Name};
if ( $driver =~ /^(maxdb|mysql|mysqlpp)$/ ) {
$sql_parts{limit} = ' LIMIT ?, ?';
push ( @columns, '__OFFSET', '__ROWS' );
push ( @values, $offset, $rows );
}
elsif ( $driver =~ /^(pg|pgpp|sqlite|sqlite2)$/ ) {
$sql_parts{limit} = ' LIMIT ? OFFSET ?';
push ( @columns, '__ROWS', '__OFFSET' );
push ( @values, $rows, $offset );
}
elsif ( $driver =~ /^(interbase)$/ ) {
$sql_parts{limit} = ' ROWS ? TO ?';
push ( @columns, '__ROWS', '__OFFSET' );
push ( @values, $rows, $offset + $rows );
}
}
return ( \%sql_parts, $classes, \@columns, \@values );
}
sub _search_args {
my $proto = shift;
my ( $criteria, $attributes );
if ( @_ == 2
&& ref( $_[0] ) =~ /^(ARRAY|HASH)$/
&& ref( $_[1] ) eq 'HASH' )
{
$criteria = $_[0];
$attributes = $_[1];
}
elsif ( @_ == 1 && ref( $_[0] ) =~ /^(ARRAY|HASH)$/ ) {
$criteria = $_[0];
$attributes = {};
}
else {
$attributes = @_ % 2 ? pop (@_) : {};
$criteria = {@_};
}
# Need to pass things in $attributes, so don't create a new hash
for my $key ( keys %{ $proto->default_search_attributes } ) {
$attributes->{$key} ||= $proto->default_search_attributes->{$key};
}
return ( $criteria, $attributes );
}
#----------------------------------------------------------------------
# CACHING
#----------------------------------------------------------------------
__PACKAGE__->mk_classdata('cache');
sub cache_key {
my $proto = shift;
my $class = ref($proto) || $proto;
my $data;
my @primary_columns = $class->primary_columns;
if (@_) {
if ( @_ == 1 && ref( $_[0] ) eq 'HASH' ) {
$data = $_[0];
}
elsif ( @_ == 1 ) {
$data = { $primary_columns[0] => $_[0] };
}
else {
$data = {@_};
}
}
else {
@{$data}{@primary_columns} = $proto->get(@primary_columns);
}
unless ( @primary_columns == grep defined, @{$data}{@primary_columns} ) {
return;
}
return join "|", $class, map $_ . '=' . $data->{$_}, sort @primary_columns;
}
sub _resultset_cache_key {
my ( $class, $sql, $values, $prefetch ) = @_;
$class = ref $class if ref $class;
my @pre = map { "=${_}"; } @{ $prefetch || [] };
my $it = $class->iterator_class;
return join "|", $class, "=${sql}", "=${it}", @pre, @{ $values || [] };
}
sub _staleness_cache_key {
my ($class) = @_;
$class = ref $class if ref $class;
return "${class}|+staleness_key";
}
sub _init {
my $class = shift;
my $data = $_[0] || {};
unless ( $class->cache || $data->{'sweet__joins'} ) {
return $class->SUPER::_init(@_);
}
my $key = $class->cache_key($data);
my $object;
if ( $class->cache and $key and $object = $class->cache->get($key) ) {
push ( @{ $class->profiling_data->{object_cache} }, [ 'HIT', $key ] )
if ( $class->default_search_attributes->{profile_cache} );
# ensure that objects from the cache get inflated properly
if ( ( caller(1) )[3] eq "Class::DBI::_simple_bless" ) {
$object->call_trigger('select');
}
return $object;
}
push ( @{ $class->profiling_data->{object_cache} }, [ 'MISS', $key ] )
if ( $class->default_search_attributes->{profile_cache} );
$object = bless {}, $class;
if ( my $joins = $data->{'sweet__joins'} ) {
my $meta = $class->meta_info;
my $jnum = 0;
foreach my $join ( split ( / /, $joins ) ) {
my ( $rel, $f_class );
$jnum++;
if ( $rel = $meta->{has_a}{$join} ) {
$f_class = $rel->foreign_class;
my %attrs =
map { ( $_ => $data->{"sweet__${jnum}_${_}"} ) }
$f_class->columns('Essential');
$data->{$join} = $f_class->construct( \%attrs );
}
elsif ( $rel = $meta->{might_have}{$join} ) {
$f_class = $rel->foreign_class;
my %attrs =
map { ( $_ => $data->{"sweet__${jnum}_${_}"} ) }
$f_class->columns('Essential');
$object->{"_${join}_object"} = $f_class->construct( \%attrs );
}
else {
croak("Unable to find relationship ${join} on ${class}");
}
}
}
$object->_attribute_store(%$data);
if ( $class->cache and $key ) {
$object->call_trigger('deflate_for_create');
$class->cache->set( $key, $object );
}
return $object;
}
sub retrieve {
my $class = shift;
if ( $class->cache ) {
if ( my $key = $class->cache_key(@_) ) {
if ( my $object = $class->cache->get($key) ) {
$object->call_trigger('select');
push (
@{ $class->profiling_data->{object_cache} },
[ 'HIT', $key ]
)
if ( $class->default_search_attributes->{profile_cache} );
return $object;
}
push ( @{ $class->profiling_data->{object_cache} },
[ 'MISS', $key ] )
if ( $class->default_search_attributes->{profile_cache} );
}
}
return $class->SUPER::retrieve(@_);
}
*create = \&insert;
sub insert {
my $self = shift;
if ( $self->cache ) {
$self->cache->set( $self->_staleness_cache_key, time() );
}
return $self->SUPER::insert(@_);
}
sub update {
my $self = shift;
if ( $self->cache ) {
$self->cache->remove( $self->cache_key );
$self->cache->set( $self->_staleness_cache_key, time() );
}
return $self->SUPER::update(@_);
}
sub delete {
my $self = shift;
return $self->_search_delete(@_) if not ref $self;
if ( $self->cache ) {
$self->cache->remove( $self->cache_key );
$self->cache->set( $self->_staleness_cache_key, time() );
}
return $self->SUPER::delete(@_);
}
#----------------------------------------------------------------------
# UNIVERSALLY UNIQUE IDENTIFIERS
#----------------------------------------------------------------------
sub _next_in_sequence {
my $self = shift;
if ( lc $self->sequence eq 'uuid' ) {
die "UUID features not available" unless $UUID_Is_Available;
if ( $^O eq 'MSWin32' ) {
return Win32API::GUID::CreateGuid();
}
else {
return Data::UUID->new->create_str;
}
}
return $self->SUPER::_next_in_sequence;
}
#----------------------------------------------------------------------
# MORE MAGIC
#----------------------------------------------------------------------
package Class::DBI::Sweet::SQL::Abstract;
use base qw/SQL::Abstract/;
use Carp qw/croak/;
sub where {
my ( $self, $where, $order, $must_join ) = @_;
my $me = $self->{cdbi_me_alias};
$self->{cdbi_table_aliases} = { $me => $self->{cdbi_class} };
$self->{cdbi_join_info} = {};
$self->{cdbi_column_cache} = {};
foreach my $join ( @{ $must_join || [] } ) {
$self->_resolve_join( $me => $join );
}
my $sql = '';
my (@ret) = $self->_recurse_where($where);
if (@ret) {
my $wh = shift @ret;
$sql .= $self->_sqlcase(' where ') . $wh if $wh;
}
$sql =~ s/(\S+)( IS(?: NOT)? NULL)/$self->_default_tables($1).$2/ge;
my $joins = delete $self->{cdbi_join_info};
my $tables = delete $self->{cdbi_table_aliases};
my $from = $self->{cdbi_class}->table . " ${me}";
foreach my $join ( keys %{$joins} ) {
my $table = $tables->{$join}->table;
$from .= ", ${table} ${join}";
my ( $l_alias, $l_key, $f_key ) =
@{ $joins->{$join} }{qw/l_alias l_key f_key/};
$sql .= " AND ${l_alias}.${l_key} = ${join}.${f_key}";
}
# order by?
#if ($order) {
# $sql .= $self->_order_by($order);
#}
delete $self->{cdbi_column_cache};
return wantarray ? ( $sql, $from, $tables, @ret ) : $sql;
}
sub _convert {
my ( $self, $to_convert ) = @_;
return $self->SUPER::_convert($to_convert) if $to_convert eq '?';
return $self->SUPER::_convert( $self->_default_tables($to_convert) );
}
sub _default_tables {
my ( $self, $to_convert ) = @_;
my $alias = $self->{cdbi_me_alias};
my @alias = split ( /\./, $to_convert );
my $field = pop (@alias);
foreach my $f_alias (@alias) {
$self->_resolve_join( $alias => $f_alias )
unless $self->{cdbi_table_aliases}{$f_alias};
$alias = $f_alias;
}
if ( my $meta = $self->{cdbi_class}->meta_info( has_many => $field ) ) {
my $f_alias = $field;
$self->_resolve_join( $alias => $f_alias )
unless $self->{cdbi_table_aliases}{$f_alias};
$field = ( ( $meta->foreign_class->columns('Primary') )[0] );
$alias = $f_alias;
}
return "${alias}.${field}";
}
sub _resolve_join {
my ( $self, $l_alias, $f_alias ) = @_;
my $l_class = $self->{cdbi_table_aliases}->{$l_alias};
my $meta = $l_class->meta_info;
my ( $rel, $f_class );
if ( $rel = $meta->{has_a}{$f_alias} ) {
$f_class = $rel->foreign_class;
$self->{cdbi_join_info}{$f_alias} = {
l_alias => $l_alias,
l_key => $f_alias,
f_key => ( $f_class->columns('Primary') )[0]
};
}
elsif ( $rel = $meta->{has_many}{$f_alias} ) {
$f_class = $rel->foreign_class;
$self->{cdbi_join_info}{$f_alias} = {
l_alias => $l_alias,
l_key => ( $l_class->columns('Primary') )[0],
f_key => $rel->args->{foreign_key}
};
}
elsif ( $rel = $meta->{might_have}{$f_alias} ) {
$f_class = $rel->foreign_class;
$self->{cdbi_join_info}{$f_alias} = {
l_alias => $l_alias,
l_key => ( $l_class->columns('Primary') )[0],
f_key => ( $f_class->columns('Primary') )[0]
};
}
else {
croak("Unable to find join info for ${f_alias} from ${l_class}");
}
$self->{cdbi_table_aliases}{$f_alias} = $f_class;
}
sub _bindtype {
my ( $self, $var, $val, @rest ) = @_;
$var = $self->_default_tables($var);
my ( $alias, $col ) = split ( /\./, $var );
my $f_class = $self->{cdbi_table_aliases}{$alias};
my $column = $self->{cdbi_column_cache}{$alias}{$col};
unless ($column) {
$column = $f_class->find_column($col)
|| ( List::Util::first { $_->accessor eq $col } $f_class->columns )
|| croak("$col is not a column of ${f_class}");
$self->{cdbi_column_cache}{$alias}{$col} = $column;
}
if ( ref $val eq $f_class ) {
my $accessor = $column->accessor;
$val = $val->$accessor;
}
$val = $f_class->_deflated_column( $column, $val );
return $self->SUPER::_bindtype( $var, $val, @rest );
}
1;
__END__
=head1 NAME
Class::DBI::Sweet - Making sweet things sweeter
=head1 SYNOPSIS
package MyApp::DBI;
use base 'Class::DBI::Sweet';
MyApp::DBI->connection('dbi:driver:dbname', 'username', 'password');
package MyApp::Article;
use base 'MyApp::DBI';
use DateTime;
__PACKAGE__->table('article');
__PACKAGE__->columns( Primary => qw[ id ] );
__PACKAGE__->columns( Essential => qw[ title created_on created_by ] );
__PACKAGE__->has_a(
created_on => 'DateTime',
inflate => sub { DateTime->from_epoch( epoch => shift ) },
deflate => sub { shift->epoch }
);
# Simple search
MyApp::Article->search( created_by => 'sri', { order_by => 'title' } );
MyApp::Article->count( created_by => 'sri' );
MyApp::Article->page( created_by => 'sri', { page => 5 } );
MyApp::Article->retrieve_all( order_by => 'created_on' );
# More powerful search with deflating
$criteria = {
created_on => {
-between => [
DateTime->new( year => 2004 ),
DateTime->new( year => 2005 ),
]
},
created_by => [ qw(chansen draven gabb jester sri) ],
title => {
-like => [ qw( perl% catalyst% ) ]
}
};
MyApp::Article->search( $criteria, { rows => 30 } );
MyApp::Article->count($criteria);
MyApp::Article->page( $criteria, { rows => 10, page => 2 } );
MyApp::Article->retrieve_next( $criteria,
{ order_by => 'created_on' } );
MyApp::Article->retrieve_previous( $criteria,
{ order_by => 'created_on' } );
MyApp::Article->default_search_attributes(
{ order_by => 'created_on' } );
# Automatic joins for search and count
MyApp::CD->has_many(tracks => 'MyApp::Track');
MyApp::CD->has_many(tags => 'MyApp::Tag');
MyApp::CD->has_a(artist => 'MyApp::Artist');
MyApp::CD->might_have(liner_notes
=> 'MyApp::LinerNotes' => qw/notes/);
MyApp::Artist->search({ 'cds.year' => $cd }, # $cd->year subtituted
{ order_by => 'artistid DESC' });
my ($tag) = $cd->tags; # Grab first tag off CD
my ($next) = $cd->retrieve_next( { 'tags.tag' => $tag },
{ order_by => 'title' } );
MyApp::CD->search( { 'liner_notes.notes' => { "!=" => undef } } );
MyApp::CD->count(
{ 'year' => { '>', 1998 }, 'tags.tag' => 'Cheesy',
'liner_notes.notes' => { 'like' => 'Buy%' } } );
# Multi-step joins
MyApp::Artist->search({ 'cds.tags.tag' => 'Shiny' });
# Retrieval with pre-loading
my ($cd) = MyApp::CD->search( { ... },
{ prefetch => [ qw/artist liner_notes/ ] } );
$cd->artist # Pre-loaded
# Caching of resultsets (*experimental*)
__PACKAGE__->default_search_attributes( { use_resultset_cache => 1 } );
=head1 DESCRIPTION
Class::DBI::Sweet provides convenient count, search, page, and
cache functions in a sweet package. It integrates these functions with
C<Class::DBI> in a convenient and efficient way.
=head1 RETRIEVING OBJECTS
All retrieving methods can take the same criteria and attributes. Criteria is
the only required parameter.
=head2 criteria
Can be a hash, hashref, or an arrayref. Takes the same options as the
L<SQL::Abstract> C<where> method. If values contain any objects, they
will be deflated before querying the database.
=head2 attributes
=over 4
=item case, cmp, convert, and logic
These attributes are passed to L<SQL::Abstract>'s constuctor and alter the
behavior of the criteria.
{ cmp => 'like' }
=item order_by
Specifies the sort order of the results.
{ order_by => 'created_on DESC' }
=item rows
Specifies the maximum number of rows to return. Currently supported RDBMs are
Interbase, MaxDB, MySQL, PostgreSQL and SQLite. For other RDBMs, it will be
emulated.
{ rows => 10 }
=item offset
Specifies the offset of the first row to return. Defaults to 0 if unspecified.
{ offset => 0 }
=item page
Specifies the current page in C<page>. Defaults to 1 if unspecified.
{ page => 1 }
=item prefetch
Specifies a listref of relationships to prefetch. These must be has_a or
might_haves or Sweet will throw an error. This will cause Sweet to do
a join across to the related tables in order to return the related object
without a second trip to the database. All 'Essential' columns of the
foreign table are retrieved.
{ prefetch => [ qw/some_rel some_other_rel/ ] }
Sweet constructs the joined SQL statement by aliasing the columns in
each table and prefixing the column name with 'sweet__N_' where N is a
counter starting at 1. Note that if your database has a column length limit
(for example, Oracle's limit is 30) and you use long column names in
your application, Sweet's addition of at least 9 extra characters to your
column name may cause database errors.
=item use_resultset_cache
Enables the resultset cache. This is a little experimental and massive gotchas
may rear their ugly head at some stage, but it does seem to work pretty well.
For best results, the resultset cache should only be used selectively on
queries where you experience performance problems. Enabling it for every
single query in your application will most likely cause a drop in performance
as the cache overhead is greater than simply fetching the data from the
database.
=item profile_cache
Records cache hits/misses and what keys they were for in ->profiling_data.
Note that this is class metadata so if you don't want it to be global for
Sweet you need to do
__PACKAGE__->profiling_data({ });
in either your base class or your table classes to taste.
=item disable_sql_paging
Disables the use of paging in SQL statements if set, forcing Sweet to emulate
paging by slicing the iterator at the end of ->search (which it normally only
uses as a fallback mechanism). Useful for testing or for causing the entire
query to be retrieved initially when the resultset cache is used.
This is also useful when using custom SQL via C<set_sql> and setting
C<sql_method> (see below) where a COUNT(*) may not make sense (i.e. when
the COUNT(*) might be as expensive as just running the full query and just slicing
the iterator).
=item sql_method
This sets the name of the sql fragment to use as previously set by a
C<set_sql> call. The default name is "Join_Retrieve" and the associated
default sql fragment set in this class is:
__PACKAGE__->set_sql( Join_Retrieve => <<'SQL' );
SELECT __ESSENTIAL(me)__%s
FROM %s
WHERE %s
SQL
You may override this in your table or base class using the same name and CDBI::Sweet
will use your custom fragment, instead.
If you need to use more than one sql fragment in a given class you may create a new
sql fragment and then specify its name using the C<sql_method> attribute.
The %s strings are replaced by sql parts as described in L<Ima::DBI>. See
"statement_order" for the sql part that replaces each instance of %s.
In addition, the associated statment for COUNT(*) statement has "_Count"
appended to the sql_method name. Only "from" and "where" are passed to the sprintf
function.
The default sql fragment used for "Join_Retrieve" is:
__PACKAGE__->set_sql( Join_Retrieve_Count => <<'SQL' );
SELECT COUNT(*)
FROM %s
WHERE %s
SQL
If you create a custom sql method (and set the C<sql_method> attribute) then
you will likely need to also create an associated _Count fragment. If you do
not have an associated _Count, and wish to call the C<page> method, then set
C<disable_sql_paging> to true and your result set from the select will be spliced
to return the page you request.
Here's an example.
Assume a CD has_a Artist (and thus Artists have_many CDs), and you wish to
return a list of artists and how many CDs each have:
In package MyDB::Artist
__PACKAGE__->columns( TEMP => 'cd_count');
__PACKAGE__->set_sql( 'count_by_cd', <<'');
SELECT __ESSENTIAL(me)__, COUNT(cds.cdid) as cd_count
FROM %s -- ("from")
WHERE %s -- ("where")
GROUP BY __ESSENTIAL(me)__
%s %s -- ("limit" and "order_by")
Then in your application code:
my ($pager, $iterator) = MyDB::Artist->page(
{
'cds.title' => { '!=', undef },
},
{
sql_method => 'count_by_cd',
statement_order => [qw/ from where limit order_by / ],
disable_sql_paging => 1,
order_by => 'cd_count desc',
rows => 10,
page => 1,
} );
The above generates the following SQL:
SELECT me.artistid, me.name, COUNT(cds.cdid) as cd_count
FROM artist me, cd cds
WHERE ( cds.title IS NOT NULL ) AND me.artistid = cds.artist
GROUP BY me.artistid, me.name
ORDER BY cd_count desc
The one caveat is that Sweet cannot figure out the has_many joins unless you
specify them in the $criteria. In the previous example that's done by asking
for all cd titles that are not null (which should be all).
To fetch a list like above but limited to cds that were created before the year
2000, you might do:
my ($pager, $iterator) = MyDB::Artist->page(
{
'cds.year' => { '<', 2000 },
},
{
sql_method => 'count_by_cd',
statement_order => [qw/ from where limit order_by / ],
disable_sql_paging => 1,
order_by => 'cd_count desc',
rows => 10,
page => 1,
} );
=item statement_order
Specifies a list reference of SQL parts that are replaced in the SQL fragment (which
is defined with "sql_method" above). The available SQL parts are:
prefetch_cols from where order_by limit sql prefetch_names
The "sql" part is shortcut notation for these three combined:
where order_by limit
Prefecch_cols are the columns selected when a prefetch is speccified -- use in the SELECT.
Prefetch_names are just the column names for use in GROUP BY.
This is useful when statement order needs to be changed, such as when using a
GROUP BY:
=back
=head2 count
Returns a count of the number of rows matching the criteria. C<count> will
discard C<offset>, C<order_by>, and C<rows>.
$count = MyApp::Article->count(%criteria);
=head2 search
Returns an iterator in scalar context, or an array of objects in list
context.
@objects = MyApp::Article->search(%criteria);
$iterator = MyApp::Article->search(%criteria);
=head2 search_like
As search but adds the attribute { cmp => 'like' }.
=head2 page
Retuns a page object and an iterator. The page object is an instance of
L<Data::Page>.
( $page, $iterator )
= MyApp::Article->page( $criteria, { rows => 10, page => 2 );
printf( "Results %d - %d of %d Found\n",
$page->first, $page->last, $page->total_entries );
=head2 pager
An alias to page.
=head2 retrieve_all
Same as C<Class::DBI> with addition that it takes C<attributes> as arguments,
C<attributes> can be a hash or a hashref.
$iterator = MyApp::Article->retrieve_all( order_by => 'created_on' );
=head2 retrieve_next
Returns the next record after the current one according to the order_by
attribute (or primary key if no order_by specified) matching the criteria.
Must be called as an object method.
=head2 retrieve_previous
As retrieve_next but retrieves the previous record.
=head1 CACHING OBJECTS
Objects will be stored deflated in cache. Only C<Primary> and C<Essential>
columns will be cached.
=head2 cache
Class method: if this is set caching is enabled. Any cache object that has a
C<get>, C<set>, and C<remove> method is supported.
__PACKAGE__->cache(
Cache::FastMmap->new(
share_file => '/tmp/cdbi',
expire_time => 3600
)
);
=head2 cache_key
Returns a cache key for an object consisting of class and primary keys.
=head2 Overloaded methods
=over 4
=item _init
Overrides C<Class::DBI>'s internal cache. On a cache hit, it will return
a cached object; on a cache miss it will create an new object and store
it in the cache.
=item create
=item insert
All caches for this table are marked stale and will be re-cached on next
retrieval. create is an alias kept for backwards compatibility.
=item retrieve
On a cache hit the object will be inflated by the C<select> trigger and
then served.
=item update
Object is removed from the cache and will be cached on next retrieval.
=item delete
Object is removed from the cache.
=back
=head1 UNIVERSALLY UNIQUE IDENTIFIERS
If enabled a UUID string will be generated for primary column. A CHAR(36)
column is suitable for storage.
__PACKAGE__->sequence('uuid');
=head1 MAINTAINERS
Fred Moyer <fred@redhotpenguin.com>
=head1 AUTHORS
Christian Hansen <ch@ngmedia.com>
Matt S Trout <mstrout@cpan.org>
Andy Grundman <andy@hybridized.org>
=head1 THANKS TO
Danijel Milicevic, Jesse Sheidlower, Marcus Ramberg, Sebastian Riedel,
Viljo Marrandi, Bill Moseley
=head1 SUPPORT
#catalyst on L<irc://irc.perl.org>
L<http://lists.rawmode.org/mailman/listinfo/catalyst>
L<http://lists.rawmode.org/mailman/listinfo/catalyst-dev>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<Class::DBI>
L<Data::Page>
L<Data::UUID>
L<SQL::Abstract>
L<Catalyst>
L<http://cpan.robm.fastmail.fm/cache_perf.html>
A comparison of different caching modules for perl.
=cut
|