File Coverage

blib/lib/SQL/Entity/Index.pm
Criterion Covered Total %
statement 20 20 100.0
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 30 31 96.7


line stmt bran cond sub pod time code
1             package SQL::Entity::Index;
2              
3 5     5   32 use strict;
  5         10  
  5         227  
4 5     5   29 use warnings;
  5         10  
  5         163  
5 5     5   25 use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION);
  5         10  
  5         548  
6              
7             $VERSION = 0.01;
8              
9 5     5   26 use base 'Exporter';
  5         16  
  5         537  
10 5     5   30 use Abstract::Meta::Class ':all';
  5         18  
  5         2004  
11              
12             @EXPORT_OK = qw(sql_index);
13             %EXPORT_TAGS = (all => \@EXPORT_OK);
14              
15             =head1 NAME
16              
17             SQL::Entity::Index - Entity index abstraction.
18              
19             =cut
20              
21             =head1 SYNOPSIS
22              
23             use SQL::Entity::Index;
24              
25             my $index = SQL::Entity::Index->new(
26             name => 'idx_empno',
27             columns => ['empno'],
28             hint => ' INDEX_ASC( emp empno ) '
29             );
30              
31             =head1 DESCRIPTION
32              
33             Represents index, that force resultset order by either generating ORDER BY sql fragment,
34             or by adding hint sql fragement (Oracle, MySQL)
35              
36              
37             my $entity = SQL::Entity->new(
38             name => 'emp',
39             primary_key => ['empno'],
40             unique_expression => 'rowid',
41             columns => [
42             sql_column(name => 'ename'),
43             sql_column(name => 'empno'),
44             sql_column(name => 'deptno')
45             ],
46             indexes => [
47             sql_index(name => 'idx_emp_empno', columns => ['empno'], hint => 'INDEX_ASC(emp idx_emp_empno)'),
48             sql_index(name => 'idx_emp_ename', columns => ['ename']),
49             ],
50             order_index => 'idx_emp_ename'
51             );
52              
53              
54             my ($sql, $bind_variables) = $query->query();
55             will return
56             SELECT emp.*
57             FROM (
58             SELECT /*+ INDEX_ASC(emp idx_emp_ename) */ ROWNUM AS the_rownum,
59             emp.rowid AS the_rowid,
60             emp.deptno,
61             emp.ename,
62             emp.empno
63             FROM emp
64             WHERE ROWNUM < ?) emp
65             WHERE the_rownum >=
66              
67             =head1 EXPORT
68              
69             sql_index by 'all' tag.
70              
71             =head2 ATTRIBUTES
72              
73             =over
74              
75             =item name
76              
77             =cut
78              
79             has '$.name' => (required => 1);
80              
81              
82             =item order_by_cluase
83              
84             order_by_cluase => 'empno desc, ename asc'
85              
86             =cut
87              
88             has '$.order_by_cluase';
89              
90             =item columns
91              
92             =cut
93              
94             has '@.columns';
95              
96              
97             =item hint
98              
99             Cost base optymizer hitn (Oracle, MySQL)
100              
101             #TODO add dybnamic hint based on condition objects
102              
103             =cut
104              
105             has '$.hint';
106              
107             =back
108              
109             =head2 METHODS
110              
111             =over
112              
113             =item sql_index
114              
115             Creates a new instance of the SQL::Entity::Index
116              
117             =cut
118              
119             sub sql_index {
120 2     2 1 225 __PACKAGE__->new(@_);
121             }
122              
123              
124             =item order_by_operand
125              
126             Returns sql fragment operand to ORDER BY cluase
127              
128             =cut
129              
130             sub order_by_operand {
131 2     2 1 4 my ($self, $entity) = @_;
132 2         10 my @columns = $self->columns;
133 2         33 my $order_by_cluase = $self->order_by_cluase;
134 2 50       25 $order_by_cluase ? $order_by_cluase : (join ",",@columns);
135             }
136              
137             1;
138              
139             __END__