OpenInteract2/Manage/Package/CreatePackageFromTable.pm

filter: Statementstmt 28.3% 40 / 141 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 5 totaltotal 21.3% 49 / 230 CC 32 SCAR 38.5
CC
32
Cov
20%
run_task
90.0
validate_param
72.0
_get_key_field
56.0
CRAP 46.9
« OpenInteract2/DatasourceManager.pm ↑ Summary OpenInteract2/Action/CommonRemove.pm »
1 package OpenInteract2::Manage::Package::CreatePackageFromTable;
2  
3 # $Id: CreatePackageFromTable.pm,v 1.2 2005/02/18 03:28:08 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( OpenInteract2::Manage::Package );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use DBI qw( :sql_types );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 87 $OpenInteract2::Manage::Package::CreatePackageFromTable::VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
14  
15 87 my ( $log );
16  
17 87 ▶sub get_name {
Subroutine get_name: called CC=1 SCAR=0.0
Pod undocumented
18 87 return 'easy_app';
19 }
20  
21 0 ▶sub get_brief_description {
Subroutine get_brief_description: not called CC=1 SCAR=6.9
Pod undocumented
22 0 return "Generate a package with create, update, delete and search capabilities.";
23 }
24  
25 1 ▶sub get_parameters {
Subroutine get_parameters: called CC=1 SCAR=0.0
Pod undocumented
26 1 my ( $self ) = @_;
27 return {
28 1 package => {
29 description => 'Name of the package to create',
30 is_required => 'yes',
31 },
32 package_dir => {
33 description => 'Directory to create package in',
34 do_validate => 'yes',
35 },
36 dsn => {
37 description => 'A DBI DSN used to connect -- e.g., "DBI:Pg:dbname=oi2"',
38 is_required => 'yes',
39 do_validate => 'yes',
40 },
41 table => {
42 description => "Name of your table.",
43 is_required => 'yes',
44 do_validate => 'no',
45 },
46 username => {
47 description => 'A username for connecting to the DSN',
48 is_required => 'yes',
49 do_validate => 'no',
50 },
51 password => {
52 description => 'A password for connecting the username to the DSN',
53 is_required => 'yes',
54 do_validate => 'no',
55 },
56 };
57 }
58  
59 87 my %VALID_DRIVERS = map { $_ => 1 } qw( Pg mysql );
60  
61 0 ▶sub validate_param {
Subroutine validate_param: not called CC=8 SCAR=42.8
Pod undocumented
62 0 my ( $self, $name, $value ) = @_;
63 0 ▶ if ( $name eq 'package_dir' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'package_dir') { } 0 0 0
elsif ($name eq 'dsn') { } 0 0 0
64 0 ▶ return undef unless ( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
65 0 ▶ return undef if ( -d $value );
Branch: 1 branch
BranchTrueFalseTotal
if -d $value 0 0 0
66 0 return "If specified 'package_dir' must be a valid directory";
67 }
68 elsif ( $name eq 'dsn' ) {
69 0 ▶ unless ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 0 0 0
70 0 return "DSN must be specified";
71 }
72 0 my ( $d, $driver, $info ) = split ( ':', $value );
73 0 ▶ unless ( $driver ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver) 0 0 0
74 0 return "DSN must have a driver in the second parameter delimited by ':'";
75 }
76 0 ▶ unless ( $VALID_DRIVERS{ $driver } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($VALID_DRIVERS{$driver}) 0 0 0
77 0 return "I cannot use '$driver' database; I'm restricted to: " .
78 join( ', ', sort keys %VALID_DRIVERS );
79 }
80 0 return undef;
81 }
82 0 return $self->SUPER::validate_param( $name, $value );
83 }
84  
85 0 ▶sub run_task {
Subroutine run_task: not called CC=9 SCAR=45.0
Pod undocumented
86 0 my ( $self ) = @_;
87 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
88  
89 0 my $dsn = $self->param( 'dsn' );
90 0 my ( $driver ) = $dsn =~ /^dbi:([^:]+):.*$/i;
91 0 my $user = $self->param( 'username' );
92 0 my $pass = $self->param( 'password' );
93 0 my $table = $self->param( 'table' );
94  
95 0 ▶ my $dbh = DBI->connect( $dsn, $user, $pass )
Condition0%0/3
'DBI'->connect($dsn, $user, $pass) || oi_error("Cannot connect using DSN '${dsn}', username '${user}' ", "and the given password: $DBI::errstr")
l!l&&r!l&&!r
000
MC/DC0%0/2
'DBI'->connect($dsn, $user, $pass) || oi_error("Cannot connect using DSN '${dsn}', username '${user}' ", "and the given password: $DBI::errstr")
'DBI'->connect($dsn, $user, $pass) oi_error("Cannot connect using DSN '${dsn}', username '${user}' ", "and the given password: $DBI::errstr")
Truth table0%0/3
A || B
A= 'DBI'->connect($dsn, $user, $pass)B= oi_error("Cannot connect using DSN '${dsn}', username '${user}' ", "and the given password: $DBI::errstr")
ABresult
1X1
011
000
96 || oi_error "Cannot connect using DSN '$dsn', username '$user' ",
97 "and the given password: $DBI::errstr";
98 0 $dbh->{RaiseError} = 1;
99 0 my $sql = "SELECT * FROM $table WHERE 1 = 0";
100  
101 0 my ( $table_h );
102 0 eval {
103 0 $table_h = $dbh->prepare( $sql );
104 0 $table_h->execute();
105 };
106 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 0 oi_error "Failed to send dummy sql ($sql) to retrieve field ",
108 "metadata for table '$table': $@";
109 }
110  
111 0 my @fields = @{ $table_h->{NAME} };
112 0 my $num_fields = scalar @fields;
113  
114 0 my $key_field = $self->_get_key_field( $dbh, $table_h, $driver, $table, @fields );
115 0 my @field_info = $self->_get_table_info( $dbh, $driver, $table, @fields );
116  
117 0 my ( $name_field );
118 0 for ( @field_info ) {
119 # make a best-guess...
120 0 my $name = $_->{name};
121 0 ▶ if ( $name =~ /(title|name)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /(title|name)/) 0 0 0
122 0 ▶ $name_field ||= $name;
Condition0%0/3
$name_field ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$name_field ||= $name
$name_field $name
Truth table0%0/3
A ||= B
A= $name_fieldB= $name
ABresult
1X1
011
000
123 }
124 0 ▶ if ( $name eq $key_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $key_field) 0 0 0
125 0 $_->{is_key} = 1;
126 }
127 }
128 0 ▶ $name_field ||= $key_field;
Condition0%0/3
$name_field ||= $key_field
l!l&&r!l&&!r
000
MC/DC0%0/2
$name_field ||= $key_field
$name_field $key_field
Truth table0%0/3
A ||= B
A= $name_fieldB= $key_field
ABresult
1X1
011
000
129  
130  
131 0 my $package_name = $self->param( 'package' )->[0];
132 my $package = OpenInteract2::Package->create_skeleton({
133 name => $package_name,
134 brick_name => 'package_from_table',
135 brick_vars => {
136 key_field => $key_field,
137 name_field => $name_field,
138 fields => \@field_info,
139 0 field_titles => [ map { $_->{display} } @field_info ],
140 0 field_names => [ map { $_->{name} } @field_info ],
141 table => $table,
142 },
143 });
144 0 my $msg = sprintf( 'Package %s created ok in %s',
145 $package->name, $package->directory );
146 0 $self->_ok( "create package $package_name", $msg );
147 0 return;
148 }
149  
150 0 ▶sub _get_key_field {
Subroutine _get_key_field: not called CC=7 SCAR=40.3
151 0 my ( $self, $dbh, $sth, $driver, $table, @fields ) = @_;
152 0 my @keys = ();
153 0 eval {
154 0 ▶ if ( $driver eq 'mysql' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver eq 'mysql') { } 0 0 0
155 0 my $key_idx = $sth->{mysql_is_pri_key};
156 0 for ( 0 .. ( scalar @fields - 1 ) ) {
157 0 ▶ if ( $key_idx->[ $_ ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_idx->[$_]) 0 0 0
158 0 push @keys, $fields[ $_ ];
159 }
160 }
161 }
162 else {
163 0 @keys = $dbh->primary_key( undef, undef, $table );
164 }
165 };
166 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
167 0 oi_error "Cannot retrieve primary key info from '$table': $@";
168 }
169 0 ▶ if ( scalar @keys > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys > 1) 0 0 0
170 0 oi_error "Sorry, I cannot create a package from a table with ",
171 "multiple primary keys -- table $table seems to have ",
172 "the keys [", join( ', ', @keys ), "].";
173 }
174 0 ▶ unless ( $keys[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keys[0]) 0 0 0
175 0 oi_error "Sorry, no primary key seems to be available table '$table'";
176 }
177 0 return $keys[0];
178 }
179  
180  
181  
182 87 my %TEXT = map { $_ => 1 } (
183 SQL_WVARCHAR, SQL_WCHAR, SQL_CHAR, SQL_VARCHAR,
184 );
185  
186 87 my %DATE = map { $_ => 1 } (
187 SQL_DATE, SQL_TYPE_DATE,
188 );
189  
190 87 my %DATETIME = map { $_ => 1 } (
191 SQL_DATETIME, SQL_TIMESTAMP, SQL_TYPE_TIMESTAMP, SQL_TYPE_TIMESTAMP_WITH_TIMEZONE,
192 );
193  
194 87 my %NUMBER = map { $_ => 1 } (
195 SQL_TINYINT, SQL_NUMERIC, SQL_DECIMAL, SQL_INTEGER, SQL_SMALLINT, SQL_FLOAT, SQL_REAL, SQL_DOUBLE,
196 );
197  
198 87 my %BOOLEAN = map { $_ => 1 } (
199 SQL_BIT, SQL_BOOLEAN,
200 );
201  
202  
203  
204 0 ▶sub _get_table_info {
Subroutine _get_table_info: not called CC=5 SCAR=34.0
205 0 my ( $self, $dbh, $driver, $table, @fields ) = @_;
206  
207 0 my $sth = $dbh->column_info( undef, undef, $table, '%' );
208 0 my %by_field = ();
209 0 ▶ while ( my $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
210 0 $by_field{ $row->{COLUMN_NAME} } = { %{ $row } };
211 }
212  
213 0 my @field_info = ();
214 0 foreach my $field ( @fields ) {
215 0 my $metadata = $by_field{ $field };
216 0 ▶ unless ( $metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($metadata) 0 0 0
217 0 oi_error "Field name mismatch -- got field '$field' from ",
218 "'NAME' attribute of statement handle but it was ",
219 "not found when getting data using 'column_info()'.";
220 }
221 0 my $type = $metadata->{DATA_TYPE};
222 0 my $is_boolean = $self->_is_boolean( $driver, $metadata );
223 0 ▶ my $is_number = ( $is_boolean ) ? 0 : $NUMBER{ $type };
Branch: 1 branch
BranchTrueFalseTotal
$is_boolean ? : 0 0 0
224 0 $log->info( "Field '$field' info: ",
225 "[Type: $type] ",
226 "[Size: $metadata->{COLUMN_SIZE}] ",
227 "[Nullable? $metadata->{NULLABLE}] ",
228 "[Default: $metadata->{COLUMN_DEF}] ",
229 "[is_text: $TEXT{ $type }] ",
230 "[is_date: $DATE{ $type }] ",
231 "[is_datetime: $DATETIME{ $type }] ",
232 "[is_number: $is_number] ",
233 "[is_boolean: $is_boolean] " );
234  
235 push @field_info, {
236 name => $field,
237 display => $self->_to_display_name( $field ),
238 is_text => $TEXT{ $type },
239 is_date => $DATE{ $type },
240 is_datetime => $DATETIME{ $type },
241 is_number => $is_number,
242 is_boolean => $is_boolean,
243 sql_type => $type,
244 db_type => $metadata->{TYPE_NAME},
245 size => $metadata->{COLUMN_SIZE},
246 nullable => $metadata->{NULLABLE},
247 default => $metadata->{COLUMN_DEF},
248 0 };
249 }
250 0 return @field_info;
251 }
252  
253 0 ▶sub _is_boolean {
Subroutine _is_boolean: not called CC=6 SCAR=37.4
254 0 my ( $self, $driver, $metadata ) = @_;
255 0 my $type_name = $metadata->{TYPE_NAME};
256 0 ▶ if ( $driver eq 'mysql' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver eq 'mysql') { } 0 0 0
257 0 ▶ return 0 unless ( 'tinyint' eq lc $type_name );
Branch: 1 branch
BranchTrueFalseTotal
unless "tinyint" eq lc $type_name 0 0 0
258 0 ▶ return 0 unless ( $metadata->{COLUMN_SIZE} == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless $metadata->{"COLUMN_SIZE"} == 1 0 0 0
259 0 return 1;
260 }
261 else {
262 0 my $type = $metadata->{DATA_TYPE};
263 0 ▶ return 1 if ( $BOOLEAN{ $type } );
Branch: 1 branch
BranchTrueFalseTotal
if $BOOLEAN{$type} 0 0 0
264 0 ▶ return 1 if ( $type_name =~ /^(boolean|bit)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $type_name =~ /^(boolean|bit)$/i 0 0 0
265 0 return 0;
266 }
267 }
268  
269  
270 # someFieldName => Some Field Name
271 # SomeFieldName => Some Field Name
272 # some_field_name => Some Field Name
273 # is_my_field => Is My Field?
274 # isMyField => Is My Field?
275 # IsMyField => Is My Field?
276  
277 0 ▶sub _to_display_name {
Subroutine _to_display_name: not called CC=2 SCAR=17.9
278 0 my ( $self, $field ) = @_;
279 0 my $display = $field;
280 0 $display =~ s/_(\w)/\U$1\U/g;
281 0 $display =~ s/([A-Z])/ $1/g;
282 0 ▶ if ( $display =~ /^is\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($display =~ /^is\b/i) 0 0 0
283 0 $display .= '?';
284 }
285 0 return ucfirst $display;
286 }
287  
288 87 OpenInteract2::Manage->register_factory_type( get_name() => __PACKAGE__ );
289  
290 87 1;
291  
292 __END__
« OpenInteract2/DatasourceManager.pm ↑ Summary OpenInteract2/Action/CommonRemove.pm »