Loader/SQLite.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 126 CC 11 SCAR 20.9
CC
11
Cov
0%
BEGIN
12.0
_sqlite_statistics_info
12.0
_is_auto_increment
12.0
CRAP 8.1
1 package Fey::Loader::SQLite;
2 {
3 $Fey::Loader::SQLite::VERSION = '0.13';
4 }
5  
6 use Moose;
7  
8 use namespace::autoclean;
9  
10 use DBD::SQLite 1.20;
11  
12 extends 'Fey::Loader::DBI';
13  
14 package # hide from PAUSE
15 DBD::SQLite::Fixup;
16  
17 BEGIN {
18 unless ( defined &DBD::SQLite::db::column_info ) {
19 *DBD::SQLite::db::column_info = \&_sqlite_column_info;
20 }
21  
22 unless ( defined &DBD::SQLite::db::statistics_info ) {
23 *DBD::SQLite::db::statistics_info = \&_sqlite_statistics_info;
24 }
25 }
26  
27 sub _sqlite_statistics_info {
28 my ( $dbh, $catalog, $schema, $table, $unique_only ) = @_;
29  
30 my @names
31 = qw( TABLE_CAT TABLE_SCHEM TABLE_NAME NON_UNIQUE INDEX_QUALIFIER
32 INDEX_NAME TYPE ORDINAL_POSITION COLUMN_NAME COLLATION
33 CARDINALITY PAGES FILTER_CONDITION
34 );
35  
36 my $sth_indexes = $dbh->prepare(qq{PRAGMA index_list('$table')});
37 $sth_indexes->execute;
38  
39 my @indexes;
40 for my $index ( @{ $sth_indexes->fetchall_arrayref } ) {
41 next if $unique_only && !$index->[2];
42  
43 my $sth_index_info
44 = $dbh->prepare(qq{PRAGMA index_info('$index->[1]')});
45 $sth_index_info->execute;
46  
47 for my $index_part ( @{ $sth_index_info->fetchall_arrayref } ) {
48 my %index;
49  
50 $index{TABLE_NAME} = $table;
51 $index{NON_UNIQUE} = $index->[2] ? 0 : 1;
52 $index{INDEX_NAME} = $index->[1];
53 $index{ORDINAL_POSITION} = $index_part->[1] + 1;
54 $index{COLUMN_NAME} = $index_part->[2];
55  
56 push @indexes, \%index;
57 }
58 }
59  
60 my $sponge = DBI->connect( "DBI:Sponge:", '', '' )
61 or
62 return $dbh->DBI::set_err( $DBI::err, "DBI::Sponge: $DBI::errstr" );
63 my $sth = $sponge->prepare(
64 "statistics_info $table", {
65 rows => [ map { [ @{$_}{@names} ] } @indexes ],
66 NUM_OF_FIELDS => scalar @names,
67 NAME => \@names,
68 }
69 ) or return $dbh->DBI::set_err( $sponge->err(), $sponge->errstr() );
70 return $sth;
71 }
72  
73 package Fey::Loader::SQLite;
74  
75 sub _add_table {
76 my $self = shift;
77 my $schema = shift;
78 my $table_info = shift;
79  
80 return if $table_info->{TABLE_NAME} =~ /^sqlite_/;
81  
82 $self->SUPER::_add_table( $schema, $table_info );
83 }
84  
85 sub _is_auto_increment {
86 my $self = shift;
87 my $table = shift;
88 my $col_info = shift;
89  
90 my $name = $col_info->{COLUMN_NAME};
91  
92 my @pk = $self->_primary_key( $table->name() );
93  
94 # With SQLite3, a table can only have one autoincrement column,
95 # and it must be that table's primary key ...
96 return 0 unless @pk == 1 && $pk[0] eq $name;
97  
98 my $sql = $self->_table_sql( $table->name() );
99  
100 # ... therefore if the table's SQL includes the string
101 # autoincrement, then the primary key must be auto-incremented.
102 return $sql =~ /autoincrement/mi ? 1 : 0;
103 }
104  
105 sub _primary_key {
106 my $self = shift;
107 my $name = shift;
108  
109 return @{ $self->{__primary_key__}{$name} }
110 if $self->{__primary_key__}{$name};
111  
112 my @pk = $self->dbh()->primary_key( undef, undef, $name );
113 $self->{__primary_key__}{$name} = \@pk;
114  
115 return @pk;
116 }
117  
118 sub _table_sql {
119 my $self = shift;
120 my $name = shift;
121  
122 return $self->{__table_sql__}{$name}
123 if $self->{__table_sql__}{$name};
124  
125 return $self->{__table_sql__}{$name}
126 = $self->dbh()
127 ->selectcol_arrayref(
128 'SELECT sql FROM sqlite_master WHERE tbl_name = ?', {}, $name )->[0];
129 }
130  
131 sub _default {
132 my $self = shift;
133 my $default = shift;
134  
135 if ( $default =~ /CURRENT_(?:TIME(?:STAMP)?|DATE)/i ) {
136 return Fey::Literal::Term->new($default);
137 }
138 else {
139 return $self->SUPER::_default($default);
140 }
141 }
142  
143 __PACKAGE__->meta()->make_immutable();
144  
145 1;
146  
147 # ABSTRACT: Loader for SQLite schemas
148  
149  
150  
151 =pod
152  
153 =head1 NAME
154  
155 Fey::Loader::SQLite - Loader for SQLite schemas
156  
157 =head1 VERSION
158  
159 version 0.13
160  
161 =head1 SYNOPSIS
162  
163 my $loader = Fey::Loader->new( dbh => $dbh );
164  
165 my $schema = $loader->make_schema( name => $name );
166  
167 =head1 DESCRIPTION
168  
169 C<Fey::Loader::SQLite> implements some SQLite-specific loader
170 behavior.
171  
172 =head1 METHODS
173  
174 This class provides the same public methods as L<Fey::Loader::DBI>.
175  
176 =head1 BUGS
177  
178 Please report any bugs or feature requests to
179 C<bug-fey-loader@rt.cpan.org>, or through the web interface at
180 L<http://rt.cpan.org>. I will be notified, and then you'll
181 automatically be notified of progress on your bug as I make changes.
182  
183 =head1 AUTHOR
184  
185 Dave Rolsky <autarch@urth.org>
186  
187 =head1 COPYRIGHT AND LICENSE
188  
189 This software is Copyright (c) 2011 by Dave Rolsky.
190  
191 This is free software, licensed under:
192  
193 The Artistic License 2.0 (GPL Compatible)
194  
195 =cut
196  
197  
198 __END__